summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/gallery3d/ui/GLViewTest.java
blob: b17b254407e8c3e81813b11befa41efd0a3b3b62 (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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * Copyright (C) 2010 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 com.android.gallery3d.ui;

import android.graphics.Rect;
import android.test.suitebuilder.annotation.SmallTest;
import android.view.MotionEvent;

import junit.framework.TestCase;

@SmallTest
public class GLViewTest extends TestCase {
    @SuppressWarnings("unused")
    private static final String TAG = "GLViewTest";

    @SmallTest
    public void testVisibility() {
        GLViewMock a = new GLViewMock();
        assertEquals(GLView.VISIBLE, a.getVisibility());
        assertEquals(0, a.mOnVisibilityChangedCalled);
        a.setVisibility(GLView.INVISIBLE);
        assertEquals(GLView.INVISIBLE, a.getVisibility());
        assertEquals(1, a.mOnVisibilityChangedCalled);
        a.setVisibility(GLView.VISIBLE);
        assertEquals(GLView.VISIBLE, a.getVisibility());
        assertEquals(2, a.mOnVisibilityChangedCalled);
    }

    @SmallTest
    public void testComponents() {
        GLView view = new GLView();
        assertEquals(0, view.getComponentCount());
        try {
            view.getComponent(0);
            fail();
        } catch (IndexOutOfBoundsException ex) {
            // expected
        }

        GLView x = new GLView();
        GLView y = new GLView();
        view.addComponent(x);
        view.addComponent(y);
        assertEquals(2, view.getComponentCount());
        assertSame(x, view.getComponent(0));
        assertSame(y, view.getComponent(1));
        view.removeComponent(x);
        assertSame(y, view.getComponent(0));
        try {
            view.getComponent(1);
            fail();
        } catch (IndexOutOfBoundsException ex) {
            // expected
        }
        try {
            view.addComponent(y);
            fail();
        } catch (IllegalStateException ex) {
            // expected
        }
        view.addComponent(x);
        view.removeAllComponents();
        assertEquals(0, view.getComponentCount());
    }

    @SmallTest
    public void testBounds() {
        GLView view = new GLView();

        assertEquals(0, view.getWidth());
        assertEquals(0, view.getHeight());

        Rect b = view.bounds();
        assertEquals(0, b.left);
        assertEquals(0, b.top);
        assertEquals(0, b.right);
        assertEquals(0, b.bottom);

        view.layout(10, 20, 30, 100);
        assertEquals(20, view.getWidth());
        assertEquals(80, view.getHeight());

        b = view.bounds();
        assertEquals(10, b.left);
        assertEquals(20, b.top);
        assertEquals(30, b.right);
        assertEquals(100, b.bottom);
    }

    @SmallTest
    public void testParent() {
        GLView a = new GLView();
        GLView b = new GLView();
        assertNull(b.mParent);
        a.addComponent(b);
        assertSame(a, b.mParent);
        a.removeComponent(b);
        assertNull(b.mParent);
    }

    @SmallTest
    public void testRoot() {
        GLViewMock a = new GLViewMock();
        GLViewMock b = new GLViewMock();
        GLRoot r = new GLRootStub();
        GLRoot r2 = new GLRootStub();
        a.addComponent(b);

        // Attach to root r
        assertEquals(0, a.mOnAttachCalled);
        assertEquals(0, b.mOnAttachCalled);
        a.attachToRoot(r);
        assertEquals(1, a.mOnAttachCalled);
        assertEquals(1, b.mOnAttachCalled);
        assertSame(r, a.getGLRoot());
        assertSame(r, b.getGLRoot());

        // Detach from r
        assertEquals(0, a.mOnDetachCalled);
        assertEquals(0, b.mOnDetachCalled);
        a.detachFromRoot();
        assertEquals(1, a.mOnDetachCalled);
        assertEquals(1, b.mOnDetachCalled);

        // Attach to another root r2
        assertEquals(1, a.mOnAttachCalled);
        assertEquals(1, b.mOnAttachCalled);
        a.attachToRoot(r2);
        assertEquals(2, a.mOnAttachCalled);
        assertEquals(2, b.mOnAttachCalled);
        assertSame(r2, a.getGLRoot());
        assertSame(r2, b.getGLRoot());

        // Detach from r2
        assertEquals(1, a.mOnDetachCalled);
        assertEquals(1, b.mOnDetachCalled);
        a.detachFromRoot();
        assertEquals(2, a.mOnDetachCalled);
        assertEquals(2, b.mOnDetachCalled);
    }

    @SmallTest
    public void testRoot2() {
        GLView a = new GLViewMock();
        GLViewMock b = new GLViewMock();
        GLRoot r = new GLRootStub();

        a.attachToRoot(r);

        assertEquals(0, b.mOnAttachCalled);
        a.addComponent(b);
        assertEquals(1, b.mOnAttachCalled);

        assertEquals(0, b.mOnDetachCalled);
        a.removeComponent(b);
        assertEquals(1, b.mOnDetachCalled);
    }

    @SmallTest
    public void testInvalidate() {
        GLView a = new GLView();
        GLRootMock r = new GLRootMock();
        a.attachToRoot(r);
        assertEquals(0, r.mRequestRenderCalled);
        a.invalidate();
        assertEquals(1, r.mRequestRenderCalled);
    }

    @SmallTest
    public void testRequestLayout() {
        GLView a = new GLView();
        GLView b = new GLView();
        GLRootMock r = new GLRootMock();
        a.attachToRoot(r);
        a.addComponent(b);
        assertEquals(0, r.mRequestLayoutContentPaneCalled);
        b.requestLayout();
        assertEquals(1, r.mRequestLayoutContentPaneCalled);
    }

    @SmallTest
    public void testLayout() {
        GLViewMock a = new GLViewMock();
        GLViewMock b = new GLViewMock();
        GLViewMock c = new GLViewMock();
        GLRootMock r = new GLRootMock();

        a.attachToRoot(r);
        a.addComponent(b);
        a.addComponent(c);

        assertEquals(0, a.mOnLayoutCalled);
        a.layout(10, 20, 60, 100);
        assertEquals(1, a.mOnLayoutCalled);
        assertEquals(1, b.mOnLayoutCalled);
        assertEquals(1, c.mOnLayoutCalled);
        assertTrue(a.mOnLayoutChangeSize);
        assertTrue(b.mOnLayoutChangeSize);
        assertTrue(c.mOnLayoutChangeSize);

        // same size should not trigger onLayout
        a.layout(10, 20, 60, 100);
        assertEquals(1, a.mOnLayoutCalled);

        // unless someone requested it, but only those on the path
        // to the requester.
        assertEquals(0, r.mRequestLayoutContentPaneCalled);
        b.requestLayout();
        a.layout(10, 20, 60, 100);
        assertEquals(1, r.mRequestLayoutContentPaneCalled);
        assertEquals(2, a.mOnLayoutCalled);
        assertEquals(2, b.mOnLayoutCalled);
        assertEquals(1, c.mOnLayoutCalled);
    }

    @SmallTest
    public void testRender() {
        GLViewMock a = new GLViewMock();
        GLViewMock b = new GLViewMock();

        a.addComponent(b);
        GLCanvasStub canvas = new GLCanvasStub();
        assertEquals(0, a.mRenderBackgroundCalled);
        assertEquals(0, b.mRenderBackgroundCalled);
        a.render(canvas);
        assertEquals(1, a.mRenderBackgroundCalled);
        assertEquals(1, b.mRenderBackgroundCalled);
    }

    @SmallTest
    public void testMeasure() {
        GLViewMock a = new GLViewMock();
        GLViewMock b = new GLViewMock();
        GLViewMock c = new GLViewMock();
        GLRootMock r = new GLRootMock();

        a.addComponent(b);
        a.addComponent(c);
        a.attachToRoot(r);

        assertEquals(0, a.mOnMeasureCalled);
        a.measure(100, 200);
        assertEquals(1, a.mOnMeasureCalled);
        assertEquals(1, b.mOnMeasureCalled);
        assertEquals(100, a.mOnMeasureWidthSpec);
        assertEquals(200, a.mOnMeasureHeightSpec);
        assertEquals(100, b.mOnMeasureWidthSpec);
        assertEquals(200, b.mOnMeasureHeightSpec);
        assertEquals(100, a.getMeasuredWidth());
        assertEquals(200, b.getMeasuredHeight());

        // same spec should not trigger onMeasure
        a.measure(100, 200);
        assertEquals(1, a.mOnMeasureCalled);

        // unless someone requested it, but only those on the path
        // to the requester.
        b.requestLayout();
        a.measure(100, 200);
        assertEquals(2, a.mOnMeasureCalled);
        assertEquals(2, b.mOnMeasureCalled);
        assertEquals(1, c.mOnMeasureCalled);
    }

    class MyGLView extends GLView {
        private int mWidth;
        int mOnTouchCalled;
        int mOnTouchX;
        int mOnTouchY;
        int mOnTouchAction;

        public MyGLView(int width) {
            mWidth = width;
        }

        @Override
        protected void onLayout(boolean changeSize, int left, int top,
                int right, int bottom) {
            // layout children from left to right
            // call children's layout.
            int x = 0;
            for (int i = 0, n = getComponentCount(); i < n; ++i) {
                GLView item = getComponent(i);
                item.measure(0, 0);
                int w = item.getMeasuredWidth();
                int h = item.getMeasuredHeight();
                item.layout(x, 0, x + w, h);
                x += w;
            }
        }

        @Override
        protected void onMeasure(int widthSpec, int heightSpec) {
            setMeasuredSize(mWidth, 100);
        }

        @Override
        protected boolean onTouch(MotionEvent event) {
            mOnTouchCalled++;
            mOnTouchX = (int) event.getX();
            mOnTouchY = (int) event.getY();
            mOnTouchAction = event.getAction();
            return true;
        }
    }

    private MotionEvent NewMotionEvent(int action, int x, int y) {
        return MotionEvent.obtain(0, 0, action, x, y, 0);
    }

    @SmallTest
    public void testTouchEvent() {
        // We construct a tree with four nodes. Only the x coordinate is used:
        // A = [0..............................300)
        // B = [0......100)
        // C =             [100......200)
        // D =             [100..150)

        MyGLView a = new MyGLView(300);
        MyGLView b = new MyGLView(100);
        MyGLView c = new MyGLView(100);
        MyGLView d = new MyGLView(50);
        GLRoot r = new GLRootStub();

        a.addComponent(b);
        a.addComponent(c);
        c.addComponent(d);
        a.attachToRoot(r);
        a.layout(0, 0, 300, 100);

        int DOWN = MotionEvent.ACTION_DOWN;
        int UP = MotionEvent.ACTION_UP;
        int MOVE = MotionEvent.ACTION_MOVE;
        int CANCEL = MotionEvent.ACTION_CANCEL;

        // simple case
        assertEquals(0, a.mOnTouchCalled);
        a.dispatchTouchEvent(NewMotionEvent(DOWN, 250, 0));
        assertEquals(DOWN, a.mOnTouchAction);
        a.dispatchTouchEvent(NewMotionEvent(UP, 250, 0));
        assertEquals(UP, a.mOnTouchAction);
        assertEquals(2, a.mOnTouchCalled);

        // pass to a child, check the location is offseted.
        assertEquals(0, c.mOnTouchCalled);
        a.dispatchTouchEvent(NewMotionEvent(DOWN, 175, 0));
        a.dispatchTouchEvent(NewMotionEvent(UP, 175, 0));
        assertEquals(75, c.mOnTouchX);
        assertEquals(0, c.mOnTouchY);
        assertEquals(2, c.mOnTouchCalled);
        assertEquals(2, a.mOnTouchCalled);

        // motion target cancel event
        assertEquals(0, d.mOnTouchCalled);
        a.dispatchTouchEvent(NewMotionEvent(DOWN, 125, 0));
        assertEquals(1, d.mOnTouchCalled);
        a.dispatchTouchEvent(NewMotionEvent(MOVE, 250, 0));
        assertEquals(2, d.mOnTouchCalled);
        a.dispatchTouchEvent(NewMotionEvent(MOVE, 50, 0));
        assertEquals(3, d.mOnTouchCalled);
        a.dispatchTouchEvent(NewMotionEvent(DOWN, 175, 0));
        assertEquals(4, d.mOnTouchCalled);
        assertEquals(CANCEL, d.mOnTouchAction);
        assertEquals(3, c.mOnTouchCalled);
        assertEquals(DOWN, c.mOnTouchAction);
        a.dispatchTouchEvent(NewMotionEvent(UP, 175, 0));

        // motion target is removed
        assertEquals(4, d.mOnTouchCalled);
        a.dispatchTouchEvent(NewMotionEvent(DOWN, 125, 0));
        assertEquals(5, d.mOnTouchCalled);
        a.removeComponent(c);
        assertEquals(6, d.mOnTouchCalled);
        assertEquals(CANCEL, d.mOnTouchAction);

        // invisible component should not get events
        assertEquals(2, a.mOnTouchCalled);
        assertEquals(0, b.mOnTouchCalled);
        b.setVisibility(GLView.INVISIBLE);
        a.dispatchTouchEvent(NewMotionEvent(DOWN, 50, 0));
        assertEquals(3, a.mOnTouchCalled);
        assertEquals(0, b.mOnTouchCalled);
    }
}