blob: 8f51dece64e551017db6e5678f79c12cf3811a79 [file] [log] [blame]
Leif Wilden5eb77482018-01-23 23:54:05 +00001/*
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 */
16package com.android.settingslib.core.instrumentation;
17
Fan Zhangf63a61d2018-11-12 23:34:32 -080018import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_SETTINGS_PREFERENCE_CHANGE;
Fan Zhangf7802ea2018-08-28 15:15:19 -070019
James Lemieuxec3ad9e2018-11-28 17:49:14 -080020import static org.mockito.ArgumentMatchers.anyInt;
Fan Zhangf63a61d2018-11-12 23:34:32 -080021import static org.mockito.ArgumentMatchers.eq;
Leif Wilden5eb77482018-01-23 23:54:05 +000022import static org.mockito.Mockito.times;
23import static org.mockito.Mockito.verify;
24
Fan Zhangf63a61d2018-11-12 23:34:32 -080025import android.app.settings.SettingsEnums;
Leif Wilden5eb77482018-01-23 23:54:05 +000026import android.content.Context;
27import android.content.SharedPreferences;
Leif Wilden5eb77482018-01-23 23:54:05 +000028
Leif Wilden5eb77482018-01-23 23:54:05 +000029import org.junit.Before;
30import org.junit.Test;
31import org.junit.runner.RunWith;
32import org.mockito.Answers;
Leif Wilden5eb77482018-01-23 23:54:05 +000033import org.mockito.Mock;
34import org.mockito.MockitoAnnotations;
James Lemieuxec3ad9e2018-11-28 17:49:14 -080035import org.robolectric.RobolectricTestRunner;
Leif Wilden5eb77482018-01-23 23:54:05 +000036
James Lemieuxec3ad9e2018-11-28 17:49:14 -080037@RunWith(RobolectricTestRunner.class)
Leif Wilden5eb77482018-01-23 23:54:05 +000038public class SharedPreferenceLoggerTest {
39
40 private static final String TEST_TAG = "tag";
41 private static final String TEST_KEY = "key";
Fan Zhangf63a61d2018-11-12 23:34:32 -080042 private static final String TEST_TAGGED_KEY = TEST_TAG + "/" + TEST_KEY;
Leif Wilden5eb77482018-01-23 23:54:05 +000043
44 @Mock(answer = Answers.RETURNS_DEEP_STUBS)
45 private Context mContext;
46
Leif Wilden5eb77482018-01-23 23:54:05 +000047 @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 Wilden5eb77482018-01-23 23:54:05 +000055 }
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 Zhangf63a61d2018-11-12 23:34:32 -080068 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 Wilden5eb77482018-01-23 23:54:05 +000073 }
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 Zhangf63a61d2018-11-12 23:34:32 -080085 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 Wilden5eb77482018-01-23 23:54:05 +000095 }
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 Zhangf63a61d2018-11-12 23:34:32 -0800106 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 Wilden5eb77482018-01-23 23:54:05 +0000111 }
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 Zhangf63a61d2018-11-12 23:34:32 -0800120 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 Wilden5eb77482018-01-23 23:54:05 +0000125 }
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 Zhangf63a61d2018-11-12 23:34:32 -0800134 verify(mMetricsFeature).action(SettingsEnums.PAGE_UNKNOWN,
135 SettingsEnums.ACTION_SETTINGS_PREFERENCE_CHANGE,
136 SettingsEnums.PAGE_UNKNOWN,
137 TEST_TAGGED_KEY, Integer.MIN_VALUE);
Leif Wilden5eb77482018-01-23 23:54:05 +0000138 }
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 Zhangf63a61d2018-11-12 23:34:32 -0800149 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 Wilden5eb77482018-01-23 23:54:05 +0000154 }
155
156 @Test
157 public void logPackage_shouldUseLogPackageApi() {
158 mSharedPrefLogger.logPackageName("key", "com.android.settings");
Fan Zhangf63a61d2018-11-12 23:34:32 -0800159 verify(mMetricsFeature).action(SettingsEnums.PAGE_UNKNOWN,
160 ACTION_SETTINGS_PREFERENCE_CHANGE,
161 SettingsEnums.PAGE_UNKNOWN,
162 "tag/key:com.android.settings",
163 0);
Aurimas Liutikas22c35852018-02-16 16:19:04 +0000164 }
Leif Wilden5eb77482018-01-23 23:54:05 +0000165}