Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | package com.android.settingslib.core.instrumentation; |
| 17 | |
| 18 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_SETTINGS_PREFERENCE_CHANGE; |
| 19 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_FLOAT_VALUE; |
| 20 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_INT_VALUE; |
| 21 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_NAME; |
| 22 | import static org.mockito.Matchers.any; |
| 23 | import static org.mockito.Matchers.anyInt; |
| 24 | import static org.mockito.Matchers.argThat; |
| 25 | import static org.mockito.Matchers.eq; |
| 26 | import static org.mockito.Mockito.times; |
| 27 | import static org.mockito.Mockito.verify; |
| 28 | |
| 29 | import android.content.Context; |
| 30 | import android.content.SharedPreferences; |
| 31 | import android.util.Pair; |
| 32 | |
| 33 | import com.android.settingslib.TestConfig; |
| 34 | import com.android.settingslib.SettingsLibRobolectricTestRunner; |
| 35 | |
| 36 | import com.google.common.truth.Platform; |
| 37 | |
| 38 | import org.junit.Before; |
| 39 | import org.junit.Test; |
| 40 | import org.junit.runner.RunWith; |
| 41 | import org.mockito.Answers; |
| 42 | import org.mockito.ArgumentMatcher; |
| 43 | import org.mockito.Mock; |
| 44 | import org.mockito.MockitoAnnotations; |
| 45 | import org.robolectric.annotation.Config; |
| 46 | |
| 47 | @RunWith(SettingsLibRobolectricTestRunner.class) |
| 48 | @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) |
| 49 | public class SharedPreferenceLoggerTest { |
| 50 | |
| 51 | private static final String TEST_TAG = "tag"; |
| 52 | private static final String TEST_KEY = "key"; |
| 53 | |
| 54 | @Mock(answer = Answers.RETURNS_DEEP_STUBS) |
| 55 | private Context mContext; |
| 56 | |
| 57 | private ArgumentMatcher<Pair<Integer, Object>> mNamePairMatcher; |
| 58 | @Mock |
| 59 | private MetricsFeatureProvider mMetricsFeature; |
| 60 | private SharedPreferencesLogger mSharedPrefLogger; |
| 61 | |
| 62 | @Before |
| 63 | public void init() { |
| 64 | MockitoAnnotations.initMocks(this); |
| 65 | mSharedPrefLogger = new SharedPreferencesLogger(mContext, TEST_TAG, mMetricsFeature); |
| 66 | mNamePairMatcher = pairMatches(FIELD_SETTINGS_PREFERENCE_CHANGE_NAME, String.class); |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void putInt_shouldNotLogInitialPut() { |
| 71 | final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); |
| 72 | editor.putInt(TEST_KEY, 1); |
| 73 | editor.putInt(TEST_KEY, 1); |
| 74 | editor.putInt(TEST_KEY, 1); |
| 75 | editor.putInt(TEST_KEY, 2); |
| 76 | editor.putInt(TEST_KEY, 2); |
| 77 | editor.putInt(TEST_KEY, 2); |
| 78 | editor.putInt(TEST_KEY, 2); |
| 79 | |
| 80 | verify(mMetricsFeature, times(6)).action(any(Context.class), anyInt(), |
| 81 | argThat(mNamePairMatcher), |
| 82 | argThat(pairMatches(FIELD_SETTINGS_PREFERENCE_CHANGE_INT_VALUE, Integer.class))); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void putBoolean_shouldNotLogInitialPut() { |
| 87 | final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); |
| 88 | editor.putBoolean(TEST_KEY, true); |
| 89 | editor.putBoolean(TEST_KEY, true); |
| 90 | editor.putBoolean(TEST_KEY, false); |
| 91 | editor.putBoolean(TEST_KEY, false); |
| 92 | editor.putBoolean(TEST_KEY, false); |
| 93 | |
| 94 | |
| 95 | verify(mMetricsFeature).action(any(Context.class), anyInt(), |
| 96 | argThat(mNamePairMatcher), |
| 97 | argThat(pairMatches(FIELD_SETTINGS_PREFERENCE_CHANGE_INT_VALUE, true))); |
| 98 | verify(mMetricsFeature, times(3)).action(any(Context.class), anyInt(), |
| 99 | argThat(mNamePairMatcher), |
| 100 | argThat(pairMatches(FIELD_SETTINGS_PREFERENCE_CHANGE_INT_VALUE, false))); |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void putLong_shouldNotLogInitialPut() { |
| 105 | final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); |
| 106 | editor.putLong(TEST_KEY, 1); |
| 107 | editor.putLong(TEST_KEY, 1); |
| 108 | editor.putLong(TEST_KEY, 1); |
| 109 | editor.putLong(TEST_KEY, 1); |
| 110 | editor.putLong(TEST_KEY, 2); |
| 111 | |
| 112 | verify(mMetricsFeature, times(4)).action(any(Context.class), anyInt(), |
| 113 | argThat(mNamePairMatcher), |
| 114 | argThat(pairMatches(FIELD_SETTINGS_PREFERENCE_CHANGE_INT_VALUE, Integer.class))); |
| 115 | } |
| 116 | |
| 117 | @Test |
| 118 | public void putLong_biggerThanIntMax_shouldLogIntMax() { |
| 119 | final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); |
| 120 | final long veryBigNumber = 500L + Integer.MAX_VALUE; |
| 121 | editor.putLong(TEST_KEY, 1); |
| 122 | editor.putLong(TEST_KEY, veryBigNumber); |
| 123 | |
| 124 | verify(mMetricsFeature).action(any(Context.class), anyInt(), |
| 125 | argThat(mNamePairMatcher), |
| 126 | argThat(pairMatches( |
| 127 | FIELD_SETTINGS_PREFERENCE_CHANGE_INT_VALUE, Integer.MAX_VALUE))); |
| 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void putLong_smallerThanIntMin_shouldLogIntMin() { |
| 132 | final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); |
| 133 | final long veryNegativeNumber = -500L + Integer.MIN_VALUE; |
| 134 | editor.putLong(TEST_KEY, 1); |
| 135 | editor.putLong(TEST_KEY, veryNegativeNumber); |
| 136 | |
| 137 | verify(mMetricsFeature).action(any(Context.class), anyInt(), |
| 138 | argThat(mNamePairMatcher), |
| 139 | argThat(pairMatches( |
| 140 | FIELD_SETTINGS_PREFERENCE_CHANGE_INT_VALUE, Integer.MIN_VALUE))); |
| 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void putFloat_shouldNotLogInitialPut() { |
| 145 | final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); |
| 146 | editor.putFloat(TEST_KEY, 1); |
| 147 | editor.putFloat(TEST_KEY, 1); |
| 148 | editor.putFloat(TEST_KEY, 1); |
| 149 | editor.putFloat(TEST_KEY, 1); |
| 150 | editor.putFloat(TEST_KEY, 2); |
| 151 | |
| 152 | verify(mMetricsFeature, times(4)).action(any(Context.class), anyInt(), |
| 153 | argThat(mNamePairMatcher), |
| 154 | argThat(pairMatches(FIELD_SETTINGS_PREFERENCE_CHANGE_FLOAT_VALUE, Float.class))); |
| 155 | } |
| 156 | |
| 157 | @Test |
| 158 | public void logPackage_shouldUseLogPackageApi() { |
| 159 | mSharedPrefLogger.logPackageName("key", "com.android.settings"); |
| 160 | verify(mMetricsFeature).action(any(Context.class), |
| 161 | eq(ACTION_SETTINGS_PREFERENCE_CHANGE), |
| 162 | eq("com.android.settings"), |
| 163 | any(Pair.class)); |
| 164 | } |
| 165 | |
| 166 | private ArgumentMatcher<Pair<Integer, Object>> pairMatches(int tag, Class clazz) { |
| 167 | return pair -> pair.first == tag && Platform.isInstanceOfType(pair.second, clazz); |
| 168 | } |
| 169 | |
| 170 | private ArgumentMatcher<Pair<Integer, Object>> pairMatches(int tag, boolean bool) { |
| 171 | return pair -> pair.first == tag |
| 172 | && Platform.isInstanceOfType(pair.second, Integer.class) |
| 173 | && pair.second.equals((bool ? 1 : 0)); |
| 174 | } |
| 175 | |
| 176 | private ArgumentMatcher<Pair<Integer, Object>> pairMatches(int tag, int val) { |
| 177 | return pair -> pair.first == tag |
| 178 | && Platform.isInstanceOfType(pair.second, Integer.class) |
| 179 | && pair.second.equals(val); |
| 180 | } |
| 181 | } |