xref: /aosp_15_r20/cts/hostsidetests/monkey/src/com/android/cts/monkey/PackageTest.java (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1 /*
2  * Copyright (C) 2012 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 
17 package com.android.cts.monkey;
18 
19 import com.android.tradefed.device.CollectingOutputReceiver;
20 
21 import java.util.concurrent.TimeUnit;
22 import java.util.regex.Pattern;
23 
24 public class PackageTest extends AbstractMonkeyTest {
25 
26     private static final long MAX_TIMEOUT = 5 * 60 * 1000; // 5 min
27     private static final int MAX_ERROR_LENGTH = 256;
28     private static final Pattern ALLOW_MONKEY =
29             Pattern.compile("^.*Allowing.*cmp=com\\.android\\.cts\\.monkey/\\.MonkeyActivity.*$",
30                     Pattern.MULTILINE);
31 
32     private static final Pattern ALLOW_CHIMP =
33             Pattern.compile("^.*Allowing.*cmp=com\\.android\\.cts\\.monkey2/\\.ChimpActivity.*$",
34                     Pattern.MULTILINE);
35 
testSinglePackage()36     public void testSinglePackage() throws Exception {
37         String cmd = MONKEY_CMD + " -v -p " + PKGS[0] + " 5000";
38         CollectingOutputReceiver receiver = new CollectingOutputReceiver();
39         try {
40             mDevice.executeShellCommand(cmd, receiver, MAX_TIMEOUT, TimeUnit.MILLISECONDS, 0);
41             String out = receiver.getOutput();
42             String error = truncateError(out);
43             assertTrue("Monkey not found in: " + error, ALLOW_MONKEY.matcher(out).find());
44             assertFalse("Chimp found in: " + error, ALLOW_CHIMP.matcher(out).find());
45         } finally {
46             receiver.cancel();
47             receiver.clearBuffer();
48             receiver = null;
49         }
50 
51         String cmd2 = MONKEY_CMD + " -v -p " + PKGS[1] + " 5000";
52         CollectingOutputReceiver receiver2 = new CollectingOutputReceiver();
53         try {
54             mDevice.executeShellCommand(cmd2, receiver2, MAX_TIMEOUT, TimeUnit.MILLISECONDS, 0);
55             String out = receiver2.getOutput();
56             String error = truncateError(out);
57             assertFalse("Monkey found in: " + error, ALLOW_MONKEY.matcher(out).find());
58             assertTrue("Chimp not found in: " + error, ALLOW_CHIMP.matcher(out).find());
59         } finally {
60             receiver2.cancel();
61             receiver2.clearBuffer();
62             receiver2 = null;
63         }
64     }
65 
testMultiplePackages()66     public void testMultiplePackages() throws Exception {
67         String cmd = MONKEY_CMD + " -v -p " + PKGS[0] + " -p " + PKGS[1] + " 5000";
68         CollectingOutputReceiver receiver = new CollectingOutputReceiver();
69         try {
70             mDevice.executeShellCommand(cmd, receiver, MAX_TIMEOUT, TimeUnit.MILLISECONDS, 0);
71             String out = receiver.getOutput();
72             String error = truncateError(out);
73             assertTrue("Monkey not found in: " + error, ALLOW_MONKEY.matcher(out).find());
74             assertTrue("Chimp not found in: " + error, ALLOW_CHIMP.matcher(out).find());
75         } finally {
76             receiver.cancel();
77             receiver.clearBuffer();
78             receiver = null;
79         }
80     }
81 
truncateError(String input)82     private static final String truncateError(String input) {
83         return input.substring(0, Math.min(input.length(), MAX_ERROR_LENGTH));
84     }
85 }
86