From 1395dffdb1025a5a14b522842d427a6387b027d2 Mon Sep 17 00:00:00 2001 From: Neil Fuller Date: Thu, 5 Mar 2020 17:22:21 +0000 Subject: [PATCH] Make manual suggestions synchronous/return result Modify the suggestManual...() methods on TimeDetector and TimeZoneDetector to be synchronous, and have them return true/false to indicate if the call "succeeded". This is being done before adding more calls that will be used by apps like SettingsUI; generally all calls that are user facing and could conceivably fail should return success/failure information and therefore need to happen synchronously. Test: atest services/tests/servicestests/src/com/android/server/timedetector/ Test: atest services/tests/servicestests/src/com/android/server/timezonedetector/ Bug: 140712361 Merged-In: I5b6b7fb5af2ffe88392b2ca8d1e8fff2a187521b Change-Id: I5b6b7fb5af2ffe88392b2ca8d1e8fff2a187521b --- .../timedetector/ITimeDetectorService.aidl | 2 +- .../app/timedetector/TimeDetector.java | 8 +++- .../app/timedetector/TimeDetectorImpl.java | 4 +- .../ITimeZoneDetectorService.aidl | 2 +- .../timezonedetector/TimeZoneDetector.java | 9 ++-- .../TimeZoneDetectorImpl.java | 4 +- .../timedetector/TimeDetectorService.java | 10 +++- .../timedetector/TimeDetectorStrategy.java | 9 +++- .../TimeDetectorStrategyImpl.java | 19 ++++---- .../TimeZoneDetectorService.java | 10 +++- .../TimeZoneDetectorStrategy.java | 8 +++- .../TimeZoneDetectorStrategyImpl.java | 14 +++--- .../timedetector/TimeDetectorServiceTest.java | 14 +++--- .../TimeDetectorStrategyImplTest.java | 11 +++-- .../TimeZoneDetectorServiceTest.java | 14 +++--- .../TimeZoneDetectorStrategyImplTest.java | 46 ++++++++++--------- 16 files changed, 110 insertions(+), 74 deletions(-) diff --git a/core/java/android/app/timedetector/ITimeDetectorService.aidl b/core/java/android/app/timedetector/ITimeDetectorService.aidl index 5ead0c90c80e8..7bea5d7d60b12 100644 --- a/core/java/android/app/timedetector/ITimeDetectorService.aidl +++ b/core/java/android/app/timedetector/ITimeDetectorService.aidl @@ -34,7 +34,7 @@ import android.app.timedetector.TelephonyTimeSuggestion; * {@hide} */ interface ITimeDetectorService { - void suggestManualTime(in ManualTimeSuggestion timeSuggestion); + boolean suggestManualTime(in ManualTimeSuggestion timeSuggestion); void suggestNetworkTime(in NetworkTimeSuggestion timeSuggestion); void suggestTelephonyTime(in TelephonyTimeSuggestion timeSuggestion); } diff --git a/core/java/android/app/timedetector/TimeDetector.java b/core/java/android/app/timedetector/TimeDetector.java index 84ad495da09b9..162e18215cae2 100644 --- a/core/java/android/app/timedetector/TimeDetector.java +++ b/core/java/android/app/timedetector/TimeDetector.java @@ -53,12 +53,16 @@ public interface TimeDetector { void suggestTelephonyTime(@NonNull TelephonyTimeSuggestion timeSuggestion); /** - * Suggests the user's manually entered current time to the detector. + * Suggests the current time, determined from the user's manually entered information, to + * the detector. Returns {@code false} if the suggestion was invalid, or the device + * configuration prevented the suggestion being used, {@code true} if the suggestion was + * accepted. A suggestion that is valid but does not change the time because it matches the + * current device time is considered accepted. * * @hide */ @RequiresPermission(android.Manifest.permission.SUGGEST_MANUAL_TIME_AND_ZONE) - void suggestManualTime(@NonNull ManualTimeSuggestion timeSuggestion); + boolean suggestManualTime(@NonNull ManualTimeSuggestion timeSuggestion); /** * Suggests the time according to a network time source like NTP. diff --git a/core/java/android/app/timedetector/TimeDetectorImpl.java b/core/java/android/app/timedetector/TimeDetectorImpl.java index c1d66672f9d27..ac02c8930982f 100644 --- a/core/java/android/app/timedetector/TimeDetectorImpl.java +++ b/core/java/android/app/timedetector/TimeDetectorImpl.java @@ -52,12 +52,12 @@ public final class TimeDetectorImpl implements TimeDetector { } @Override - public void suggestManualTime(@NonNull ManualTimeSuggestion timeSuggestion) { + public boolean suggestManualTime(@NonNull ManualTimeSuggestion timeSuggestion) { if (DEBUG) { Log.d(TAG, "suggestManualTime called: " + timeSuggestion); } try { - mITimeDetectorService.suggestManualTime(timeSuggestion); + return mITimeDetectorService.suggestManualTime(timeSuggestion); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } diff --git a/core/java/android/app/timezonedetector/ITimeZoneDetectorService.aidl b/core/java/android/app/timezonedetector/ITimeZoneDetectorService.aidl index b06f4b8ecdb89..6d7b26d8aff97 100644 --- a/core/java/android/app/timezonedetector/ITimeZoneDetectorService.aidl +++ b/core/java/android/app/timezonedetector/ITimeZoneDetectorService.aidl @@ -33,6 +33,6 @@ import android.app.timezonedetector.TelephonyTimeZoneSuggestion; * {@hide} */ interface ITimeZoneDetectorService { - void suggestManualTimeZone(in ManualTimeZoneSuggestion timeZoneSuggestion); + boolean suggestManualTimeZone(in ManualTimeZoneSuggestion timeZoneSuggestion); void suggestTelephonyTimeZone(in TelephonyTimeZoneSuggestion timeZoneSuggestion); } diff --git a/core/java/android/app/timezonedetector/TimeZoneDetector.java b/core/java/android/app/timezonedetector/TimeZoneDetector.java index 34a7586192fe5..b78368496f8a3 100644 --- a/core/java/android/app/timezonedetector/TimeZoneDetector.java +++ b/core/java/android/app/timezonedetector/TimeZoneDetector.java @@ -41,13 +41,16 @@ public interface TimeZoneDetector { } /** - * Suggests the current time zone, determined using the user's manually entered information, to - * the detector. The detector may ignore the signal based on system settings. + * Suggests the current time zone, determined from the user's manually entered information, to + * the detector. Returns {@code false} if the suggestion was invalid, or the device + * configuration prevented the suggestion being used, {@code true} if the suggestion was + * accepted. A suggestion that is valid but does not change the time zone because it matches + * the current device time zone is considered accepted. * * @hide */ @RequiresPermission(android.Manifest.permission.SUGGEST_MANUAL_TIME_AND_ZONE) - void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion timeZoneSuggestion); + boolean suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion timeZoneSuggestion); /** * Suggests the current time zone, determined using telephony signals, to the detector. The diff --git a/core/java/android/app/timezonedetector/TimeZoneDetectorImpl.java b/core/java/android/app/timezonedetector/TimeZoneDetectorImpl.java index 54cf1f380d971..3bd6b4bd692a0 100644 --- a/core/java/android/app/timezonedetector/TimeZoneDetectorImpl.java +++ b/core/java/android/app/timezonedetector/TimeZoneDetectorImpl.java @@ -40,12 +40,12 @@ public final class TimeZoneDetectorImpl implements TimeZoneDetector { } @Override - public void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion timeZoneSuggestion) { + public boolean suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion timeZoneSuggestion) { if (DEBUG) { Log.d(TAG, "suggestManualTimeZone called: " + timeZoneSuggestion); } try { - mITimeZoneDetectorService.suggestManualTimeZone(timeZoneSuggestion); + return mITimeZoneDetectorService.suggestManualTimeZone(timeZoneSuggestion); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } diff --git a/services/core/java/com/android/server/timedetector/TimeDetectorService.java b/services/core/java/com/android/server/timedetector/TimeDetectorService.java index 49a090b86d1db..c63f306ec84f7 100644 --- a/services/core/java/com/android/server/timedetector/TimeDetectorService.java +++ b/services/core/java/com/android/server/timedetector/TimeDetectorService.java @@ -25,6 +25,7 @@ import android.app.timedetector.TelephonyTimeSuggestion; import android.content.ContentResolver; import android.content.Context; import android.database.ContentObserver; +import android.os.Binder; import android.os.Handler; import android.provider.Settings; @@ -101,11 +102,16 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub { } @Override - public void suggestManualTime(@NonNull ManualTimeSuggestion timeSignal) { + public boolean suggestManualTime(@NonNull ManualTimeSuggestion timeSignal) { enforceSuggestManualTimePermission(); Objects.requireNonNull(timeSignal); - mHandler.post(() -> mTimeDetectorStrategy.suggestManualTime(timeSignal)); + long token = Binder.clearCallingIdentity(); + try { + return mTimeDetectorStrategy.suggestManualTime(timeSignal); + } finally { + Binder.restoreCallingIdentity(token); + } } @Override diff --git a/services/core/java/com/android/server/timedetector/TimeDetectorStrategy.java b/services/core/java/com/android/server/timedetector/TimeDetectorStrategy.java index 97687f7fae816..13f0ab605b3a9 100644 --- a/services/core/java/com/android/server/timedetector/TimeDetectorStrategy.java +++ b/services/core/java/com/android/server/timedetector/TimeDetectorStrategy.java @@ -40,8 +40,13 @@ public interface TimeDetectorStrategy { /** Process the suggested time from telephony sources. */ void suggestTelephonyTime(@NonNull TelephonyTimeSuggestion timeSuggestion); - /** Process the suggested manually entered time. */ - void suggestManualTime(@NonNull ManualTimeSuggestion timeSuggestion); + /** + * Process the suggested manually entered time. Returns {@code false} if the suggestion was + * invalid, or the device configuration prevented the suggestion being used, {@code true} if the + * suggestion was accepted. A suggestion that is valid but does not change the time because it + * matches the current device time is considered accepted. + */ + boolean suggestManualTime(@NonNull ManualTimeSuggestion timeSuggestion); /** Process the suggested time from network sources. */ void suggestNetworkTime(@NonNull NetworkTimeSuggestion timeSuggestion); diff --git a/services/core/java/com/android/server/timedetector/TimeDetectorStrategyImpl.java b/services/core/java/com/android/server/timedetector/TimeDetectorStrategyImpl.java index 6ada077073144..d447a785f3bb9 100644 --- a/services/core/java/com/android/server/timedetector/TimeDetectorStrategyImpl.java +++ b/services/core/java/com/android/server/timedetector/TimeDetectorStrategyImpl.java @@ -164,15 +164,15 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { } @Override - public synchronized void suggestManualTime(@NonNull ManualTimeSuggestion suggestion) { + public synchronized boolean suggestManualTime(@NonNull ManualTimeSuggestion suggestion) { final TimestampedValue newUtcTime = suggestion.getUtcTime(); if (!validateSuggestionTime(newUtcTime, suggestion)) { - return; + return false; } String cause = "Manual time suggestion received: suggestion=" + suggestion; - setSystemClockIfRequired(ORIGIN_MANUAL, newUtcTime, cause); + return setSystemClockIfRequired(ORIGIN_MANUAL, newUtcTime, cause); } @Override @@ -489,7 +489,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { } @GuardedBy("this") - private void setSystemClockIfRequired( + private boolean setSystemClockIfRequired( @Origin int origin, @NonNull TimestampedValue time, @NonNull String cause) { boolean isOriginAutomatic = isOriginAutomatic(origin); @@ -501,7 +501,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { + ", time=" + time + ", cause=" + cause); } - return; + return false; } } else { if (mCallback.isAutoTimeDetectionEnabled()) { @@ -511,13 +511,13 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { + ", time=" + time + ", cause=" + cause); } - return; + return false; } } mCallback.acquireWakeLock(); try { - setSystemClockUnderWakeLock(origin, time, cause); + return setSystemClockUnderWakeLock(origin, time, cause); } finally { mCallback.releaseWakeLock(); } @@ -528,7 +528,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { } @GuardedBy("this") - private void setSystemClockUnderWakeLock( + private boolean setSystemClockUnderWakeLock( int origin, @NonNull TimestampedValue newTime, @NonNull Object cause) { long elapsedRealtimeMillis = mCallback.elapsedRealtimeMillis(); @@ -572,7 +572,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { + " systemClockUpdateThreshold=" + systemClockUpdateThreshold + " absTimeDifference=" + absTimeDifference); } - return; + return true; } mCallback.setSystemClock(newSystemClockMillis); @@ -592,6 +592,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { } else { mLastAutoSystemClockTimeSet = null; } + return true; } /** diff --git a/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorService.java b/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorService.java index fc5258403685b..0b85052e15584 100644 --- a/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorService.java +++ b/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorService.java @@ -24,6 +24,7 @@ import android.app.timezonedetector.TelephonyTimeZoneSuggestion; import android.content.ContentResolver; import android.content.Context; import android.database.ContentObserver; +import android.os.Binder; import android.os.Handler; import android.os.ResultReceiver; import android.os.ShellCallback; @@ -95,11 +96,16 @@ public final class TimeZoneDetectorService extends ITimeZoneDetectorService.Stub } @Override - public void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion timeZoneSuggestion) { + public boolean suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion timeZoneSuggestion) { enforceSuggestManualTimeZonePermission(); Objects.requireNonNull(timeZoneSuggestion); - mHandler.post(() -> mTimeZoneDetectorStrategy.suggestManualTimeZone(timeZoneSuggestion)); + long token = Binder.clearCallingIdentity(); + try { + return mTimeZoneDetectorStrategy.suggestManualTimeZone(timeZoneSuggestion); + } finally { + Binder.restoreCallingIdentity(token); + } } @Override diff --git a/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorStrategy.java b/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorStrategy.java index e0b3ad526f350..7f22e6ce204c8 100644 --- a/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorStrategy.java +++ b/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorStrategy.java @@ -37,9 +37,13 @@ import java.io.PrintWriter; public interface TimeZoneDetectorStrategy { /** - * Suggests a time zone for the device using manually-entered (i.e. user sourced) information. + * Suggests a time zone for the device, determined from the user's manually entered information. + * Returns {@code false} if the suggestion was invalid, or the device configuration prevented + * the suggestion being used, {@code true} if the suggestion was accepted. A suggestion that is + * valid but does not change the time zone because it matches the current device time zone is + * considered accepted. */ - void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion suggestion); + boolean suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion suggestion); /** * Suggests a time zone for the device, or withdraws a previous suggestion if diff --git a/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorStrategyImpl.java b/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorStrategyImpl.java index d318b1a4732cd..69a2b54e54c14 100644 --- a/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorStrategyImpl.java +++ b/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorStrategyImpl.java @@ -199,12 +199,13 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat } @Override - public synchronized void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion suggestion) { + public synchronized boolean suggestManualTimeZone( + @NonNull ManualTimeZoneSuggestion suggestion) { Objects.requireNonNull(suggestion); String timeZoneId = suggestion.getZoneId(); String cause = "Manual time suggestion received: suggestion=" + suggestion; - setDeviceTimeZoneIfRequired(ORIGIN_MANUAL, timeZoneId, cause); + return setDeviceTimeZoneIfRequired(ORIGIN_MANUAL, timeZoneId, cause); } @Override @@ -305,7 +306,7 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat } @GuardedBy("this") - private void setDeviceTimeZoneIfRequired( + private boolean setDeviceTimeZoneIfRequired( @Origin int origin, @NonNull String newZoneId, @NonNull String cause) { Objects.requireNonNull(newZoneId); Objects.requireNonNull(cause); @@ -319,7 +320,7 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat + ", newZoneId=" + newZoneId + ", cause=" + cause); } - return; + return false; } } else { if (mCallback.isAutoTimeZoneDetectionEnabled()) { @@ -329,7 +330,7 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat + ", newZoneId=" + newZoneId + ", cause=" + cause); } - return; + return false; } } @@ -346,7 +347,7 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat + ", newZoneId=" + newZoneId + ", cause=" + cause); } - return; + return true; } mCallback.setDeviceTimeZone(newZoneId); @@ -359,6 +360,7 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat Slog.d(LOG_TAG, msg); } mTimeZoneChangesLog.log(msg); + return true; } private static boolean isOriginAutomatic(@Origin int origin) { diff --git a/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorServiceTest.java b/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorServiceTest.java index fc28167cd20d3..02031bc4f210c 100644 --- a/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorServiceTest.java @@ -132,15 +132,14 @@ public class TimeDetectorServiceTest { doNothing().when(mMockContext).enforceCallingOrSelfPermission(anyString(), any()); ManualTimeSuggestion manualTimeSuggestion = createManualTimeSuggestion(); - mTimeDetectorService.suggestManualTime(manualTimeSuggestion); - mTestHandler.assertTotalMessagesEnqueued(1); + + assertTrue(mTimeDetectorService.suggestManualTime(manualTimeSuggestion)); + mStubbedTimeDetectorStrategy.verifySuggestManualTimeCalled(manualTimeSuggestion); verify(mMockContext).enforceCallingOrSelfPermission( eq(android.Manifest.permission.SUGGEST_MANUAL_TIME_AND_ZONE), anyString()); - mTestHandler.waitForMessagesToBeProcessed(); - mStubbedTimeDetectorStrategy.verifySuggestManualTimeCalled(manualTimeSuggestion); } @Test(expected = SecurityException.class) @@ -232,8 +231,9 @@ public class TimeDetectorServiceTest { } @Override - public void suggestManualTime(ManualTimeSuggestion timeSuggestion) { + public boolean suggestManualTime(ManualTimeSuggestion timeSuggestion) { mLastManualSuggestion = timeSuggestion; + return true; } @Override @@ -263,11 +263,11 @@ public class TimeDetectorServiceTest { assertEquals(expectedSuggestion, mLastTelephonySuggestion); } - public void verifySuggestManualTimeCalled(ManualTimeSuggestion expectedSuggestion) { + void verifySuggestManualTimeCalled(ManualTimeSuggestion expectedSuggestion) { assertEquals(expectedSuggestion, mLastManualSuggestion); } - public void verifySuggestNetworkTimeCalled(NetworkTimeSuggestion expectedSuggestion) { + void verifySuggestNetworkTimeCalled(NetworkTimeSuggestion expectedSuggestion) { assertEquals(expectedSuggestion, mLastNetworkSuggestion); } diff --git a/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorStrategyImplTest.java b/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorStrategyImplTest.java index 1f38b7093f7a5..1be074dfa5a3f 100644 --- a/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorStrategyImplTest.java +++ b/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorStrategyImplTest.java @@ -403,7 +403,7 @@ public class TimeDetectorStrategyImplTest { long expectedSystemClockMillis = mScript.calculateTimeInMillisForNow(timeSuggestion.getUtcTime()); - mScript.simulateManualTimeSuggestion(timeSuggestion) + mScript.simulateManualTimeSuggestion(timeSuggestion, true /* expectedResult */) .verifySystemClockWasSetAndResetCallTracking(expectedSystemClockMillis); } @@ -448,7 +448,7 @@ public class TimeDetectorStrategyImplTest { long expectedManualClockMillis = mScript.calculateTimeInMillisForNow(manualTimeSuggestion.getUtcTime()); - mScript.simulateManualTimeSuggestion(manualTimeSuggestion) + mScript.simulateManualTimeSuggestion(manualTimeSuggestion, true /* expectedResult */) .verifySystemClockWasSetAndResetCallTracking(expectedManualClockMillis) .assertLatestTelephonySuggestion(slotIndex, telephonyTimeSuggestion); @@ -481,7 +481,7 @@ public class TimeDetectorStrategyImplTest { mScript.generateManualTimeSuggestion(ARBITRARY_TEST_TIME_MILLIS); mScript.simulateTimePassing() - .simulateManualTimeSuggestion(timeSuggestion) + .simulateManualTimeSuggestion(timeSuggestion, false /* expectedResult */) .verifySystemClockWasNotSetAndResetCallTracking(); } @@ -763,8 +763,9 @@ public class TimeDetectorStrategyImplTest { return this; } - Script simulateManualTimeSuggestion(ManualTimeSuggestion timeSuggestion) { - mTimeDetectorStrategy.suggestManualTime(timeSuggestion); + Script simulateManualTimeSuggestion( + ManualTimeSuggestion timeSuggestion, boolean expectedResult) { + assertEquals(expectedResult, mTimeDetectorStrategy.suggestManualTime(timeSuggestion)); return this; } diff --git a/services/tests/servicestests/src/com/android/server/timezonedetector/TimeZoneDetectorServiceTest.java b/services/tests/servicestests/src/com/android/server/timezonedetector/TimeZoneDetectorServiceTest.java index da34e1b792c16..baec558f3ac65 100644 --- a/services/tests/servicestests/src/com/android/server/timezonedetector/TimeZoneDetectorServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/timezonedetector/TimeZoneDetectorServiceTest.java @@ -96,15 +96,16 @@ public class TimeZoneDetectorServiceTest { doNothing().when(mMockContext).enforceCallingOrSelfPermission(anyString(), any()); ManualTimeZoneSuggestion timeZoneSuggestion = createManualTimeZoneSuggestion(); - mTimeZoneDetectorService.suggestManualTimeZone(timeZoneSuggestion); - mTestHandler.assertTotalMessagesEnqueued(1); + + boolean expectedResult = true; // The test strategy always returns true. + assertEquals(expectedResult, + mTimeZoneDetectorService.suggestManualTimeZone(timeZoneSuggestion)); + + mStubbedTimeZoneDetectorStrategy.verifySuggestManualTimeZoneCalled(timeZoneSuggestion); verify(mMockContext).enforceCallingOrSelfPermission( eq(android.Manifest.permission.SUGGEST_MANUAL_TIME_AND_ZONE), anyString()); - - mTestHandler.waitForMessagesToBeProcessed(); - mStubbedTimeZoneDetectorStrategy.verifySuggestManualTimeZoneCalled(timeZoneSuggestion); } @Test(expected = SecurityException.class) @@ -187,8 +188,9 @@ public class TimeZoneDetectorServiceTest { private boolean mDumpCalled; @Override - public void suggestManualTimeZone(ManualTimeZoneSuggestion timeZoneSuggestion) { + public boolean suggestManualTimeZone(ManualTimeZoneSuggestion timeZoneSuggestion) { mLastManualSuggestion = timeZoneSuggestion; + return true; } @Override diff --git a/services/tests/servicestests/src/com/android/server/timezonedetector/TimeZoneDetectorStrategyImplTest.java b/services/tests/servicestests/src/com/android/server/timezonedetector/TimeZoneDetectorStrategyImplTest.java index 30bb12e3e6169..1fbe781a67b7d 100644 --- a/services/tests/servicestests/src/com/android/server/timezonedetector/TimeZoneDetectorStrategyImplTest.java +++ b/services/tests/servicestests/src/com/android/server/timezonedetector/TimeZoneDetectorStrategyImplTest.java @@ -98,7 +98,7 @@ public class TimeZoneDetectorStrategyImplTest { .initializeAutoTimeZoneDetection(true) .initializeTimeZoneSetting(ARBITRARY_TIME_ZONE_ID); - script.suggestTelephonyTimeZone(slotIndex1TimeZoneSuggestion) + script.simulateTelephonyTimeZoneSuggestion(slotIndex1TimeZoneSuggestion) .verifyTimeZoneNotSet(); // Assert internal service state. @@ -111,7 +111,7 @@ public class TimeZoneDetectorStrategyImplTest { assertEquals(expectedSlotIndex1ScoredSuggestion, mTimeZoneDetectorStrategy.findBestTelephonySuggestionForTests()); - script.suggestTelephonyTimeZone(slotIndex2TimeZoneSuggestion) + script.simulateTelephonyTimeZoneSuggestion(slotIndex2TimeZoneSuggestion) .verifyTimeZoneNotSet(); // Assert internal service state. @@ -144,8 +144,7 @@ public class TimeZoneDetectorStrategyImplTest { { TelephonyTimeZoneSuggestion lowQualitySuggestion = testCase.createSuggestion(SLOT_INDEX1, "America/New_York"); - - script.suggestTelephonyTimeZone(lowQualitySuggestion) + script.simulateTelephonyTimeZoneSuggestion(lowQualitySuggestion) .verifyTimeZoneNotSet(); // Assert internal service state. @@ -162,7 +161,7 @@ public class TimeZoneDetectorStrategyImplTest { { TelephonyTimeZoneSuggestion goodQualitySuggestion = testCase2.createSuggestion(SLOT_INDEX1, "Europe/London"); - script.suggestTelephonyTimeZone(goodQualitySuggestion) + script.simulateTelephonyTimeZoneSuggestion(goodQualitySuggestion) .verifyTimeZoneSetAndReset(goodQualitySuggestion); // Assert internal service state. @@ -179,7 +178,7 @@ public class TimeZoneDetectorStrategyImplTest { { TelephonyTimeZoneSuggestion lowQualitySuggestion2 = testCase.createSuggestion(SLOT_INDEX1, "America/Los_Angeles"); - script.suggestTelephonyTimeZone(lowQualitySuggestion2) + script.simulateTelephonyTimeZoneSuggestion(lowQualitySuggestion2) .verifyTimeZoneNotSet(); // Assert internal service state. @@ -208,7 +207,7 @@ public class TimeZoneDetectorStrategyImplTest { TelephonyTimeZoneSuggestion suggestion = testCase.createSuggestion(SLOT_INDEX1, "Europe/London"); - script.suggestTelephonyTimeZone(suggestion); + script.simulateTelephonyTimeZoneSuggestion(suggestion); // When time zone detection is not enabled, the time zone suggestion will not be set // regardless of the score. @@ -288,7 +287,7 @@ public class TimeZoneDetectorStrategyImplTest { new QualifiedTelephonyTimeZoneSuggestion( zoneSlotIndex1Suggestion, testCase.expectedScore); - script.suggestTelephonyTimeZone(zoneSlotIndex1Suggestion); + script.simulateTelephonyTimeZoneSuggestion(zoneSlotIndex1Suggestion); if (testCase.expectedScore >= TELEPHONY_SCORE_USAGE_THRESHOLD) { script.verifyTimeZoneSetAndReset(zoneSlotIndex1Suggestion); } else { @@ -324,8 +323,8 @@ public class TimeZoneDetectorStrategyImplTest { .initializeTimeZoneSetting(ARBITRARY_TIME_ZONE_ID) // Initialize the latest suggestions as empty so we don't need to worry about nulls // below for the first loop. - .suggestTelephonyTimeZone(emptySlotIndex1Suggestion) - .suggestTelephonyTimeZone(emptySlotIndex2Suggestion) + .simulateTelephonyTimeZoneSuggestion(emptySlotIndex1Suggestion) + .simulateTelephonyTimeZoneSuggestion(emptySlotIndex2Suggestion) .resetState(); for (SuggestionTestCase testCase : TEST_CASES) { @@ -341,7 +340,7 @@ public class TimeZoneDetectorStrategyImplTest { testCase.expectedScore); // Start the test by making a suggestion for slotIndex1. - script.suggestTelephonyTimeZone(zoneSlotIndex1Suggestion); + script.simulateTelephonyTimeZoneSuggestion(zoneSlotIndex1Suggestion); if (testCase.expectedScore >= TELEPHONY_SCORE_USAGE_THRESHOLD) { script.verifyTimeZoneSetAndReset(zoneSlotIndex1Suggestion); } else { @@ -358,7 +357,7 @@ public class TimeZoneDetectorStrategyImplTest { // SlotIndex2 then makes an alternative suggestion with an identical score. SlotIndex1's // suggestion should still "win" if it is above the required threshold. - script.suggestTelephonyTimeZone(zoneSlotIndex2Suggestion); + script.simulateTelephonyTimeZoneSuggestion(zoneSlotIndex2Suggestion); script.verifyTimeZoneNotSet(); // Assert internal service state. @@ -373,7 +372,7 @@ public class TimeZoneDetectorStrategyImplTest { // Withdrawing slotIndex1's suggestion should leave slotIndex2 as the new winner. Since // the zoneId is different, the time zone setting should be updated if the score is high // enough. - script.suggestTelephonyTimeZone(emptySlotIndex1Suggestion); + script.simulateTelephonyTimeZoneSuggestion(emptySlotIndex1Suggestion); if (testCase.expectedScore >= TELEPHONY_SCORE_USAGE_THRESHOLD) { script.verifyTimeZoneSetAndReset(zoneSlotIndex2Suggestion); } else { @@ -389,7 +388,7 @@ public class TimeZoneDetectorStrategyImplTest { mTimeZoneDetectorStrategy.findBestTelephonySuggestionForTests()); // Reset the state for the next loop. - script.suggestTelephonyTimeZone(emptySlotIndex2Suggestion) + script.simulateTelephonyTimeZoneSuggestion(emptySlotIndex2Suggestion) .verifyTimeZoneNotSet(); assertEquals(expectedEmptySlotIndex1ScoredSuggestion, mTimeZoneDetectorStrategy.getLatestTelephonySuggestion(SLOT_INDEX1)); @@ -417,10 +416,10 @@ public class TimeZoneDetectorStrategyImplTest { testCase.createSuggestion(SLOT_INDEX1, "America/New_York"); // Initialization. - script.suggestTelephonyTimeZone(losAngelesSuggestion) + script.simulateTelephonyTimeZoneSuggestion(losAngelesSuggestion) .verifyTimeZoneSetAndReset(losAngelesSuggestion); // Suggest it again - it should not be set because it is already set. - script.suggestTelephonyTimeZone(losAngelesSuggestion) + script.simulateTelephonyTimeZoneSuggestion(losAngelesSuggestion) .verifyTimeZoneNotSet(); // Toggling time zone detection should set the device time zone only if the current setting @@ -433,7 +432,7 @@ public class TimeZoneDetectorStrategyImplTest { // Simulate a user turning auto detection off, a new suggestion being made while auto // detection is off, and the user turning it on again. script.autoTimeZoneDetectionEnabled(false) - .suggestTelephonyTimeZone(newYorkSuggestion) + .simulateTelephonyTimeZoneSuggestion(newYorkSuggestion) .verifyTimeZoneNotSet(); // Latest suggestion should be used. script.autoTimeZoneDetectionEnabled(true) @@ -447,7 +446,8 @@ public class TimeZoneDetectorStrategyImplTest { .initializeAutoTimeZoneDetection(true); // Auto time zone detection is enabled so the manual suggestion should be ignored. - script.suggestManualTimeZone(createManualSuggestion("Europe/Paris")) + script.simulateManualTimeZoneSuggestion( + createManualSuggestion("Europe/Paris"), false /* expectedResult */) .verifyTimeZoneNotSet(); } @@ -460,7 +460,7 @@ public class TimeZoneDetectorStrategyImplTest { // Auto time zone detection is disabled so the manual suggestion should be used. ManualTimeZoneSuggestion manualSuggestion = createManualSuggestion("Europe/Paris"); - script.suggestManualTimeZone(manualSuggestion) + script.simulateManualTimeZoneSuggestion(manualSuggestion, true /* expectedResult */) .verifyTimeZoneSetAndReset(manualSuggestion); } @@ -603,14 +603,16 @@ public class TimeZoneDetectorStrategyImplTest { /** * Simulates the time zone detection strategy receiving a telephony-originated suggestion. */ - Script suggestTelephonyTimeZone(TelephonyTimeZoneSuggestion timeZoneSuggestion) { + Script simulateTelephonyTimeZoneSuggestion(TelephonyTimeZoneSuggestion timeZoneSuggestion) { mTimeZoneDetectorStrategy.suggestTelephonyTimeZone(timeZoneSuggestion); return this; } /** Simulates the time zone detection strategy receiving a user-originated suggestion. */ - Script suggestManualTimeZone(ManualTimeZoneSuggestion manualTimeZoneSuggestion) { - mTimeZoneDetectorStrategy.suggestManualTimeZone(manualTimeZoneSuggestion); + Script simulateManualTimeZoneSuggestion( + ManualTimeZoneSuggestion manualTimeZoneSuggestion, boolean expectedResult) { + assertEquals(expectedResult, + mTimeZoneDetectorStrategy.suggestManualTimeZone(manualTimeZoneSuggestion)); return this; }