/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Eclipse Public License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.eclipse.org/org/documents/epl-v10.php
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.ide.common.layout.relative;
import static com.android.SdkConstants.ATTR_ID;
import static com.android.SdkConstants.ANDROID_URI;
import com.android.ide.common.api.INode;
import com.android.ide.common.layout.BaseViewRule;
import com.android.ide.common.layout.TestNode;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import junit.framework.TestCase;
@SuppressWarnings("javadoc")
public class DeletionHandlerTest extends TestCase {
public void testSimple() {
String xml = "" +
"\n" +
"\n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
"";
TestNode targetNode = TestNode.createFromXml(xml);
assertNotNull(targetNode);
TestNode button2 = TestNode.findById(targetNode, "@+id/button2");
INode layout = button2.getParent();
List deletedNodes = Collections.singletonList(button2);
List movedNodes = Collections.emptyList();
assertSame(layout, targetNode);
layout.removeChild(button2);
DeletionHandler handler = new DeletionHandler(deletedNodes, movedNodes, layout);
handler.updateConstraints();
String updated = TestNode.toXml(targetNode);
assertEquals(
"\n" +
"\n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
"",
updated);
assertFalse(updated.contains(BaseViewRule.stripIdPrefix(button2.getStringAttr(ANDROID_URI,
ATTR_ID))));
}
public void testTransitive() {
String xml = "" +
"\n" +
"\n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
"";
TestNode targetNode = TestNode.createFromXml(xml);
assertNotNull(targetNode);
TestNode button7 = TestNode.findById(targetNode, "@+id/button7");
TestNode checkBox = TestNode.findById(targetNode, "@+id/checkBox1");
INode layout = button7.getParent();
List deletedNodes = Arrays.asList(button7, checkBox);
List movedNodes = Collections.emptyList();
assertSame(layout, targetNode);
layout.removeChild(button7);
layout.removeChild(checkBox);
DeletionHandler handler = new DeletionHandler(deletedNodes, movedNodes, layout);
handler.updateConstraints();
String updated = TestNode.toXml(targetNode);
assertEquals(
"\n" +
"\n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
"",
updated);
assertFalse(updated.contains(BaseViewRule.stripIdPrefix(button7.getStringAttr(ANDROID_URI,
ATTR_ID))));
}
public void testCenter() {
String xml =
"\n" +
"\n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
"";
TestNode targetNode = TestNode.createFromXml(xml);
assertNotNull(targetNode);
TestNode button1 = TestNode.findById(targetNode, "@+id/button1");
INode layout = button1.getParent();
List deletedNodes = Collections.singletonList(button1);
List movedNodes = Collections.emptyList();
assertSame(layout, targetNode);
layout.removeChild(button1);
DeletionHandler handler = new DeletionHandler(deletedNodes, movedNodes, layout);
handler.updateConstraints();
String updated = TestNode.toXml(targetNode);
assertEquals(
"\n" +
"\n" +
"\n" +
" \n" +
" \n" +
"\n" +
"",
updated);
assertFalse(updated.contains(BaseViewRule.stripIdPrefix(button1.getStringAttr(ANDROID_URI,
ATTR_ID))));
}
public void testMove() {
String xml = "" +
"\n" +
"\n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
"";
TestNode targetNode = TestNode.createFromXml(xml);
assertNotNull(targetNode);
TestNode button2 = TestNode.findById(targetNode, "@+id/button2");
INode layout = button2.getParent();
List deletedNodes = Collections.singletonList(button2);
List movedNodes = Collections.singletonList(button2);
assertSame(layout, targetNode);
DeletionHandler handler = new DeletionHandler(deletedNodes, movedNodes, layout);
handler.updateConstraints();
String updated = TestNode.toXml(targetNode);
assertEquals(
"\n" +
"\n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
" \n" +
"\n" +
"",
updated);
assertTrue(updated.contains(BaseViewRule.stripIdPrefix(button2.getStringAttr(ANDROID_URI,
ATTR_ID))));
}
}