Tony Mak | 5a5f0d5 | 2019-01-08 11:07:23 +0000 | [diff] [blame] | 1 | /* |
| 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. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 16 | |
Tony Mak | 5a5f0d5 | 2019-01-08 11:07:23 +0000 | [diff] [blame] | 17 | package android.view.textclassifier.logging; |
| 18 | |
| 19 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_TEXT_SELECTION_SMART_SHARE; |
| 20 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.CONVERSATION_ACTIONS; |
Tony Mak | 5a5f0d5 | 2019-01-08 11:07:23 +0000 | [diff] [blame] | 21 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TEXT_CLASSIFIER_EVENT_TIME; |
Tony Mak | 03a1d03 | 2019-01-24 15:12:00 +0000 | [diff] [blame] | 22 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TEXT_CLASSIFIER_FIRST_ENTITY_TYPE; |
| 23 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TEXT_CLASSIFIER_SCORE; |
| 24 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TEXT_CLASSIFIER_WIDGET_TYPE; |
Tony Mak | 5a5f0d5 | 2019-01-08 11:07:23 +0000 | [diff] [blame] | 25 | |
| 26 | import static com.google.common.truth.Truth.assertThat; |
| 27 | |
| 28 | import android.metrics.LogMaker; |
Tony Mak | ae85aae | 2019-01-09 15:59:56 +0000 | [diff] [blame] | 29 | import android.view.textclassifier.ConversationAction; |
Tony Mak | 5a5f0d5 | 2019-01-08 11:07:23 +0000 | [diff] [blame] | 30 | import android.view.textclassifier.TextClassificationContext; |
| 31 | import android.view.textclassifier.TextClassifierEvent; |
| 32 | import android.view.textclassifier.TextClassifierEventTronLogger; |
| 33 | |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 34 | import androidx.test.filters.SmallTest; |
| 35 | import androidx.test.runner.AndroidJUnit4; |
| 36 | |
Tony Mak | 5a5f0d5 | 2019-01-08 11:07:23 +0000 | [diff] [blame] | 37 | import com.android.internal.logging.MetricsLogger; |
| 38 | |
| 39 | import org.junit.Before; |
| 40 | import org.junit.Test; |
| 41 | import org.junit.runner.RunWith; |
| 42 | import org.mockito.ArgumentCaptor; |
| 43 | import org.mockito.Mock; |
| 44 | import org.mockito.Mockito; |
| 45 | import org.mockito.MockitoAnnotations; |
| 46 | |
| 47 | @SmallTest |
| 48 | @RunWith(AndroidJUnit4.class) |
| 49 | public class TextClassifierEventTronLoggerTest { |
| 50 | private static final String WIDGET_TYPE = "notification"; |
| 51 | private static final String PACKAGE_NAME = "pkg"; |
Tony Mak | 5a5f0d5 | 2019-01-08 11:07:23 +0000 | [diff] [blame] | 52 | |
| 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 Toki | 6d06337 | 2019-04-11 22:36:04 +0100 | [diff] [blame] | 69 | TextClassifierEvent.ConversationActionsEvent textClassifierEvent = |
| 70 | new TextClassifierEvent.ConversationActionsEvent.Builder( |
Tony Mak | 5a5f0d5 | 2019-01-08 11:07:23 +0000 | [diff] [blame] | 71 | TextClassifierEvent.TYPE_SMART_ACTION) |
Tony Mak | 03a1d03 | 2019-01-24 15:12:00 +0000 | [diff] [blame] | 72 | .setEntityTypes(ConversationAction.TYPE_CALL_PHONE) |
Abodunrinwa Toki | 6d06337 | 2019-04-11 22:36:04 +0100 | [diff] [blame] | 73 | .setScores(0.5f) |
Tony Mak | 5a5f0d5 | 2019-01-08 11:07:23 +0000 | [diff] [blame] | 74 | .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 Toki | 6d06337 | 2019-04-11 22:36:04 +0100 | [diff] [blame] | 82 | assertThat(logMaker.getCategory()).isEqualTo(CONVERSATION_ACTIONS); |
| 83 | assertThat(logMaker.getSubtype()).isEqualTo(ACTION_TEXT_SELECTION_SMART_SHARE); |
Tony Mak | 03a1d03 | 2019-01-24 15:12:00 +0000 | [diff] [blame] | 84 | assertThat(logMaker.getTaggedData(FIELD_TEXT_CLASSIFIER_FIRST_ENTITY_TYPE)) |
Tony Mak | ae85aae | 2019-01-09 15:59:56 +0000 | [diff] [blame] | 85 | .isEqualTo(ConversationAction.TYPE_CALL_PHONE); |
Tony Mak | 03a1d03 | 2019-01-24 15:12:00 +0000 | [diff] [blame] | 86 | assertThat((float) logMaker.getTaggedData(FIELD_TEXT_CLASSIFIER_SCORE)) |
| 87 | .isWithin(0.00001f).of(0.5f); |
Abodunrinwa Toki | c33fc77 | 2019-02-06 01:17:10 +0000 | [diff] [blame] | 88 | // Never write event time. |
| 89 | assertThat(logMaker.getTaggedData(FIELD_TEXT_CLASSIFIER_EVENT_TIME)).isNull(); |
Tony Mak | 5a5f0d5 | 2019-01-08 11:07:23 +0000 | [diff] [blame] | 90 | assertThat(logMaker.getPackageName()).isEqualTo(PACKAGE_NAME); |
Tony Mak | 03a1d03 | 2019-01-24 15:12:00 +0000 | [diff] [blame] | 91 | assertThat(logMaker.getTaggedData(FIELD_TEXT_CLASSIFIER_WIDGET_TYPE)) |
Tony Mak | 5a5f0d5 | 2019-01-08 11:07:23 +0000 | [diff] [blame] | 92 | .isEqualTo(WIDGET_TYPE); |
Tony Mak | 03a1d03 | 2019-01-24 15:12:00 +0000 | [diff] [blame] | 93 | |
Tony Mak | 5a5f0d5 | 2019-01-08 11:07:23 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Test |
| 97 | public void testWriteEvent_unsupportedCategory() { |
Abodunrinwa Toki | 6d06337 | 2019-04-11 22:36:04 +0100 | [diff] [blame] | 98 | TextClassifierEvent.TextSelectionEvent textClassifierEvent = |
| 99 | new TextClassifierEvent.TextSelectionEvent.Builder( |
Tony Mak | 5a5f0d5 | 2019-01-08 11:07:23 +0000 | [diff] [blame] | 100 | 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 | } |