blob: 36265d485e2ccdcf8eff5b125ba1567c7a741e10 [file] [log] [blame]
Robert Snoebergerb300a4e2019-02-13 20:13:53 +00001/*
2 * Copyright (C) 2019 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 */
16package com.android.keyguard.clock;
17
18import static com.google.common.truth.Truth.assertThat;
19
Robert Snoeberger71e50792019-02-15 15:48:01 -050020import static org.mockito.ArgumentMatchers.any;
21import static org.mockito.Mockito.when;
22
23import android.content.ContentResolver;
24import android.database.ContentObserver;
Robert Snoebergerb300a4e2019-02-13 20:13:53 +000025import android.test.suitebuilder.annotation.SmallTest;
26import android.testing.AndroidTestingRunner;
Robert Snoebergerb300a4e2019-02-13 20:13:53 +000027import android.testing.TestableLooper.RunWithLooper;
Robert Snoeberger71e50792019-02-15 15:48:01 -050028import android.view.LayoutInflater;
Robert Snoebergerb300a4e2019-02-13 20:13:53 +000029
30import com.android.systemui.SysuiTestCase;
Robert Snoeberger71e50792019-02-15 15:48:01 -050031import com.android.systemui.colorextraction.SysuiColorExtractor;
Robert Snoebergerb300a4e2019-02-13 20:13:53 +000032import com.android.systemui.dock.DockManager;
33import com.android.systemui.dock.DockManagerFake;
Robert Snoeberger9ad03f42019-02-28 14:47:49 -050034import com.android.systemui.shared.plugins.PluginManager;
Robert Snoeberger71e50792019-02-15 15:48:01 -050035import com.android.systemui.util.InjectionInflationController;
Robert Snoebergerb300a4e2019-02-13 20:13:53 +000036
37import org.junit.After;
38import org.junit.Before;
39import org.junit.Test;
40import org.junit.runner.RunWith;
41import org.mockito.Mock;
42import org.mockito.MockitoAnnotations;
43
44@SmallTest
45@RunWith(AndroidTestingRunner.class)
46@RunWithLooper
47public final class ClockManagerTest extends SysuiTestCase {
48
Robert Snoeberger71e50792019-02-15 15:48:01 -050049 private static final String BUBBLE_CLOCK = BubbleClockController.class.getName();
50 private static final Class<?> BUBBLE_CLOCK_CLASS = BubbleClockController.class;
51
Robert Snoebergerb300a4e2019-02-13 20:13:53 +000052 private ClockManager mClockManager;
Robert Snoeberger71e50792019-02-15 15:48:01 -050053 private ContentObserver mContentObserver;
Robert Snoebergerb300a4e2019-02-13 20:13:53 +000054 private DockManagerFake mFakeDockManager;
Robert Snoeberger71e50792019-02-15 15:48:01 -050055 @Mock InjectionInflationController mMockInjectionInflationController;
Robert Snoeberger9ad03f42019-02-28 14:47:49 -050056 @Mock PluginManager mMockPluginManager;
Robert Snoeberger71e50792019-02-15 15:48:01 -050057 @Mock SysuiColorExtractor mMockColorExtractor;
58 @Mock ContentResolver mMockContentResolver;
59 @Mock SettingsWrapper mMockSettingsWrapper;
Robert Snoebergerb300a4e2019-02-13 20:13:53 +000060 @Mock ClockManager.ClockChangedListener mMockListener;
61
62 @Before
63 public void setUp() {
64 MockitoAnnotations.initMocks(this);
Robert Snoeberger71e50792019-02-15 15:48:01 -050065
66 LayoutInflater inflater = LayoutInflater.from(getContext());
67 when(mMockInjectionInflationController.injectable(any())).thenReturn(inflater);
68
Robert Snoebergerb300a4e2019-02-13 20:13:53 +000069 mFakeDockManager = new DockManagerFake();
Robert Snoeberger71e50792019-02-15 15:48:01 -050070 mClockManager = new ClockManager(getContext(), mMockInjectionInflationController,
Robert Snoeberger9ad03f42019-02-28 14:47:49 -050071 mMockPluginManager, mFakeDockManager, mMockColorExtractor, mMockContentResolver,
72 mMockSettingsWrapper);
Robert Snoeberger71e50792019-02-15 15:48:01 -050073
Robert Snoebergerb300a4e2019-02-13 20:13:53 +000074 mClockManager.addOnClockChangedListener(mMockListener);
Robert Snoeberger71e50792019-02-15 15:48:01 -050075 mContentObserver = mClockManager.getContentObserver();
Robert Snoebergerb300a4e2019-02-13 20:13:53 +000076 }
77
78 @After
79 public void tearDown() {
80 mClockManager.removeOnClockChangedListener(mMockListener);
81 }
82
83 @Test
84 public void dockEvent() {
85 mFakeDockManager.setDockEvent(DockManager.STATE_DOCKED);
86 assertThat(mClockManager.isDocked()).isTrue();
87 }
88
89 @Test
90 public void undockEvent() {
91 mFakeDockManager.setDockEvent(DockManager.STATE_NONE);
92 assertThat(mClockManager.isDocked()).isFalse();
93 }
Robert Snoeberger71e50792019-02-15 15:48:01 -050094
95 @Test
96 public void getCurrentClock_default() {
97 // GIVEN that settings doesn't contain any values
98 when(mMockSettingsWrapper.getLockScreenCustomClockFace()).thenReturn(null);
99 when(mMockSettingsWrapper.getDockedClockFace()).thenReturn(null);
100 // WHEN settings change event is fired
101 mContentObserver.onChange(false);
102 // THEN the result is null, indicated the default clock face should be used.
103 assertThat(mClockManager.getCurrentClock()).isNull();
104 }
105
106 @Test
107 public void getCurrentClock_customClock() {
108 // GIVEN that settings is set to the bubble clock face
109 when(mMockSettingsWrapper.getLockScreenCustomClockFace()).thenReturn(BUBBLE_CLOCK);
110 // WHEN settings change event is fired
111 mContentObserver.onChange(false);
112 // THEN the plugin is the bubble clock face.
113 assertThat(mClockManager.getCurrentClock()).isInstanceOf(BUBBLE_CLOCK_CLASS);
114 }
115
116 @Test
117 public void getCurrentClock_badSettingsValue() {
118 // GIVEN that settings contains a value that doesn't correspond to a
119 // custom clock face.
120 when(mMockSettingsWrapper.getLockScreenCustomClockFace()).thenReturn("bad value");
121 // WHEN settings change event is fired
122 mContentObserver.onChange(false);
123 // THEN the result is null.
124 assertThat(mClockManager.getCurrentClock()).isNull();
125 }
126
127 @Test
128 public void getCurrentClock_dockedDefault() {
129 // WHEN dock event is fired
130 mFakeDockManager.setDockEvent(DockManager.STATE_DOCKED);
131 // THEN the result is null, indicating the default clock face.
132 assertThat(mClockManager.getCurrentClock()).isNull();
133 }
134
135 @Test
136 public void getCurrentClock_dockedCustomClock() {
137 // GIVEN settings is set to the bubble clock face
138 when(mMockSettingsWrapper.getDockedClockFace()).thenReturn(BUBBLE_CLOCK);
139 // WHEN dock event fires
140 mFakeDockManager.setDockEvent(DockManager.STATE_DOCKED);
141 // THEN the plugin is the bubble clock face.
142 assertThat(mClockManager.getCurrentClock()).isInstanceOf(BUBBLE_CLOCK_CLASS);
143 }
144
145 @Test
146 public void getCurrentClock_badDockedSettingsValue() {
147 // GIVEN settings contains a value that doesn't correspond to an available clock face.
148 when(mMockSettingsWrapper.getDockedClockFace()).thenReturn("bad value");
149 // WHEN dock event fires
150 mFakeDockManager.setDockEvent(DockManager.STATE_DOCKED);
151 // THEN the result is null.
152 assertThat(mClockManager.getCurrentClock()).isNull();
153 }
154
155 @Test
156 public void getCurrentClock_badDockedSettingsFallback() {
157 // GIVEN settings contains a value that doesn't correspond to an available clock face, but
158 // locked screen settings is set to bubble clock.
159 when(mMockSettingsWrapper.getDockedClockFace()).thenReturn("bad value");
160 when(mMockSettingsWrapper.getLockScreenCustomClockFace()).thenReturn(BUBBLE_CLOCK);
161 // WHEN dock event is fired
162 mFakeDockManager.setDockEvent(DockManager.STATE_DOCKED);
163 // THEN the plugin is the bubble clock face.
164 assertThat(mClockManager.getCurrentClock()).isInstanceOf(BUBBLE_CLOCK_CLASS);
165 }
Robert Snoebergerb300a4e2019-02-13 20:13:53 +0000166}