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 package com.android.keyguard; 17 18 import static org.junit.Assume.assumeFalse; 19 import static org.mockito.ArgumentMatchers.any; 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.when; 22 23 import android.content.pm.PackageManager; 24 import android.os.Handler; 25 import android.testing.TestableLooper; 26 import android.testing.TestableLooper.RunWithLooper; 27 import android.view.View; 28 29 import androidx.test.ext.junit.runners.AndroidJUnit4; 30 import androidx.test.filters.SmallTest; 31 32 import com.android.systemui.SysuiTestCase; 33 import com.android.systemui.dump.DumpManager; 34 import com.android.systemui.keyguard.KeyguardSliceProvider; 35 import com.android.systemui.plugins.ActivityStarter; 36 import com.android.systemui.settings.FakeDisplayTracker; 37 import com.android.systemui.statusbar.policy.ConfigurationController; 38 39 import org.junit.After; 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.ArgumentCaptor; 44 import org.mockito.Mock; 45 import org.mockito.MockitoAnnotations; 46 47 @SmallTest 48 @RunWith(AndroidJUnit4.class) 49 @RunWithLooper(setAsMainLooper = true) 50 public class KeyguardSliceViewControllerTest extends SysuiTestCase { 51 @Mock 52 private KeyguardSliceView mView; 53 @Mock 54 private ConfigurationController mConfigurationController; 55 @Mock 56 private ActivityStarter mActivityStarter; 57 private FakeDisplayTracker mDisplayTracker = new FakeDisplayTracker(mContext); 58 private DumpManager mDumpManager = new DumpManager(); 59 private Handler mHandler; 60 private Handler mBgHandler; 61 private KeyguardSliceViewController mController; 62 63 @Before setUp()64 public void setUp() throws Exception { 65 MockitoAnnotations.initMocks(this); 66 TestableLooper testableLooper = TestableLooper.get(this); 67 assert testableLooper != null; 68 mHandler = new Handler(testableLooper.getLooper()); 69 mBgHandler = new Handler(testableLooper.getLooper()); 70 when(mView.isAttachedToWindow()).thenReturn(true); 71 when(mView.getContext()).thenReturn(mContext); 72 mController = new KeyguardSliceViewController(mHandler, mBgHandler, mView, 73 mActivityStarter, mConfigurationController, mDumpManager, 74 mDisplayTracker); 75 mController.setupUri(KeyguardSliceProvider.KEYGUARD_SLICE_URI); 76 } 77 78 @After tearDown()79 public void tearDown() { 80 mController.onViewDetached(); 81 } 82 83 @Test refresh_replacesSliceContentAndNotifiesListener()84 public void refresh_replacesSliceContentAndNotifiesListener() { 85 // Skips the test if running on a watch because watches don't have a SliceManager system 86 // service. 87 assumeFalse(isWatch()); 88 89 mController.refresh(); 90 verify(mView).hideSlice(); 91 } 92 93 @Test onAttachedToWindow_registersListeners()94 public void onAttachedToWindow_registersListeners() { 95 // Skips the test if running on a watch because watches don't have a SliceManager system 96 // service. 97 assumeFalse(isWatch()); 98 99 mController.init(); 100 verify(mConfigurationController).addCallback( 101 any(ConfigurationController.ConfigurationListener.class)); 102 } 103 104 @Test onDetachedFromWindow_unregistersListeners()105 public void onDetachedFromWindow_unregistersListeners() { 106 // Skips the test if running on a watch because watches don't have a SliceManager system 107 // service. 108 assumeFalse(isWatch()); 109 110 ArgumentCaptor<View.OnAttachStateChangeListener> attachListenerArgumentCaptor = 111 ArgumentCaptor.forClass(View.OnAttachStateChangeListener.class); 112 113 mController.init(); 114 verify(mView).addOnAttachStateChangeListener(attachListenerArgumentCaptor.capture()); 115 116 attachListenerArgumentCaptor.getValue().onViewDetachedFromWindow(mView); 117 118 verify(mConfigurationController).removeCallback( 119 any(ConfigurationController.ConfigurationListener.class)); 120 } 121 isWatch()122 private boolean isWatch() { 123 final PackageManager pm = mContext.getPackageManager(); 124 return pm.hasSystemFeature(PackageManager.FEATURE_WATCH); 125 } 126 } 127