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