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 | 99d042d | 2018-11-12 00:57:37 -0800 | [diff] [blame] | 18 | import static com.google.common.truth.Truth.assertThat; |
| 19 | |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 20 | import static org.mockito.Mockito.verify; |
| 21 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 22 | |
Fan Zhang | 99d042d | 2018-11-12 00:57:37 -0800 | [diff] [blame] | 23 | import android.app.Activity; |
| 24 | import android.app.settings.SettingsEnums; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 25 | import android.content.ComponentName; |
| 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 28 | |
Jason Chiu | 4f6a1c0 | 2019-12-12 17:31:32 +0800 | [diff] [blame^] | 29 | import androidx.preference.Preference; |
| 30 | |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 31 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 32 | |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | import org.junit.runner.RunWith; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 36 | import org.mockito.Mock; |
| 37 | import org.mockito.MockitoAnnotations; |
Fan Zhang | 99d042d | 2018-11-12 00:57:37 -0800 | [diff] [blame] | 38 | import org.robolectric.Robolectric; |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 39 | import org.robolectric.RobolectricTestRunner; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 40 | import org.robolectric.RuntimeEnvironment; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 41 | import org.robolectric.util.ReflectionHelpers; |
| 42 | |
| 43 | import java.util.ArrayList; |
| 44 | import java.util.List; |
| 45 | |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 46 | @RunWith(RobolectricTestRunner.class) |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 47 | public class MetricsFeatureProviderTest { |
Fan Zhang | 793cbdd | 2018-10-23 13:20:57 -0700 | [diff] [blame] | 48 | @Mock |
| 49 | private LogWriter mLogWriter; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 50 | |
| 51 | private Context mContext; |
| 52 | private MetricsFeatureProvider mProvider; |
| 53 | |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 54 | @Before |
| 55 | public void setUp() { |
| 56 | MockitoAnnotations.initMocks(this); |
| 57 | mContext = RuntimeEnvironment.application; |
| 58 | mProvider = new MetricsFeatureProvider(); |
| 59 | List<LogWriter> writers = new ArrayList<>(); |
Fan Zhang | 793cbdd | 2018-10-23 13:20:57 -0700 | [diff] [blame] | 60 | writers.add(mLogWriter); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 61 | ReflectionHelpers.setField(mProvider, "mLoggerWriters", writers); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Test |
Jason Chiu | 4f6a1c0 | 2019-12-12 17:31:32 +0800 | [diff] [blame^] | 65 | public void logClickedPreference_preferenceEmpty_shouldNotLog() { |
| 66 | final boolean loggable = mProvider.logClickedPreference(null /* preference */, |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 67 | MetricsEvent.SETTINGS_GESTURES); |
| 68 | |
Jason Chiu | 4f6a1c0 | 2019-12-12 17:31:32 +0800 | [diff] [blame^] | 69 | assertThat(loggable).isFalse(); |
Fan Zhang | 793cbdd | 2018-10-23 13:20:57 -0700 | [diff] [blame] | 70 | verifyNoMoreInteractions(mLogWriter); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Test |
Jason Chiu | 4f6a1c0 | 2019-12-12 17:31:32 +0800 | [diff] [blame^] | 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 |
| 92 | public void logClickedPreference_preferenceHasIntent_shouldLog() { |
| 93 | final Preference preference = new Preference(mContext); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 94 | final Intent intent = new Intent(Intent.ACTION_ASSIST); |
Jason Chiu | 4f6a1c0 | 2019-12-12 17:31:32 +0800 | [diff] [blame^] | 95 | preference.setIntent(intent); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 96 | |
Jason Chiu | 4f6a1c0 | 2019-12-12 17:31:32 +0800 | [diff] [blame^] | 97 | final boolean loggable = mProvider.logClickedPreference(preference, |
| 98 | MetricsEvent.SETTINGS_GESTURES); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 99 | |
Jason Chiu | 4f6a1c0 | 2019-12-12 17:31:32 +0800 | [diff] [blame^] | 100 | assertThat(loggable).isTrue(); |
Fan Zhang | 793cbdd | 2018-10-23 13:20:57 -0700 | [diff] [blame] | 101 | verify(mLogWriter).action( |
Fan Zhang | f63a61d | 2018-11-12 23:34:32 -0800 | [diff] [blame] | 102 | MetricsEvent.SETTINGS_GESTURES, |
| 103 | MetricsEvent.ACTION_SETTINGS_TILE_CLICK, |
| 104 | SettingsEnums.PAGE_UNKNOWN, |
| 105 | Intent.ACTION_ASSIST, |
| 106 | 0); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Test |
Jason Chiu | 4f6a1c0 | 2019-12-12 17:31:32 +0800 | [diff] [blame^] | 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 |
| 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 |
| 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 |
| 152 | public void logStartedIntent_intentIsExternal_shouldLog() { |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 153 | final Intent intent = new Intent().setComponent(new ComponentName("pkg", "cls")); |
| 154 | |
Jason Chiu | 4f6a1c0 | 2019-12-12 17:31:32 +0800 | [diff] [blame^] | 155 | final boolean loggable = mProvider.logStartedIntent(intent, MetricsEvent.SETTINGS_GESTURES); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 156 | |
Jason Chiu | 4f6a1c0 | 2019-12-12 17:31:32 +0800 | [diff] [blame^] | 157 | assertThat(loggable).isTrue(); |
Fan Zhang | 793cbdd | 2018-10-23 13:20:57 -0700 | [diff] [blame] | 158 | verify(mLogWriter).action( |
Fan Zhang | f63a61d | 2018-11-12 23:34:32 -0800 | [diff] [blame] | 159 | MetricsEvent.SETTINGS_GESTURES, |
| 160 | MetricsEvent.ACTION_SETTINGS_TILE_CLICK, |
| 161 | SettingsEnums.PAGE_UNKNOWN, |
| 162 | "pkg/cls", |
| 163 | 0); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 164 | } |
Fan Zhang | 99d042d | 2018-11-12 00:57:37 -0800 | [diff] [blame] | 165 | |
| 166 | @Test |
| 167 | public void getAttribution_noActivity_shouldReturnUnknown() { |
| 168 | assertThat(mProvider.getAttribution(null /* activity */)) |
| 169 | .isEqualTo(SettingsEnums.PAGE_UNKNOWN); |
| 170 | } |
| 171 | |
| 172 | @Test |
| 173 | public void getAttribution_notSet_shouldReturnUnknown() { |
| 174 | final Activity activity = Robolectric.setupActivity(Activity.class); |
| 175 | |
| 176 | assertThat(mProvider.getAttribution(activity)) |
| 177 | .isEqualTo(SettingsEnums.PAGE_UNKNOWN); |
| 178 | } |
| 179 | |
| 180 | @Test |
| 181 | public void getAttribution_set_shouldReturnAttribution() { |
| 182 | final Intent intent = new Intent() |
| 183 | .putExtra(MetricsFeatureProvider.EXTRA_SOURCE_METRICS_CATEGORY, 100); |
| 184 | |
| 185 | final Activity activity = Robolectric.buildActivity(Activity.class, intent).create().get(); |
| 186 | |
| 187 | assertThat(mProvider.getAttribution(activity)).isEqualTo(100); |
| 188 | } |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 189 | } |