1 /*
2  * Copyright (C) 2013 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.inputmethod.latin;
18 
19 import android.content.pm.ApplicationInfo;
20 import android.content.pm.PackageManager;
21 import android.os.Build.VERSION_CODES;
22 import android.view.inputmethod.EditorInfo;
23 
24 import androidx.test.filters.LargeTest;
25 
26 import com.android.inputmethod.latin.settings.Settings;
27 
28 @LargeTest
29 public class AppWorkaroundsTests extends InputTestsBase {
30     String packageNameOfAppBeforeJellyBean;
31     String packageNameOfAppAfterJellyBean;
32 
33     @Override
setUp()34     protected void setUp() throws Exception {
35         // NOTE: this will fail if there is no app installed that targets an SDK
36         // before Jelly Bean. For the moment, it's fine.
37         final PackageManager pm = getContext().getPackageManager();
38         for (ApplicationInfo ai : pm.getInstalledApplications(0 /* flags */)) {
39             if (ai.targetSdkVersion < VERSION_CODES.JELLY_BEAN) {
40                 packageNameOfAppBeforeJellyBean = ai.packageName;
41             } else {
42                 packageNameOfAppAfterJellyBean = ai.packageName;
43             }
44         }
45         super.setUp();
46     }
47 
48     // We want to test if the app package info is correctly retrieved by LatinIME. Since it
49     // asks this information to the package manager from the package name, and that it takes
50     // the package name from the EditorInfo, all we have to do it put the correct package
51     // name in the editor info.
52     // To this end, our base class InputTestsBase offers a hook for us to touch the EditorInfo.
53     // We override this hook to write the package name that we need.
54     @Override
enrichEditorInfo(final EditorInfo ei)55     protected EditorInfo enrichEditorInfo(final EditorInfo ei) {
56         if ("testBeforeJellyBeanTrue".equals(getName())) {
57             ei.packageName = packageNameOfAppBeforeJellyBean;
58         } else if ("testBeforeJellyBeanFalse".equals(getName())) {
59             ei.packageName = packageNameOfAppAfterJellyBean;
60         }
61         return ei;
62     }
63 
testBeforeJellyBeanTrue()64     public void testBeforeJellyBeanTrue() {
65         assertTrue("Couldn't successfully detect this app targets < Jelly Bean (package is "
66                 + packageNameOfAppBeforeJellyBean + ")",
67                 Settings.getInstance().getCurrent().isBeforeJellyBean());
68     }
69 
testBeforeJellyBeanFalse()70     public void testBeforeJellyBeanFalse() {
71         assertFalse("Couldn't successfully detect this app targets >= Jelly Bean (package is "
72                 + packageNameOfAppAfterJellyBean + ")",
73                 Settings.getInstance().getCurrent().isBeforeJellyBean());
74     }
75 }