1// Copyright (C) 2024 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// 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
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {BlockingCallMetricData} from './metricUtils';
16import {pinBlockingCallHandlerInstance} from './pinBlockingCall';
17
18const validMetricsTest: {
19  inputMetric: string;
20  expectedOutput: BlockingCallMetricData;
21}[] = [
22  {
23    inputMetric:
24      'perfetto_android_blocking_call-cuj-name-com.google.android.apps.nexuslauncher-name-TASKBAR_EXPAND-blocking_calls-name-animation-total_dur_ms-mean',
25    expectedOutput: {
26      process: 'com.google.android.apps.nexuslauncher',
27      cujName: 'TASKBAR_EXPAND',
28      blockingCallName: 'animation',
29      aggregation: 'total_dur_ms-mean',
30    },
31  },
32
33  {
34    inputMetric:
35      'perfetto_android_blocking_call-cuj-name-com.android.systemui-name-NOTIFICATION_SHADE_EXPAND_COLLAPSE::Collapse-blocking_calls-name-AIDL::java::IPackageManager::isProtectedBroadcast::server-cnt-mean',
36    expectedOutput: {
37      process: 'com.android.systemui',
38      cujName: 'NOTIFICATION_SHADE_EXPAND_COLLAPSE::Collapse',
39      blockingCallName:
40        'AIDL::java::IPackageManager::isProtectedBroadcast::server',
41      aggregation: 'cnt-mean',
42    },
43  },
44  {
45    inputMetric:
46      'perfetto_android_blocking_call-cuj-name-com.android.systemui-name-SPLASHSCREEN_EXIT_ANIM-blocking_calls-name-AIDL::java::IPackageManager::isProtectedBroadcast::server-cnt-mean',
47    expectedOutput: {
48      process: 'com.android.systemui',
49      cujName: 'SPLASHSCREEN_EXIT_ANIM',
50      blockingCallName:
51        'AIDL::java::IPackageManager::isProtectedBroadcast::server',
52      aggregation: 'cnt-mean',
53    },
54  },
55];
56
57const invalidMetricsTest: string[] = [
58  'perfetto_ft_launcher-missed_sf_frames-mean',
59  'perfetto_cuj_launcher-RECENTS_SCROLLING-counter_metrics-missed_sf_frames-mean',
60];
61
62const tester = pinBlockingCallHandlerInstance;
63
64describe('testMetricParser_match', () => {
65  it('parses metrics and returns expected data', () => {
66    for (const testCase of validMetricsTest) {
67      const parsedData = tester.match(testCase.inputMetric);
68      // without this explicit check, undefined also passes the test
69      expect(parsedData).toBeDefined();
70      if (parsedData) {
71        expect(parsedData).toEqual(testCase.expectedOutput);
72      }
73    }
74  });
75  it('parses metrics and returns undefined', () => {
76    for (const testCase of invalidMetricsTest) {
77      const parsedData = tester.match(testCase);
78
79      expect(parsedData).toBeUndefined();
80    }
81  });
82});
83