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 */ 16 package android.cts.statsd.metric; 17 18 import com.android.internal.os.StatsdConfigProto; 19 import com.android.internal.os.StatsdConfigProto.AtomMatcher; 20 import com.android.internal.os.StatsdConfigProto.FieldValueMatcher; 21 import com.android.internal.os.StatsdConfigProto.SimpleAtomMatcher; 22 import com.android.os.AtomsProto.Atom; 23 import com.android.os.AtomsProto.AppBreadcrumbReported; 24 import com.google.protobuf.Message; 25 import com.google.protobuf.Descriptors.Descriptor; 26 import com.google.protobuf.Descriptors.FieldDescriptor; 27 28 import static org.junit.Assert.assertTrue; 29 30 public class MetricsUtils { 31 public static final long COUNT_METRIC_ID = 3333; 32 public static final long DURATION_METRIC_ID = 4444; 33 public static final long GAUGE_METRIC_ID = 5555; 34 public static final long VALUE_METRIC_ID = 6666; 35 36 public static AtomMatcher.Builder getAtomMatcher(int atomId) { 37 AtomMatcher.Builder builder = AtomMatcher.newBuilder(); 38 builder.setSimpleAtomMatcher(SimpleAtomMatcher.newBuilder() 39 .setAtomId(atomId)); 40 return builder; 41 } 42 43 public static AtomMatcher startAtomMatcher(int id) { 44 return AtomMatcher.newBuilder() 45 .setId(id) 46 .setSimpleAtomMatcher( 47 SimpleAtomMatcher.newBuilder() 48 .setAtomId(Atom.APP_BREADCRUMB_REPORTED_FIELD_NUMBER) 49 .addFieldValueMatcher(FieldValueMatcher.newBuilder() 50 .setField(AppBreadcrumbReported.STATE_FIELD_NUMBER) 51 .setEqInt(AppBreadcrumbReported.State.START.ordinal()))) 52 .build(); 53 } 54 55 public static AtomMatcher stopAtomMatcher(int id) { 56 return AtomMatcher.newBuilder() 57 .setId(id) 58 .setSimpleAtomMatcher( 59 SimpleAtomMatcher.newBuilder() 60 .setAtomId(Atom.APP_BREADCRUMB_REPORTED_FIELD_NUMBER) 61 .addFieldValueMatcher(FieldValueMatcher.newBuilder() 62 .setField(AppBreadcrumbReported.STATE_FIELD_NUMBER) 63 .setEqInt(AppBreadcrumbReported.State.STOP.ordinal()))) 64 .build(); 65 } 66 67 public static AtomMatcher unspecifiedAtomMatcher(int id) { 68 return AtomMatcher.newBuilder() 69 .setId(id) 70 .setSimpleAtomMatcher(SimpleAtomMatcher.newBuilder() 71 .setAtomId(Atom.APP_BREADCRUMB_REPORTED_FIELD_NUMBER) 72 .addFieldValueMatcher(FieldValueMatcher.newBuilder() 73 .setField(AppBreadcrumbReported.STATE_FIELD_NUMBER) 74 .setEqInt(AppBreadcrumbReported.State.UNSPECIFIED.ordinal()))) 75 .build(); 76 } 77 78 public static AtomMatcher simpleAtomMatcher(int id) { 79 return AtomMatcher.newBuilder() 80 .setId(id) 81 .setSimpleAtomMatcher( 82 SimpleAtomMatcher.newBuilder().setAtomId(Atom.APP_BREADCRUMB_REPORTED_FIELD_NUMBER)) 83 .build(); 84 } 85 86 public static long StringToId(String str) { 87 return str.hashCode(); 88 } 89 90 public static void assertBucketTimePresent(Message bucketInfo) { 91 Descriptor descriptor = bucketInfo.getDescriptorForType(); 92 boolean found = false; 93 FieldDescriptor bucketNum = descriptor.findFieldByName("bucket_num"); 94 FieldDescriptor startMillis = descriptor.findFieldByName("start_bucket_elapsed_millis"); 95 FieldDescriptor endMillis = descriptor.findFieldByName("end_bucket_elapsed_millis"); 96 if (bucketNum != null && bucketInfo.hasField(bucketNum)) { 97 found = true; 98 } else if (startMillis != null && bucketInfo.hasField(startMillis) && 99 endMillis != null && bucketInfo.hasField(endMillis)) { 100 found = true; 101 } 102 assertTrue("Bucket info did not have either bucket num or start and end elapsed millis", 103 found); 104 } 105 } 106