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