Robert Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.keyguard.clock; |
| 17 | |
| 18 | import static com.google.common.truth.Truth.assertThat; |
| 19 | |
Robert Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 20 | import static org.mockito.ArgumentMatchers.any; |
| 21 | import static org.mockito.Mockito.when; |
| 22 | |
| 23 | import android.content.ContentResolver; |
| 24 | import android.database.ContentObserver; |
Robert Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 25 | import android.test.suitebuilder.annotation.SmallTest; |
| 26 | import android.testing.AndroidTestingRunner; |
Robert Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 27 | import android.testing.TestableLooper.RunWithLooper; |
Robert Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 28 | import android.view.LayoutInflater; |
Robert Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 29 | |
| 30 | import com.android.systemui.SysuiTestCase; |
Robert Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 31 | import com.android.systemui.colorextraction.SysuiColorExtractor; |
Robert Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 32 | import com.android.systemui.dock.DockManager; |
| 33 | import com.android.systemui.dock.DockManagerFake; |
Robert Snoeberger | 9ad03f4 | 2019-02-28 14:47:49 -0500 | [diff] [blame] | 34 | import com.android.systemui.shared.plugins.PluginManager; |
Robert Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 35 | import com.android.systemui.util.InjectionInflationController; |
Robert Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 36 | |
| 37 | import org.junit.After; |
| 38 | import org.junit.Before; |
| 39 | import org.junit.Test; |
| 40 | import org.junit.runner.RunWith; |
| 41 | import org.mockito.Mock; |
| 42 | import org.mockito.MockitoAnnotations; |
| 43 | |
| 44 | @SmallTest |
| 45 | @RunWith(AndroidTestingRunner.class) |
| 46 | @RunWithLooper |
| 47 | public final class ClockManagerTest extends SysuiTestCase { |
| 48 | |
Robert Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 49 | private static final String BUBBLE_CLOCK = BubbleClockController.class.getName(); |
| 50 | private static final Class<?> BUBBLE_CLOCK_CLASS = BubbleClockController.class; |
| 51 | |
Robert Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 52 | private ClockManager mClockManager; |
Robert Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 53 | private ContentObserver mContentObserver; |
Robert Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 54 | private DockManagerFake mFakeDockManager; |
Robert Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 55 | @Mock InjectionInflationController mMockInjectionInflationController; |
Robert Snoeberger | 9ad03f4 | 2019-02-28 14:47:49 -0500 | [diff] [blame] | 56 | @Mock PluginManager mMockPluginManager; |
Robert Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 57 | @Mock SysuiColorExtractor mMockColorExtractor; |
| 58 | @Mock ContentResolver mMockContentResolver; |
| 59 | @Mock SettingsWrapper mMockSettingsWrapper; |
Robert Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 60 | @Mock ClockManager.ClockChangedListener mMockListener; |
| 61 | |
| 62 | @Before |
| 63 | public void setUp() { |
| 64 | MockitoAnnotations.initMocks(this); |
Robert Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 65 | |
| 66 | LayoutInflater inflater = LayoutInflater.from(getContext()); |
| 67 | when(mMockInjectionInflationController.injectable(any())).thenReturn(inflater); |
| 68 | |
Robert Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 69 | mFakeDockManager = new DockManagerFake(); |
Robert Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 70 | mClockManager = new ClockManager(getContext(), mMockInjectionInflationController, |
Robert Snoeberger | 9ad03f4 | 2019-02-28 14:47:49 -0500 | [diff] [blame] | 71 | mMockPluginManager, mFakeDockManager, mMockColorExtractor, mMockContentResolver, |
| 72 | mMockSettingsWrapper); |
Robert Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 73 | |
Robert Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 74 | mClockManager.addOnClockChangedListener(mMockListener); |
Robert Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 75 | mContentObserver = mClockManager.getContentObserver(); |
Robert Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 76 | } |
| 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 Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 94 | |
| 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 Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 166 | } |