Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 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 | |
| 17 | package com.android.server.timedetector; |
| 18 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 19 | import static org.junit.Assert.assertEquals; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 20 | import static org.junit.Assert.assertTrue; |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 21 | import static org.mockito.ArgumentMatchers.any; |
| 22 | import static org.mockito.ArgumentMatchers.anyString; |
| 23 | import static org.mockito.ArgumentMatchers.eq; |
| 24 | import static org.mockito.Mockito.doNothing; |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 25 | import static org.mockito.Mockito.mock; |
| 26 | import static org.mockito.Mockito.verify; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 27 | import static org.mockito.Mockito.when; |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 28 | |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 29 | import android.app.timedetector.ManualTimeSuggestion; |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 30 | import android.app.timedetector.PhoneTimeSuggestion; |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 31 | import android.content.Context; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 32 | import android.content.pm.PackageManager; |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 33 | import android.os.Handler; |
| 34 | import android.os.HandlerThread; |
| 35 | import android.os.Looper; |
| 36 | import android.os.Message; |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 37 | import android.util.TimestampedValue; |
| 38 | |
Brett Chabot | 8091d9e | 2019-02-26 14:52:33 -0800 | [diff] [blame] | 39 | import androidx.test.runner.AndroidJUnit4; |
| 40 | |
| 41 | import com.android.server.timedetector.TimeDetectorStrategy.Callback; |
| 42 | |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 43 | import org.junit.After; |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 44 | import org.junit.Before; |
| 45 | import org.junit.Test; |
| 46 | import org.junit.runner.RunWith; |
| 47 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 48 | import java.io.PrintWriter; |
| 49 | |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 50 | @RunWith(AndroidJUnit4.class) |
| 51 | public class TimeDetectorServiceTest { |
| 52 | |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 53 | private Context mMockContext; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 54 | private StubbedTimeDetectorStrategy mStubbedTimeDetectorStrategy; |
| 55 | private Callback mMockCallback; |
| 56 | |
| 57 | private TimeDetectorService mTimeDetectorService; |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 58 | private HandlerThread mHandlerThread; |
| 59 | private TestHandler mTestHandler; |
| 60 | |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 61 | |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 62 | @Before |
| 63 | public void setUp() { |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 64 | mMockContext = mock(Context.class); |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 65 | |
| 66 | // Create a thread + handler for processing the work that the service posts. |
| 67 | mHandlerThread = new HandlerThread("TimeDetectorServiceTest"); |
| 68 | mHandlerThread.start(); |
| 69 | mTestHandler = new TestHandler(mHandlerThread.getLooper()); |
| 70 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 71 | mMockCallback = mock(Callback.class); |
| 72 | mStubbedTimeDetectorStrategy = new StubbedTimeDetectorStrategy(); |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 73 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 74 | mTimeDetectorService = new TimeDetectorService( |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 75 | mMockContext, mTestHandler, mMockCallback, |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 76 | mStubbedTimeDetectorStrategy); |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 79 | @After |
| 80 | public void tearDown() throws Exception { |
| 81 | mHandlerThread.quit(); |
| 82 | mHandlerThread.join(); |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Test |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 86 | public void testSuggestPhoneTime() throws Exception { |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 87 | doNothing().when(mMockContext).enforceCallingPermission(anyString(), any()); |
| 88 | |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 89 | PhoneTimeSuggestion phoneTimeSuggestion = createPhoneTimeSuggestion(); |
| 90 | mTimeDetectorService.suggestPhoneTime(phoneTimeSuggestion); |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 91 | mTestHandler.assertTotalMessagesEnqueued(1); |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 92 | |
| 93 | verify(mMockContext).enforceCallingPermission( |
| 94 | eq(android.Manifest.permission.SET_TIME), |
| 95 | anyString()); |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 96 | |
| 97 | mTestHandler.waitForEmptyQueue(); |
| 98 | mStubbedTimeDetectorStrategy.verifySuggestPhoneTimeCalled(phoneTimeSuggestion); |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testSuggestManualTime() throws Exception { |
| 103 | doNothing().when(mMockContext).enforceCallingPermission(anyString(), any()); |
| 104 | |
| 105 | ManualTimeSuggestion manualTimeSuggestion = createManualTimeSuggestion(); |
| 106 | mTimeDetectorService.suggestManualTime(manualTimeSuggestion); |
| 107 | mTestHandler.assertTotalMessagesEnqueued(1); |
| 108 | |
| 109 | verify(mMockContext).enforceCallingPermission( |
| 110 | eq(android.Manifest.permission.SET_TIME), |
| 111 | anyString()); |
| 112 | |
| 113 | mTestHandler.waitForEmptyQueue(); |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 114 | mStubbedTimeDetectorStrategy.verifySuggestManualTimeCalled(manualTimeSuggestion); |
| 115 | } |
| 116 | |
| 117 | @Test |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 118 | public void testDump() { |
| 119 | when(mMockContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)) |
| 120 | .thenReturn(PackageManager.PERMISSION_GRANTED); |
| 121 | |
| 122 | mTimeDetectorService.dump(null, null, null); |
| 123 | |
| 124 | verify(mMockContext).checkCallingOrSelfPermission(eq(android.Manifest.permission.DUMP)); |
| 125 | mStubbedTimeDetectorStrategy.verifyDumpCalled(); |
| 126 | } |
| 127 | |
| 128 | @Test |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 129 | public void testAutoTimeDetectionToggle() throws Exception { |
| 130 | mTimeDetectorService.handleAutoTimeDetectionToggle(); |
| 131 | mTestHandler.assertTotalMessagesEnqueued(1); |
| 132 | mTestHandler.waitForEmptyQueue(); |
| 133 | mStubbedTimeDetectorStrategy.verifyHandleAutoTimeDetectionToggleCalled(); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 134 | |
| 135 | mTimeDetectorService.handleAutoTimeDetectionToggle(); |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 136 | mTestHandler.assertTotalMessagesEnqueued(2); |
| 137 | mTestHandler.waitForEmptyQueue(); |
| 138 | mStubbedTimeDetectorStrategy.verifyHandleAutoTimeDetectionToggleCalled(); |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 141 | private static PhoneTimeSuggestion createPhoneTimeSuggestion() { |
| 142 | int phoneId = 1234; |
Neil Fuller | 568fd89 | 2019-11-20 14:39:06 +0000 | [diff] [blame] | 143 | PhoneTimeSuggestion suggestion = new PhoneTimeSuggestion(phoneId); |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 144 | TimestampedValue<Long> timeValue = new TimestampedValue<>(100L, 1_000_000L); |
Neil Fuller | 568fd89 | 2019-11-20 14:39:06 +0000 | [diff] [blame] | 145 | suggestion.setUtcTime(timeValue); |
| 146 | return suggestion; |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 147 | } |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 148 | |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 149 | private static ManualTimeSuggestion createManualTimeSuggestion() { |
| 150 | TimestampedValue<Long> timeValue = new TimestampedValue<>(100L, 1_000_000L); |
| 151 | return new ManualTimeSuggestion(timeValue); |
| 152 | } |
| 153 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 154 | private static class StubbedTimeDetectorStrategy implements TimeDetectorStrategy { |
| 155 | |
| 156 | // Call tracking. |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 157 | private PhoneTimeSuggestion mLastPhoneSuggestion; |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 158 | private ManualTimeSuggestion mLastManualSuggestion; |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 159 | private boolean mLastAutoTimeDetectionToggleCalled; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 160 | private boolean mDumpCalled; |
| 161 | |
| 162 | @Override |
| 163 | public void initialize(Callback ignored) { |
| 164 | } |
| 165 | |
| 166 | @Override |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 167 | public void suggestPhoneTime(PhoneTimeSuggestion timeSuggestion) { |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 168 | resetCallTracking(); |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 169 | mLastPhoneSuggestion = timeSuggestion; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 173 | public void suggestManualTime(ManualTimeSuggestion timeSuggestion) { |
| 174 | resetCallTracking(); |
| 175 | mLastManualSuggestion = timeSuggestion; |
| 176 | } |
| 177 | |
| 178 | @Override |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 179 | public void handleAutoTimeDetectionChanged() { |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 180 | resetCallTracking(); |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 181 | mLastAutoTimeDetectionToggleCalled = true; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void dump(PrintWriter pw, String[] args) { |
| 186 | resetCallTracking(); |
| 187 | mDumpCalled = true; |
| 188 | } |
| 189 | |
| 190 | void resetCallTracking() { |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 191 | mLastPhoneSuggestion = null; |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 192 | mLastManualSuggestion = null; |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 193 | mLastAutoTimeDetectionToggleCalled = false; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 194 | mDumpCalled = false; |
| 195 | } |
| 196 | |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 197 | void verifySuggestPhoneTimeCalled(PhoneTimeSuggestion expectedSuggestion) { |
| 198 | assertEquals(expectedSuggestion, mLastPhoneSuggestion); |
| 199 | } |
| 200 | |
| 201 | public void verifySuggestManualTimeCalled(ManualTimeSuggestion expectedSuggestion) { |
| 202 | assertEquals(expectedSuggestion, mLastManualSuggestion); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 205 | void verifyHandleAutoTimeDetectionToggleCalled() { |
| 206 | assertTrue(mLastAutoTimeDetectionToggleCalled); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void verifyDumpCalled() { |
| 210 | assertTrue(mDumpCalled); |
| 211 | } |
| 212 | } |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 213 | |
| 214 | /** |
| 215 | * A Handler that can track posts/sends and wait for work to be completed. |
| 216 | */ |
| 217 | private static class TestHandler extends Handler { |
| 218 | |
| 219 | private int mMessagesSent; |
| 220 | |
| 221 | TestHandler(Looper looper) { |
| 222 | super(looper); |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public boolean sendMessageAtTime(Message msg, long uptimeMillis) { |
| 227 | mMessagesSent++; |
| 228 | return super.sendMessageAtTime(msg, uptimeMillis); |
| 229 | } |
| 230 | |
| 231 | /** Asserts the number of messages posted or sent is as expected. */ |
| 232 | void assertTotalMessagesEnqueued(int expected) { |
| 233 | assertEquals(expected, mMessagesSent); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Waits for all currently enqueued work due to be processed to be completed before |
| 238 | * returning. |
| 239 | */ |
| 240 | void waitForEmptyQueue() throws InterruptedException { |
| 241 | while (!getLooper().getQueue().isIdle()) { |
| 242 | Thread.sleep(100); |
| 243 | } |
| 244 | } |
| 245 | } |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 246 | } |