summaryrefslogtreecommitdiffstats
path: root/test-runner/src/android/test/ViewAsserts.java
blob: c575fc5982bf6ff0d3260ab098aa828d8677fe57 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * 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 android.test;

import static junit.framework.Assert.*;

import android.view.View;
import android.view.ViewGroup;

/**
 * Some useful assertions about views.
 */
public class ViewAsserts {

    private ViewAsserts() {}

    /**
     * Assert that view is on the screen.
     * @param origin The root view of the screen.
     * @param view The view.
     */
    static public void assertOnScreen(View origin, View view) {
        int[] xy = new int[2];
        view.getLocationOnScreen(xy);

        int[] xyRoot = new int[2];
        origin.getLocationOnScreen(xyRoot);

        int y = xy[1] - xyRoot[1];

        assertTrue("view should have positive y coordinate on screen",
                y  >= 0);

        assertTrue("view should have y location on screen less than drawing "
                + "height of root view",
                y <= view.getRootView().getHeight());
    }

    /**
     * Assert that view is below the visible screen.
     * @param origin The root view of the screen.
     * @param view The view
     */
    static public void assertOffScreenBelow(View origin, View view) {
        int[] xy = new int[2];
        view.getLocationOnScreen(xy);

        int[] xyRoot = new int[2];
        origin.getLocationOnScreen(xyRoot);

        int y = xy[1] - xyRoot[1];

        assertTrue("view should have y location on screen greater than drawing "
                + "height of origen view (" + y + " is not greater than "
                + origin.getHeight() + ")",
                y > origin.getHeight());
    }

    /**
     * Assert that view is above the visible screen.
     * @param origin Te root view of the screen.
     * @param view The view
     */
    static public void assertOffScreenAbove(View origin, View view) {
        int[] xy = new int[2];
        view.getLocationOnScreen(xy);

        int[] xyRoot = new int[2];
        origin.getLocationOnScreen(xyRoot);

        int y = xy[1] - xyRoot[1];

        assertTrue("view should have y location less than that of origin view",
                y < 0);
    }

    /**
     * Assert that a view has a particular x and y position on the visible screen.
     * @param origin The root view of the screen.
     * @param view The view.
     * @param x The expected x coordinate.
     * @param y The expected y coordinate.
     */
    static public void assertHasScreenCoordinates(View origin, View view, int x, int y) {
        int[] xy = new int[2];
        view.getLocationOnScreen(xy);

        int[] xyRoot = new int[2];
        origin.getLocationOnScreen(xyRoot);

        assertEquals("x coordinate", x, xy[0] - xyRoot[0]);
        assertEquals("y coordinate", y, xy[1] - xyRoot[1]);
    }

    /**
     * Assert that two views are aligned on their baseline, that is that their baselines
     * are on the same y location.
     *
     * @param first The first view
     * @param second The second view
     */
    static public void assertBaselineAligned(View first, View second) {
        int[] xy = new int[2];
        first.getLocationOnScreen(xy);
        int firstTop = xy[1] + first.getBaseline();

        second.getLocationOnScreen(xy);
        int secondTop = xy[1] + second.getBaseline();

        assertEquals("views are not baseline aligned", firstTop, secondTop);
    }

    /**
     * Assert that two views are right aligned, that is that their right edges
     * are on the same x location.
     *
     * @param first The first view
     * @param second The second view
     */
    static public void assertRightAligned(View first, View second) {
        int[] xy = new int[2];
        first.getLocationOnScreen(xy);
        int firstRight = xy[0] + first.getMeasuredWidth();

        second.getLocationOnScreen(xy);
        int secondRight = xy[0] + second.getMeasuredWidth();

        assertEquals("views are not right aligned", firstRight, secondRight);
    }

    /**
     * Assert that two views are right aligned, that is that their right edges
     * are on the same x location, with respect to the specified margin.
     *
     * @param first The first view
     * @param second The second view
     * @param margin The margin between the first view and the second view
     */
    static public void assertRightAligned(View first, View second, int margin) {
        int[] xy = new int[2];
        first.getLocationOnScreen(xy);
        int firstRight = xy[0] + first.getMeasuredWidth();

        second.getLocationOnScreen(xy);
        int secondRight = xy[0] + second.getMeasuredWidth();

        assertEquals("views are not right aligned", Math.abs(firstRight - secondRight), margin);
    }

    /**
     * Assert that two views are left aligned, that is that their left edges
     * are on the same x location.
     *
     * @param first The first view
     * @param second The second view
     */
    static public void assertLeftAligned(View first, View second) {
        int[] xy = new int[2];
        first.getLocationOnScreen(xy);
        int firstLeft = xy[0];

        second.getLocationOnScreen(xy);
        int secondLeft = xy[0];

        assertEquals("views are not left aligned", firstLeft, secondLeft);
    }

    /**
     * Assert that two views are left aligned, that is that their left edges
     * are on the same x location, with respect to the specified margin.
     *
     * @param first The first view
     * @param second The second view
     * @param margin The margin between the first view and the second view
     */
    static public void assertLeftAligned(View first, View second, int margin) {
        int[] xy = new int[2];
        first.getLocationOnScreen(xy);
        int firstLeft = xy[0];

        second.getLocationOnScreen(xy);
        int secondLeft = xy[0];

        assertEquals("views are not left aligned", Math.abs(firstLeft - secondLeft), margin);
    }

    /**
     * Assert that two views are bottom aligned, that is that their bottom edges
     * are on the same y location.
     *
     * @param first The first view
     * @param second The second view
     */
    static public void assertBottomAligned(View first, View second) {
        int[] xy = new int[2];
        first.getLocationOnScreen(xy);
        int firstBottom = xy[1] + first.getMeasuredHeight();

        second.getLocationOnScreen(xy);
        int secondBottom = xy[1] + second.getMeasuredHeight();

        assertEquals("views are not bottom aligned", firstBottom, secondBottom);
    }

    /**
     * Assert that two views are bottom aligned, that is that their bottom edges
     * are on the same y location, with respect to the specified margin.
     *
     * @param first The first view
     * @param second The second view
     * @param margin The margin between the first view and the second view
     */
    static public void assertBottomAligned(View first, View second, int margin) {
        int[] xy = new int[2];
        first.getLocationOnScreen(xy);
        int firstBottom = xy[1] + first.getMeasuredHeight();

        second.getLocationOnScreen(xy);
        int secondBottom = xy[1] + second.getMeasuredHeight();

        assertEquals("views are not bottom aligned", Math.abs(firstBottom - secondBottom), margin);
    }

    /**
     * Assert that two views are top aligned, that is that their top edges
     * are on the same y location.
     *
     * @param first The first view
     * @param second The second view
     */
    static public void assertTopAligned(View first, View second) {
        int[] xy = new int[2];
        first.getLocationOnScreen(xy);
        int firstTop = xy[1];

        second.getLocationOnScreen(xy);
        int secondTop = xy[1];

        assertEquals("views are not top aligned", firstTop, secondTop);
    }

    /**
     * Assert that two views are top aligned, that is that their top edges
     * are on the same y location, with respect to the specified margin.
     *
     * @param first The first view
     * @param second The second view
     * @param margin The margin between the first view and the second view
     */
    static public void assertTopAligned(View first, View second, int margin) {
        int[] xy = new int[2];
        first.getLocationOnScreen(xy);
        int firstTop = xy[1];

        second.getLocationOnScreen(xy);
        int secondTop = xy[1];

        assertEquals("views are not top aligned", Math.abs(firstTop - secondTop), margin);
    }

    /**
     * Assert that the <code>test</code> view is horizontally center aligned
     * with respect to the <code>reference</code> view.
     *
     * @param reference The reference view
     * @param test The view that should be center aligned with the reference view
     */
    static public void assertHorizontalCenterAligned(View reference, View test) {
        int[] xy = new int[2];
        reference.getLocationOnScreen(xy);
        int referenceLeft = xy[0];

        test.getLocationOnScreen(xy);
        int testLeft = xy[0];

        int center = (reference.getMeasuredWidth() - test.getMeasuredWidth()) / 2;
        int delta = testLeft - referenceLeft;

        assertEquals("views are not horizontally center aligned", center, delta);
    }

    /**
     * Assert that the <code>test</code> view is vertically center aligned
     * with respect to the <code>reference</code> view.
     *
     * @param reference The reference view
     * @param test The view that should be center aligned with the reference view
     */
    static public void assertVerticalCenterAligned(View reference, View test) {
        int[] xy = new int[2];
        reference.getLocationOnScreen(xy);
        int referenceTop = xy[1];

        test.getLocationOnScreen(xy);
        int testTop = xy[1];

        int center = (reference.getMeasuredHeight() - test.getMeasuredHeight()) / 2;
        int delta = testTop - referenceTop;

        assertEquals("views are not vertically center aligned", center, delta);
    }

    /**
     * Assert the specified group's integrity. The children count should be >= 0 and each
     * child should be non-null.
     *
     * @param parent The group whose integrity to check
     */
    static public void assertGroupIntegrity(ViewGroup parent) {
        final int count = parent.getChildCount();
        assertTrue("child count should be >= 0", count >= 0);

        for (int i = 0; i < count; i++) {
            assertNotNull("group should not contain null children", parent.getChildAt(i));
            assertSame(parent, parent.getChildAt(i).getParent());
        }
    }

    /**
     * Assert that the specified group contains a specific child once and only once.
     *
     * @param parent The group
     * @param child The child that should belong to group
     */
    static public void assertGroupContains(ViewGroup parent, View child) {
        final int count = parent.getChildCount();
        assertTrue("Child count should be >= 0", count >= 0);

        boolean found = false;
        for (int i = 0; i < count; i++) {
            if (parent.getChildAt(i) == child) {
                if (!found) {
                    found = true;
                } else {
                    assertTrue("child " + child + " is duplicated in parent", false);
                }
            }
        }

        assertTrue("group does not contain " + child, found);
    }

    /**
     * Assert that the specified group does not contain a specific child.
     *
     * @param parent The group
     * @param child The child that should not belong to group
     */
    static public void assertGroupNotContains(ViewGroup parent, View child) {
        final int count = parent.getChildCount();
        assertTrue("Child count should be >= 0", count >= 0);

        for (int i = 0; i < count; i++) {
            if (parent.getChildAt(i) == child) {
                assertTrue("child " + child + " is found in parent", false);
            }
        }
    }
}