1 /*
2  * Copyright (C) 2017 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 android.view.inputmethod.cts.util;
18 
19 import static org.junit.Assert.fail;
20 import static org.junit.Assume.assumeFalse;
21 import static org.junit.Assume.assumeTrue;
22 
23 import android.content.Intent;
24 import android.content.pm.PackageManager;
25 import android.platform.test.annotations.AppModeFull;
26 import android.platform.test.annotations.AppModeInstant;
27 
28 import androidx.test.platform.app.InstrumentationRegistry;
29 
30 import org.junit.Before;
31 import org.junit.Rule;
32 import org.junit.rules.TestName;
33 
34 import java.lang.reflect.Method;
35 
36 public class EndToEndImeTestBase {
37     @Rule
38     public TestName mTestName = new TestName();
39 
40     /**
41      * Our own safeguard in case "atest" command is regressed and start running tests with
42      * {@link AppModeInstant} even when {@code --instant} option is not specified.
43      *
44      * <p>Unfortunately this scenario had regressed at least 3 times.  That's why we also check
45      * this in our side.  See Bug 158617529, Bug 187211725 and Bug 187222205 for examples.</p>
46      */
47     @Before
verifyAppModeConsistency()48     public void verifyAppModeConsistency() {
49         final Class<?> thisClass = this.getClass();
50         final String testMethodName = mTestName.getMethodName();
51         final String fullTestMethodName = thisClass.getSimpleName() + "#" + testMethodName;
52 
53         final Method testMethod;
54         try {
55             testMethod = thisClass.getMethod(testMethodName);
56         } catch (NoSuchMethodException e) {
57             throw new IllegalStateException("Failed to find " + fullTestMethodName, e);
58         }
59 
60         final boolean hasAppModeFull = testMethod.getAnnotation(AppModeFull.class) != null;
61         final boolean hasAppModeInstant = testMethod.getAnnotation(AppModeInstant.class) != null;
62 
63         if (hasAppModeFull && hasAppModeInstant) {
64             fail("Both @AppModeFull and @AppModeInstant are found in " + fullTestMethodName
65                     + ", which does not make sense. "
66                     + "Remove both to make it clear that this test is app-mode agnostic, "
67                     + "or specify one of them otherwise.");
68         }
69 
70         // We want to explicitly check this condition in case tests are executed with atest
71         // command.  See Bug 158617529 for details.
72         if (hasAppModeFull) {
73             assumeFalse("This test should run under and only under the full app mode.",
74                     InstrumentationRegistry.getInstrumentation().getTargetContext()
75                             .getPackageManager().isInstantApp());
76         }
77         if (hasAppModeInstant) {
78             assumeTrue("This test should run under and only under the instant app mode.",
79                     InstrumentationRegistry.getInstrumentation().getTargetContext()
80                             .getPackageManager().isInstantApp());
81         }
82     }
83 
84     @Before
showStateInitializeActivity()85     public void showStateInitializeActivity() {
86         // TODO(b/37502066): Move this back to @BeforeClass once b/37502066 is fixed.
87         assumeTrue("MockIme cannot be used for devices that do not support installable IMEs",
88                 InstrumentationRegistry.getInstrumentation().getContext().getPackageManager()
89                         .hasSystemFeature(PackageManager.FEATURE_INPUT_METHODS));
90 
91         final Intent intent = new Intent()
92                 .setAction(Intent.ACTION_MAIN)
93                 .setClass(InstrumentationRegistry.getInstrumentation().getTargetContext(),
94                         StateInitializeActivity.class)
95                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
96                 .addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
97                 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
98         InstrumentationRegistry.getInstrumentation().startActivitySync(intent);
99     }
100 }
101