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 | |
Fan Zhang | f63a61d | 2018-11-12 23:34:32 -0800 | [diff] [blame] | 18 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_SETTINGS_PREFERENCE_CHANGE; |
Fan Zhang | f7802ea | 2018-08-28 15:15:19 -0700 | [diff] [blame] | 19 | |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 20 | import static org.mockito.ArgumentMatchers.anyInt; |
Fan Zhang | f63a61d | 2018-11-12 23:34:32 -0800 | [diff] [blame] | 21 | import static org.mockito.ArgumentMatchers.eq; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 22 | import static org.mockito.Mockito.times; |
| 23 | import static org.mockito.Mockito.verify; |
| 24 | |
Fan Zhang | f63a61d | 2018-11-12 23:34:32 -0800 | [diff] [blame] | 25 | import android.app.settings.SettingsEnums; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 26 | import android.content.Context; |
| 27 | import android.content.SharedPreferences; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 28 | |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 29 | import org.junit.Before; |
| 30 | import org.junit.Test; |
| 31 | import org.junit.runner.RunWith; |
| 32 | import org.mockito.Answers; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 33 | import org.mockito.Mock; |
| 34 | import org.mockito.MockitoAnnotations; |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 35 | import org.robolectric.RobolectricTestRunner; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 36 | |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 37 | @RunWith(RobolectricTestRunner.class) |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 38 | public class SharedPreferenceLoggerTest { |
| 39 | |
| 40 | private static final String TEST_TAG = "tag"; |
| 41 | private static final String TEST_KEY = "key"; |
Fan Zhang | f63a61d | 2018-11-12 23:34:32 -0800 | [diff] [blame] | 42 | private static final String TEST_TAGGED_KEY = TEST_TAG + "/" + TEST_KEY; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 43 | |
| 44 | @Mock(answer = Answers.RETURNS_DEEP_STUBS) |
| 45 | private Context mContext; |
| 46 | |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 47 | @Mock |
| 48 | private MetricsFeatureProvider mMetricsFeature; |
| 49 | private SharedPreferencesLogger mSharedPrefLogger; |
| 50 | |
| 51 | @Before |
| 52 | public void init() { |
| 53 | MockitoAnnotations.initMocks(this); |
| 54 | mSharedPrefLogger = new SharedPreferencesLogger(mContext, TEST_TAG, mMetricsFeature); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void putInt_shouldNotLogInitialPut() { |
| 59 | final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); |
| 60 | editor.putInt(TEST_KEY, 1); |
| 61 | editor.putInt(TEST_KEY, 1); |
| 62 | editor.putInt(TEST_KEY, 1); |
| 63 | editor.putInt(TEST_KEY, 2); |
| 64 | editor.putInt(TEST_KEY, 2); |
| 65 | editor.putInt(TEST_KEY, 2); |
| 66 | editor.putInt(TEST_KEY, 2); |
| 67 | |
Fan Zhang | f63a61d | 2018-11-12 23:34:32 -0800 | [diff] [blame] | 68 | verify(mMetricsFeature, times(6)).action(eq(SettingsEnums.PAGE_UNKNOWN), |
| 69 | eq(SettingsEnums.ACTION_SETTINGS_PREFERENCE_CHANGE), |
| 70 | eq(SettingsEnums.PAGE_UNKNOWN), |
| 71 | eq(TEST_TAGGED_KEY), |
| 72 | anyInt()); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void putBoolean_shouldNotLogInitialPut() { |
| 77 | final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); |
| 78 | editor.putBoolean(TEST_KEY, true); |
| 79 | editor.putBoolean(TEST_KEY, true); |
| 80 | editor.putBoolean(TEST_KEY, false); |
| 81 | editor.putBoolean(TEST_KEY, false); |
| 82 | editor.putBoolean(TEST_KEY, false); |
| 83 | |
| 84 | |
Fan Zhang | f63a61d | 2018-11-12 23:34:32 -0800 | [diff] [blame] | 85 | verify(mMetricsFeature).action(SettingsEnums.PAGE_UNKNOWN, |
| 86 | SettingsEnums.ACTION_SETTINGS_PREFERENCE_CHANGE, |
| 87 | SettingsEnums.PAGE_UNKNOWN, |
| 88 | TEST_TAGGED_KEY, |
| 89 | 1); |
| 90 | verify(mMetricsFeature, times(3)).action(SettingsEnums.PAGE_UNKNOWN, |
| 91 | SettingsEnums.ACTION_SETTINGS_PREFERENCE_CHANGE, |
| 92 | SettingsEnums.PAGE_UNKNOWN, |
| 93 | TEST_TAGGED_KEY, |
| 94 | 0); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Test |
| 98 | public void putLong_shouldNotLogInitialPut() { |
| 99 | final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); |
| 100 | editor.putLong(TEST_KEY, 1); |
| 101 | editor.putLong(TEST_KEY, 1); |
| 102 | editor.putLong(TEST_KEY, 1); |
| 103 | editor.putLong(TEST_KEY, 1); |
| 104 | editor.putLong(TEST_KEY, 2); |
| 105 | |
Fan Zhang | f63a61d | 2018-11-12 23:34:32 -0800 | [diff] [blame] | 106 | verify(mMetricsFeature, times(4)).action(eq(SettingsEnums.PAGE_UNKNOWN), |
| 107 | eq(SettingsEnums.ACTION_SETTINGS_PREFERENCE_CHANGE), |
| 108 | eq(SettingsEnums.PAGE_UNKNOWN), |
| 109 | eq(TEST_TAGGED_KEY), |
| 110 | anyInt()); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void putLong_biggerThanIntMax_shouldLogIntMax() { |
| 115 | final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); |
| 116 | final long veryBigNumber = 500L + Integer.MAX_VALUE; |
| 117 | editor.putLong(TEST_KEY, 1); |
| 118 | editor.putLong(TEST_KEY, veryBigNumber); |
| 119 | |
Fan Zhang | f63a61d | 2018-11-12 23:34:32 -0800 | [diff] [blame] | 120 | verify(mMetricsFeature).action(SettingsEnums.PAGE_UNKNOWN, |
| 121 | SettingsEnums.ACTION_SETTINGS_PREFERENCE_CHANGE, |
| 122 | SettingsEnums.PAGE_UNKNOWN, |
| 123 | TEST_TAGGED_KEY, |
| 124 | Integer.MAX_VALUE); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void putLong_smallerThanIntMin_shouldLogIntMin() { |
| 129 | final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); |
| 130 | final long veryNegativeNumber = -500L + Integer.MIN_VALUE; |
| 131 | editor.putLong(TEST_KEY, 1); |
| 132 | editor.putLong(TEST_KEY, veryNegativeNumber); |
| 133 | |
Fan Zhang | f63a61d | 2018-11-12 23:34:32 -0800 | [diff] [blame] | 134 | verify(mMetricsFeature).action(SettingsEnums.PAGE_UNKNOWN, |
| 135 | SettingsEnums.ACTION_SETTINGS_PREFERENCE_CHANGE, |
| 136 | SettingsEnums.PAGE_UNKNOWN, |
| 137 | TEST_TAGGED_KEY, Integer.MIN_VALUE); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Test |
| 141 | public void putFloat_shouldNotLogInitialPut() { |
| 142 | final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); |
| 143 | editor.putFloat(TEST_KEY, 1); |
| 144 | editor.putFloat(TEST_KEY, 1); |
| 145 | editor.putFloat(TEST_KEY, 1); |
| 146 | editor.putFloat(TEST_KEY, 1); |
| 147 | editor.putFloat(TEST_KEY, 2); |
| 148 | |
Fan Zhang | f63a61d | 2018-11-12 23:34:32 -0800 | [diff] [blame] | 149 | verify(mMetricsFeature, times(4)).action(eq(SettingsEnums.PAGE_UNKNOWN), |
| 150 | eq(SettingsEnums.ACTION_SETTINGS_PREFERENCE_CHANGE), |
| 151 | eq(SettingsEnums.PAGE_UNKNOWN), |
| 152 | eq(TEST_TAGGED_KEY), |
| 153 | anyInt()); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Test |
| 157 | public void logPackage_shouldUseLogPackageApi() { |
| 158 | mSharedPrefLogger.logPackageName("key", "com.android.settings"); |
Fan Zhang | f63a61d | 2018-11-12 23:34:32 -0800 | [diff] [blame] | 159 | verify(mMetricsFeature).action(SettingsEnums.PAGE_UNKNOWN, |
| 160 | ACTION_SETTINGS_PREFERENCE_CHANGE, |
| 161 | SettingsEnums.PAGE_UNKNOWN, |
| 162 | "tag/key:com.android.settings", |
| 163 | 0); |
Aurimas Liutikas | 22c3585 | 2018-02-16 16:19:04 +0000 | [diff] [blame] | 164 | } |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 165 | } |