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 | |
| 29 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 30 | |
| 31 | import org.junit.Before; |
| 32 | import org.junit.Test; |
| 33 | import org.junit.runner.RunWith; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 34 | import org.mockito.Mock; |
| 35 | import org.mockito.MockitoAnnotations; |
Fan Zhang | 99d042d | 2018-11-12 00:57:37 -0800 | [diff] [blame] | 36 | import org.robolectric.Robolectric; |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame^] | 37 | import org.robolectric.RobolectricTestRunner; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 38 | import org.robolectric.RuntimeEnvironment; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 39 | import org.robolectric.util.ReflectionHelpers; |
| 40 | |
| 41 | import java.util.ArrayList; |
| 42 | import java.util.List; |
| 43 | |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame^] | 44 | @RunWith(RobolectricTestRunner.class) |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 45 | public class MetricsFeatureProviderTest { |
Fan Zhang | 793cbdd | 2018-10-23 13:20:57 -0700 | [diff] [blame] | 46 | @Mock |
| 47 | private LogWriter mLogWriter; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 48 | |
| 49 | private Context mContext; |
| 50 | private MetricsFeatureProvider mProvider; |
| 51 | |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 52 | @Before |
| 53 | public void setUp() { |
| 54 | MockitoAnnotations.initMocks(this); |
| 55 | mContext = RuntimeEnvironment.application; |
| 56 | mProvider = new MetricsFeatureProvider(); |
| 57 | List<LogWriter> writers = new ArrayList<>(); |
Fan Zhang | 793cbdd | 2018-10-23 13:20:57 -0700 | [diff] [blame] | 58 | writers.add(mLogWriter); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 59 | ReflectionHelpers.setField(mProvider, "mLoggerWriters", writers); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void logDashboardStartIntent_intentEmpty_shouldNotLog() { |
| 64 | mProvider.logDashboardStartIntent(mContext, null /* intent */, |
| 65 | MetricsEvent.SETTINGS_GESTURES); |
| 66 | |
Fan Zhang | 793cbdd | 2018-10-23 13:20:57 -0700 | [diff] [blame] | 67 | verifyNoMoreInteractions(mLogWriter); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void logDashboardStartIntent_intentHasNoComponent_shouldLog() { |
| 72 | final Intent intent = new Intent(Intent.ACTION_ASSIST); |
| 73 | |
| 74 | mProvider.logDashboardStartIntent(mContext, intent, MetricsEvent.SETTINGS_GESTURES); |
| 75 | |
Fan Zhang | 793cbdd | 2018-10-23 13:20:57 -0700 | [diff] [blame] | 76 | verify(mLogWriter).action( |
Fan Zhang | f63a61d | 2018-11-12 23:34:32 -0800 | [diff] [blame] | 77 | MetricsEvent.SETTINGS_GESTURES, |
| 78 | MetricsEvent.ACTION_SETTINGS_TILE_CLICK, |
| 79 | SettingsEnums.PAGE_UNKNOWN, |
| 80 | Intent.ACTION_ASSIST, |
| 81 | 0); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void logDashboardStartIntent_intentIsExternal_shouldLog() { |
| 86 | final Intent intent = new Intent().setComponent(new ComponentName("pkg", "cls")); |
| 87 | |
| 88 | mProvider.logDashboardStartIntent(mContext, intent, MetricsEvent.SETTINGS_GESTURES); |
| 89 | |
Fan Zhang | 793cbdd | 2018-10-23 13:20:57 -0700 | [diff] [blame] | 90 | verify(mLogWriter).action( |
Fan Zhang | f63a61d | 2018-11-12 23:34:32 -0800 | [diff] [blame] | 91 | MetricsEvent.SETTINGS_GESTURES, |
| 92 | MetricsEvent.ACTION_SETTINGS_TILE_CLICK, |
| 93 | SettingsEnums.PAGE_UNKNOWN, |
| 94 | "pkg/cls", |
| 95 | 0); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 96 | } |
Fan Zhang | 99d042d | 2018-11-12 00:57:37 -0800 | [diff] [blame] | 97 | |
| 98 | @Test |
| 99 | public void getAttribution_noActivity_shouldReturnUnknown() { |
| 100 | assertThat(mProvider.getAttribution(null /* activity */)) |
| 101 | .isEqualTo(SettingsEnums.PAGE_UNKNOWN); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void getAttribution_notSet_shouldReturnUnknown() { |
| 106 | final Activity activity = Robolectric.setupActivity(Activity.class); |
| 107 | |
| 108 | assertThat(mProvider.getAttribution(activity)) |
| 109 | .isEqualTo(SettingsEnums.PAGE_UNKNOWN); |
| 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void getAttribution_set_shouldReturnAttribution() { |
| 114 | final Intent intent = new Intent() |
| 115 | .putExtra(MetricsFeatureProvider.EXTRA_SOURCE_METRICS_CATEGORY, 100); |
| 116 | |
| 117 | final Activity activity = Robolectric.buildActivity(Activity.class, intent).create().get(); |
| 118 | |
| 119 | assertThat(mProvider.getAttribution(activity)).isEqualTo(100); |
| 120 | } |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 121 | } |