1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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
17package android.test;
18
19import static junit.framework.Assert.*;
20
21import android.view.View;
22import android.view.ViewGroup;
23
24/**
25 * Some useful assertions about views.
26 *
27 * @deprecated Use
28 * <a href="{@docRoot}reference/android/support/test/espresso/matcher/ViewMatchers.html">Espresso
29 * View Matchers</a> instead. New test should be written using the
30 * <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a>.
31 * For more information about UI testing, take the
32 * <a href="{@docRoot}tools/testing-support-library/index.html">Espresso UI testing</a> training.
33 */
34@Deprecated
35public class ViewAsserts {
36
37    private ViewAsserts() {}
38
39    /**
40     * Assert that view is on the screen.
41     * @param origin The root view of the screen.
42     * @param view The view.
43     */
44    static public void assertOnScreen(View origin, View view) {
45        int[] xy = new int[2];
46        view.getLocationOnScreen(xy);
47
48        int[] xyRoot = new int[2];
49        origin.getLocationOnScreen(xyRoot);
50
51        int y = xy[1] - xyRoot[1];
52
53        assertTrue("view should have positive y coordinate on screen",
54                y  >= 0);
55
56        assertTrue("view should have y location on screen less than drawing "
57                + "height of root view",
58                y <= view.getRootView().getHeight());
59    }
60
61    /**
62     * Assert that view is below the visible screen.
63     * @param origin The root view of the screen.
64     * @param view The view
65     */
66    static public void assertOffScreenBelow(View origin, View view) {
67        int[] xy = new int[2];
68        view.getLocationOnScreen(xy);
69
70        int[] xyRoot = new int[2];
71        origin.getLocationOnScreen(xyRoot);
72
73        int y = xy[1] - xyRoot[1];
74
75        assertTrue("view should have y location on screen greater than drawing "
76                + "height of origen view (" + y + " is not greater than "
77                + origin.getHeight() + ")",
78                y > origin.getHeight());
79    }
80
81    /**
82     * Assert that view is above the visible screen.
83     * @param origin Te root view of the screen.
84     * @param view The view
85     */
86    static public void assertOffScreenAbove(View origin, View view) {
87        int[] xy = new int[2];
88        view.getLocationOnScreen(xy);
89
90        int[] xyRoot = new int[2];
91        origin.getLocationOnScreen(xyRoot);
92
93        int y = xy[1] - xyRoot[1];
94
95        assertTrue("view should have y location less than that of origin view",
96                y < 0);
97    }
98
99    /**
100     * Assert that a view has a particular x and y position on the visible screen.
101     * @param origin The root view of the screen.
102     * @param view The view.
103     * @param x The expected x coordinate.
104     * @param y The expected y coordinate.
105     */
106    static public void assertHasScreenCoordinates(View origin, View view, int x, int y) {
107        int[] xy = new int[2];
108        view.getLocationOnScreen(xy);
109
110        int[] xyRoot = new int[2];
111        origin.getLocationOnScreen(xyRoot);
112
113        assertEquals("x coordinate", x, xy[0] - xyRoot[0]);
114        assertEquals("y coordinate", y, xy[1] - xyRoot[1]);
115    }
116
117    /**
118     * Assert that two views are aligned on their baseline, that is that their baselines
119     * are on the same y location.
120     *
121     * @param first The first view
122     * @param second The second view
123     */
124    static public void assertBaselineAligned(View first, View second) {
125        int[] xy = new int[2];
126        first.getLocationOnScreen(xy);
127        int firstTop = xy[1] + first.getBaseline();
128
129        second.getLocationOnScreen(xy);
130        int secondTop = xy[1] + second.getBaseline();
131
132        assertEquals("views are not baseline aligned", firstTop, secondTop);
133    }
134
135    /**
136     * Assert that two views are right aligned, that is that their right edges
137     * are on the same x location.
138     *
139     * @param first The first view
140     * @param second The second view
141     */
142    static public void assertRightAligned(View first, View second) {
143        int[] xy = new int[2];
144        first.getLocationOnScreen(xy);
145        int firstRight = xy[0] + first.getMeasuredWidth();
146
147        second.getLocationOnScreen(xy);
148        int secondRight = xy[0] + second.getMeasuredWidth();
149
150        assertEquals("views are not right aligned", firstRight, secondRight);
151    }
152
153    /**
154     * Assert that two views are right aligned, that is that their right edges
155     * are on the same x location, with respect to the specified margin.
156     *
157     * @param first The first view
158     * @param second The second view
159     * @param margin The margin between the first view and the second view
160     */
161    static public void assertRightAligned(View first, View second, int margin) {
162        int[] xy = new int[2];
163        first.getLocationOnScreen(xy);
164        int firstRight = xy[0] + first.getMeasuredWidth();
165
166        second.getLocationOnScreen(xy);
167        int secondRight = xy[0] + second.getMeasuredWidth();
168
169        assertEquals("views are not right aligned", Math.abs(firstRight - secondRight), margin);
170    }
171
172    /**
173     * Assert that two views are left aligned, that is that their left edges
174     * are on the same x location.
175     *
176     * @param first The first view
177     * @param second The second view
178     */
179    static public void assertLeftAligned(View first, View second) {
180        int[] xy = new int[2];
181        first.getLocationOnScreen(xy);
182        int firstLeft = xy[0];
183
184        second.getLocationOnScreen(xy);
185        int secondLeft = xy[0];
186
187        assertEquals("views are not left aligned", firstLeft, secondLeft);
188    }
189
190    /**
191     * Assert that two views are left aligned, that is that their left edges
192     * are on the same x location, with respect to the specified margin.
193     *
194     * @param first The first view
195     * @param second The second view
196     * @param margin The margin between the first view and the second view
197     */
198    static public void assertLeftAligned(View first, View second, int margin) {
199        int[] xy = new int[2];
200        first.getLocationOnScreen(xy);
201        int firstLeft = xy[0];
202
203        second.getLocationOnScreen(xy);
204        int secondLeft = xy[0];
205
206        assertEquals("views are not left aligned", Math.abs(firstLeft - secondLeft), margin);
207    }
208
209    /**
210     * Assert that two views are bottom aligned, that is that their bottom edges
211     * are on the same y location.
212     *
213     * @param first The first view
214     * @param second The second view
215     */
216    static public void assertBottomAligned(View first, View second) {
217        int[] xy = new int[2];
218        first.getLocationOnScreen(xy);
219        int firstBottom = xy[1] + first.getMeasuredHeight();
220
221        second.getLocationOnScreen(xy);
222        int secondBottom = xy[1] + second.getMeasuredHeight();
223
224        assertEquals("views are not bottom aligned", firstBottom, secondBottom);
225    }
226
227    /**
228     * Assert that two views are bottom aligned, that is that their bottom edges
229     * are on the same y location, with respect to the specified margin.
230     *
231     * @param first The first view
232     * @param second The second view
233     * @param margin The margin between the first view and the second view
234     */
235    static public void assertBottomAligned(View first, View second, int margin) {
236        int[] xy = new int[2];
237        first.getLocationOnScreen(xy);
238        int firstBottom = xy[1] + first.getMeasuredHeight();
239
240        second.getLocationOnScreen(xy);
241        int secondBottom = xy[1] + second.getMeasuredHeight();
242
243        assertEquals("views are not bottom aligned", Math.abs(firstBottom - secondBottom), margin);
244    }
245
246    /**
247     * Assert that two views are top aligned, that is that their top edges
248     * are on the same y location.
249     *
250     * @param first The first view
251     * @param second The second view
252     */
253    static public void assertTopAligned(View first, View second) {
254        int[] xy = new int[2];
255        first.getLocationOnScreen(xy);
256        int firstTop = xy[1];
257
258        second.getLocationOnScreen(xy);
259        int secondTop = xy[1];
260
261        assertEquals("views are not top aligned", firstTop, secondTop);
262    }
263
264    /**
265     * Assert that two views are top aligned, that is that their top edges
266     * are on the same y location, with respect to the specified margin.
267     *
268     * @param first The first view
269     * @param second The second view
270     * @param margin The margin between the first view and the second view
271     */
272    static public void assertTopAligned(View first, View second, int margin) {
273        int[] xy = new int[2];
274        first.getLocationOnScreen(xy);
275        int firstTop = xy[1];
276
277        second.getLocationOnScreen(xy);
278        int secondTop = xy[1];
279
280        assertEquals("views are not top aligned", Math.abs(firstTop - secondTop), margin);
281    }
282
283    /**
284     * Assert that the <code>test</code> view is horizontally center aligned
285     * with respect to the <code>reference</code> view.
286     *
287     * @param reference The reference view
288     * @param test The view that should be center aligned with the reference view
289     */
290    static public void assertHorizontalCenterAligned(View reference, View test) {
291        int[] xy = new int[2];
292        reference.getLocationOnScreen(xy);
293        int referenceLeft = xy[0];
294
295        test.getLocationOnScreen(xy);
296        int testLeft = xy[0];
297
298        int center = (reference.getMeasuredWidth() - test.getMeasuredWidth()) / 2;
299        int delta = testLeft - referenceLeft;
300
301        assertEquals("views are not horizontally center aligned", center, delta);
302    }
303
304    /**
305     * Assert that the <code>test</code> view is vertically center aligned
306     * with respect to the <code>reference</code> view.
307     *
308     * @param reference The reference view
309     * @param test The view that should be center aligned with the reference view
310     */
311    static public void assertVerticalCenterAligned(View reference, View test) {
312        int[] xy = new int[2];
313        reference.getLocationOnScreen(xy);
314        int referenceTop = xy[1];
315
316        test.getLocationOnScreen(xy);
317        int testTop = xy[1];
318
319        int center = (reference.getMeasuredHeight() - test.getMeasuredHeight()) / 2;
320        int delta = testTop - referenceTop;
321
322        assertEquals("views are not vertically center aligned", center, delta);
323    }
324
325    /**
326     * Assert the specified group's integrity. The children count should be >= 0 and each
327     * child should be non-null.
328     *
329     * @param parent The group whose integrity to check
330     */
331    static public void assertGroupIntegrity(ViewGroup parent) {
332        final int count = parent.getChildCount();
333        assertTrue("child count should be >= 0", count >= 0);
334
335        for (int i = 0; i < count; i++) {
336            assertNotNull("group should not contain null children", parent.getChildAt(i));
337            assertSame(parent, parent.getChildAt(i).getParent());
338        }
339    }
340
341    /**
342     * Assert that the specified group contains a specific child once and only once.
343     *
344     * @param parent The group
345     * @param child The child that should belong to group
346     */
347    static public void assertGroupContains(ViewGroup parent, View child) {
348        final int count = parent.getChildCount();
349        assertTrue("Child count should be >= 0", count >= 0);
350
351        boolean found = false;
352        for (int i = 0; i < count; i++) {
353            if (parent.getChildAt(i) == child) {
354                if (!found) {
355                    found = true;
356                } else {
357                    assertTrue("child " + child + " is duplicated in parent", false);
358                }
359            }
360        }
361
362        assertTrue("group does not contain " + child, found);
363    }
364
365    /**
366     * Assert that the specified group does not contain a specific child.
367     *
368     * @param parent The group
369     * @param child The child that should not belong to group
370     */
371    static public void assertGroupNotContains(ViewGroup parent, View child) {
372        final int count = parent.getChildCount();
373        assertTrue("Child count should be >= 0", count >= 0);
374
375        for (int i = 0; i < count; i++) {
376            if (parent.getChildAt(i) == child) {
377                assertTrue("child " + child + " is found in parent", false);
378            }
379        }
380    }
381}
382