blob: 58701e72e40637846b6228ee078116070ffba172 [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 Snoeberger71e50792019-02-15 15:48:01 -050034import com.android.systemui.util.InjectionInflationController;
Robert Snoebergerb300a4e2019-02-13 20:13:53 +000035
36import org.junit.After;
37import org.junit.Before;
38import org.junit.Test;
39import org.junit.runner.RunWith;
40import org.mockito.Mock;
41import org.mockito.MockitoAnnotations;
42
43@SmallTest
44@RunWith(AndroidTestingRunner.class)
45@RunWithLooper
46public final class ClockManagerTest extends SysuiTestCase {
47
Robert Snoeberger71e50792019-02-15 15:48:01 -050048 private static final String BUBBLE_CLOCK = BubbleClockController.class.getName();
49 private static final Class<?> BUBBLE_CLOCK_CLASS = BubbleClockController.class;
50
Robert Snoebergerb300a4e2019-02-13 20:13:53 +000051 private ClockManager mClockManager;
Robert Snoeberger71e50792019-02-15 15:48:01 -050052 private ContentObserver mContentObserver;
Robert Snoebergerb300a4e2019-02-13 20:13:53 +000053 private DockManagerFake mFakeDockManager;
Robert Snoeberger71e50792019-02-15 15:48:01 -050054 @Mock InjectionInflationController mMockInjectionInflationController;
55 @Mock SysuiColorExtractor mMockColorExtractor;
56 @Mock ContentResolver mMockContentResolver;
57 @Mock SettingsWrapper mMockSettingsWrapper;
Robert Snoebergerb300a4e2019-02-13 20:13:53 +000058 @Mock ClockManager.ClockChangedListener mMockListener;
59
60 @Before
61 public void setUp() {
62 MockitoAnnotations.initMocks(this);
Robert Snoeberger71e50792019-02-15 15:48:01 -050063
64 LayoutInflater inflater = LayoutInflater.from(getContext());
65 when(mMockInjectionInflationController.injectable(any())).thenReturn(inflater);
66
Robert Snoebergerb300a4e2019-02-13 20:13:53 +000067 mFakeDockManager = new DockManagerFake();
Robert Snoeberger71e50792019-02-15 15:48:01 -050068 mClockManager = new ClockManager(getContext(), mMockInjectionInflationController,
69 mFakeDockManager, mMockColorExtractor, mMockContentResolver, mMockSettingsWrapper);
70
Robert Snoebergerb300a4e2019-02-13 20:13:53 +000071 mClockManager.addOnClockChangedListener(mMockListener);
Robert Snoeberger71e50792019-02-15 15:48:01 -050072 mContentObserver = mClockManager.getContentObserver();
Robert Snoebergerb300a4e2019-02-13 20:13:53 +000073 }
74
75 @After
76 public void tearDown() {
77 mClockManager.removeOnClockChangedListener(mMockListener);
78 }
79
80 @Test
81 public void dockEvent() {
82 mFakeDockManager.setDockEvent(DockManager.STATE_DOCKED);
83 assertThat(mClockManager.isDocked()).isTrue();
84 }
85
86 @Test
87 public void undockEvent() {
88 mFakeDockManager.setDockEvent(DockManager.STATE_NONE);
89 assertThat(mClockManager.isDocked()).isFalse();
90 }
Robert Snoeberger71e50792019-02-15 15:48:01 -050091
92 @Test
93 public void getCurrentClock_default() {
94 // GIVEN that settings doesn't contain any values
95 when(mMockSettingsWrapper.getLockScreenCustomClockFace()).thenReturn(null);
96 when(mMockSettingsWrapper.getDockedClockFace()).thenReturn(null);
97 // WHEN settings change event is fired
98 mContentObserver.onChange(false);
99 // THEN the result is null, indicated the default clock face should be used.
100 assertThat(mClockManager.getCurrentClock()).isNull();
101 }
102
103 @Test
104 public void getCurrentClock_customClock() {
105 // GIVEN that settings is set to the bubble clock face
106 when(mMockSettingsWrapper.getLockScreenCustomClockFace()).thenReturn(BUBBLE_CLOCK);
107 // WHEN settings change event is fired
108 mContentObserver.onChange(false);
109 // THEN the plugin is the bubble clock face.
110 assertThat(mClockManager.getCurrentClock()).isInstanceOf(BUBBLE_CLOCK_CLASS);
111 }
112
113 @Test
114 public void getCurrentClock_badSettingsValue() {
115 // GIVEN that settings contains a value that doesn't correspond to a
116 // custom clock face.
117 when(mMockSettingsWrapper.getLockScreenCustomClockFace()).thenReturn("bad value");
118 // WHEN settings change event is fired
119 mContentObserver.onChange(false);
120 // THEN the result is null.
121 assertThat(mClockManager.getCurrentClock()).isNull();
122 }
123
124 @Test
125 public void getCurrentClock_dockedDefault() {
126 // WHEN dock event is fired
127 mFakeDockManager.setDockEvent(DockManager.STATE_DOCKED);
128 // THEN the result is null, indicating the default clock face.
129 assertThat(mClockManager.getCurrentClock()).isNull();
130 }
131
132 @Test
133 public void getCurrentClock_dockedCustomClock() {
134 // GIVEN settings is set to the bubble clock face
135 when(mMockSettingsWrapper.getDockedClockFace()).thenReturn(BUBBLE_CLOCK);
136 // WHEN dock event fires
137 mFakeDockManager.setDockEvent(DockManager.STATE_DOCKED);
138 // THEN the plugin is the bubble clock face.
139 assertThat(mClockManager.getCurrentClock()).isInstanceOf(BUBBLE_CLOCK_CLASS);
140 }
141
142 @Test
143 public void getCurrentClock_badDockedSettingsValue() {
144 // GIVEN settings contains a value that doesn't correspond to an available clock face.
145 when(mMockSettingsWrapper.getDockedClockFace()).thenReturn("bad value");
146 // WHEN dock event fires
147 mFakeDockManager.setDockEvent(DockManager.STATE_DOCKED);
148 // THEN the result is null.
149 assertThat(mClockManager.getCurrentClock()).isNull();
150 }
151
152 @Test
153 public void getCurrentClock_badDockedSettingsFallback() {
154 // GIVEN settings contains a value that doesn't correspond to an available clock face, but
155 // locked screen settings is set to bubble clock.
156 when(mMockSettingsWrapper.getDockedClockFace()).thenReturn("bad value");
157 when(mMockSettingsWrapper.getLockScreenCustomClockFace()).thenReturn(BUBBLE_CLOCK);
158 // WHEN dock event is fired
159 mFakeDockManager.setDockEvent(DockManager.STATE_DOCKED);
160 // THEN the plugin is the bubble clock face.
161 assertThat(mClockManager.getCurrentClock()).isInstanceOf(BUBBLE_CLOCK_CLASS);
162 }
Robert Snoebergerb300a4e2019-02-13 20:13:53 +0000163}