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.mockito.ArgumentMatchers.any; 19 import static org.mockito.ArgumentMatchers.anyString; 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.when; 22 23 import android.test.suitebuilder.annotation.SmallTest; 24 import android.testing.AndroidTestingRunner; 25 import android.testing.TestableLooper.RunWithLooper; 26 import android.view.View; 27 28 import com.android.systemui.SysuiTestCase; 29 import com.android.systemui.dump.DumpManager; 30 import com.android.systemui.keyguard.KeyguardSliceProvider; 31 import com.android.systemui.plugins.ActivityStarter; 32 import com.android.systemui.settings.FakeDisplayTracker; 33 import com.android.systemui.statusbar.policy.ConfigurationController; 34 import com.android.systemui.tuner.TunerService; 35 36 import org.junit.After; 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.mockito.ArgumentCaptor; 41 import org.mockito.Mock; 42 import org.mockito.MockitoAnnotations; 43 44 @SmallTest 45 @RunWith(AndroidTestingRunner.class) 46 @RunWithLooper(setAsMainLooper = true) 47 public class KeyguardSliceViewControllerTest extends SysuiTestCase { 48 @Mock 49 private KeyguardSliceView mView; 50 @Mock 51 private TunerService mTunerService; 52 @Mock 53 private ConfigurationController mConfigurationController; 54 @Mock 55 private ActivityStarter mActivityStarter; 56 private FakeDisplayTracker mDisplayTracker = new FakeDisplayTracker(mContext); 57 private DumpManager mDumpManager = new DumpManager(); 58 59 private KeyguardSliceViewController mController; 60 61 @Before setUp()62 public void setUp() throws Exception { 63 MockitoAnnotations.initMocks(this); 64 when(mView.isAttachedToWindow()).thenReturn(true); 65 when(mView.getContext()).thenReturn(mContext); 66 mController = new KeyguardSliceViewController( 67 mView, mActivityStarter, mConfigurationController, 68 mTunerService, mDumpManager, mDisplayTracker); 69 mController.setupUri(KeyguardSliceProvider.KEYGUARD_SLICE_URI); 70 } 71 72 @After tearDown()73 public void tearDown() { 74 mController.onViewDetached(); 75 } 76 77 @Test refresh_replacesSliceContentAndNotifiesListener()78 public void refresh_replacesSliceContentAndNotifiesListener() { 79 mController.refresh(); 80 verify(mView).hideSlice(); 81 } 82 83 @Test onAttachedToWindow_registersListeners()84 public void onAttachedToWindow_registersListeners() { 85 mController.init(); 86 verify(mTunerService).addTunable(any(TunerService.Tunable.class), anyString()); 87 verify(mConfigurationController).addCallback( 88 any(ConfigurationController.ConfigurationListener.class)); 89 } 90 91 @Test onDetachedFromWindow_unregistersListeners()92 public void onDetachedFromWindow_unregistersListeners() { 93 ArgumentCaptor<View.OnAttachStateChangeListener> attachListenerArgumentCaptor = 94 ArgumentCaptor.forClass(View.OnAttachStateChangeListener.class); 95 96 mController.init(); 97 verify(mView).addOnAttachStateChangeListener(attachListenerArgumentCaptor.capture()); 98 99 attachListenerArgumentCaptor.getValue().onViewDetachedFromWindow(mView); 100 101 verify(mTunerService).removeTunable(any(TunerService.Tunable.class)); 102 verify(mConfigurationController).removeCallback( 103 any(ConfigurationController.ConfigurationListener.class)); 104 } 105 } 106