Merge "Small fixes to basic classes and refactorings"

This commit is contained in:
Neil Fuller
2020-09-04 15:57:03 +00:00
committed by Android (Google) Code Review
6 changed files with 114 additions and 63 deletions

View File

@@ -162,9 +162,9 @@ public final class TimeZoneConfiguration implements Parcelable {
@Override
public String toString() {
return "TimeZoneDetectorConfiguration{"
return "TimeZoneConfiguration{"
+ "mUserId=" + mUserId
+ "mBundle=" + mBundle
+ ", mBundle=" + mBundle
+ '}';
}

View File

@@ -35,7 +35,8 @@ import java.util.Objects;
*/
public final class LocationTimeZoneEvent implements Parcelable {
@IntDef({ EVENT_TYPE_UNKNOWN, EVENT_TYPE_SUCCESS, EVENT_TYPE_SUCCESS })
@IntDef({ EVENT_TYPE_UNKNOWN, EVENT_TYPE_PERMANENT_FAILURE, EVENT_TYPE_SUCCESS,
EVENT_TYPE_UNCERTAIN })
@interface EventType {}
/** Uninitialized value for {@link #mEventType} - must not be used for real events. */
@@ -43,7 +44,7 @@ public final class LocationTimeZoneEvent implements Parcelable {
/**
* Indicates there was a permanent failure. This is not generally expected, and probably means a
* required backend service is no longer supported / available.
* required backend service has been turned down, or the client is unreasonably old.
*/
public static final int EVENT_TYPE_PERMANENT_FAILURE = 1;
@@ -54,8 +55,9 @@ public final class LocationTimeZoneEvent implements Parcelable {
public static final int EVENT_TYPE_SUCCESS = 2;
/**
* Indicates the time zone is not known because there was a (temporary) error, e.g. when
* detecting location, or when resolving the location to a time zone.
* Indicates the time zone is not known because of an expected runtime state or error, e.g. when
* the provider is unable to detect location, or there was a problem when resolving the location
* to a time zone.
*/
public static final int EVENT_TYPE_UNCERTAIN = 3;

View File

@@ -25,6 +25,7 @@ import android.annotation.NonNull;
import android.annotation.UserIdInt;
import android.app.timezonedetector.TimeZoneCapabilities;
import android.app.timezonedetector.TimeZoneConfiguration;
import android.os.UserHandle;
import java.util.Objects;
@@ -56,6 +57,12 @@ public final class ConfigurationInternal {
return mUserId;
}
/** Returns the handle of the user this configuration is associated with. */
@NonNull
public UserHandle getUserHandle() {
return UserHandle.of(mUserId);
}
/** Returns true if the user allowed to modify time zone configuration. */
public boolean isUserConfigAllowed() {
return mUserConfigAllowed;
@@ -198,13 +205,13 @@ public final class ConfigurationInternal {
@Override
public String toString() {
return "TimeZoneDetectorConfiguration{"
return "ConfigurationInternal{"
+ "mUserId=" + mUserId
+ "mUserConfigAllowed=" + mUserConfigAllowed
+ "mAutoDetectionSupported=" + mAutoDetectionSupported
+ "mAutoDetectionEnabled=" + mAutoDetectionEnabled
+ "mLocationEnabled=" + mLocationEnabled
+ "mGeoDetectionEnabled=" + mGeoDetectionEnabled
+ ", mUserConfigAllowed=" + mUserConfigAllowed
+ ", mAutoDetectionSupported=" + mAutoDetectionSupported
+ ", mAutoDetectionEnabled=" + mAutoDetectionEnabled
+ ", mLocationEnabled=" + mLocationEnabled
+ ", mGeoDetectionEnabled=" + mGeoDetectionEnabled
+ '}';
}

View File

@@ -23,7 +23,7 @@ import java.io.PrintWriter;
import java.util.function.Consumer;
import java.util.function.Supplier;
/** Implemented the shell command interface for {@link TimeZoneDetectorService}. */
/** Implements the shell command interface for {@link TimeZoneDetectorService}. */
class TimeZoneDetectorShellCommand extends ShellCommand {
private final TimeZoneDetectorService mInterface;

View File

@@ -0,0 +1,92 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.timezonedetector;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
/**
* A test support class used for tracking a piece of state in test objects like fakes and mocks.
* State can optionally be initialized using {@link #init}, which sets the value to an initial
* value, but is not treated as a change. Calls to {@link #set} are tracked and can be checked for
* during tests. The change-tracking can be cleared by calling {@link #commitLatest}, which puts the
* object into an unchanged state and sets the initial value to the latest value passed to
* {@link #set}.
*/
public class TestState<T> {
private T mInitialValue;
private final ArrayList<T> mValues = new ArrayList<>(5);
/** Sets the initial value for the state. */
public void init(T value) {
mValues.clear();
mInitialValue = value;
}
/** Sets the latest value for the state. */
public void set(T value) {
mValues.add(value);
}
/** Returns {@code true} if {@link #set} has been called. */
public boolean hasBeenSet() {
return mValues.size() > 0;
}
/** Fails if {@link #set} has been called. */
public void assertHasNotBeenSet() {
assertFalse(hasBeenSet());
}
/** Fails if {@link #set} has not been called. */
public void assertHasBeenSet() {
assertTrue(hasBeenSet());
}
/**
* Clears tracked changes and re-initializes using the latest set value as the initial value.
*/
public void commitLatest() {
if (hasBeenSet()) {
mInitialValue = mValues.get(mValues.size() - 1);
mValues.clear();
}
}
/** Asserts the latest value passed to {@link #set} equals {@code expected}. */
public void assertLatestEquals(T expected) {
assertEquals(expected, getLatest());
}
/** Asserts the number of times {@link #set} has been called. */
public void assertChangeCount(int expectedCount) {
assertEquals(expectedCount, mValues.size());
}
/**
* Returns the latest value passed to {@link #set}. If {@link #set} hasn't been called then the
* initial value is returned.
*/
public T getLatest() {
if (hasBeenSet()) {
return mValues.get(mValues.size() - 1);
}
return mInitialValue;
}
}

View File

@@ -54,7 +54,6 @@ import org.junit.Test;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -994,55 +993,6 @@ public class TimeZoneDetectorStrategyImplTest {
}
}
/** Some piece of state that tests want to track. */
private static class TestState<T> {
private T mInitialValue;
private LinkedList<T> mValues = new LinkedList<>();
void init(T value) {
mValues.clear();
mInitialValue = value;
}
void set(T value) {
mValues.addFirst(value);
}
boolean hasBeenSet() {
return mValues.size() > 0;
}
void assertHasNotBeenSet() {
assertFalse(hasBeenSet());
}
void assertHasBeenSet() {
assertTrue(hasBeenSet());
}
void commitLatest() {
if (hasBeenSet()) {
mInitialValue = mValues.getLast();
mValues.clear();
}
}
void assertLatestEquals(T expected) {
assertEquals(expected, getLatest());
}
void assertChangeCount(int expectedCount) {
assertEquals(expectedCount, mValues.size());
}
public T getLatest() {
if (hasBeenSet()) {
return mValues.getFirst();
}
return mInitialValue;
}
}
/**
* A "fluent" class allows reuse of code in tests: initialization, simulation and verification
* logic.