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 | |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 18 | import static org.mockito.Matchers.anyString; |
| 19 | import static org.mockito.Matchers.eq; |
| 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 | |
| 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.util.Pair; |
| 27 | |
| 28 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 29 | import com.android.settingslib.SettingsLibRobolectricTestRunner; |
| 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; |
| 36 | import org.robolectric.RuntimeEnvironment; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 37 | import org.robolectric.util.ReflectionHelpers; |
| 38 | |
| 39 | import java.util.ArrayList; |
| 40 | import java.util.List; |
| 41 | |
| 42 | @RunWith(SettingsLibRobolectricTestRunner.class) |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 43 | public class MetricsFeatureProviderTest { |
Fan Zhang | 793cbdd | 2018-10-23 13:20:57 -0700 | [diff] [blame^] | 44 | @Mock |
| 45 | private LogWriter mLogWriter; |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 46 | |
| 47 | private Context mContext; |
| 48 | private MetricsFeatureProvider mProvider; |
| 49 | |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 50 | @Before |
| 51 | public void setUp() { |
| 52 | MockitoAnnotations.initMocks(this); |
| 53 | mContext = RuntimeEnvironment.application; |
| 54 | mProvider = new MetricsFeatureProvider(); |
| 55 | List<LogWriter> writers = new ArrayList<>(); |
Fan Zhang | 793cbdd | 2018-10-23 13:20:57 -0700 | [diff] [blame^] | 56 | writers.add(mLogWriter); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 57 | ReflectionHelpers.setField(mProvider, "mLoggerWriters", writers); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Test |
| 61 | public void logDashboardStartIntent_intentEmpty_shouldNotLog() { |
| 62 | mProvider.logDashboardStartIntent(mContext, null /* intent */, |
| 63 | MetricsEvent.SETTINGS_GESTURES); |
| 64 | |
Fan Zhang | 793cbdd | 2018-10-23 13:20:57 -0700 | [diff] [blame^] | 65 | verifyNoMoreInteractions(mLogWriter); |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void logDashboardStartIntent_intentHasNoComponent_shouldLog() { |
| 70 | final Intent intent = new Intent(Intent.ACTION_ASSIST); |
| 71 | |
| 72 | mProvider.logDashboardStartIntent(mContext, intent, MetricsEvent.SETTINGS_GESTURES); |
| 73 | |
Fan Zhang | 793cbdd | 2018-10-23 13:20:57 -0700 | [diff] [blame^] | 74 | verify(mLogWriter).action( |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 75 | eq(mContext), |
| 76 | eq(MetricsEvent.ACTION_SETTINGS_TILE_CLICK), |
| 77 | anyString(), |
| 78 | eq(Pair.create(MetricsEvent.FIELD_CONTEXT, MetricsEvent.SETTINGS_GESTURES))); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void logDashboardStartIntent_intentIsExternal_shouldLog() { |
| 83 | final Intent intent = new Intent().setComponent(new ComponentName("pkg", "cls")); |
| 84 | |
| 85 | mProvider.logDashboardStartIntent(mContext, intent, MetricsEvent.SETTINGS_GESTURES); |
| 86 | |
Fan Zhang | 793cbdd | 2018-10-23 13:20:57 -0700 | [diff] [blame^] | 87 | verify(mLogWriter).action( |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 88 | eq(mContext), |
| 89 | eq(MetricsEvent.ACTION_SETTINGS_TILE_CLICK), |
| 90 | anyString(), |
| 91 | eq(Pair.create(MetricsEvent.FIELD_CONTEXT, MetricsEvent.SETTINGS_GESTURES))); |
| 92 | } |
Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 93 | } |