Merge "Make manual suggestions synchronous/return result" am: 08466f6151

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1252169

Change-Id: I1cec1c82891d75167041b7eee5bd97b34d153b33
This commit is contained in:
Almaz Mingaleev
2020-11-25 12:01:50 +00:00
committed by Automerger Merge Worker
16 changed files with 110 additions and 74 deletions

View File

@@ -34,7 +34,7 @@ import android.app.timedetector.TelephonyTimeSuggestion;
* {@hide} * {@hide}
*/ */
interface ITimeDetectorService { interface ITimeDetectorService {
void suggestManualTime(in ManualTimeSuggestion timeSuggestion); boolean suggestManualTime(in ManualTimeSuggestion timeSuggestion);
void suggestNetworkTime(in NetworkTimeSuggestion timeSuggestion); void suggestNetworkTime(in NetworkTimeSuggestion timeSuggestion);
void suggestTelephonyTime(in TelephonyTimeSuggestion timeSuggestion); void suggestTelephonyTime(in TelephonyTimeSuggestion timeSuggestion);
} }

View File

@@ -53,12 +53,16 @@ public interface TimeDetector {
void suggestTelephonyTime(@NonNull TelephonyTimeSuggestion timeSuggestion); 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 * @hide
*/ */
@RequiresPermission(android.Manifest.permission.SUGGEST_MANUAL_TIME_AND_ZONE) @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. * Suggests the time according to a network time source like NTP.

View File

@@ -52,12 +52,12 @@ public final class TimeDetectorImpl implements TimeDetector {
} }
@Override @Override
public void suggestManualTime(@NonNull ManualTimeSuggestion timeSuggestion) { public boolean suggestManualTime(@NonNull ManualTimeSuggestion timeSuggestion) {
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "suggestManualTime called: " + timeSuggestion); Log.d(TAG, "suggestManualTime called: " + timeSuggestion);
} }
try { try {
mITimeDetectorService.suggestManualTime(timeSuggestion); return mITimeDetectorService.suggestManualTime(timeSuggestion);
} catch (RemoteException e) { } catch (RemoteException e) {
throw e.rethrowFromSystemServer(); throw e.rethrowFromSystemServer();
} }

View File

@@ -33,6 +33,6 @@ import android.app.timezonedetector.TelephonyTimeZoneSuggestion;
* {@hide} * {@hide}
*/ */
interface ITimeZoneDetectorService { interface ITimeZoneDetectorService {
void suggestManualTimeZone(in ManualTimeZoneSuggestion timeZoneSuggestion); boolean suggestManualTimeZone(in ManualTimeZoneSuggestion timeZoneSuggestion);
void suggestTelephonyTimeZone(in TelephonyTimeZoneSuggestion timeZoneSuggestion); void suggestTelephonyTimeZone(in TelephonyTimeZoneSuggestion timeZoneSuggestion);
} }

View File

@@ -41,13 +41,16 @@ public interface TimeZoneDetector {
} }
/** /**
* Suggests the current time zone, determined using the user's manually entered information, to * Suggests the current time zone, determined from the user's manually entered information, to
* the detector. The detector may ignore the signal based on system settings. * 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 * @hide
*/ */
@RequiresPermission(android.Manifest.permission.SUGGEST_MANUAL_TIME_AND_ZONE) @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 * Suggests the current time zone, determined using telephony signals, to the detector. The

View File

@@ -40,12 +40,12 @@ public final class TimeZoneDetectorImpl implements TimeZoneDetector {
} }
@Override @Override
public void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion timeZoneSuggestion) { public boolean suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion timeZoneSuggestion) {
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "suggestManualTimeZone called: " + timeZoneSuggestion); Log.d(TAG, "suggestManualTimeZone called: " + timeZoneSuggestion);
} }
try { try {
mITimeZoneDetectorService.suggestManualTimeZone(timeZoneSuggestion); return mITimeZoneDetectorService.suggestManualTimeZone(timeZoneSuggestion);
} catch (RemoteException e) { } catch (RemoteException e) {
throw e.rethrowFromSystemServer(); throw e.rethrowFromSystemServer();
} }

View File

@@ -25,6 +25,7 @@ import android.app.timedetector.TelephonyTimeSuggestion;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.database.ContentObserver; import android.database.ContentObserver;
import android.os.Binder;
import android.os.Handler; import android.os.Handler;
import android.provider.Settings; import android.provider.Settings;
@@ -101,11 +102,16 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub {
} }
@Override @Override
public void suggestManualTime(@NonNull ManualTimeSuggestion timeSignal) { public boolean suggestManualTime(@NonNull ManualTimeSuggestion timeSignal) {
enforceSuggestManualTimePermission(); enforceSuggestManualTimePermission();
Objects.requireNonNull(timeSignal); Objects.requireNonNull(timeSignal);
mHandler.post(() -> mTimeDetectorStrategy.suggestManualTime(timeSignal)); long token = Binder.clearCallingIdentity();
try {
return mTimeDetectorStrategy.suggestManualTime(timeSignal);
} finally {
Binder.restoreCallingIdentity(token);
}
} }
@Override @Override

View File

@@ -40,8 +40,13 @@ public interface TimeDetectorStrategy {
/** Process the suggested time from telephony sources. */ /** Process the suggested time from telephony sources. */
void suggestTelephonyTime(@NonNull TelephonyTimeSuggestion timeSuggestion); 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. */ /** Process the suggested time from network sources. */
void suggestNetworkTime(@NonNull NetworkTimeSuggestion timeSuggestion); void suggestNetworkTime(@NonNull NetworkTimeSuggestion timeSuggestion);

View File

@@ -164,15 +164,15 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy {
} }
@Override @Override
public synchronized void suggestManualTime(@NonNull ManualTimeSuggestion suggestion) { public synchronized boolean suggestManualTime(@NonNull ManualTimeSuggestion suggestion) {
final TimestampedValue<Long> newUtcTime = suggestion.getUtcTime(); final TimestampedValue<Long> newUtcTime = suggestion.getUtcTime();
if (!validateSuggestionTime(newUtcTime, suggestion)) { if (!validateSuggestionTime(newUtcTime, suggestion)) {
return; return false;
} }
String cause = "Manual time suggestion received: suggestion=" + suggestion; String cause = "Manual time suggestion received: suggestion=" + suggestion;
setSystemClockIfRequired(ORIGIN_MANUAL, newUtcTime, cause); return setSystemClockIfRequired(ORIGIN_MANUAL, newUtcTime, cause);
} }
@Override @Override
@@ -489,7 +489,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy {
} }
@GuardedBy("this") @GuardedBy("this")
private void setSystemClockIfRequired( private boolean setSystemClockIfRequired(
@Origin int origin, @NonNull TimestampedValue<Long> time, @NonNull String cause) { @Origin int origin, @NonNull TimestampedValue<Long> time, @NonNull String cause) {
boolean isOriginAutomatic = isOriginAutomatic(origin); boolean isOriginAutomatic = isOriginAutomatic(origin);
@@ -501,7 +501,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy {
+ ", time=" + time + ", time=" + time
+ ", cause=" + cause); + ", cause=" + cause);
} }
return; return false;
} }
} else { } else {
if (mCallback.isAutoTimeDetectionEnabled()) { if (mCallback.isAutoTimeDetectionEnabled()) {
@@ -511,13 +511,13 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy {
+ ", time=" + time + ", time=" + time
+ ", cause=" + cause); + ", cause=" + cause);
} }
return; return false;
} }
} }
mCallback.acquireWakeLock(); mCallback.acquireWakeLock();
try { try {
setSystemClockUnderWakeLock(origin, time, cause); return setSystemClockUnderWakeLock(origin, time, cause);
} finally { } finally {
mCallback.releaseWakeLock(); mCallback.releaseWakeLock();
} }
@@ -528,7 +528,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy {
} }
@GuardedBy("this") @GuardedBy("this")
private void setSystemClockUnderWakeLock( private boolean setSystemClockUnderWakeLock(
int origin, @NonNull TimestampedValue<Long> newTime, @NonNull Object cause) { int origin, @NonNull TimestampedValue<Long> newTime, @NonNull Object cause) {
long elapsedRealtimeMillis = mCallback.elapsedRealtimeMillis(); long elapsedRealtimeMillis = mCallback.elapsedRealtimeMillis();
@@ -572,7 +572,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy {
+ " systemClockUpdateThreshold=" + systemClockUpdateThreshold + " systemClockUpdateThreshold=" + systemClockUpdateThreshold
+ " absTimeDifference=" + absTimeDifference); + " absTimeDifference=" + absTimeDifference);
} }
return; return true;
} }
mCallback.setSystemClock(newSystemClockMillis); mCallback.setSystemClock(newSystemClockMillis);
@@ -592,6 +592,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy {
} else { } else {
mLastAutoSystemClockTimeSet = null; mLastAutoSystemClockTimeSet = null;
} }
return true;
} }
/** /**

View File

@@ -24,6 +24,7 @@ import android.app.timezonedetector.TelephonyTimeZoneSuggestion;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.database.ContentObserver; import android.database.ContentObserver;
import android.os.Binder;
import android.os.Handler; import android.os.Handler;
import android.os.ResultReceiver; import android.os.ResultReceiver;
import android.os.ShellCallback; import android.os.ShellCallback;
@@ -95,11 +96,16 @@ public final class TimeZoneDetectorService extends ITimeZoneDetectorService.Stub
} }
@Override @Override
public void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion timeZoneSuggestion) { public boolean suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion timeZoneSuggestion) {
enforceSuggestManualTimeZonePermission(); enforceSuggestManualTimeZonePermission();
Objects.requireNonNull(timeZoneSuggestion); Objects.requireNonNull(timeZoneSuggestion);
mHandler.post(() -> mTimeZoneDetectorStrategy.suggestManualTimeZone(timeZoneSuggestion)); long token = Binder.clearCallingIdentity();
try {
return mTimeZoneDetectorStrategy.suggestManualTimeZone(timeZoneSuggestion);
} finally {
Binder.restoreCallingIdentity(token);
}
} }
@Override @Override

View File

@@ -37,9 +37,13 @@ import java.io.PrintWriter;
public interface TimeZoneDetectorStrategy { 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 * Suggests a time zone for the device, or withdraws a previous suggestion if

View File

@@ -199,12 +199,13 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat
} }
@Override @Override
public synchronized void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion suggestion) { public synchronized boolean suggestManualTimeZone(
@NonNull ManualTimeZoneSuggestion suggestion) {
Objects.requireNonNull(suggestion); Objects.requireNonNull(suggestion);
String timeZoneId = suggestion.getZoneId(); String timeZoneId = suggestion.getZoneId();
String cause = "Manual time suggestion received: suggestion=" + suggestion; String cause = "Manual time suggestion received: suggestion=" + suggestion;
setDeviceTimeZoneIfRequired(ORIGIN_MANUAL, timeZoneId, cause); return setDeviceTimeZoneIfRequired(ORIGIN_MANUAL, timeZoneId, cause);
} }
@Override @Override
@@ -305,7 +306,7 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat
} }
@GuardedBy("this") @GuardedBy("this")
private void setDeviceTimeZoneIfRequired( private boolean setDeviceTimeZoneIfRequired(
@Origin int origin, @NonNull String newZoneId, @NonNull String cause) { @Origin int origin, @NonNull String newZoneId, @NonNull String cause) {
Objects.requireNonNull(newZoneId); Objects.requireNonNull(newZoneId);
Objects.requireNonNull(cause); Objects.requireNonNull(cause);
@@ -319,7 +320,7 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat
+ ", newZoneId=" + newZoneId + ", newZoneId=" + newZoneId
+ ", cause=" + cause); + ", cause=" + cause);
} }
return; return false;
} }
} else { } else {
if (mCallback.isAutoTimeZoneDetectionEnabled()) { if (mCallback.isAutoTimeZoneDetectionEnabled()) {
@@ -329,7 +330,7 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat
+ ", newZoneId=" + newZoneId + ", newZoneId=" + newZoneId
+ ", cause=" + cause); + ", cause=" + cause);
} }
return; return false;
} }
} }
@@ -346,7 +347,7 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat
+ ", newZoneId=" + newZoneId + ", newZoneId=" + newZoneId
+ ", cause=" + cause); + ", cause=" + cause);
} }
return; return true;
} }
mCallback.setDeviceTimeZone(newZoneId); mCallback.setDeviceTimeZone(newZoneId);
@@ -359,6 +360,7 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat
Slog.d(LOG_TAG, msg); Slog.d(LOG_TAG, msg);
} }
mTimeZoneChangesLog.log(msg); mTimeZoneChangesLog.log(msg);
return true;
} }
private static boolean isOriginAutomatic(@Origin int origin) { private static boolean isOriginAutomatic(@Origin int origin) {

View File

@@ -132,15 +132,14 @@ public class TimeDetectorServiceTest {
doNothing().when(mMockContext).enforceCallingOrSelfPermission(anyString(), any()); doNothing().when(mMockContext).enforceCallingOrSelfPermission(anyString(), any());
ManualTimeSuggestion manualTimeSuggestion = createManualTimeSuggestion(); ManualTimeSuggestion manualTimeSuggestion = createManualTimeSuggestion();
mTimeDetectorService.suggestManualTime(manualTimeSuggestion);
mTestHandler.assertTotalMessagesEnqueued(1); assertTrue(mTimeDetectorService.suggestManualTime(manualTimeSuggestion));
mStubbedTimeDetectorStrategy.verifySuggestManualTimeCalled(manualTimeSuggestion);
verify(mMockContext).enforceCallingOrSelfPermission( verify(mMockContext).enforceCallingOrSelfPermission(
eq(android.Manifest.permission.SUGGEST_MANUAL_TIME_AND_ZONE), eq(android.Manifest.permission.SUGGEST_MANUAL_TIME_AND_ZONE),
anyString()); anyString());
mTestHandler.waitForMessagesToBeProcessed();
mStubbedTimeDetectorStrategy.verifySuggestManualTimeCalled(manualTimeSuggestion);
} }
@Test(expected = SecurityException.class) @Test(expected = SecurityException.class)
@@ -232,8 +231,9 @@ public class TimeDetectorServiceTest {
} }
@Override @Override
public void suggestManualTime(ManualTimeSuggestion timeSuggestion) { public boolean suggestManualTime(ManualTimeSuggestion timeSuggestion) {
mLastManualSuggestion = timeSuggestion; mLastManualSuggestion = timeSuggestion;
return true;
} }
@Override @Override
@@ -263,11 +263,11 @@ public class TimeDetectorServiceTest {
assertEquals(expectedSuggestion, mLastTelephonySuggestion); assertEquals(expectedSuggestion, mLastTelephonySuggestion);
} }
public void verifySuggestManualTimeCalled(ManualTimeSuggestion expectedSuggestion) { void verifySuggestManualTimeCalled(ManualTimeSuggestion expectedSuggestion) {
assertEquals(expectedSuggestion, mLastManualSuggestion); assertEquals(expectedSuggestion, mLastManualSuggestion);
} }
public void verifySuggestNetworkTimeCalled(NetworkTimeSuggestion expectedSuggestion) { void verifySuggestNetworkTimeCalled(NetworkTimeSuggestion expectedSuggestion) {
assertEquals(expectedSuggestion, mLastNetworkSuggestion); assertEquals(expectedSuggestion, mLastNetworkSuggestion);
} }

View File

@@ -403,7 +403,7 @@ public class TimeDetectorStrategyImplTest {
long expectedSystemClockMillis = long expectedSystemClockMillis =
mScript.calculateTimeInMillisForNow(timeSuggestion.getUtcTime()); mScript.calculateTimeInMillisForNow(timeSuggestion.getUtcTime());
mScript.simulateManualTimeSuggestion(timeSuggestion) mScript.simulateManualTimeSuggestion(timeSuggestion, true /* expectedResult */)
.verifySystemClockWasSetAndResetCallTracking(expectedSystemClockMillis); .verifySystemClockWasSetAndResetCallTracking(expectedSystemClockMillis);
} }
@@ -448,7 +448,7 @@ public class TimeDetectorStrategyImplTest {
long expectedManualClockMillis = long expectedManualClockMillis =
mScript.calculateTimeInMillisForNow(manualTimeSuggestion.getUtcTime()); mScript.calculateTimeInMillisForNow(manualTimeSuggestion.getUtcTime());
mScript.simulateManualTimeSuggestion(manualTimeSuggestion) mScript.simulateManualTimeSuggestion(manualTimeSuggestion, true /* expectedResult */)
.verifySystemClockWasSetAndResetCallTracking(expectedManualClockMillis) .verifySystemClockWasSetAndResetCallTracking(expectedManualClockMillis)
.assertLatestTelephonySuggestion(slotIndex, telephonyTimeSuggestion); .assertLatestTelephonySuggestion(slotIndex, telephonyTimeSuggestion);
@@ -481,7 +481,7 @@ public class TimeDetectorStrategyImplTest {
mScript.generateManualTimeSuggestion(ARBITRARY_TEST_TIME_MILLIS); mScript.generateManualTimeSuggestion(ARBITRARY_TEST_TIME_MILLIS);
mScript.simulateTimePassing() mScript.simulateTimePassing()
.simulateManualTimeSuggestion(timeSuggestion) .simulateManualTimeSuggestion(timeSuggestion, false /* expectedResult */)
.verifySystemClockWasNotSetAndResetCallTracking(); .verifySystemClockWasNotSetAndResetCallTracking();
} }
@@ -763,8 +763,9 @@ public class TimeDetectorStrategyImplTest {
return this; return this;
} }
Script simulateManualTimeSuggestion(ManualTimeSuggestion timeSuggestion) { Script simulateManualTimeSuggestion(
mTimeDetectorStrategy.suggestManualTime(timeSuggestion); ManualTimeSuggestion timeSuggestion, boolean expectedResult) {
assertEquals(expectedResult, mTimeDetectorStrategy.suggestManualTime(timeSuggestion));
return this; return this;
} }

View File

@@ -96,15 +96,16 @@ public class TimeZoneDetectorServiceTest {
doNothing().when(mMockContext).enforceCallingOrSelfPermission(anyString(), any()); doNothing().when(mMockContext).enforceCallingOrSelfPermission(anyString(), any());
ManualTimeZoneSuggestion timeZoneSuggestion = createManualTimeZoneSuggestion(); 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( verify(mMockContext).enforceCallingOrSelfPermission(
eq(android.Manifest.permission.SUGGEST_MANUAL_TIME_AND_ZONE), eq(android.Manifest.permission.SUGGEST_MANUAL_TIME_AND_ZONE),
anyString()); anyString());
mTestHandler.waitForMessagesToBeProcessed();
mStubbedTimeZoneDetectorStrategy.verifySuggestManualTimeZoneCalled(timeZoneSuggestion);
} }
@Test(expected = SecurityException.class) @Test(expected = SecurityException.class)
@@ -187,8 +188,9 @@ public class TimeZoneDetectorServiceTest {
private boolean mDumpCalled; private boolean mDumpCalled;
@Override @Override
public void suggestManualTimeZone(ManualTimeZoneSuggestion timeZoneSuggestion) { public boolean suggestManualTimeZone(ManualTimeZoneSuggestion timeZoneSuggestion) {
mLastManualSuggestion = timeZoneSuggestion; mLastManualSuggestion = timeZoneSuggestion;
return true;
} }
@Override @Override

View File

@@ -98,7 +98,7 @@ public class TimeZoneDetectorStrategyImplTest {
.initializeAutoTimeZoneDetection(true) .initializeAutoTimeZoneDetection(true)
.initializeTimeZoneSetting(ARBITRARY_TIME_ZONE_ID); .initializeTimeZoneSetting(ARBITRARY_TIME_ZONE_ID);
script.suggestTelephonyTimeZone(slotIndex1TimeZoneSuggestion) script.simulateTelephonyTimeZoneSuggestion(slotIndex1TimeZoneSuggestion)
.verifyTimeZoneNotSet(); .verifyTimeZoneNotSet();
// Assert internal service state. // Assert internal service state.
@@ -111,7 +111,7 @@ public class TimeZoneDetectorStrategyImplTest {
assertEquals(expectedSlotIndex1ScoredSuggestion, assertEquals(expectedSlotIndex1ScoredSuggestion,
mTimeZoneDetectorStrategy.findBestTelephonySuggestionForTests()); mTimeZoneDetectorStrategy.findBestTelephonySuggestionForTests());
script.suggestTelephonyTimeZone(slotIndex2TimeZoneSuggestion) script.simulateTelephonyTimeZoneSuggestion(slotIndex2TimeZoneSuggestion)
.verifyTimeZoneNotSet(); .verifyTimeZoneNotSet();
// Assert internal service state. // Assert internal service state.
@@ -144,8 +144,7 @@ public class TimeZoneDetectorStrategyImplTest {
{ {
TelephonyTimeZoneSuggestion lowQualitySuggestion = TelephonyTimeZoneSuggestion lowQualitySuggestion =
testCase.createSuggestion(SLOT_INDEX1, "America/New_York"); testCase.createSuggestion(SLOT_INDEX1, "America/New_York");
script.simulateTelephonyTimeZoneSuggestion(lowQualitySuggestion)
script.suggestTelephonyTimeZone(lowQualitySuggestion)
.verifyTimeZoneNotSet(); .verifyTimeZoneNotSet();
// Assert internal service state. // Assert internal service state.
@@ -162,7 +161,7 @@ public class TimeZoneDetectorStrategyImplTest {
{ {
TelephonyTimeZoneSuggestion goodQualitySuggestion = TelephonyTimeZoneSuggestion goodQualitySuggestion =
testCase2.createSuggestion(SLOT_INDEX1, "Europe/London"); testCase2.createSuggestion(SLOT_INDEX1, "Europe/London");
script.suggestTelephonyTimeZone(goodQualitySuggestion) script.simulateTelephonyTimeZoneSuggestion(goodQualitySuggestion)
.verifyTimeZoneSetAndReset(goodQualitySuggestion); .verifyTimeZoneSetAndReset(goodQualitySuggestion);
// Assert internal service state. // Assert internal service state.
@@ -179,7 +178,7 @@ public class TimeZoneDetectorStrategyImplTest {
{ {
TelephonyTimeZoneSuggestion lowQualitySuggestion2 = TelephonyTimeZoneSuggestion lowQualitySuggestion2 =
testCase.createSuggestion(SLOT_INDEX1, "America/Los_Angeles"); testCase.createSuggestion(SLOT_INDEX1, "America/Los_Angeles");
script.suggestTelephonyTimeZone(lowQualitySuggestion2) script.simulateTelephonyTimeZoneSuggestion(lowQualitySuggestion2)
.verifyTimeZoneNotSet(); .verifyTimeZoneNotSet();
// Assert internal service state. // Assert internal service state.
@@ -208,7 +207,7 @@ public class TimeZoneDetectorStrategyImplTest {
TelephonyTimeZoneSuggestion suggestion = TelephonyTimeZoneSuggestion suggestion =
testCase.createSuggestion(SLOT_INDEX1, "Europe/London"); 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 // When time zone detection is not enabled, the time zone suggestion will not be set
// regardless of the score. // regardless of the score.
@@ -288,7 +287,7 @@ public class TimeZoneDetectorStrategyImplTest {
new QualifiedTelephonyTimeZoneSuggestion( new QualifiedTelephonyTimeZoneSuggestion(
zoneSlotIndex1Suggestion, testCase.expectedScore); zoneSlotIndex1Suggestion, testCase.expectedScore);
script.suggestTelephonyTimeZone(zoneSlotIndex1Suggestion); script.simulateTelephonyTimeZoneSuggestion(zoneSlotIndex1Suggestion);
if (testCase.expectedScore >= TELEPHONY_SCORE_USAGE_THRESHOLD) { if (testCase.expectedScore >= TELEPHONY_SCORE_USAGE_THRESHOLD) {
script.verifyTimeZoneSetAndReset(zoneSlotIndex1Suggestion); script.verifyTimeZoneSetAndReset(zoneSlotIndex1Suggestion);
} else { } else {
@@ -324,8 +323,8 @@ public class TimeZoneDetectorStrategyImplTest {
.initializeTimeZoneSetting(ARBITRARY_TIME_ZONE_ID) .initializeTimeZoneSetting(ARBITRARY_TIME_ZONE_ID)
// Initialize the latest suggestions as empty so we don't need to worry about nulls // Initialize the latest suggestions as empty so we don't need to worry about nulls
// below for the first loop. // below for the first loop.
.suggestTelephonyTimeZone(emptySlotIndex1Suggestion) .simulateTelephonyTimeZoneSuggestion(emptySlotIndex1Suggestion)
.suggestTelephonyTimeZone(emptySlotIndex2Suggestion) .simulateTelephonyTimeZoneSuggestion(emptySlotIndex2Suggestion)
.resetState(); .resetState();
for (SuggestionTestCase testCase : TEST_CASES) { for (SuggestionTestCase testCase : TEST_CASES) {
@@ -341,7 +340,7 @@ public class TimeZoneDetectorStrategyImplTest {
testCase.expectedScore); testCase.expectedScore);
// Start the test by making a suggestion for slotIndex1. // Start the test by making a suggestion for slotIndex1.
script.suggestTelephonyTimeZone(zoneSlotIndex1Suggestion); script.simulateTelephonyTimeZoneSuggestion(zoneSlotIndex1Suggestion);
if (testCase.expectedScore >= TELEPHONY_SCORE_USAGE_THRESHOLD) { if (testCase.expectedScore >= TELEPHONY_SCORE_USAGE_THRESHOLD) {
script.verifyTimeZoneSetAndReset(zoneSlotIndex1Suggestion); script.verifyTimeZoneSetAndReset(zoneSlotIndex1Suggestion);
} else { } else {
@@ -358,7 +357,7 @@ public class TimeZoneDetectorStrategyImplTest {
// SlotIndex2 then makes an alternative suggestion with an identical score. SlotIndex1's // SlotIndex2 then makes an alternative suggestion with an identical score. SlotIndex1's
// suggestion should still "win" if it is above the required threshold. // suggestion should still "win" if it is above the required threshold.
script.suggestTelephonyTimeZone(zoneSlotIndex2Suggestion); script.simulateTelephonyTimeZoneSuggestion(zoneSlotIndex2Suggestion);
script.verifyTimeZoneNotSet(); script.verifyTimeZoneNotSet();
// Assert internal service state. // Assert internal service state.
@@ -373,7 +372,7 @@ public class TimeZoneDetectorStrategyImplTest {
// Withdrawing slotIndex1's suggestion should leave slotIndex2 as the new winner. Since // 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 // the zoneId is different, the time zone setting should be updated if the score is high
// enough. // enough.
script.suggestTelephonyTimeZone(emptySlotIndex1Suggestion); script.simulateTelephonyTimeZoneSuggestion(emptySlotIndex1Suggestion);
if (testCase.expectedScore >= TELEPHONY_SCORE_USAGE_THRESHOLD) { if (testCase.expectedScore >= TELEPHONY_SCORE_USAGE_THRESHOLD) {
script.verifyTimeZoneSetAndReset(zoneSlotIndex2Suggestion); script.verifyTimeZoneSetAndReset(zoneSlotIndex2Suggestion);
} else { } else {
@@ -389,7 +388,7 @@ public class TimeZoneDetectorStrategyImplTest {
mTimeZoneDetectorStrategy.findBestTelephonySuggestionForTests()); mTimeZoneDetectorStrategy.findBestTelephonySuggestionForTests());
// Reset the state for the next loop. // Reset the state for the next loop.
script.suggestTelephonyTimeZone(emptySlotIndex2Suggestion) script.simulateTelephonyTimeZoneSuggestion(emptySlotIndex2Suggestion)
.verifyTimeZoneNotSet(); .verifyTimeZoneNotSet();
assertEquals(expectedEmptySlotIndex1ScoredSuggestion, assertEquals(expectedEmptySlotIndex1ScoredSuggestion,
mTimeZoneDetectorStrategy.getLatestTelephonySuggestion(SLOT_INDEX1)); mTimeZoneDetectorStrategy.getLatestTelephonySuggestion(SLOT_INDEX1));
@@ -417,10 +416,10 @@ public class TimeZoneDetectorStrategyImplTest {
testCase.createSuggestion(SLOT_INDEX1, "America/New_York"); testCase.createSuggestion(SLOT_INDEX1, "America/New_York");
// Initialization. // Initialization.
script.suggestTelephonyTimeZone(losAngelesSuggestion) script.simulateTelephonyTimeZoneSuggestion(losAngelesSuggestion)
.verifyTimeZoneSetAndReset(losAngelesSuggestion); .verifyTimeZoneSetAndReset(losAngelesSuggestion);
// Suggest it again - it should not be set because it is already set. // Suggest it again - it should not be set because it is already set.
script.suggestTelephonyTimeZone(losAngelesSuggestion) script.simulateTelephonyTimeZoneSuggestion(losAngelesSuggestion)
.verifyTimeZoneNotSet(); .verifyTimeZoneNotSet();
// Toggling time zone detection should set the device time zone only if the current setting // 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 // Simulate a user turning auto detection off, a new suggestion being made while auto
// detection is off, and the user turning it on again. // detection is off, and the user turning it on again.
script.autoTimeZoneDetectionEnabled(false) script.autoTimeZoneDetectionEnabled(false)
.suggestTelephonyTimeZone(newYorkSuggestion) .simulateTelephonyTimeZoneSuggestion(newYorkSuggestion)
.verifyTimeZoneNotSet(); .verifyTimeZoneNotSet();
// Latest suggestion should be used. // Latest suggestion should be used.
script.autoTimeZoneDetectionEnabled(true) script.autoTimeZoneDetectionEnabled(true)
@@ -447,7 +446,8 @@ public class TimeZoneDetectorStrategyImplTest {
.initializeAutoTimeZoneDetection(true); .initializeAutoTimeZoneDetection(true);
// Auto time zone detection is enabled so the manual suggestion should be ignored. // 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(); .verifyTimeZoneNotSet();
} }
@@ -460,7 +460,7 @@ public class TimeZoneDetectorStrategyImplTest {
// Auto time zone detection is disabled so the manual suggestion should be used. // Auto time zone detection is disabled so the manual suggestion should be used.
ManualTimeZoneSuggestion manualSuggestion = createManualSuggestion("Europe/Paris"); ManualTimeZoneSuggestion manualSuggestion = createManualSuggestion("Europe/Paris");
script.suggestManualTimeZone(manualSuggestion) script.simulateManualTimeZoneSuggestion(manualSuggestion, true /* expectedResult */)
.verifyTimeZoneSetAndReset(manualSuggestion); .verifyTimeZoneSetAndReset(manualSuggestion);
} }
@@ -603,14 +603,16 @@ public class TimeZoneDetectorStrategyImplTest {
/** /**
* Simulates the time zone detection strategy receiving a telephony-originated suggestion. * Simulates the time zone detection strategy receiving a telephony-originated suggestion.
*/ */
Script suggestTelephonyTimeZone(TelephonyTimeZoneSuggestion timeZoneSuggestion) { Script simulateTelephonyTimeZoneSuggestion(TelephonyTimeZoneSuggestion timeZoneSuggestion) {
mTimeZoneDetectorStrategy.suggestTelephonyTimeZone(timeZoneSuggestion); mTimeZoneDetectorStrategy.suggestTelephonyTimeZone(timeZoneSuggestion);
return this; return this;
} }
/** Simulates the time zone detection strategy receiving a user-originated suggestion. */ /** Simulates the time zone detection strategy receiving a user-originated suggestion. */
Script suggestManualTimeZone(ManualTimeZoneSuggestion manualTimeZoneSuggestion) { Script simulateManualTimeZoneSuggestion(
mTimeZoneDetectorStrategy.suggestManualTimeZone(manualTimeZoneSuggestion); ManualTimeZoneSuggestion manualTimeZoneSuggestion, boolean expectedResult) {
assertEquals(expectedResult,
mTimeZoneDetectorStrategy.suggestManualTimeZone(manualTimeZoneSuggestion));
return this; return this;
} }