SystemBarHelperTest.java revision 899caf2cf473e512567832d283e9750679f0bf4d
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.os.Build;
23import android.os.Build.VERSION;
24import android.os.Build.VERSION_CODES;
25import android.os.Handler;
26import android.os.HandlerThread;
27import android.os.SystemClock;
28import android.test.AndroidTestCase;
29import android.test.suitebuilder.annotation.SmallTest;
30import android.view.View;
31import android.view.Window;
32import android.view.WindowManager;
33
34import com.android.setupwizardlib.test.util.MockWindow;
35import com.android.setupwizardlib.util.SystemBarHelper;
36
37public class SystemBarHelperTest extends AndroidTestCase {
38
39    private static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
40
41    @SuppressLint("InlinedApi")
42    private static final int DEFAULT_IMMERSIVE_FLAGS =
43            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
44            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
45            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
46            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
47
48    @SuppressLint("InlinedApi")
49    private static final int DIALOG_IMMERSIVE_FLAGS =
50            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
51                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
52
53    @SmallTest
54    public void testAddVisibilityFlagView() {
55        final View view = createViewWithSystemUiVisibility(0x456);
56        SystemBarHelper.addVisibilityFlag(view, 0x1400);
57        if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
58            // Check that result is 0x1456, because 0x1400 | 0x456 = 0x1456.
59            assertEquals("View visibility should be 0x1456", 0x1456, view.getSystemUiVisibility());
60        }
61    }
62
63    @SmallTest
64    public void testRemoveVisibilityFlagView() {
65        final View view = createViewWithSystemUiVisibility(0x456);
66        SystemBarHelper.removeVisibilityFlag(view, 0x1400);
67        if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
68            // Check that result is 0x56, because 0x456 & ~0x1400 = 0x56.
69            assertEquals("View visibility should be 0x56", 0x56, view.getSystemUiVisibility());
70        }
71    }
72
73    @SmallTest
74    public void testAddVisibilityFlagWindow() {
75        final Window window = createWindowWithSystemUiVisibility(0x456);
76        SystemBarHelper.addVisibilityFlag(window, 0x1400);
77        if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
78            // Check that result is 0x1456 = 0x1400 | 0x456.
79            assertEquals("View visibility should be 0x1456", 0x1456,
80                    window.getAttributes().systemUiVisibility);
81        }
82    }
83
84    @SmallTest
85    public void testRemoveVisibilityFlagWindow() {
86        final Window window = createWindowWithSystemUiVisibility(0x456);
87        SystemBarHelper.removeVisibilityFlag(window, 0x1400);
88        if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
89            // Check that result is 0x56 = 0x456 & ~0x1400.
90            assertEquals("View visibility should be 0x56", 0x56,
91                    window.getAttributes().systemUiVisibility);
92        }
93    }
94
95    @SmallTest
96    public void testHideSystemBarsWindow() {
97        final Window window = createWindowWithSystemUiVisibility(0x456);
98        SystemBarHelper.hideSystemBars(window);
99        if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
100            assertEquals("DEFAULT_IMMERSIVE_FLAGS should be added to window's systemUiVisibility",
101                    DEFAULT_IMMERSIVE_FLAGS | 0x456,
102                    window.getAttributes().systemUiVisibility);
103            assertEquals(
104                    "DEFAULT_IMMERSIVE_FLAGS should be added to decorView's systemUiVisibility",
105                    DEFAULT_IMMERSIVE_FLAGS | 0x456,
106                    window.getDecorView().getSystemUiVisibility());
107        }
108    }
109
110    @SmallTest
111    public void testHideSystemBarsNoInfiniteLoop() throws InterruptedException {
112        final TestWindow window = new TestWindow(getContext(), null);
113        final HandlerThread thread = new HandlerThread("SystemBarHelperTest");
114        thread.start();
115        final Handler handler = new Handler(thread.getLooper());
116        handler.post(new Runnable() {
117            @Override
118            public void run() {
119                SystemBarHelper.hideSystemBars(window);
120            }
121        });
122        SystemClock.sleep(500);  // Wait for the looper to drain all the messages
123        thread.quit();
124        // Initial peek + 3 retries = 4 tries total
125        if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
126            assertEquals("Peek decor view should give up after 4 tries", 4,
127                    window.peekDecorViewCount);
128        }
129    }
130
131    @SmallTest
132    public void testHideSystemBarsDialog() {
133        final Dialog dialog = new Dialog(mContext);
134        if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
135            final WindowManager.LayoutParams attrs = dialog.getWindow().getAttributes();
136            attrs.systemUiVisibility = 0x456;
137            dialog.getWindow().setAttributes(attrs);
138        }
139
140        SystemBarHelper.hideSystemBars(dialog);
141        if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
142            assertEquals("DIALOG_IMMERSIVE_FLAGS should be added to window's systemUiVisibility",
143                    DIALOG_IMMERSIVE_FLAGS | 0x456,
144                    dialog.getWindow().getAttributes().systemUiVisibility);
145        }
146    }
147
148    @SmallTest
149    public void testSetBackButtonVisibleTrue() {
150        final Window window = createWindowWithSystemUiVisibility(0x456);
151        SystemBarHelper.setBackButtonVisible(window, true);
152        if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
153            assertEquals("View visibility should be 0x456", 0x456,
154                    window.getAttributes().systemUiVisibility);
155        }
156    }
157
158    @SmallTest
159    public void testSetBackButtonVisibleFalse() {
160        final Window window = createWindowWithSystemUiVisibility(0x456);
161        SystemBarHelper.setBackButtonVisible(window, false);
162        if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
163            assertEquals("STATUS_BAR_DISABLE_BACK should be added to systemUiVisibility",
164                    0x456 | STATUS_BAR_DISABLE_BACK, window.getAttributes().systemUiVisibility);
165        }
166    }
167
168    private View createViewWithSystemUiVisibility(int vis) {
169        final View view = new View(getContext());
170        if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
171            view.setSystemUiVisibility(vis);
172        }
173        return view;
174    }
175
176    private Window createWindowWithSystemUiVisibility(int vis) {
177        final Window window = new TestWindow(getContext(), createViewWithSystemUiVisibility(vis));
178        if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
179            WindowManager.LayoutParams attrs = window.getAttributes();
180            attrs.systemUiVisibility = vis;
181            window.setAttributes(attrs);
182        }
183        return window;
184    }
185
186    private static class TestWindow extends MockWindow {
187
188        private View mDecorView;
189        public int peekDecorViewCount = 0;
190
191        public TestWindow(Context context, View decorView) {
192            super(context);
193            mDecorView = decorView;
194        }
195
196        @Override
197        public View getDecorView() {
198            return mDecorView;
199        }
200
201        @Override
202        public View peekDecorView() {
203            peekDecorViewCount++;
204            return mDecorView;
205        }
206
207        @Override
208        public void setNavigationBarColor(int i) {
209        }
210
211        @Override
212        public void setStatusBarColor(int i) {
213        }
214    }
215}
216