Log different type of values into different constants
Bug: 38258793 Test: make RunSettingsRoboTests Change-Id: I3ed170613709be04ffb2c561e132094d64619dd4
This commit is contained in:
@@ -23,16 +23,26 @@ import com.android.settings.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
|
||||
import com.google.common.truth.Platform;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.ArgumentMatcher;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import static com.android.internal.logging.nano.MetricsProto.MetricsEvent
|
||||
.FIELD_SETTINGS_PREFERENCE_CHANGE_FLOAT_VALUE;
|
||||
import static com.android.internal.logging.nano.MetricsProto.MetricsEvent
|
||||
.FIELD_SETTINGS_PREFERENCE_CHANGE_LONG_VALUE;
|
||||
import static com.android.internal.logging.nano.MetricsProto.MetricsEvent
|
||||
.FIELD_SETTINGS_PREFERENCE_CHANGE_NAME;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.argThat;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@@ -46,6 +56,7 @@ public class SharedPreferenceLoggerTest {
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private Context mContext;
|
||||
|
||||
private PairMatcher mNamePairMatcher;
|
||||
private FakeFeatureFactory mFactory;
|
||||
private MetricsFeatureProvider mMetricsFeature;
|
||||
private SharedPreferencesLogger mSharedPrefLogger;
|
||||
@@ -58,6 +69,7 @@ public class SharedPreferenceLoggerTest {
|
||||
mMetricsFeature = mFactory.metricsFeatureProvider;
|
||||
|
||||
mSharedPrefLogger = new SharedPreferencesLogger(mContext, TEST_TAG);
|
||||
mNamePairMatcher = new PairMatcher(FIELD_SETTINGS_PREFERENCE_CHANGE_NAME, String.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -71,8 +83,11 @@ public class SharedPreferenceLoggerTest {
|
||||
editor.putInt(TEST_KEY, 2);
|
||||
editor.putInt(TEST_KEY, 2);
|
||||
|
||||
final PairMatcher longMatcher =
|
||||
new PairMatcher(FIELD_SETTINGS_PREFERENCE_CHANGE_LONG_VALUE, Long.class);
|
||||
|
||||
verify(mMetricsFeature, times(6)).action(any(Context.class), anyInt(),
|
||||
any(Pair.class), any(Pair.class));
|
||||
argThat(mNamePairMatcher), argThat(longMatcher));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -84,8 +99,16 @@ public class SharedPreferenceLoggerTest {
|
||||
editor.putBoolean(TEST_KEY, false);
|
||||
editor.putBoolean(TEST_KEY, false);
|
||||
|
||||
verify(mMetricsFeature, times(4)).action(any(Context.class), anyInt(),
|
||||
any(Pair.class), any(Pair.class));
|
||||
|
||||
final PairMatcher trueMatcher =
|
||||
new PairMatcher(FIELD_SETTINGS_PREFERENCE_CHANGE_LONG_VALUE, true);
|
||||
final PairMatcher falseMatcher =
|
||||
new PairMatcher(FIELD_SETTINGS_PREFERENCE_CHANGE_LONG_VALUE, false);
|
||||
|
||||
verify(mMetricsFeature).action(any(Context.class), anyInt(),
|
||||
argThat(mNamePairMatcher), argThat(trueMatcher));
|
||||
verify(mMetricsFeature, times(3)).action(any(Context.class), anyInt(),
|
||||
argThat(mNamePairMatcher), argThat(falseMatcher));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -97,8 +120,11 @@ public class SharedPreferenceLoggerTest {
|
||||
editor.putLong(TEST_KEY, 1);
|
||||
editor.putLong(TEST_KEY, 2);
|
||||
|
||||
final PairMatcher longMatcher =
|
||||
new PairMatcher(FIELD_SETTINGS_PREFERENCE_CHANGE_LONG_VALUE, Long.class);
|
||||
|
||||
verify(mMetricsFeature, times(4)).action(any(Context.class), anyInt(),
|
||||
any(Pair.class), any(Pair.class));
|
||||
argThat(mNamePairMatcher), argThat(longMatcher));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -110,8 +136,40 @@ public class SharedPreferenceLoggerTest {
|
||||
editor.putFloat(TEST_KEY, 1);
|
||||
editor.putFloat(TEST_KEY, 2);
|
||||
|
||||
final PairMatcher floatMatcher =
|
||||
new PairMatcher(FIELD_SETTINGS_PREFERENCE_CHANGE_FLOAT_VALUE, Float.class);
|
||||
|
||||
verify(mMetricsFeature, times(4)).action(any(Context.class), anyInt(),
|
||||
any(Pair.class), any(Pair.class));
|
||||
argThat(mNamePairMatcher), argThat(floatMatcher));
|
||||
}
|
||||
|
||||
private static class PairMatcher extends ArgumentMatcher<Pair<Integer, Object>> {
|
||||
|
||||
private final int mExpectedTag;
|
||||
private final Class mExpectedClass;
|
||||
private final Long mExpectedBoolean;
|
||||
|
||||
|
||||
public PairMatcher(int tag, Class clazz) {
|
||||
mExpectedTag = tag;
|
||||
mExpectedClass = clazz;
|
||||
mExpectedBoolean = null;
|
||||
}
|
||||
|
||||
public PairMatcher(int tag, boolean bool) {
|
||||
mExpectedTag = tag;
|
||||
mExpectedClass = Long.class;
|
||||
mExpectedBoolean = bool ? 1L : 0L;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Object arg) {
|
||||
final Pair<Integer, Object> pair = (Pair) arg;
|
||||
boolean booleanMatch = mExpectedBoolean == null
|
||||
|| mExpectedBoolean == pair.second;
|
||||
return pair.first == mExpectedTag
|
||||
&& Platform.isInstanceOfType(pair.second, mExpectedClass)
|
||||
&& booleanMatch;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user