1/*
2 * Copyright (C) 2015 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 com.android.setupwizardlib.test;
18
19import android.annotation.SuppressLint;
20import android.app.Dialog;
21import android.content.Context;
22import android.content.res.Configuration;
23import android.graphics.drawable.Drawable;
24import android.net.Uri;
25import android.os.Build.VERSION;
26import android.os.Build.VERSION_CODES;
27import android.os.Bundle;
28import android.test.AndroidTestCase;
29import android.test.suitebuilder.annotation.SmallTest;
30import android.view.InputQueue;
31import android.view.KeyEvent;
32import android.view.LayoutInflater;
33import android.view.MotionEvent;
34import android.view.SurfaceHolder.Callback2;
35import android.view.View;
36import android.view.ViewGroup.LayoutParams;
37import android.view.Window;
38import android.view.WindowManager;
39
40import com.android.setupwizardlib.util.SystemBarHelper;
41
42public class SystemBarHelperTest extends AndroidTestCase {
43
44    private static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
45
46    @SuppressLint("InlinedApi")
47    private static final int DEFAULT_IMMERSIVE_FLAGS =
48            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
49            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
50            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
51            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
52
53    @SuppressLint("InlinedApi")
54    private static final int DIALOG_IMMERSIVE_FLAGS =
55            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
56                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
57
58    @SmallTest
59    public void testAddVisibilityFlagView() {
60        final View view = createViewWithSystemUiVisibility(0x456);
61        SystemBarHelper.addVisibilityFlag(view, 0x1400);
62        if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
63            // Check that result is 0x1456, because 0x1400 | 0x456 = 0x1456.
64            assertEquals("View visibility should be 0x1456", 0x1456, view.getSystemUiVisibility());
65        }
66    }
67
68    @SmallTest
69    public void testRemoveVisibilityFlagView() {
70        final View view = createViewWithSystemUiVisibility(0x456);
71        SystemBarHelper.removeVisibilityFlag(view, 0x1400);
72        if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
73            // Check that result is 0x56, because 0x456 & ~0x1400 = 0x56.
74            assertEquals("View visibility should be 0x56", 0x56, view.getSystemUiVisibility());
75        }
76    }
77
78    @SmallTest
79    public void testAddVisibilityFlagWindow() {
80        final Window window = createWindowWithSystemUiVisibility(0x456);
81        SystemBarHelper.addVisibilityFlag(window, 0x1400);
82        if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
83            // Check that result is 0x1456 = 0x1400 | 0x456.
84            assertEquals("View visibility should be 0x1456", 0x1456,
85                    window.getAttributes().systemUiVisibility);
86        }
87    }
88
89    @SmallTest
90    public void testRemoveVisibilityFlagWindow() {
91        final Window window = createWindowWithSystemUiVisibility(0x456);
92        SystemBarHelper.removeVisibilityFlag(window, 0x1400);
93        if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
94            // Check that result is 0x56 = 0x456 & ~0x1400.
95            assertEquals("View visibility should be 0x56", 0x56,
96                    window.getAttributes().systemUiVisibility);
97        }
98    }
99
100    @SmallTest
101    public void testHideSystemBarsWindow() {
102        final Window window = createWindowWithSystemUiVisibility(0x456);
103        SystemBarHelper.hideSystemBars(window);
104        if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
105            assertEquals("DEFAULT_IMMERSIVE_FLAGS should be added to window's systemUiVisibility",
106                    DEFAULT_IMMERSIVE_FLAGS | 0x456,
107                    window.getAttributes().systemUiVisibility);
108            assertEquals(
109                    "DEFAULT_IMMERSIVE_FLAGS should be added to decorView's systemUiVisibility",
110                    DEFAULT_IMMERSIVE_FLAGS | 0x456,
111                    window.getDecorView().getSystemUiVisibility());
112        }
113    }
114
115    @SmallTest
116    public void testHideSystemBarsDialog() {
117        final Dialog dialog = new Dialog(mContext);
118        if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
119            final WindowManager.LayoutParams attrs = dialog.getWindow().getAttributes();
120            attrs.systemUiVisibility = 0x456;
121            dialog.getWindow().setAttributes(attrs);
122        }
123
124        SystemBarHelper.hideSystemBars(dialog);
125        if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
126            assertEquals("DIALOG_IMMERSIVE_FLAGS should be added to window's systemUiVisibility",
127                    DIALOG_IMMERSIVE_FLAGS | 0x456,
128                    dialog.getWindow().getAttributes().systemUiVisibility);
129        }
130    }
131
132    @SmallTest
133    public void testSetBackButtonVisibleTrue() {
134        final Window window = createWindowWithSystemUiVisibility(0x456);
135        SystemBarHelper.setBackButtonVisible(window, true);
136        if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
137            assertEquals("View visibility should be 0x456", 0x456,
138                    window.getAttributes().systemUiVisibility);
139        }
140    }
141
142    @SmallTest
143    public void testSetBackButtonVisibleFalse() {
144        final Window window = createWindowWithSystemUiVisibility(0x456);
145        SystemBarHelper.setBackButtonVisible(window, false);
146        if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
147            assertEquals("STATUS_BAR_DISABLE_BACK should be added to systemUiVisibility",
148                    0x456 | STATUS_BAR_DISABLE_BACK, window.getAttributes().systemUiVisibility);
149        }
150    }
151
152    private View createViewWithSystemUiVisibility(int vis) {
153        final View view = new View(getContext());
154        if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
155            view.setSystemUiVisibility(vis);
156        }
157        return view;
158    }
159
160    private Window createWindowWithSystemUiVisibility(int vis) {
161        final Window window = new TestWindow(getContext(), createViewWithSystemUiVisibility(vis));
162        if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
163            WindowManager.LayoutParams attrs = window.getAttributes();
164            attrs.systemUiVisibility = vis;
165            window.setAttributes(attrs);
166        }
167        return window;
168    }
169
170    private static class TestWindow extends Window {
171
172        private View mDecorView;
173
174        public TestWindow(Context context, View decorView) {
175            super(context);
176            mDecorView = decorView;
177        }
178
179        @Override
180        public void takeSurface(Callback2 callback2) {
181
182        }
183
184        @Override
185        public void takeInputQueue(InputQueue.Callback callback) {
186
187        }
188
189        @Override
190        public boolean isFloating() {
191            return false;
192        }
193
194        @Override
195        public void setContentView(int i) {
196
197        }
198
199        @Override
200        public void setContentView(View view) {
201
202        }
203
204        @Override
205        public void setContentView(View view, LayoutParams layoutParams) {
206
207        }
208
209        @Override
210        public void addContentView(View view, LayoutParams layoutParams) {
211
212        }
213
214        @Override
215        public View getCurrentFocus() {
216            return null;
217        }
218
219        @Override
220        public LayoutInflater getLayoutInflater() {
221            return LayoutInflater.from(getContext());
222        }
223
224        @Override
225        public void setTitle(CharSequence charSequence) {
226
227        }
228
229        @Override
230        public void setTitleColor(int i) {
231
232        }
233
234        @Override
235        public void openPanel(int i, KeyEvent keyEvent) {
236
237        }
238
239        @Override
240        public void closePanel(int i) {
241
242        }
243
244        @Override
245        public void togglePanel(int i, KeyEvent keyEvent) {
246
247        }
248
249        @Override
250        public void invalidatePanelMenu(int i) {
251
252        }
253
254        @Override
255        public boolean performPanelShortcut(int i, int i2, KeyEvent keyEvent, int i3) {
256            return false;
257        }
258
259        @Override
260        public boolean performPanelIdentifierAction(int i, int i2, int i3) {
261            return false;
262        }
263
264        @Override
265        public void closeAllPanels() {
266
267        }
268
269        @Override
270        public boolean performContextMenuIdentifierAction(int i, int i2) {
271            return false;
272        }
273
274        @Override
275        public void onConfigurationChanged(Configuration configuration) {
276
277        }
278
279        @Override
280        public void setBackgroundDrawable(Drawable drawable) {
281
282        }
283
284        @Override
285        public void setFeatureDrawableResource(int i, int i2) {
286
287        }
288
289        @Override
290        public void setFeatureDrawableUri(int i, Uri uri) {
291
292        }
293
294        @Override
295        public void setFeatureDrawable(int i, Drawable drawable) {
296
297        }
298
299        @Override
300        public void setFeatureDrawableAlpha(int i, int i2) {
301
302        }
303
304        @Override
305        public void setFeatureInt(int i, int i2) {
306
307        }
308
309        @Override
310        public void takeKeyEvents(boolean b) {
311
312        }
313
314        @Override
315        public boolean superDispatchKeyEvent(KeyEvent keyEvent) {
316            return false;
317        }
318
319        @Override
320        public boolean superDispatchKeyShortcutEvent(KeyEvent keyEvent) {
321            return false;
322        }
323
324        @Override
325        public boolean superDispatchTouchEvent(MotionEvent motionEvent) {
326            return false;
327        }
328
329        @Override
330        public boolean superDispatchTrackballEvent(MotionEvent motionEvent) {
331            return false;
332        }
333
334        @Override
335        public boolean superDispatchGenericMotionEvent(MotionEvent motionEvent) {
336            return false;
337        }
338
339        @Override
340        public View getDecorView() {
341            return mDecorView;
342        }
343
344        @Override
345        public View peekDecorView() {
346            return mDecorView;
347        }
348
349        @Override
350        public Bundle saveHierarchyState() {
351            return null;
352        }
353
354        @Override
355        public void restoreHierarchyState(Bundle bundle) {
356
357        }
358
359        @Override
360        protected void onActive() {
361
362        }
363
364        @Override
365        public void setChildDrawable(int i, Drawable drawable) {
366
367        }
368
369        @Override
370        public void setChildInt(int i, int i2) {
371
372        }
373
374        @Override
375        public boolean isShortcutKey(int i, KeyEvent keyEvent) {
376            return false;
377        }
378
379        @Override
380        public void setVolumeControlStream(int i) {
381
382        }
383
384        @Override
385        public int getVolumeControlStream() {
386            return 0;
387        }
388
389        @Override
390        public int getStatusBarColor() {
391            return 0;
392        }
393
394        @Override
395        public void setStatusBarColor(int i) {
396
397        }
398
399        @Override
400        public int getNavigationBarColor() {
401            return 0;
402        }
403
404        @Override
405        public void setNavigationBarColor(int i) {
406
407        }
408    }
409}
410