1 /*
2  * Copyright (C) 2010 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 
17 package com.android.ide.common.layout;
18 
19 
20 import static com.android.SdkConstants.ANDROID_URI;
21 
22 import com.android.ide.common.api.INode;
23 import com.android.ide.common.api.Point;
24 import com.android.ide.common.api.Rect;
25 
26 /** Test the {@link RelativeLayoutRule} */
27 public class RelativeLayoutRuleTest extends LayoutTestBase {
28     // Utility for other tests
dragInto(Rect dragBounds, Point dragPoint, Point secondDragPoint, int insertIndex, int currentIndex, String... graphicsFragments)29     protected INode dragInto(Rect dragBounds, Point dragPoint, Point secondDragPoint,
30             int insertIndex, int currentIndex, String... graphicsFragments) {
31         INode layout = TestNode.create("android.widget.RelativeLayout").id("@+id/RelativeLayout01")
32                 .bounds(new Rect(0, 0, 240, 480)).add(
33                 // Add centered button as the anchor
34                         TestNode.create("android.widget.Button").id("@+id/Centered").bounds(
35                                 new Rect(70, 200, 100, 80)).set(ANDROID_URI,
36                                 "layout_centerInParent", "true"),
37                         // Add a second button anchored to it
38                         TestNode.create("android.widget.Button").id("@+id/Below").bounds(
39                                 new Rect(70, 280, 100, 80)).set(ANDROID_URI, "layout_below",
40                                 "@+id/Centered").set(ANDROID_URI, "layout_alignLeft",
41                                 "@+id/Centered"));
42 
43         return super.dragInto(new RelativeLayoutRule(), layout, dragBounds, dragPoint,
44                 secondDragPoint, insertIndex, currentIndex, graphicsFragments);
45     }
46 
dragInto(Rect dragBounds, Point dragPoint, Point secondDragPoint, int insertIndex, int currentIndex, String[] extraFragments, String... graphicsFragments)47     protected INode dragInto(Rect dragBounds, Point dragPoint, Point secondDragPoint,
48             int insertIndex, int currentIndex, String[] extraFragments,
49             String... graphicsFragments) {
50 
51         // When we switch to JDK6, use Arrays#copyOf instead
52         String[] combined = new String[extraFragments.length + graphicsFragments.length];
53         System.arraycopy(graphicsFragments, 0, combined, 0, graphicsFragments.length);
54         System.arraycopy(extraFragments, 0, combined, graphicsFragments.length,
55                 extraFragments.length);
56 
57         return dragInto(dragBounds, dragPoint, secondDragPoint, insertIndex,
58                 currentIndex, combined);
59     }
60 
61     /* This needs to be updated for the new interaction
62     public void testDropTopEdge() {
63         // If we drag right into the button itself, not a valid drop position
64         INode inserted = dragInto(
65                 new Rect(0, 0, 105, 80), new Point(30, -10), null, 2, -1,
66                 // Bounds rectangle
67                 "useStyle(DROP_RECIPIENT), drawRect(Rect[0,0,240,480])",
68 
69                 // Preview line + drop zone rectangle along the top
70                 "useStyle(DROP_ZONE), drawRect(Rect[0,-10,240,20])",
71                 "useStyle(DROP_ZONE_ACTIVE), fillRect(Rect[0,-10,240,20])",
72                 "useStyle(DROP_PREVIEW), drawLine(0,0,240,0)",
73 
74                 // Tip
75                 "useStyle(HELP), drawBoxedStrings(5,15,[alignParentTop])",
76 
77                 // Drop preview
78                 "useStyle(DROP_PREVIEW), drawRect(Rect[0,0,105,80])");
79 
80         assertEquals("true", inserted.getStringAttr(ANDROID_URI,
81                 "layout_alignParentTop"));
82     }
83 
84     public void testDropZones() {
85         List<Pair<Point,String[]>> zones = new ArrayList<Pair<Point,String[]>>();
86 
87         zones.add(Pair.of(new Point(51+10, 181+10),
88                 new String[] {"above=@+id/Centered", "toLeftOf=@+id/Centered"}));
89         zones.add(Pair.of(new Point(71+10, 181+10),
90                 new String[] {"above=@+id/Centered", "alignLeft=@+id/Centered"}));
91         zones.add(Pair.of(new Point(104+10, 181+10),
92                 new String[] {"above=@+id/Centered", "alignRight=@+id/Centered"}));
93         zones.add(Pair.of(new Point(137+10, 181+10),
94                 new String[] {"above=@+id/Centered", "alignRight=@+id/Centered"}));
95         zones.add(Pair.of(new Point(170+10, 181+10),
96                 new String[] {"above=@+id/Centered", "toRightOf=@+id/Centered"}));
97         zones.add(Pair.of(new Point(51+10, 279+10),
98                 new String[] {"below=@+id/Centered", "toLeftOf=@+id/Centered"}));
99         zones.add(Pair.of(new Point(71+10, 279+10),
100                 new String[] {"below=@+id/Centered", "alignLeft=@+id/Centered"}));
101         zones.add(Pair.of(new Point(104+10, 279+10),
102                 new String[] {"below=@+id/Centered", "alignLeft=@+id/Centered"}));
103         zones.add(Pair.of(new Point(137+10, 279+10),
104                 new String[] {"below=@+id/Centered", "alignRight=@+id/Centered"}));
105         zones.add(Pair.of(new Point(170+10, 279+10),
106                 new String[] {"below=@+id/Centered", "toRightOf=@+id/Centered"}));
107         zones.add(Pair.of(new Point(51+10, 201+10),
108                 new String[] {"toLeftOf=@+id/Centered", "alignTop=@+id/Centered"}));
109         zones.add(Pair.of(new Point(51+10, 227+10),
110                 new String[] {"toLeftOf=@+id/Centered", "alignTop=@+id/Centered"}));
111         zones.add(Pair.of(new Point(170+10, 201+10),
112                 new String[] {"toRightOf=@+id/Centered", "alignTop=@+id/Centered"}));
113         zones.add(Pair.of(new Point(51+10, 253+10),
114                 new String[] {"toLeftOf=@+id/Centered", "alignBottom=@+id/Centered"}));
115         zones.add(Pair.of(new Point(170+10, 227+10),
116                 new String[] {"toRightOf=@+id/Centered", "alignTop=@+id/Centered",
117             "alignBottom=@+id/Centered"}));
118         zones.add(Pair.of(new Point(170+10, 253+10),
119                 new String[] {"toRightOf=@+id/Centered", "alignBottom=@+id/Centered"}));
120 
121         for (Pair<Point,String[]> zonePair : zones) {
122             Point dropPoint = zonePair.getFirst();
123             String[] attachments = zonePair.getSecond();
124             // If we drag right into the button itself, not a valid drop position
125 
126             INode inserted = dragInto(
127                     new Rect(0, 0, 105, 80), new Point(120, 240), dropPoint, 1, -1,
128                     attachments,
129 
130                     // Bounds rectangle
131                     "useStyle(DROP_RECIPIENT), drawRect(Rect[0,0,240,480])",
132 
133                     // Drop zones
134                     "useStyle(DROP_ZONE), "
135                             + "drawRect(Rect[51,181,20,20]), drawRect(Rect[71,181,33,20]), "
136                             + "drawRect(Rect[104,181,33,20]), drawRect(Rect[137,181,33,20]), "
137                             + "drawRect(Rect[170,181,20,20]), drawRect(Rect[51,279,20,20]), "
138                             + "drawRect(Rect[71,279,33,20]), drawRect(Rect[104,279,33,20]), "
139                             + "drawRect(Rect[137,279,33,20]), drawRect(Rect[170,279,20,20]), "
140                             + "drawRect(Rect[51,201,20,26]), drawRect(Rect[51,227,20,26]), "
141                             + "drawRect(Rect[51,253,20,26]), drawRect(Rect[170,201,20,26]), "
142                             + "drawRect(Rect[170,227,20,26]), drawRect(Rect[170,253,20,26])");
143 
144             for (String attachment : attachments) {
145                 String[] elements = attachment.split("=");
146                 String name = "layout_" + elements[0];
147                 String value = elements[1];
148                 assertEquals(value, inserted.getStringAttr(ANDROID_URI, name));
149             }
150         }
151     }
152 
153 
154     public void testDragInvalid() {
155         // If we drag right into the button itself, not a valid drop position
156         dragInto(new Rect(70, 200, 100, 80), new Point(120, 240), new Point(120, 240), -1, 0,
157         // Bounds rectangle
158                 "useStyle(DROP_RECIPIENT), drawRect(Rect[0,0,240,480])",
159 
160                 // Invalid marker
161                 "useStyle(INVALID), fillRect(Rect[70,200,100,80]), drawLine(70,200,170,280), "
162                         + "drawLine(70,280,170,200)");
163     }
164 
165     // TODO: Test error (dragging on ancestor)
166      */
167 }
168