1 /*
2  * Copyright (C) 2018 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 
17 package com.android.settings.homepage;
18 
19 import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 
28 import android.os.Build;
29 import android.view.View;
30 import android.view.Window;
31 import android.view.WindowManager;
32 import android.widget.FrameLayout;
33 
34 import com.android.settings.R;
35 import com.android.settings.core.HideNonSystemOverlayMixin;
36 import com.android.settings.homepage.contextualcards.slices.BatteryFixSliceTest;
37 
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.ArgumentCaptor;
42 import org.mockito.MockitoAnnotations;
43 import org.robolectric.Robolectric;
44 import org.robolectric.RobolectricTestRunner;
45 import org.robolectric.android.controller.ActivityController;
46 import org.robolectric.annotation.Config;
47 import org.robolectric.util.ReflectionHelpers;
48 
49 @RunWith(RobolectricTestRunner.class)
50 public class SettingsHomepageActivityTest {
51     @Before
setUp()52     public void setUp() {
53         MockitoAnnotations.initMocks(this);
54     }
55 
56     @Test
setHomepageContainerPaddingTop_shouldBeSetPaddingTop()57     public void setHomepageContainerPaddingTop_shouldBeSetPaddingTop() {
58         final SettingsHomepageActivity activity = Robolectric.buildActivity(
59                 SettingsHomepageActivity.class).create().get();
60         final int searchBarHeight = activity.getResources().getDimensionPixelSize(
61                 R.dimen.search_bar_height);
62         final int searchBarMargin = activity.getResources().getDimensionPixelSize(
63                 R.dimen.search_bar_margin);
64         final View view = activity.findViewById(R.id.homepage_container);
65 
66         activity.setHomepageContainerPaddingTop();
67 
68         final int actualPaddingTop = view.getPaddingTop();
69         assertThat(actualPaddingTop).isEqualTo(searchBarHeight + searchBarMargin * 2);
70     }
71 
72     @Test
launch_shouldHaveAnimationForIaFragment()73     public void launch_shouldHaveAnimationForIaFragment() {
74         final SettingsHomepageActivity activity = Robolectric.buildActivity(
75                 SettingsHomepageActivity.class).create().get();
76         final FrameLayout frameLayout = activity.findViewById(R.id.main_content);
77 
78         assertThat(frameLayout.getLayoutTransition()).isNotNull();
79     }
80 
81     @Test
82     @Config(shadows = {
83             BatteryFixSliceTest.ShadowBatteryStatsHelperLoader.class,
84             BatteryFixSliceTest.ShadowBatteryTipLoader.class
85     })
onStart_isNotDebuggable_shouldHideSystemOverlay()86     public void onStart_isNotDebuggable_shouldHideSystemOverlay() {
87         ReflectionHelpers.setStaticField(Build.class, "IS_DEBUGGABLE", false);
88 
89         final ActivityController<SettingsHomepageActivity> activityController =
90                 Robolectric.buildActivity(SettingsHomepageActivity.class).create();
91         final SettingsHomepageActivity activity = spy(activityController.get());
92         final Window window = mock(Window.class);
93         when(activity.getWindow()).thenReturn(window);
94         activity.getLifecycle().addObserver(new HideNonSystemOverlayMixin(activity));
95 
96         activityController.start();
97 
98         verify(window).addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
99     }
100 
101     @Test
102     @Config(shadows = {
103             BatteryFixSliceTest.ShadowBatteryStatsHelperLoader.class,
104             BatteryFixSliceTest.ShadowBatteryTipLoader.class,
105     })
onStop_isNotDebuggable_shouldRemoveHideSystemOverlay()106     public void onStop_isNotDebuggable_shouldRemoveHideSystemOverlay() {
107         ReflectionHelpers.setStaticField(Build.class, "IS_DEBUGGABLE", false);
108 
109         final ActivityController<SettingsHomepageActivity> activityController =
110                 Robolectric.buildActivity(SettingsHomepageActivity.class).create();
111         final SettingsHomepageActivity activity = spy(activityController.get());
112         final Window window = mock(Window.class);
113         when(activity.getWindow()).thenReturn(window);
114         activity.getLifecycle().addObserver(new HideNonSystemOverlayMixin(activity));
115 
116         activityController.start();
117 
118         verify(window).addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
119 
120         final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
121         when(window.getAttributes()).thenReturn(layoutParams);
122 
123         activityController.stop();
124         final ArgumentCaptor<WindowManager.LayoutParams> paramCaptor = ArgumentCaptor.forClass(
125                 WindowManager.LayoutParams.class);
126 
127         verify(window).setAttributes(paramCaptor.capture());
128         assertThat(paramCaptor.getValue().privateFlags
129                 & SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS).isEqualTo(0);
130     }
131 }
132