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