| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 127 |
someService.readDisk(10); |
| 128 |
} |
| 129 |
context.unbindService(connection); |
| 130 |
} |
| 131 |
} |