blob: 2c540e560f6b40fd0c6ed43212584bfe02713064 [file] [log] [blame]
Tony Mak5a5f0d52019-01-08 11:07:23 +00001/*
2 * Copyright (C) 2018 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 */
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090016
Tony Mak5a5f0d52019-01-08 11:07:23 +000017package android.view.textclassifier.logging;
18
19import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_TEXT_SELECTION_SMART_SHARE;
20import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.CONVERSATION_ACTIONS;
Tony Mak5a5f0d52019-01-08 11:07:23 +000021import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TEXT_CLASSIFIER_EVENT_TIME;
Tony Mak03a1d032019-01-24 15:12:00 +000022import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TEXT_CLASSIFIER_FIRST_ENTITY_TYPE;
23import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TEXT_CLASSIFIER_SCORE;
24import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TEXT_CLASSIFIER_WIDGET_TYPE;
Tony Mak5a5f0d52019-01-08 11:07:23 +000025
26import static com.google.common.truth.Truth.assertThat;
27
28import android.metrics.LogMaker;
Tony Makae85aae2019-01-09 15:59:56 +000029import android.view.textclassifier.ConversationAction;
Tony Mak5a5f0d52019-01-08 11:07:23 +000030import android.view.textclassifier.TextClassificationContext;
31import android.view.textclassifier.TextClassifierEvent;
32import android.view.textclassifier.TextClassifierEventTronLogger;
33
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090034import androidx.test.filters.SmallTest;
35import androidx.test.runner.AndroidJUnit4;
36
Tony Mak5a5f0d52019-01-08 11:07:23 +000037import com.android.internal.logging.MetricsLogger;
38
39import org.junit.Before;
40import org.junit.Test;
41import org.junit.runner.RunWith;
42import org.mockito.ArgumentCaptor;
43import org.mockito.Mock;
44import org.mockito.Mockito;
45import org.mockito.MockitoAnnotations;
46
47@SmallTest
48@RunWith(AndroidJUnit4.class)
49public class TextClassifierEventTronLoggerTest {
50 private static final String WIDGET_TYPE = "notification";
51 private static final String PACKAGE_NAME = "pkg";
Tony Mak5a5f0d52019-01-08 11:07:23 +000052
53 @Mock
54 private MetricsLogger mMetricsLogger;
55 private TextClassifierEventTronLogger mTextClassifierEventTronLogger;
56
57
58 @Before
59 public void setup() {
60 MockitoAnnotations.initMocks(this);
61 mTextClassifierEventTronLogger = new TextClassifierEventTronLogger(mMetricsLogger);
62 }
63
64 @Test
65 public void testWriteEvent() {
66 TextClassificationContext textClassificationContext =
67 new TextClassificationContext.Builder(PACKAGE_NAME, WIDGET_TYPE)
68 .build();
Abodunrinwa Toki6d063372019-04-11 22:36:04 +010069 TextClassifierEvent.ConversationActionsEvent textClassifierEvent =
70 new TextClassifierEvent.ConversationActionsEvent.Builder(
Tony Mak5a5f0d52019-01-08 11:07:23 +000071 TextClassifierEvent.TYPE_SMART_ACTION)
Tony Mak03a1d032019-01-24 15:12:00 +000072 .setEntityTypes(ConversationAction.TYPE_CALL_PHONE)
Abodunrinwa Toki6d063372019-04-11 22:36:04 +010073 .setScores(0.5f)
Tony Mak5a5f0d52019-01-08 11:07:23 +000074 .setEventContext(textClassificationContext)
75 .build();
76
77 mTextClassifierEventTronLogger.writeEvent(textClassifierEvent);
78
79 ArgumentCaptor<LogMaker> captor = ArgumentCaptor.forClass(LogMaker.class);
80 Mockito.verify(mMetricsLogger).write(captor.capture());
81 LogMaker logMaker = captor.getValue();
Abodunrinwa Toki6d063372019-04-11 22:36:04 +010082 assertThat(logMaker.getCategory()).isEqualTo(CONVERSATION_ACTIONS);
83 assertThat(logMaker.getSubtype()).isEqualTo(ACTION_TEXT_SELECTION_SMART_SHARE);
Tony Mak03a1d032019-01-24 15:12:00 +000084 assertThat(logMaker.getTaggedData(FIELD_TEXT_CLASSIFIER_FIRST_ENTITY_TYPE))
Tony Makae85aae2019-01-09 15:59:56 +000085 .isEqualTo(ConversationAction.TYPE_CALL_PHONE);
Tony Mak03a1d032019-01-24 15:12:00 +000086 assertThat((float) logMaker.getTaggedData(FIELD_TEXT_CLASSIFIER_SCORE))
87 .isWithin(0.00001f).of(0.5f);
Abodunrinwa Tokic33fc772019-02-06 01:17:10 +000088 // Never write event time.
89 assertThat(logMaker.getTaggedData(FIELD_TEXT_CLASSIFIER_EVENT_TIME)).isNull();
Tony Mak5a5f0d52019-01-08 11:07:23 +000090 assertThat(logMaker.getPackageName()).isEqualTo(PACKAGE_NAME);
Tony Mak03a1d032019-01-24 15:12:00 +000091 assertThat(logMaker.getTaggedData(FIELD_TEXT_CLASSIFIER_WIDGET_TYPE))
Tony Mak5a5f0d52019-01-08 11:07:23 +000092 .isEqualTo(WIDGET_TYPE);
Tony Mak03a1d032019-01-24 15:12:00 +000093
Tony Mak5a5f0d52019-01-08 11:07:23 +000094 }
95
96 @Test
97 public void testWriteEvent_unsupportedCategory() {
Abodunrinwa Toki6d063372019-04-11 22:36:04 +010098 TextClassifierEvent.TextSelectionEvent textClassifierEvent =
99 new TextClassifierEvent.TextSelectionEvent.Builder(
Tony Mak5a5f0d52019-01-08 11:07:23 +0000100 TextClassifierEvent.TYPE_SMART_ACTION)
101 .build();
102
103 mTextClassifierEventTronLogger.writeEvent(textClassifierEvent);
104
105 Mockito.verify(mMetricsLogger, Mockito.never()).write(Mockito.any(LogMaker.class));
106 }
107}