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 | |
| 18 | import static com.google.common.truth.Truth.assertThat; |
| 19 | import static org.mockito.Matchers.anyString; |
| 20 | import static org.mockito.Matchers.eq; |
| 21 | import static org.mockito.Mockito.verify; |
| 22 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 23 | import static org.mockito.Mockito.when; |
| 24 | |
| 25 | import android.content.ComponentName; |
| 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| 28 | import android.util.Pair; |
| 29 | |
| 30 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
| 31 | import com.android.settingslib.TestConfig; |
| 32 | import com.android.settingslib.SettingsLibRobolectricTestRunner; |
| 33 | |
| 34 | import org.junit.Before; |
| 35 | import org.junit.Test; |
| 36 | import org.junit.runner.RunWith; |
| 37 | import org.mockito.ArgumentCaptor; |
| 38 | import org.mockito.Captor; |
| 39 | import org.mockito.Mock; |
| 40 | import org.mockito.MockitoAnnotations; |
| 41 | import org.robolectric.RuntimeEnvironment; |
| 42 | import org.robolectric.annotation.Config; |
| 43 | import org.robolectric.util.ReflectionHelpers; |
| 44 | |
| 45 | import java.util.ArrayList; |
| 46 | import java.util.List; |
| 47 | |
| 48 | @RunWith(SettingsLibRobolectricTestRunner.class) |
| 49 | @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) |
| 50 | public class MetricsFeatureProviderTest { |
| 51 | private static int CATEGORY = 10; |
| 52 | private static boolean SUBTYPE_BOOLEAN = true; |
| 53 | private static int SUBTYPE_INTEGER = 1; |
| 54 | private static long ELAPSED_TIME = 1000; |
| 55 | |
| 56 | @Mock private LogWriter mockLogWriter; |
| 57 | @Mock private VisibilityLoggerMixin mockVisibilityLogger; |
| 58 | |
| 59 | private Context mContext; |
| 60 | private MetricsFeatureProvider mProvider; |
| 61 | |
| 62 | @Captor |
| 63 | private ArgumentCaptor<Pair> mPairCaptor; |
| 64 | |
| 65 | @Before |
| 66 | public void setUp() { |
| 67 | MockitoAnnotations.initMocks(this); |
| 68 | mContext = RuntimeEnvironment.application; |
| 69 | mProvider = new MetricsFeatureProvider(); |
| 70 | List<LogWriter> writers = new ArrayList<>(); |
| 71 | writers.add(mockLogWriter); |
| 72 | ReflectionHelpers.setField(mProvider, "mLoggerWriters", writers); |
| 73 | |
| 74 | when(mockVisibilityLogger.elapsedTimeSinceVisible()).thenReturn(ELAPSED_TIME); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void logDashboardStartIntent_intentEmpty_shouldNotLog() { |
| 79 | mProvider.logDashboardStartIntent(mContext, null /* intent */, |
| 80 | MetricsEvent.SETTINGS_GESTURES); |
| 81 | |
| 82 | verifyNoMoreInteractions(mockLogWriter); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void logDashboardStartIntent_intentHasNoComponent_shouldLog() { |
| 87 | final Intent intent = new Intent(Intent.ACTION_ASSIST); |
| 88 | |
| 89 | mProvider.logDashboardStartIntent(mContext, intent, MetricsEvent.SETTINGS_GESTURES); |
| 90 | |
| 91 | verify(mockLogWriter).action( |
| 92 | eq(mContext), |
| 93 | eq(MetricsEvent.ACTION_SETTINGS_TILE_CLICK), |
| 94 | anyString(), |
| 95 | eq(Pair.create(MetricsEvent.FIELD_CONTEXT, MetricsEvent.SETTINGS_GESTURES))); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void logDashboardStartIntent_intentIsExternal_shouldLog() { |
| 100 | final Intent intent = new Intent().setComponent(new ComponentName("pkg", "cls")); |
| 101 | |
| 102 | mProvider.logDashboardStartIntent(mContext, intent, MetricsEvent.SETTINGS_GESTURES); |
| 103 | |
| 104 | verify(mockLogWriter).action( |
| 105 | eq(mContext), |
| 106 | eq(MetricsEvent.ACTION_SETTINGS_TILE_CLICK), |
| 107 | anyString(), |
| 108 | eq(Pair.create(MetricsEvent.FIELD_CONTEXT, MetricsEvent.SETTINGS_GESTURES))); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void action_BooleanLogsElapsedTime() { |
| 113 | mProvider.action(mockVisibilityLogger, CATEGORY, SUBTYPE_BOOLEAN); |
| 114 | verify(mockLogWriter).action(eq(CATEGORY), eq(SUBTYPE_BOOLEAN), mPairCaptor.capture()); |
| 115 | |
| 116 | Pair value = mPairCaptor.getValue(); |
| 117 | assertThat(value.first instanceof Integer).isTrue(); |
| 118 | assertThat((int) value.first).isEqualTo(MetricsEvent.NOTIFICATION_SINCE_VISIBLE_MILLIS); |
| 119 | assertThat(value.second).isEqualTo(ELAPSED_TIME); |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void action_IntegerLogsElapsedTime() { |
| 124 | mProvider.action(mockVisibilityLogger, CATEGORY, SUBTYPE_INTEGER); |
| 125 | verify(mockLogWriter).action(eq(CATEGORY), eq(SUBTYPE_INTEGER), mPairCaptor.capture()); |
| 126 | |
| 127 | Pair value = mPairCaptor.getValue(); |
| 128 | assertThat(value.first instanceof Integer).isTrue(); |
| 129 | assertThat((int) value.first).isEqualTo(MetricsEvent.NOTIFICATION_SINCE_VISIBLE_MILLIS); |
| 130 | assertThat(value.second).isEqualTo(ELAPSED_TIME); |
| 131 | } |
| 132 | } |