Home | History | Annotate | Download | only in appinfo
      1 /*
      2  * Copyright (C) 2017 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.applications.appinfo;
     18 
     19 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
     20 import static org.junit.Assert.assertEquals;
     21 import static org.mockito.Matchers.eq;
     22 import static org.mockito.Matchers.nullable;
     23 import static org.mockito.Mockito.verify;
     24 import static org.mockito.Mockito.when;
     25 
     26 import android.app.Activity;
     27 import android.content.Context;
     28 import android.view.Window;
     29 import android.view.WindowManager.LayoutParams;
     30 
     31 import com.android.internal.logging.nano.MetricsProto;
     32 import com.android.settings.testutils.FakeFeatureFactory;
     33 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     34 import com.android.settings.testutils.shadow.ShadowAppInfoBase;
     35 
     36 import org.junit.Before;
     37 import org.junit.Test;
     38 import org.junit.runner.RunWith;
     39 import org.mockito.Answers;
     40 import org.mockito.ArgumentCaptor;
     41 import org.mockito.Mock;
     42 import org.mockito.MockitoAnnotations;
     43 import org.mockito.Spy;
     44 import org.robolectric.RuntimeEnvironment;
     45 import org.robolectric.annotation.Config;
     46 
     47 @RunWith(SettingsRobolectricTestRunner.class)
     48 public class DrawOverlayDetailsTest {
     49 
     50     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     51     private Activity mActivity;
     52 
     53     @Mock
     54     private Window mWindow;
     55 
     56     private LayoutParams layoutParams;
     57 
     58     private FakeFeatureFactory mFeatureFactory;
     59 
     60     @Spy
     61     private DrawOverlayDetails mFragment;
     62 
     63     @Before
     64     public void setUp() {
     65         MockitoAnnotations.initMocks(this);
     66 
     67         mFeatureFactory = FakeFeatureFactory.setupForTest();
     68         layoutParams = new LayoutParams();
     69     }
     70 
     71     @Test
     72     public void logSpecialPermissionChange() {
     73         when(mFragment.getContext()).thenReturn(RuntimeEnvironment.application);
     74 
     75         mFragment.logSpecialPermissionChange(true, "app");
     76         verify(mFeatureFactory.metricsFeatureProvider).action(nullable(Context.class),
     77                 eq(MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_APPDRAW_ALLOW), eq("app"));
     78 
     79         mFragment.logSpecialPermissionChange(false, "app");
     80         verify(mFeatureFactory.metricsFeatureProvider).action(nullable(Context.class),
     81                 eq(MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_APPDRAW_DENY), eq("app"));
     82     }
     83 
     84     @Test
     85     @Config(shadows = {ShadowAppInfoBase.class})
     86     public void hideNonSystemOverlaysWhenResumed() {
     87         when(mFragment.getActivity()).thenReturn(mActivity);
     88         when(mActivity.getWindow()).thenReturn(mWindow);
     89         when(mWindow.getAttributes()).thenReturn(layoutParams);
     90 
     91         mFragment.onResume();
     92         verify(mWindow).addPrivateFlags(PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
     93 
     94         mFragment.onPause();
     95 
     96         // There's no Window.clearPrivateFlags() method, so the Window.attributes are updated.
     97         ArgumentCaptor<LayoutParams> paramCaptor = ArgumentCaptor.forClass(LayoutParams.class);
     98         verify(mWindow).setAttributes(paramCaptor.capture());
     99         assertEquals(0,
    100                 paramCaptor.getValue().privateFlags & PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
    101     }
    102 }
    103