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.google.common.truth.Truth.assertThat; 19 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.verifyNoMoreInteractions; 22 23 import android.app.Activity; 24 import android.app.settings.SettingsEnums; 25 import android.content.ComponentName; 26 import android.content.Context; 27 import android.content.Intent; 28 29 import androidx.preference.Preference; 30 31 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 import org.robolectric.Robolectric; 39 import org.robolectric.RobolectricTestRunner; 40 import org.robolectric.RuntimeEnvironment; 41 import org.robolectric.util.ReflectionHelpers; 42 43 import java.util.ArrayList; 44 import java.util.List; 45 46 @RunWith(RobolectricTestRunner.class) 47 public class MetricsFeatureProviderTest { 48 @Mock 49 private LogWriter mLogWriter; 50 51 private Context mContext; 52 private MetricsFeatureProvider mProvider; 53 54 @Before setUp()55 public void setUp() { 56 MockitoAnnotations.initMocks(this); 57 mContext = RuntimeEnvironment.application; 58 mProvider = new MetricsFeatureProvider(); 59 List<LogWriter> writers = new ArrayList<>(); 60 writers.add(mLogWriter); 61 ReflectionHelpers.setField(mProvider, "mLoggerWriters", writers); 62 } 63 64 @Test logClickedPreference_preferenceEmpty_shouldNotLog()65 public void logClickedPreference_preferenceEmpty_shouldNotLog() { 66 final boolean loggable = mProvider.logClickedPreference(null /* preference */, 67 MetricsEvent.SETTINGS_GESTURES); 68 69 assertThat(loggable).isFalse(); 70 verifyNoMoreInteractions(mLogWriter); 71 } 72 73 @Test logClickedPreference_preferenceHasKey_shouldLog()74 public void logClickedPreference_preferenceHasKey_shouldLog() { 75 final String key = "abc"; 76 final Preference preference = new Preference(mContext); 77 preference.setKey(key); 78 79 final boolean loggable = mProvider.logClickedPreference(preference, 80 MetricsEvent.SETTINGS_GESTURES); 81 82 assertThat(loggable).isTrue(); 83 verify(mLogWriter).action( 84 MetricsEvent.SETTINGS_GESTURES, 85 MetricsEvent.ACTION_SETTINGS_TILE_CLICK, 86 SettingsEnums.PAGE_UNKNOWN, 87 key, 88 0); 89 } 90 91 @Test logClickedPreference_preferenceHasIntent_shouldLog()92 public void logClickedPreference_preferenceHasIntent_shouldLog() { 93 final Preference preference = new Preference(mContext); 94 final Intent intent = new Intent(Intent.ACTION_ASSIST); 95 preference.setIntent(intent); 96 97 final boolean loggable = mProvider.logClickedPreference(preference, 98 MetricsEvent.SETTINGS_GESTURES); 99 100 assertThat(loggable).isTrue(); 101 verify(mLogWriter).action( 102 MetricsEvent.SETTINGS_GESTURES, 103 MetricsEvent.ACTION_SETTINGS_TILE_CLICK, 104 SettingsEnums.PAGE_UNKNOWN, 105 Intent.ACTION_ASSIST, 106 0); 107 } 108 109 @Test logClickedPreference_preferenceHasFragment_shouldLog()110 public void logClickedPreference_preferenceHasFragment_shouldLog() { 111 final Preference preference = new Preference(mContext); 112 final String fragment = "com.android.settings.tts.TextToSpeechSettings"; 113 preference.setFragment(fragment); 114 115 final boolean loggable = mProvider.logClickedPreference(preference, 116 MetricsEvent.SETTINGS_GESTURES); 117 118 assertThat(loggable).isTrue(); 119 verify(mLogWriter).action( 120 MetricsEvent.SETTINGS_GESTURES, 121 MetricsEvent.ACTION_SETTINGS_TILE_CLICK, 122 SettingsEnums.PAGE_UNKNOWN, 123 fragment, 124 0); 125 } 126 127 @Test logStartedIntent_intentEmpty_shouldNotLog()128 public void logStartedIntent_intentEmpty_shouldNotLog() { 129 final boolean loggable = mProvider.logStartedIntent(null /* intent */, 130 MetricsEvent.SETTINGS_GESTURES); 131 132 assertThat(loggable).isFalse(); 133 verifyNoMoreInteractions(mLogWriter); 134 } 135 136 @Test logStartedIntent_intentHasNoComponent_shouldLog()137 public void logStartedIntent_intentHasNoComponent_shouldLog() { 138 final Intent intent = new Intent(Intent.ACTION_ASSIST); 139 140 final boolean loggable = mProvider.logStartedIntent(intent, MetricsEvent.SETTINGS_GESTURES); 141 142 assertThat(loggable).isTrue(); 143 verify(mLogWriter).action( 144 MetricsEvent.SETTINGS_GESTURES, 145 MetricsEvent.ACTION_SETTINGS_TILE_CLICK, 146 SettingsEnums.PAGE_UNKNOWN, 147 Intent.ACTION_ASSIST, 148 0); 149 } 150 151 @Test logStartedIntent_intentIsExternal_shouldLog()152 public void logStartedIntent_intentIsExternal_shouldLog() { 153 final Intent intent = new Intent().setComponent(new ComponentName("pkg", "cls")); 154 155 final boolean loggable = mProvider.logStartedIntent(intent, MetricsEvent.SETTINGS_GESTURES); 156 157 assertThat(loggable).isTrue(); 158 verify(mLogWriter).action( 159 MetricsEvent.SETTINGS_GESTURES, 160 MetricsEvent.ACTION_SETTINGS_TILE_CLICK, 161 SettingsEnums.PAGE_UNKNOWN, 162 "pkg/cls", 163 0); 164 } 165 166 @Test logStartedIntentWithProfile_isPersonalProfile_shouldTagPersonal()167 public void logStartedIntentWithProfile_isPersonalProfile_shouldTagPersonal() { 168 final Intent intent = new Intent().setComponent(new ComponentName("pkg", "cls")); 169 170 final boolean loggable = mProvider.logStartedIntentWithProfile(intent, 171 MetricsEvent.SETTINGS_GESTURES, false); 172 173 assertThat(loggable).isTrue(); 174 verify(mLogWriter).action( 175 MetricsEvent.SETTINGS_GESTURES, 176 MetricsEvent.ACTION_SETTINGS_TILE_CLICK, 177 SettingsEnums.PAGE_UNKNOWN, 178 "pkg/cls/personal", 179 0); 180 } 181 182 @Test logStartedIntentWithProfile_isWorkProfile_shouldTagWork()183 public void logStartedIntentWithProfile_isWorkProfile_shouldTagWork() { 184 final Intent intent = new Intent().setComponent(new ComponentName("pkg", "cls")); 185 186 final boolean loggable = mProvider.logStartedIntentWithProfile(intent, 187 MetricsEvent.SETTINGS_GESTURES, true); 188 189 assertThat(loggable).isTrue(); 190 verify(mLogWriter).action( 191 MetricsEvent.SETTINGS_GESTURES, 192 MetricsEvent.ACTION_SETTINGS_TILE_CLICK, 193 SettingsEnums.PAGE_UNKNOWN, 194 "pkg/cls/work", 195 0); 196 } 197 198 @Test getAttribution_noActivity_shouldReturnUnknown()199 public void getAttribution_noActivity_shouldReturnUnknown() { 200 assertThat(mProvider.getAttribution(null /* activity */)) 201 .isEqualTo(SettingsEnums.PAGE_UNKNOWN); 202 } 203 204 @Test getAttribution_notSet_shouldReturnUnknown()205 public void getAttribution_notSet_shouldReturnUnknown() { 206 final Activity activity = Robolectric.setupActivity(Activity.class); 207 208 assertThat(mProvider.getAttribution(activity)) 209 .isEqualTo(SettingsEnums.PAGE_UNKNOWN); 210 } 211 212 @Test getAttribution_set_shouldReturnAttribution()213 public void getAttribution_set_shouldReturnAttribution() { 214 final Intent intent = new Intent() 215 .putExtra(MetricsFeatureProvider.EXTRA_SOURCE_METRICS_CATEGORY, 100); 216 217 final Activity activity = Robolectric.buildActivity(Activity.class, intent).create().get(); 218 219 assertThat(mProvider.getAttribution(activity)).isEqualTo(100); 220 } 221 222 @Test logSettingsTileClick_hasKey_shouldLog()223 public void logSettingsTileClick_hasKey_shouldLog() { 224 final String key = "abc"; 225 final boolean loggable = mProvider.logSettingsTileClick(key, 226 MetricsEvent.SETTINGS_GESTURES); 227 228 assertThat(loggable).isTrue(); 229 verify(mLogWriter).action( 230 MetricsEvent.SETTINGS_GESTURES, 231 MetricsEvent.ACTION_SETTINGS_TILE_CLICK, 232 SettingsEnums.PAGE_UNKNOWN, 233 key, 234 0); 235 } 236 237 @Test logSettingsTileClick_keyEmpty_shouldNotLog()238 public void logSettingsTileClick_keyEmpty_shouldNotLog() { 239 final String key = ""; 240 boolean loggable = mProvider.logSettingsTileClick(key, 241 MetricsEvent.SETTINGS_GESTURES); 242 243 assertThat(loggable).isFalse(); 244 verifyNoMoreInteractions(mLogWriter); 245 } 246 } 247