blob: 7a7f0d4a0fd099ef042e03cfc1b10bf83ffa9773 [file] [log] [blame]
Leif Wilden5eb77482018-01-23 23:54:05 +00001/*
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 */
16package com.android.settingslib.core.instrumentation;
17
Leif Wilden5eb77482018-01-23 23:54:05 +000018import static org.mockito.Matchers.anyString;
19import static org.mockito.Matchers.eq;
20import static org.mockito.Mockito.verify;
21import static org.mockito.Mockito.verifyNoMoreInteractions;
Leif Wilden5eb77482018-01-23 23:54:05 +000022
23import android.content.ComponentName;
24import android.content.Context;
25import android.content.Intent;
26import android.util.Pair;
27
28import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Leif Wilden5eb77482018-01-23 23:54:05 +000029import com.android.settingslib.SettingsLibRobolectricTestRunner;
30
31import org.junit.Before;
32import org.junit.Test;
33import org.junit.runner.RunWith;
Leif Wilden5eb77482018-01-23 23:54:05 +000034import org.mockito.Mock;
35import org.mockito.MockitoAnnotations;
36import org.robolectric.RuntimeEnvironment;
Leif Wilden5eb77482018-01-23 23:54:05 +000037import org.robolectric.util.ReflectionHelpers;
38
39import java.util.ArrayList;
40import java.util.List;
41
42@RunWith(SettingsLibRobolectricTestRunner.class)
Leif Wilden5eb77482018-01-23 23:54:05 +000043public class MetricsFeatureProviderTest {
Fan Zhang793cbdd2018-10-23 13:20:57 -070044 @Mock
45 private LogWriter mLogWriter;
Leif Wilden5eb77482018-01-23 23:54:05 +000046
47 private Context mContext;
48 private MetricsFeatureProvider mProvider;
49
Leif Wilden5eb77482018-01-23 23:54:05 +000050 @Before
51 public void setUp() {
52 MockitoAnnotations.initMocks(this);
53 mContext = RuntimeEnvironment.application;
54 mProvider = new MetricsFeatureProvider();
55 List<LogWriter> writers = new ArrayList<>();
Fan Zhang793cbdd2018-10-23 13:20:57 -070056 writers.add(mLogWriter);
Leif Wilden5eb77482018-01-23 23:54:05 +000057 ReflectionHelpers.setField(mProvider, "mLoggerWriters", writers);
Leif Wilden5eb77482018-01-23 23:54:05 +000058 }
59
60 @Test
61 public void logDashboardStartIntent_intentEmpty_shouldNotLog() {
62 mProvider.logDashboardStartIntent(mContext, null /* intent */,
63 MetricsEvent.SETTINGS_GESTURES);
64
Fan Zhang793cbdd2018-10-23 13:20:57 -070065 verifyNoMoreInteractions(mLogWriter);
Leif Wilden5eb77482018-01-23 23:54:05 +000066 }
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 Zhang793cbdd2018-10-23 13:20:57 -070074 verify(mLogWriter).action(
Leif Wilden5eb77482018-01-23 23:54:05 +000075 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 Zhang793cbdd2018-10-23 13:20:57 -070087 verify(mLogWriter).action(
Leif Wilden5eb77482018-01-23 23:54:05 +000088 eq(mContext),
89 eq(MetricsEvent.ACTION_SETTINGS_TILE_CLICK),
90 anyString(),
91 eq(Pair.create(MetricsEvent.FIELD_CONTEXT, MetricsEvent.SETTINGS_GESTURES)));
92 }
Leif Wilden5eb77482018-01-23 23:54:05 +000093}