aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/common/layout/RelativeLayoutRuleTest.java
blob: f9747f39d41c6e051ed253296639a82336d4ef4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright (C) 2010 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;


import static com.android.SdkConstants.ANDROID_URI;

import com.android.ide.common.api.INode;
import com.android.ide.common.api.Point;
import com.android.ide.common.api.Rect;

/** Test the {@link RelativeLayoutRule} */
public class RelativeLayoutRuleTest extends LayoutTestBase {
    // Utility for other tests
    protected INode dragInto(Rect dragBounds, Point dragPoint, Point secondDragPoint,
            int insertIndex, int currentIndex, String... graphicsFragments) {
        INode layout = TestNode.create("android.widget.RelativeLayout").id("@+id/RelativeLayout01")
                .bounds(new Rect(0, 0, 240, 480)).add(
                // Add centered button as the anchor
                        TestNode.create("android.widget.Button").id("@+id/Centered").bounds(
                                new Rect(70, 200, 100, 80)).set(ANDROID_URI,
                                "layout_centerInParent", "true"),
                        // Add a second button anchored to it
                        TestNode.create("android.widget.Button").id("@+id/Below").bounds(
                                new Rect(70, 280, 100, 80)).set(ANDROID_URI, "layout_below",
                                "@+id/Centered").set(ANDROID_URI, "layout_alignLeft",
                                "@+id/Centered"));

        return super.dragInto(new RelativeLayoutRule(), layout, dragBounds, dragPoint,
                secondDragPoint, insertIndex, currentIndex, graphicsFragments);
    }

    protected INode dragInto(Rect dragBounds, Point dragPoint, Point secondDragPoint,
            int insertIndex, int currentIndex, String[] extraFragments,
            String... graphicsFragments) {

        // When we switch to JDK6, use Arrays#copyOf instead
        String[] combined = new String[extraFragments.length + graphicsFragments.length];
        System.arraycopy(graphicsFragments, 0, combined, 0, graphicsFragments.length);
        System.arraycopy(extraFragments, 0, combined, graphicsFragments.length,
                extraFragments.length);

        return dragInto(dragBounds, dragPoint, secondDragPoint, insertIndex,
                currentIndex, combined);
    }

    /* This needs to be updated for the new interaction
    public void testDropTopEdge() {
        // If we drag right into the button itself, not a valid drop position
        INode inserted = dragInto(
                new Rect(0, 0, 105, 80), new Point(30, -10), null, 2, -1,
                // Bounds rectangle
                "useStyle(DROP_RECIPIENT), drawRect(Rect[0,0,240,480])",

                // Preview line + drop zone rectangle along the top
                "useStyle(DROP_ZONE), drawRect(Rect[0,-10,240,20])",
                "useStyle(DROP_ZONE_ACTIVE), fillRect(Rect[0,-10,240,20])",
                "useStyle(DROP_PREVIEW), drawLine(0,0,240,0)",

                // Tip
                "useStyle(HELP), drawBoxedStrings(5,15,[alignParentTop])",

                // Drop preview
                "useStyle(DROP_PREVIEW), drawRect(Rect[0,0,105,80])");

        assertEquals("true", inserted.getStringAttr(ANDROID_URI,
                "layout_alignParentTop"));
    }

    public void testDropZones() {
        List<Pair<Point,String[]>> zones = new ArrayList<Pair<Point,String[]>>();

        zones.add(Pair.of(new Point(51+10, 181+10),
                new String[] {"above=@+id/Centered", "toLeftOf=@+id/Centered"}));
        zones.add(Pair.of(new Point(71+10, 181+10),
                new String[] {"above=@+id/Centered", "alignLeft=@+id/Centered"}));
        zones.add(Pair.of(new Point(104+10, 181+10),
                new String[] {"above=@+id/Centered", "alignRight=@+id/Centered"}));
        zones.add(Pair.of(new Point(137+10, 181+10),
                new String[] {"above=@+id/Centered", "alignRight=@+id/Centered"}));
        zones.add(Pair.of(new Point(170+10, 181+10),
                new String[] {"above=@+id/Centered", "toRightOf=@+id/Centered"}));
        zones.add(Pair.of(new Point(51+10, 279+10),
                new String[] {"below=@+id/Centered", "toLeftOf=@+id/Centered"}));
        zones.add(Pair.of(new Point(71+10, 279+10),
                new String[] {"below=@+id/Centered", "alignLeft=@+id/Centered"}));
        zones.add(Pair.of(new Point(104+10, 279+10),
                new String[] {"below=@+id/Centered", "alignLeft=@+id/Centered"}));
        zones.add(Pair.of(new Point(137+10, 279+10),
                new String[] {"below=@+id/Centered", "alignRight=@+id/Centered"}));
        zones.add(Pair.of(new Point(170+10, 279+10),
                new String[] {"below=@+id/Centered", "toRightOf=@+id/Centered"}));
        zones.add(Pair.of(new Point(51+10, 201+10),
                new String[] {"toLeftOf=@+id/Centered", "alignTop=@+id/Centered"}));
        zones.add(Pair.of(new Point(51+10, 227+10),
                new String[] {"toLeftOf=@+id/Centered", "alignTop=@+id/Centered"}));
        zones.add(Pair.of(new Point(170+10, 201+10),
                new String[] {"toRightOf=@+id/Centered", "alignTop=@+id/Centered"}));
        zones.add(Pair.of(new Point(51+10, 253+10),
                new String[] {"toLeftOf=@+id/Centered", "alignBottom=@+id/Centered"}));
        zones.add(Pair.of(new Point(170+10, 227+10),
                new String[] {"toRightOf=@+id/Centered", "alignTop=@+id/Centered",
            "alignBottom=@+id/Centered"}));
        zones.add(Pair.of(new Point(170+10, 253+10),
                new String[] {"toRightOf=@+id/Centered", "alignBottom=@+id/Centered"}));

        for (Pair<Point,String[]> zonePair : zones) {
            Point dropPoint = zonePair.getFirst();
            String[] attachments = zonePair.getSecond();
            // If we drag right into the button itself, not a valid drop position

            INode inserted = dragInto(
                    new Rect(0, 0, 105, 80), new Point(120, 240), dropPoint, 1, -1,
                    attachments,

                    // Bounds rectangle
                    "useStyle(DROP_RECIPIENT), drawRect(Rect[0,0,240,480])",

                    // Drop zones
                    "useStyle(DROP_ZONE), "
                            + "drawRect(Rect[51,181,20,20]), drawRect(Rect[71,181,33,20]), "
                            + "drawRect(Rect[104,181,33,20]), drawRect(Rect[137,181,33,20]), "
                            + "drawRect(Rect[170,181,20,20]), drawRect(Rect[51,279,20,20]), "
                            + "drawRect(Rect[71,279,33,20]), drawRect(Rect[104,279,33,20]), "
                            + "drawRect(Rect[137,279,33,20]), drawRect(Rect[170,279,20,20]), "
                            + "drawRect(Rect[51,201,20,26]), drawRect(Rect[51,227,20,26]), "
                            + "drawRect(Rect[51,253,20,26]), drawRect(Rect[170,201,20,26]), "
                            + "drawRect(Rect[170,227,20,26]), drawRect(Rect[170,253,20,26])");

            for (String attachment : attachments) {
                String[] elements = attachment.split("=");
                String name = "layout_" + elements[0];
                String value = elements[1];
                assertEquals(value, inserted.getStringAttr(ANDROID_URI, name));
            }
        }
    }


    public void testDragInvalid() {
        // If we drag right into the button itself, not a valid drop position
        dragInto(new Rect(70, 200, 100, 80), new Point(120, 240), new Point(120, 240), -1, 0,
        // Bounds rectangle
                "useStyle(DROP_RECIPIENT), drawRect(Rect[0,0,240,480])",

                // Invalid marker
                "useStyle(INVALID), fillRect(Rect[70,200,100,80]), drawLine(70,200,170,280), "
                        + "drawLine(70,280,170,200)");
    }

    // TODO: Test error (dragging on ancestor)
     */
}