blob: a320514dd97ab043db435af2fa29279e1534f1a6 [file] [log] [blame]
Alan Viverette3da604b2020-06-10 18:34:39 +00001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package android.app;
18
19import android.content.Context;
20import android.content.Intent;
21import android.perftests.utils.BenchmarkState;
22import android.perftests.utils.PerfStatusReporter;
23import android.perftests.utils.PerfTestActivity;
24
25import androidx.test.InstrumentationRegistry;
26import androidx.test.filters.LargeTest;
27import androidx.test.runner.AndroidJUnit4;
28
29import org.junit.Before;
30import org.junit.Rule;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33
34// Due to b/71353150, you might get "java.lang.AssertionError: Binder ProxyMap has too many
35// entries", but it's flaky. Adding "Runtime.getRuntime().gc()" between each iteration solves
36// the problem, but it doesn't seem like it's currently needed.
37@RunWith(AndroidJUnit4.class)
38@LargeTest
39public class PendingIntentPerfTest {
40
41 private Context mContext;
42
43 @Rule
44 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
45
46 private Intent mIntent;
47
48 @Before
49 public void setUp() {
50 mContext = InstrumentationRegistry.getTargetContext();
51 mIntent = PerfTestActivity.createLaunchIntent(mContext);
52 }
53
54 /**
55 * Benchmark time to create a PendingIntent.
56 */
57 @Test
58 public void create() {
59 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
60 while (state.keepRunning()) {
61 state.pauseTiming();
62 state.resumeTiming();
63
64 final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, mIntent,
65 0);
66
67 state.pauseTiming();
68 pendingIntent.cancel();
69 state.resumeTiming();
70 }
71 }
72
73 /**
74 * Benchmark time to create a PendingIntent with FLAG_CANCEL_CURRENT, already having an active
75 * PendingIntent.
76 */
77 @Test
78 public void createWithCancelFlag() {
79 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
80 while (state.keepRunning()) {
81 state.pauseTiming();
82 final PendingIntent previousPendingIntent = PendingIntent.getActivity(mContext, 0,
83 mIntent, 0);
84 state.resumeTiming();
85
86 final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, mIntent,
87 PendingIntent.FLAG_CANCEL_CURRENT);
88
89 state.pauseTiming();
90 pendingIntent.cancel();
91 state.resumeTiming();
92 }
93 }
94
95 /**
96 * Benchmark time to create a PendingIntent with FLAG_UPDATE_CURRENT, already having an active
97 * PendingIntent.
98 */
99 @Test
100 public void createWithUpdateFlag() {
101 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
102 while (state.keepRunning()) {
103 state.pauseTiming();
104 final PendingIntent previousPendingIntent = PendingIntent.getActivity(mContext, 0,
105 mIntent, 0);
106 state.resumeTiming();
107
108 final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, mIntent,
109 PendingIntent.FLAG_UPDATE_CURRENT);
110
111 state.pauseTiming();
112 previousPendingIntent.cancel();
113 pendingIntent.cancel();
114 state.resumeTiming();
115 }
116 }
117
118 /**
119 * Benchmark time to cancel a PendingIntent.
120 */
121 @Test
122 public void cancel() {
123 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
124 while (state.keepRunning()) {
125 state.pauseTiming();
126 final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,
127 mIntent, 0);
128 state.resumeTiming();
129
130 pendingIntent.cancel();
131 }
132 }
133}
134