1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.ide.eclipse.adt.internal.editors.layout.gle2;
17 
18 import com.android.ide.common.rendering.api.ViewInfo;
19 import com.android.ide.eclipse.adt.internal.editors.layout.uimodel.UiViewElementNode;
20 
21 import org.eclipse.swt.widgets.Shell;
22 
23 import java.util.Arrays;
24 
25 import junit.framework.TestCase;
26 
27 public class SelectionManagerTest extends TestCase {
createManager()28     private SelectionManager createManager() {
29         LayoutCanvas canvas = new LayoutCanvas(null, null, new Shell(), 0);
30         return new SelectionManager(canvas);
31     }
32 
testEmpty()33     public void testEmpty() {
34         SelectionManager manager = createManager();
35 
36         assertNotNull(manager.getSelections());
37         assertEquals(0, manager.getSelections().size());
38         assertFalse(manager.hasMultiSelection());
39         assertTrue(manager.isEmpty());
40     }
41 
testBasic()42     public void testBasic() {
43         SelectionManager manager = createManager();
44         assertTrue(manager.isEmpty());
45 
46         UiViewElementNode rootNode = CanvasViewInfoTest.createNode("android.widget.LinearLayout",
47                 true);
48         ViewInfo root = new ViewInfo("LinearLayout", rootNode, 10, 10, 100, 100);
49         UiViewElementNode child1Node = CanvasViewInfoTest.createNode(rootNode,
50                 "android.widget.Button", false);
51         ViewInfo child1 = new ViewInfo("Button1", child1Node, 0, 0, 50, 20);
52         UiViewElementNode child2Node = CanvasViewInfoTest.createNode(rootNode,
53                 "android.widget.Button", false);
54         ViewInfo child2 = new ViewInfo("Button2", child2Node, 0, 20, 70, 25);
55         root.setChildren(Arrays.asList(child1, child2));
56         CanvasViewInfo rootView = CanvasViewInfo.create(root, true /* layoutlib5 */).getFirst();
57         assertNotNull(rootView);
58 
59         manager.selectMultiple(Arrays.asList(rootView, rootView.getChildren().get(0), rootView
60                 .getChildren().get(1)));
61         assertEquals(3, manager.getSelections().size());
62         assertFalse(manager.isEmpty());
63         assertTrue(manager.hasMultiSelection());
64 
65         // Expect read-only result; ensure that's the case
66         try {
67             manager.getSelections().remove(0);
68             fail("Result should be read only collection");
69         } catch (Exception e) {
70             ; //ok, what we expected
71         }
72 
73         manager.selectNone();
74         assertEquals(0, manager.getSelections().size());
75         assertTrue(manager.isEmpty());
76 
77         manager.selectSingle(rootView);
78         assertEquals(1, manager.getSelections().size());
79         assertFalse(manager.isEmpty());
80         assertSame(rootView, manager.getSelections().get(0).getViewInfo());
81 
82         manager.selectMultiple(Arrays.asList(rootView, rootView.getChildren().get(0), rootView
83                 .getChildren().get(1)));
84         assertEquals(3, manager.getSelections().size());
85 
86         manager.deselect(rootView.getChildren().get(0));
87         assertEquals(2, manager.getSelections().size());
88         manager.deselect(rootView);
89         assertEquals(1, manager.getSelections().size());
90         assertSame(rootView.getChildren().get(1), manager.getSelections().get(0).getViewInfo());
91     }
92 
testSelectParent()93     public void testSelectParent() {
94         SelectionManager manager = createManager();
95         assertTrue(manager.isEmpty());
96 
97         UiViewElementNode rootNode = CanvasViewInfoTest.createNode("android.widget.LinearLayout",
98                 true);
99         ViewInfo root = new ViewInfo("LinearLayout", rootNode, 10, 10, 100, 100);
100         UiViewElementNode child1Node = CanvasViewInfoTest.createNode(rootNode,
101                 "android.widget.Button", false);
102         ViewInfo child1 = new ViewInfo("Button1", child1Node, 0, 0, 50, 20);
103         UiViewElementNode child2Node = CanvasViewInfoTest.createNode(rootNode,
104                 "android.widget.Button", false);
105         ViewInfo child2 = new ViewInfo("Button2", child2Node, 0, 20, 70, 25);
106         root.setChildren(Arrays.asList(child1, child2));
107         CanvasViewInfo rootView = CanvasViewInfo.create(root, true /* layoutlib5 */).getFirst();
108         assertNotNull(rootView);
109 
110         manager.selectMultiple(Arrays.asList(rootView.getChildren().get(0)));
111         assertEquals(1, manager.getSelections().size());
112         assertFalse(manager.isEmpty());
113         assertSame(rootView.getChildren().get(0), manager.getSelections().get(0).getViewInfo());
114 
115         manager.selectParent();
116         assertEquals(1, manager.getSelections().size());
117         assertFalse(manager.isEmpty());
118         assertSame(rootView, manager.getSelections().get(0).getViewInfo());
119     }
120 }
121