1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package android.slice.cts; 16 17 import android.app.slice.SliceMetrics; 18 import android.content.Context; 19 import android.metrics.LogMaker; 20 import android.metrics.MetricsReader; 21 import android.net.Uri; 22 import android.support.test.metricshelper.MetricsAsserts; 23 24 import androidx.test.InstrumentationRegistry; 25 import androidx.test.runner.AndroidJUnit4; 26 27 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 33 @RunWith(AndroidJUnit4.class) 34 public class SliceMetricsTest { 35 36 private static final Uri BASE_URI = Uri.parse("content://android.slice.cts.local/"); 37 private static final Uri SUB_SLICE_URI = Uri.parse("content://android.slice.cts.local/sub"); 38 private final Context mContext = InstrumentationRegistry.getContext(); 39 40 private MetricsReader mMetricsReader; 41 private SliceMetrics mSliceMetrics; 42 43 @Before 44 public void setup() { 45 mSliceMetrics = new SliceMetrics(mContext, BASE_URI); 46 mMetricsReader = new MetricsReader(); 47 mMetricsReader.checkpoint(); // clear out old logs 48 } 49 50 @Test 51 public void testLogVisible() { 52 mSliceMetrics.logVisible(); 53 MetricsAsserts.assertHasLog("Missing slice visible log", mMetricsReader, 54 getLogMaker().setCategory(MetricsEvent.SLICE).setType(MetricsEvent.TYPE_OPEN)); 55 } 56 57 @Test 58 public void testLogHidden() { 59 mSliceMetrics.logHidden(); 60 MetricsAsserts.assertHasLog("Missing slice hidden log", mMetricsReader, 61 getLogMaker().setCategory(MetricsEvent.SLICE).setType(MetricsEvent.TYPE_CLOSE)); 62 } 63 64 @Test 65 public void testLogOnTouch() { 66 mSliceMetrics.logTouch(0, SUB_SLICE_URI); 67 MetricsAsserts.assertHasLog("Missing slice touch log", mMetricsReader, 68 getLogMaker().setCategory(MetricsEvent.SLICE) 69 .setType(MetricsEvent.TYPE_ACTION) 70 .addTaggedData(MetricsEvent.FIELD_SUBSLICE_AUTHORITY, SUB_SLICE_URI.getAuthority()) 71 .addTaggedData(MetricsEvent.FIELD_SUBSLICE_PATH, SUB_SLICE_URI.getPath())); 72 } 73 74 private LogMaker getLogMaker() { 75 LogMaker logMaker = new LogMaker(MetricsEvent.VIEW_UNKNOWN); 76 logMaker.addTaggedData(MetricsEvent.FIELD_SLICE_AUTHORITY, BASE_URI.getAuthority()); 77 logMaker.addTaggedData(MetricsEvent.FIELD_SLICE_PATH, BASE_URI.getPath()); 78 return logMaker; 79 } 80 } 81