• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.helpers;
17 
18 import android.os.SystemClock;
19 import android.platform.helpers.HelperAccessor;
20 import android.platform.helpers.ICalendarHelper;
21 import androidx.test.runner.AndroidJUnit4;
22 
23 import com.android.helpers.AppStartupHelper;
24 
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 
29 import java.util.Map;
30 
31 import static org.junit.Assert.assertFalse;
32 import static org.junit.Assert.assertTrue;
33 import static org.junit.Assert.assertEquals;
34 
35 /**
36  * Android Unit tests for {@link AppStartupHelper}.
37  *
38  * To run:
39  * Connect to wifi and login to gmail.
40  * Disable SELinux: adb shell setenforce 0; if this fails with "permission denied",
41  * try "adb shell su 0 setenforce 0"
42  * atest CollectorsHelperTest:com.android.helpers.AppStartupHelperTest
43  */
44 @RunWith(AndroidJUnit4.class)
45 public class AppStartupHelperTest {
46 
47     // Kill the calendar app.
48     private static final String KILL_TEST_APP_CMD_TEMPLATE = "am force-stop %s";
49     // Package names used for testing.
50     private static final String CALENDAR_PKG_NAME = "com.google.android.calendar";
51     private static final String SETTINGS_PKG_NAME = "com.android.settings";
52     // Key prefixes to store the cold, warm or hot launch time of the calendar app, respectively.
53     private static final String COLD_LAUNCH_KEY_TEMPLATE = "cold_startup_%s";
54     private static final String COLD_LAUNCH_PROCESSS_FG_KEY_TEMPLATE =
55             "cold_startup_process_start_delay_%s_fg";
56     private static final String COLD_LAUNCH_TRANSITION_DELAY_MILLIS_KEY_TEMPLATE =
57             "cold_startup_transition_delay_millis_%s";
58     private static final String WARM_LAUNCH_TRANSITION_DELAY_MILLIS_KEY_TEMPLATE =
59             "warm_startup_transition_delay_millis_%s";
60     private static final String HOT_LAUNCH_TRANSITION_DELAY_MILLIS_KEY_TEMPLATE =
61             "hot_startup_transition_delay_millis_%s";
62 
63     private static final String COLD_LAUNCH_COUNT_PKG_KEY_TEMPLATE = "cold_startup_count_%s";
64     private static final String COLD_LAUNCH_PROCESS_COUNT_FG_KEY_TEMPLATE =
65             "cold_startup_process_start_count_%s_fg";
66     private static final String COLD_LAUNCH_TOTAL_COUNT_KEY_TEMPLATE = "cold_startup_total_count";
67     private static final String COLD_LAUNCH_PROCESS_START_TOTAL_COUNT_KEY_TEMPLATE =
68             "cold_startup_process_start_total_count";
69     private static final String WARM_LAUNCH_KEY_TEMPLATE = "warm_startup_%s";
70     private static final String HOT_LAUNCH_KEY_TEMPLATE = "hot_startup_%s";
71     // Keyword for keys to store the app startup fully drawn metric.
72     private static final String FULLY_DRAWN_KEY_KEYWORD = "fully_drawn";
73 
74     // Keycode for pressing the home button.
75     private static final String KEYCODE_HOME = "KEYCODE_HOME";
76 
77     private AppStartupHelper mAppStartupHelper = new AppStartupHelper();
78     private HelperAccessor<ICalendarHelper> mHelper =
79             new HelperAccessor<>(ICalendarHelper.class);
80 
81     @Before
setUp()82     public void setUp() {
83         mAppStartupHelper = new AppStartupHelper();
84         // Make sure the apps are starting from the clean state.
85         HelperTestUtility.clearApp(String.format(KILL_TEST_APP_CMD_TEMPLATE, CALENDAR_PKG_NAME));
86         HelperTestUtility.clearApp(String.format(KILL_TEST_APP_CMD_TEMPLATE, SETTINGS_PKG_NAME));
87         // Make sure display is on and unlocked.
88         HelperTestUtility.wakeUpAndUnlock();
89     }
90 
91     /**
92      * Test successfull app launch config.
93      */
94     @Test
testAppLaunchConfig()95     public void testAppLaunchConfig() throws Exception {
96         assertTrue(mAppStartupHelper.startCollecting());
97         assertTrue(mAppStartupHelper.stopCollecting());
98     }
99 
100     /**
101      * Test no error is thrown if there is no app launch.
102      */
103     @Test
testEmptyAppLaunchMetric()104     public void testEmptyAppLaunchMetric() throws Exception {
105         assertTrue(mAppStartupHelper.startCollecting());
106         assertTrue(mAppStartupHelper.getMetrics().isEmpty());
107         assertTrue(mAppStartupHelper.stopCollecting());
108     }
109 
110     /**
111      * Test single cold launch metric.
112      */
113     @Test
testSingleColdLaunchMetric()114     public void testSingleColdLaunchMetric() throws Exception {
115         assertTrue(mAppStartupHelper.startCollecting());
116         mHelper.get().open();
117         Map<String, StringBuilder> appLaunchMetrics = mAppStartupHelper.getMetrics();
118         // A metric key for the app cold launching should exist, and should only hold one value.
119         String coldLaunchMetricKey = String.format(COLD_LAUNCH_KEY_TEMPLATE, CALENDAR_PKG_NAME);
120         String coldLaunchCountPkgKey = String.format(COLD_LAUNCH_COUNT_PKG_KEY_TEMPLATE,
121                 CALENDAR_PKG_NAME);
122         assertTrue(appLaunchMetrics.keySet().contains(coldLaunchMetricKey));
123         assertEquals(1, appLaunchMetrics.get(coldLaunchMetricKey).toString().split(",").length);
124         assertEquals(1, Integer.parseInt(appLaunchMetrics.get(coldLaunchCountPkgKey).toString()));
125         assertEquals(1, Integer.parseInt(appLaunchMetrics.get(COLD_LAUNCH_TOTAL_COUNT_KEY_TEMPLATE)
126                 .toString()));
127 
128         // Verify transition metrics.
129         String coldLaunchTransitionMetricKey = String.format(
130                 COLD_LAUNCH_TRANSITION_DELAY_MILLIS_KEY_TEMPLATE,
131                 CALENDAR_PKG_NAME);
132         assertTrue(appLaunchMetrics.keySet().contains(coldLaunchTransitionMetricKey));
133         assertEquals(1,
134                 appLaunchMetrics.get(coldLaunchTransitionMetricKey).toString().split(",").length);
135 
136         // Verify process start values.
137         String coldLaunchProcessMetricKey = String.format(COLD_LAUNCH_PROCESSS_FG_KEY_TEMPLATE,
138                 CALENDAR_PKG_NAME);
139         String coldLaunchProcessCountPkgKey = String.format(COLD_LAUNCH_COUNT_PKG_KEY_TEMPLATE,
140                 CALENDAR_PKG_NAME);
141         assertTrue(appLaunchMetrics.keySet().contains(coldLaunchProcessMetricKey));
142         assertEquals(1,
143                 appLaunchMetrics.get(coldLaunchProcessMetricKey).toString().split(",").length);
144         assertEquals(1,
145                 Integer.parseInt(appLaunchMetrics.get(coldLaunchProcessCountPkgKey).toString()));
146         assertEquals(
147                 1,
148                 Integer.parseInt(appLaunchMetrics.get(
149                         COLD_LAUNCH_PROCESS_START_TOTAL_COUNT_KEY_TEMPLATE)
150                         .toString()));
151         assertTrue(mAppStartupHelper.stopCollecting());
152         mHelper.get().exit();
153     }
154 
155     /**
156      * Test multiple cold launch metric.
157      */
158     @Test
testMultipleColdLaunchMetric()159     public void testMultipleColdLaunchMetric() throws Exception {
160         assertTrue(mAppStartupHelper.startCollecting());
161         mHelper.get().open();
162         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
163         mHelper.get().exit();
164         HelperTestUtility.clearApp(String.format(KILL_TEST_APP_CMD_TEMPLATE, CALENDAR_PKG_NAME));
165         mHelper.get().open();
166         Map<String, StringBuilder> appLaunchMetrics = mAppStartupHelper.getMetrics();
167         // A metric key for the app cold launching should exist, and should hold two values.
168         String coldLaunchMetricKey = String.format(COLD_LAUNCH_KEY_TEMPLATE, CALENDAR_PKG_NAME);
169         String coldLaunchCountPkgKey = String.format(COLD_LAUNCH_COUNT_PKG_KEY_TEMPLATE,
170                 CALENDAR_PKG_NAME);
171         assertTrue(appLaunchMetrics.keySet().contains(coldLaunchMetricKey));
172         assertEquals(2, appLaunchMetrics.get(coldLaunchMetricKey).toString().split(",").length);
173         assertEquals(2, Integer.parseInt(appLaunchMetrics.get(coldLaunchCountPkgKey).toString()));
174         assertEquals(2, Integer.parseInt(appLaunchMetrics.get(COLD_LAUNCH_TOTAL_COUNT_KEY_TEMPLATE)
175                 .toString()));
176 
177         // Verify process start values.
178         String coldLaunchProcessMetricKey = String.format(COLD_LAUNCH_PROCESSS_FG_KEY_TEMPLATE,
179                 CALENDAR_PKG_NAME);
180         String coldLaunchProcessCountPkgKey = String.format(COLD_LAUNCH_COUNT_PKG_KEY_TEMPLATE,
181                 CALENDAR_PKG_NAME);
182         assertTrue(appLaunchMetrics.keySet().contains(coldLaunchProcessMetricKey));
183         assertEquals(2,
184                 appLaunchMetrics.get(coldLaunchProcessMetricKey).toString().split(",").length);
185         assertEquals(2,
186                 Integer.parseInt(appLaunchMetrics.get(coldLaunchProcessCountPkgKey).toString()));
187         assertEquals(
188                 2,
189                 Integer.parseInt(appLaunchMetrics.get(
190                         COLD_LAUNCH_PROCESS_START_TOTAL_COUNT_KEY_TEMPLATE)
191                         .toString()));
192         assertTrue(mAppStartupHelper.stopCollecting());
193         mHelper.get().exit();
194         assertTrue(mAppStartupHelper.stopCollecting());
195         mHelper.get().exit();
196     }
197 
198     /**
199      * Test cold launch metric of two different apps.
200      */
201     @Test
testDifferentAppColdLaunchMetric()202     public void testDifferentAppColdLaunchMetric() throws Exception {
203 
204         // Open the calendar app.
205         assertTrue(mAppStartupHelper.startCollecting());
206         mHelper.get().open();
207         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
208         mHelper.get().exit();
209         HelperTestUtility.clearApp(String.format(KILL_TEST_APP_CMD_TEMPLATE, CALENDAR_PKG_NAME));
210 
211         // Open settings app
212         HelperTestUtility.launchPackageViaAdb(SETTINGS_PKG_NAME);
213         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
214         HelperTestUtility.sendKeyCode(KEYCODE_HOME);
215         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
216         HelperTestUtility.clearApp(String.format(KILL_TEST_APP_CMD_TEMPLATE, SETTINGS_PKG_NAME));
217         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
218 
219         Map<String, StringBuilder> appLaunchMetrics = mAppStartupHelper.getMetrics();
220         String coldLaunchCalendarMetricKey = String.format(COLD_LAUNCH_KEY_TEMPLATE,
221                 CALENDAR_PKG_NAME);
222         String coldLaunchSettingsMetricKey = String.format(COLD_LAUNCH_KEY_TEMPLATE,
223                 SETTINGS_PKG_NAME);
224         String coldLaunchCalendarCountPkgKey = String.format(COLD_LAUNCH_COUNT_PKG_KEY_TEMPLATE,
225                 CALENDAR_PKG_NAME);
226         String coldLaunchSettingsCountPkgKey = String.format(COLD_LAUNCH_COUNT_PKG_KEY_TEMPLATE,
227                 CALENDAR_PKG_NAME);
228         assertTrue(appLaunchMetrics.keySet().contains(coldLaunchCalendarMetricKey));
229         assertTrue(appLaunchMetrics.keySet().contains(coldLaunchSettingsMetricKey));
230         assertEquals(1,
231                 appLaunchMetrics.get(coldLaunchCalendarMetricKey).toString().split(",").length);
232         assertEquals(1,
233                 appLaunchMetrics.get(coldLaunchSettingsCountPkgKey).toString().split(",").length);
234         assertEquals(1,
235                 Integer.parseInt(appLaunchMetrics.get(coldLaunchCalendarCountPkgKey).toString()));
236         assertEquals(1,
237                 Integer.parseInt(appLaunchMetrics.get(coldLaunchSettingsCountPkgKey).toString()));
238         assertEquals(2, Integer.parseInt(appLaunchMetrics.get(COLD_LAUNCH_TOTAL_COUNT_KEY_TEMPLATE)
239                 .toString()));
240 
241         // Verify transition metrics.
242         String coldLaunchTransCalMetricKey = String.format(
243                 COLD_LAUNCH_TRANSITION_DELAY_MILLIS_KEY_TEMPLATE,
244                 CALENDAR_PKG_NAME);
245         String coldLaunchTransSetMetricKey = String.format(
246                 COLD_LAUNCH_TRANSITION_DELAY_MILLIS_KEY_TEMPLATE,
247                 SETTINGS_PKG_NAME);
248         assertTrue(appLaunchMetrics.keySet().contains(coldLaunchTransCalMetricKey));
249         assertTrue(appLaunchMetrics.keySet().contains(coldLaunchTransSetMetricKey));
250         assertEquals(1,
251                 appLaunchMetrics.get(coldLaunchTransCalMetricKey).toString().split(",").length);
252         assertEquals(1,
253                 appLaunchMetrics.get(coldLaunchTransSetMetricKey).toString().split(",").length);
254 
255         // Verify process start values.
256         String coldLaunchProcessMetricKey = String.format(COLD_LAUNCH_PROCESSS_FG_KEY_TEMPLATE,
257                 CALENDAR_PKG_NAME);
258         String coldLaunchProcessCountPkgKey = String.format(COLD_LAUNCH_COUNT_PKG_KEY_TEMPLATE,
259                 CALENDAR_PKG_NAME);
260         String coldLaunchProcessMetricSettingsKey = String.format(
261                 COLD_LAUNCH_PROCESSS_FG_KEY_TEMPLATE, SETTINGS_PKG_NAME);
262         String coldLaunchProcessCountSettingsPkgKey = String.format(
263                 COLD_LAUNCH_COUNT_PKG_KEY_TEMPLATE,
264                 SETTINGS_PKG_NAME);
265 
266         assertTrue(appLaunchMetrics.keySet().contains(coldLaunchProcessMetricKey));
267         assertEquals(1,
268                 appLaunchMetrics.get(coldLaunchProcessMetricKey).toString().split(",").length);
269         assertEquals(1,
270                 Integer.parseInt(appLaunchMetrics.get(coldLaunchProcessCountPkgKey).toString()));
271         assertEquals(
272                 1, appLaunchMetrics.get(
273                         coldLaunchProcessMetricSettingsKey).toString().split(",").length);
274         assertEquals(1, Integer.parseInt(appLaunchMetrics.get(coldLaunchProcessCountSettingsPkgKey)
275                 .toString()));
276 
277         // Sometimes I see background process started during the test counted towards total count
278         // hence setting to gretaer than 2.
279         assertTrue(Integer.parseInt(appLaunchMetrics.get(
280                 COLD_LAUNCH_PROCESS_START_TOTAL_COUNT_KEY_TEMPLATE)
281                 .toString()) >= 2);
282 
283         assertTrue(mAppStartupHelper.stopCollecting());
284 
285     }
286 
287     /**
288      * Test warm launch metric.
289      */
290     @Test
testWarmLaunchMetric()291     public void testWarmLaunchMetric() throws Exception {
292         // Launch the app once and exit it so it resides in memory.
293         mHelper.get().open();
294         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
295         // Press home and clear the cache explicitly.
296         HelperTestUtility.sendKeyCode(KEYCODE_HOME);
297         HelperTestUtility.clearCache();
298         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
299         // Start the collection here to test warm launch.
300         assertTrue(mAppStartupHelper.startCollecting());
301         // Launch the app; a warm launch occurs.
302         mHelper.get().open();
303         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
304         Map<String, StringBuilder> appLaunchMetrics = mAppStartupHelper.getMetrics();
305         String calendarWarmLaunchKey = String.format(WARM_LAUNCH_KEY_TEMPLATE, CALENDAR_PKG_NAME);
306         assertTrue(appLaunchMetrics.keySet().contains(calendarWarmLaunchKey));
307         assertEquals(1, appLaunchMetrics.get(calendarWarmLaunchKey).toString().split(",").length);
308         assertTrue(mAppStartupHelper.stopCollecting());
309 
310         // Verify transition metrics.
311         String warmLaunchTransitionMetricKey = String.format(
312                 WARM_LAUNCH_TRANSITION_DELAY_MILLIS_KEY_TEMPLATE,
313                 CALENDAR_PKG_NAME);
314         assertTrue(appLaunchMetrics.keySet().contains(warmLaunchTransitionMetricKey));
315         assertEquals(1,
316                 appLaunchMetrics.get(warmLaunchTransitionMetricKey).toString().split(",").length);
317 
318         mHelper.get().exit();
319     }
320 
321     /**
322      * Test hot launch metric on settings, which is lightweight enough to trigger a hot launch.
323      */
324     @Test
testHotLaunchMetric()325     public void testHotLaunchMetric() throws Exception {
326         // Launch the app once and go home so the app resides in memory.
327         HelperTestUtility.launchPackageViaAdb(SETTINGS_PKG_NAME);
328         HelperTestUtility.sendKeyCode(KEYCODE_HOME);
329         // Start the collection here to test hot launch.
330         assertTrue(mAppStartupHelper.startCollecting());
331         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
332         // Launch the app; a hot launch occurs.
333         HelperTestUtility.launchPackageViaAdb(SETTINGS_PKG_NAME);
334         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
335         Map<String, StringBuilder> appLaunchMetrics = mAppStartupHelper.getMetrics();
336         String calculatoHotLaunchKey = String.format(HOT_LAUNCH_KEY_TEMPLATE, SETTINGS_PKG_NAME);
337         assertTrue(appLaunchMetrics.keySet().contains(calculatoHotLaunchKey));
338         assertEquals(1, appLaunchMetrics.get(calculatoHotLaunchKey).toString().split(",").length);
339         assertTrue(mAppStartupHelper.stopCollecting());
340         HelperTestUtility.sendKeyCode(KEYCODE_HOME);
341         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
342         HelperTestUtility.clearApp(String.format(KILL_TEST_APP_CMD_TEMPLATE, SETTINGS_PKG_NAME));
343         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
344 
345         // Verify transition metrics.
346         String hotLaunchTransitionMetricKey = String.format(
347                 HOT_LAUNCH_TRANSITION_DELAY_MILLIS_KEY_TEMPLATE,
348                 SETTINGS_PKG_NAME);
349         assertTrue(appLaunchMetrics.keySet().contains(hotLaunchTransitionMetricKey));
350         assertEquals(1,
351                 appLaunchMetrics.get(hotLaunchTransitionMetricKey).toString().split(",").length);
352 
353     }
354 
355     /**
356      * Test that app startup fully drawn metric is collected for a single launch.
357      */
358     @Test
testSingleLaunchStartupFullyDrawnMetric()359     public void testSingleLaunchStartupFullyDrawnMetric() throws Exception {
360         // The Settings app is used here as it calls reportFullyDrawn(), which is required for the
361         // AppStartFullyDrawn metric to be collected.
362         // Start metric collection and then launch the Settings app.
363         assertTrue(mAppStartupHelper.startCollecting());
364         HelperTestUtility.launchPackageViaAdb(SETTINGS_PKG_NAME);
365         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
366         // Check that the collected metrics contains the key for the AppStartFullyDrawn metric.
367         boolean hasFullyDrawnKey = false;
368         Map<String, StringBuilder> appLaunchMetrics = mAppStartupHelper.getMetrics();
369         for (String key : appLaunchMetrics.keySet()) {
370             if (key.contains(FULLY_DRAWN_KEY_KEYWORD) && key.contains(SETTINGS_PKG_NAME)) {
371                 hasFullyDrawnKey = true;
372             }
373         }
374         assertTrue(hasFullyDrawnKey);
375         assertTrue(mAppStartupHelper.stopCollecting());
376         HelperTestUtility.sendKeyCode(KEYCODE_HOME);
377         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
378         HelperTestUtility.clearApp(String.format(KILL_TEST_APP_CMD_TEMPLATE, SETTINGS_PKG_NAME));
379         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
380     }
381 
382     /**
383      * Test that app startup fully drawn metric is collected for a single launch.
384      */
385     @Test
testMultipleLaunchStartupFullyDrawnMetric()386     public void testMultipleLaunchStartupFullyDrawnMetric() throws Exception {
387         // The Settings app is used here as it calls reportFullyDrawn(), which is required for the
388         // AppStartFullyDrawn metric to be collected.
389         // Start metric collection and then cold launch the Settings app twice, as in this app
390         // reportFullyDrawn() is only called during cold launch (the calling function is only called
391         // during onCreate()).
392         assertTrue(mAppStartupHelper.startCollecting());
393         // 1st launch and kill.
394         HelperTestUtility.launchPackageViaAdb(SETTINGS_PKG_NAME);
395         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
396         HelperTestUtility.sendKeyCode(KEYCODE_HOME);
397         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
398         HelperTestUtility.clearApp(String.format(KILL_TEST_APP_CMD_TEMPLATE, SETTINGS_PKG_NAME));
399         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
400         // 2nd launch.
401         HelperTestUtility.launchPackageViaAdb(SETTINGS_PKG_NAME);
402         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
403         // Check that the collected metrics contains the key for the AppStartFullyDrawn metric,
404         // and that there are two values under this key.
405         boolean hasFullyDrawnKey = false;
406         Map<String, StringBuilder> appLaunchMetrics = mAppStartupHelper.getMetrics();
407         for (String key : appLaunchMetrics.keySet()) {
408             if (key.contains(FULLY_DRAWN_KEY_KEYWORD) && key.contains(SETTINGS_PKG_NAME)) {
409                 hasFullyDrawnKey = true;
410                 // There should be two values under this key.
411                 assertEquals(2, appLaunchMetrics.get(key).toString().split(",").length);
412             }
413         }
414         assertTrue(hasFullyDrawnKey);
415         assertTrue(mAppStartupHelper.stopCollecting());
416         HelperTestUtility.sendKeyCode(KEYCODE_HOME);
417         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
418         HelperTestUtility.clearApp(String.format(KILL_TEST_APP_CMD_TEMPLATE, SETTINGS_PKG_NAME));
419         SystemClock.sleep(HelperTestUtility.ACTION_DELAY);
420     }
421 
422     /**
423      * Test disable detailed process start delay metrics.
424      */
425     @Test
testDisableDetailedProcStartMetrics()426     public void testDisableDetailedProcStartMetrics() throws Exception {
427         mAppStartupHelper.setDisableProcStartDetails();
428         assertTrue(mAppStartupHelper.startCollecting());
429         mHelper.get().open();
430         Map<String, StringBuilder> appLaunchMetrics = mAppStartupHelper.getMetrics();
431         // A metric key for the app cold launching should exist, and should only hold one value.
432         String coldLaunchMetricKey = String.format(COLD_LAUNCH_KEY_TEMPLATE, CALENDAR_PKG_NAME);
433         String coldLaunchCountPkgKey = String.format(COLD_LAUNCH_COUNT_PKG_KEY_TEMPLATE,
434                 CALENDAR_PKG_NAME);
435         assertTrue(appLaunchMetrics.keySet().contains(coldLaunchMetricKey));
436         assertEquals(1, appLaunchMetrics.get(coldLaunchMetricKey).toString().split(",").length);
437         assertEquals(1, Integer.parseInt(appLaunchMetrics.get(coldLaunchCountPkgKey).toString()));
438         assertEquals(1, Integer.parseInt(appLaunchMetrics.get(COLD_LAUNCH_TOTAL_COUNT_KEY_TEMPLATE)
439                 .toString()));
440 
441         // Verify process start detailed values are not added.
442         String coldLaunchProcessMetricKey = String.format(COLD_LAUNCH_PROCESSS_FG_KEY_TEMPLATE,
443                 CALENDAR_PKG_NAME);
444         String coldLaunchProcessCountPkgKey = String.format(COLD_LAUNCH_COUNT_PKG_KEY_TEMPLATE,
445                 CALENDAR_PKG_NAME);
446         assertFalse(appLaunchMetrics.keySet().contains(coldLaunchProcessMetricKey));
447         assertTrue(appLaunchMetrics.keySet().contains(coldLaunchProcessCountPkgKey));
448 
449         // Total process count should not be there.
450         assertFalse(appLaunchMetrics.keySet().contains(
451                         COLD_LAUNCH_PROCESS_START_TOTAL_COUNT_KEY_TEMPLATE));
452         assertTrue(mAppStartupHelper.stopCollecting());
453         mHelper.get().exit();
454     }
455 }
456