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 | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 34 | import com.android.systemui.util.InjectionInflationController; |
Robert Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 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.Mock; |
| 41 | import org.mockito.MockitoAnnotations; |
| 42 | |
| 43 | @SmallTest |
| 44 | @RunWith(AndroidTestingRunner.class) |
| 45 | @RunWithLooper |
| 46 | public final class ClockManagerTest extends SysuiTestCase { |
| 47 | |
Robert Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 48 | private static final String BUBBLE_CLOCK = BubbleClockController.class.getName(); |
| 49 | private static final Class<?> BUBBLE_CLOCK_CLASS = BubbleClockController.class; |
| 50 | |
Robert Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 51 | private ClockManager mClockManager; |
Robert Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 52 | private ContentObserver mContentObserver; |
Robert Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 53 | private DockManagerFake mFakeDockManager; |
Robert Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 54 | @Mock InjectionInflationController mMockInjectionInflationController; |
| 55 | @Mock SysuiColorExtractor mMockColorExtractor; |
| 56 | @Mock ContentResolver mMockContentResolver; |
| 57 | @Mock SettingsWrapper mMockSettingsWrapper; |
Robert Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 58 | @Mock ClockManager.ClockChangedListener mMockListener; |
| 59 | |
| 60 | @Before |
| 61 | public void setUp() { |
| 62 | MockitoAnnotations.initMocks(this); |
Robert Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 63 | |
| 64 | LayoutInflater inflater = LayoutInflater.from(getContext()); |
| 65 | when(mMockInjectionInflationController.injectable(any())).thenReturn(inflater); |
| 66 | |
Robert Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 67 | mFakeDockManager = new DockManagerFake(); |
Robert Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 68 | mClockManager = new ClockManager(getContext(), mMockInjectionInflationController, |
| 69 | mFakeDockManager, mMockColorExtractor, mMockContentResolver, mMockSettingsWrapper); |
| 70 | |
Robert Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 71 | mClockManager.addOnClockChangedListener(mMockListener); |
Robert Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 72 | mContentObserver = mClockManager.getContentObserver(); |
Robert Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 73 | } |
| 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 Snoeberger | 71e5079 | 2019-02-15 15:48:01 -0500 | [diff] [blame] | 91 | |
| 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 Snoeberger | b300a4e | 2019-02-13 20:13:53 +0000 | [diff] [blame] | 163 | } |