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.internal.policy;
18 
19 import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
20 import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT;
21 import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER;
22 import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
23 
24 import static org.hamcrest.Matchers.is;
25 import static org.junit.Assert.assertThat;
26 
27 import android.content.Context;
28 import android.graphics.Color;
29 import android.graphics.drawable.ColorDrawable;
30 import android.graphics.drawable.Drawable;
31 import android.platform.test.annotations.Presubmit;
32 import android.view.ActionMode;
33 import android.view.ContextThemeWrapper;
34 
35 import androidx.test.InstrumentationRegistry;
36 import androidx.test.filters.SmallTest;
37 import androidx.test.runner.AndroidJUnit4;
38 
39 import com.android.frameworks.coretests.R;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 
45 /**
46  * Tests {@link PhoneWindow}'s {@link ActionMode} related methods.
47  */
48 @SmallTest
49 @Presubmit
50 @RunWith(AndroidJUnit4.class)
51 public final class PhoneWindowTest {
52 
53     private PhoneWindow mPhoneWindow;
54     private Context mContext;
55 
56     @Before
setUp()57     public void setUp() throws Exception {
58         mContext = InstrumentationRegistry.getContext();
59     }
60 
61     @Test
layoutInDisplayCutoutMode_unset()62     public void layoutInDisplayCutoutMode_unset() throws Exception {
63         createPhoneWindowWithTheme(R.style.LayoutInDisplayCutoutModeUnset);
64         installDecor();
65 
66         assertThat(mPhoneWindow.getAttributes().layoutInDisplayCutoutMode,
67                 is(LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT));
68     }
69 
70     @Test
layoutInDisplayCutoutMode_default()71     public void layoutInDisplayCutoutMode_default() throws Exception {
72         createPhoneWindowWithTheme(R.style.LayoutInDisplayCutoutModeDefault);
73         installDecor();
74 
75         assertThat(mPhoneWindow.getAttributes().layoutInDisplayCutoutMode,
76                 is(LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT));
77     }
78 
79     @Test
layoutInDisplayCutoutMode_shortEdges()80     public void layoutInDisplayCutoutMode_shortEdges() throws Exception {
81         createPhoneWindowWithTheme(R.style.LayoutInDisplayCutoutModeShortEdges);
82         installDecor();
83 
84         assertThat(mPhoneWindow.getAttributes().layoutInDisplayCutoutMode,
85                 is(LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES));
86     }
87 
88     @Test
layoutInDisplayCutoutMode_never()89     public void layoutInDisplayCutoutMode_never() throws Exception {
90         createPhoneWindowWithTheme(R.style.LayoutInDisplayCutoutModeNever);
91         installDecor();
92 
93         assertThat(mPhoneWindow.getAttributes().layoutInDisplayCutoutMode,
94                 is(LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER));
95     }
96 
97     @Test
layoutInDisplayCutoutMode_always()98     public void layoutInDisplayCutoutMode_always() throws Exception {
99         createPhoneWindowWithTheme(R.style.LayoutInDisplayCutoutModeAlways);
100         installDecor();
101 
102         assertThat(mPhoneWindow.getAttributes().layoutInDisplayCutoutMode,
103                 is(LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS));
104     }
105 
106     @Test
testWindowBackground_colorLiteral()107     public void testWindowBackground_colorLiteral() {
108         createPhoneWindowWithTheme(R.style.WindowBackgroundColorLiteral);
109         installDecor();
110 
111         Drawable backgroundDrawable = mPhoneWindow.getDecorView().getBackground();
112         assertThat(backgroundDrawable instanceof ColorDrawable, is(true));
113 
114         ColorDrawable colorDrawable = (ColorDrawable) backgroundDrawable;
115         assertThat(colorDrawable.getColor(), is(Color.GREEN));
116     }
117 
118     @Test
testWindowBackgroundFallback_colorLiteral()119     public void testWindowBackgroundFallback_colorLiteral() {
120         createPhoneWindowWithTheme(R.style.WindowBackgroundFallbackColorLiteral);
121         installDecor();
122 
123         DecorView decorView = (DecorView) mPhoneWindow.getDecorView();
124         Drawable fallbackDrawable = decorView.getBackgroundFallback();
125 
126         assertThat(fallbackDrawable instanceof ColorDrawable, is(true));
127 
128         ColorDrawable colorDrawable = (ColorDrawable) fallbackDrawable;
129         assertThat(colorDrawable.getColor(), is(Color.BLUE));
130     }
131 
132     @Test
testWindowBackgroundFallbackWithExplicitBackgroundSet_colorLiteral()133     public void testWindowBackgroundFallbackWithExplicitBackgroundSet_colorLiteral() {
134         createPhoneWindowWithTheme(R.style.WindowBackgroundFallbackColorLiteral);
135         // set background before decorView is created
136         mPhoneWindow.setBackgroundDrawable(new ColorDrawable(Color.CYAN));
137         installDecor();
138         // clear background so that fallback is used
139         mPhoneWindow.setBackgroundDrawable(null);
140 
141         DecorView decorView = (DecorView) mPhoneWindow.getDecorView();
142         Drawable fallbackDrawable = decorView.getBackgroundFallback();
143 
144         assertThat(fallbackDrawable instanceof ColorDrawable, is(true));
145 
146         ColorDrawable colorDrawable = (ColorDrawable) fallbackDrawable;
147         assertThat(colorDrawable.getColor(), is(Color.BLUE));
148     }
149 
createPhoneWindowWithTheme(int theme)150     private void createPhoneWindowWithTheme(int theme) {
151         mPhoneWindow = new PhoneWindow(new ContextThemeWrapper(mContext, theme));
152     }
153 
installDecor()154     private void installDecor() {
155         mPhoneWindow.getDecorView();
156     }
157 }