Debian Codegraph @

apct-tests/perftests/core/src/android/os/StrictModeTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2017 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
17 package android.os;
18
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.ServiceConnection;
23 import android.net.Uri;
24 import android.perftests.utils.BenchmarkState;
25 import android.perftests.utils.PerfStatusReporter;
26
27 import androidx.test.InstrumentationRegistry;
28 import androidx.test.filters.LargeTest;
29 import androidx.test.runner.AndroidJUnit4;
30
31 import com.google.common.util.concurrent.SettableFuture;
32
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36
37 import java.io.File;
38 import java.io.IOException;
39 import java.util.concurrent.ExecutionException;
40
41 @RunWith(AndroidJUnit4.class)
42 @LargeTest
43 public class StrictModeTest {
44 @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
45
46 @Test
47 public void timeVmViolation() {
48 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
49 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());
50 causeVmViolations(state);
51 }
52
53 @Test
54 public void timeVmViolationNoStrictMode() {
55 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
56 causeVmViolations(state);
57 }
58
59 private static void causeVmViolations(BenchmarkState state) {
60 Intent intent = new Intent(Intent.ACTION_VIEW);
61 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
62 intent.setDataAndType(Uri.parse("content://com.example/foobar"), "image/jpeg");
63 final Context context = InstrumentationRegistry.getTargetContext();
64 while (state.keepRunning()) {
65 context.startActivity(intent);
66 }
67 }
68
69 @Test
70 public void timeThreadViolation() throws IOException {
71 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
72 StrictMode.setThreadPolicy(
73 new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
74 causeThreadViolations(state);
75 }
76
77 @Test
78 public void timeThreadViolationNoStrictMode() throws IOException {
79 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
80 causeThreadViolations(state);
81 }
82
83 private static void causeThreadViolations(BenchmarkState state) throws IOException {
84 final File test = File.createTempFile("foo", "bar");
85 while (state.keepRunning()) {
86 test.exists();
87 }
88 test.delete();
89 }
90
91 @Test
92 public void timeCrossBinderThreadViolation() throws Exception {
93 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
94 StrictMode.setThreadPolicy(
95 new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
96 causeCrossProcessThreadViolations(state);
97 }
98
99 @Test
100 public void timeCrossBinderThreadViolationNoStrictMode() throws Exception {
101 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
102 causeCrossProcessThreadViolations(state);
103 }
104
105 private static void causeCrossProcessThreadViolations(BenchmarkState state)
106 throws ExecutionException, InterruptedException, RemoteException {
107 final Context context = InstrumentationRegistry.getTargetContext();
108
109 SettableFuture<IBinder> binder = SettableFuture.create();
110 ServiceConnection connection =
111 new ServiceConnection() {
112 @Override
113 public void onServiceConnected(ComponentName className, IBinder service) {
114 binder.set(service);
115 }
116
117 @Override
118 public void onServiceDisconnected(ComponentName arg0) {
119 binder.set(null);
120 }
121 };
122 context.bindService(
123 new Intent(context, SomeService.class), connection, Context.BIND_AUTO_CREATE);
124 ISomeService someService = ISomeService.Stub.asInterface(binder.get());
125 while (state.keepRunning()) {
126 // Violate strictmode heavily.
127 someService.readDisk(10);
128 }
129 context.unbindService(connection);
130 }
131 }