1 /*
2  * Copyright (C) 2020 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;
18 
19 import static com.android.cts.mockime.ImeEventStreamTestUtils.editorMatcher;
20 import static com.android.cts.mockime.ImeEventStreamTestUtils.expectCommand;
21 import static com.android.cts.mockime.ImeEventStreamTestUtils.expectEvent;
22 
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
25 
26 import android.content.pm.ApplicationInfo;
27 import android.content.pm.PackageManager;
28 import android.os.SystemClock;
29 import android.platform.test.annotations.AppModeFull;
30 import android.platform.test.annotations.AppModeInstant;
31 import android.view.inputmethod.cts.util.EndToEndImeTestBase;
32 import android.view.inputmethod.cts.util.MockTestActivityUtil;
33 import android.view.inputmethod.cts.util.UnlockScreenRule;
34 
35 import androidx.test.filters.MediumTest;
36 import androidx.test.platform.app.InstrumentationRegistry;
37 import androidx.test.runner.AndroidJUnit4;
38 
39 import com.android.cts.mockime.ImeCommand;
40 import com.android.cts.mockime.ImeEvent;
41 import com.android.cts.mockime.ImeEventStream;
42 import com.android.cts.mockime.ImeSettings;
43 import com.android.cts.mockime.MockImeSession;
44 
45 import org.junit.Rule;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 
49 import java.util.Map;
50 import java.util.concurrent.TimeUnit;
51 
52 @MediumTest
53 @RunWith(AndroidJUnit4.class)
54 public final class PackageVisibilityTest extends EndToEndImeTestBase {
55     static final long TIMEOUT = TimeUnit.SECONDS.toMillis(5);
56 
57     @Rule
58     public final UnlockScreenRule mUnlockScreenRule = new UnlockScreenRule();
59 
60     private static final String TEST_MARKER_PREFIX =
61             "android.view.inputmethod.cts.PackageVisibilityTest";
62 
getTestMarker()63     private static String getTestMarker() {
64         return TEST_MARKER_PREFIX + "/"  + SystemClock.elapsedRealtimeNanos();
65     }
66 
67     @AppModeFull
68     @Test
testTargetPackageIsVisibleFromImeFull()69     public void testTargetPackageIsVisibleFromImeFull() throws Exception {
70         testTargetPackageIsVisibleFromIme(false /* instant */);
71     }
72 
73     @AppModeInstant
74     @Test
testTargetPackageIsVisibleFromImeInstant()75     public void testTargetPackageIsVisibleFromImeInstant() throws Exception {
76         testTargetPackageIsVisibleFromIme(true /* instant */);
77     }
78 
testTargetPackageIsVisibleFromIme(boolean instant)79     private void testTargetPackageIsVisibleFromIme(boolean instant) throws Exception {
80         try (MockImeSession imeSession = MockImeSession.create(
81                 InstrumentationRegistry.getInstrumentation().getContext(),
82                 InstrumentationRegistry.getInstrumentation().getUiAutomation(),
83                 new ImeSettings.Builder())) {
84             final ImeEventStream stream = imeSession.openEventStream();
85 
86             final String marker = getTestMarker();
87             try (AutoCloseable closeable = MockTestActivityUtil.launchSync(instant,
88                     TIMEOUT, Map.of(MockTestActivityUtil.EXTRA_KEY_PRIVATE_IME_OPTIONS, marker))) {
89                 expectEvent(stream, editorMatcher("onStartInput", marker), TIMEOUT);
90 
91                 final ImeCommand command = imeSession.callGetApplicationInfo(
92                         MockTestActivityUtil.getPackageName(), PackageManager.GET_META_DATA);
93                 final ImeEvent event = expectCommand(stream, command, TIMEOUT);
94 
95                 if (event.isNullReturnValue()) {
96                     fail("getApplicationInfo() returned null.");
97                 }
98                 if (event.isExceptionReturnValue()) {
99                     final Exception exception = event.getReturnExceptionValue();
100                     fail(exception.toString());
101                 }
102                 final ApplicationInfo applicationInfoFromIme = event.getReturnParcelableValue();
103                 assertEquals(MockTestActivityUtil.getPackageName(),
104                         applicationInfoFromIme.packageName);
105             }
106         }
107     }
108 }
109