1 /* 2 * Copyright (C) 2018 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.autofill; 18 19 import static com.android.compatibility.common.util.ShellUtils.runShellCommand; 20 21 import static org.junit.Assert.assertTrue; 22 23 import android.os.Looper; 24 import android.perftests.utils.PerfStatusReporter; 25 import android.perftests.utils.PerfTestActivity; 26 import android.perftests.utils.SettingsStateKeeperRule; 27 import android.provider.Settings; 28 import android.util.Log; 29 30 import androidx.test.InstrumentationRegistry; 31 import androidx.test.rule.ActivityTestRule; 32 33 import org.junit.AfterClass; 34 import org.junit.Before; 35 import org.junit.BeforeClass; 36 import org.junit.ClassRule; 37 import org.junit.Rule; 38 import org.junit.rules.RuleChain; 39 40 /** 41 * Base class for all autofill tests. 42 */ 43 public abstract class AbstractAutofillPerfTestCase { 44 45 private static final String TAG = "AbstractAutofillPerfTestCase"; 46 47 @ClassRule 48 public static final SettingsStateKeeperRule mServiceSettingsKeeper = 49 new SettingsStateKeeperRule(InstrumentationRegistry.getTargetContext(), 50 Settings.Secure.AUTOFILL_SERVICE); 51 52 protected final AutofillTestWatcher mTestWatcher = MyAutofillService.getTestWatcher(); 53 protected ActivityTestRule<PerfTestActivity> mActivityRule = 54 new ActivityTestRule<>(PerfTestActivity.class); 55 protected PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); 56 57 @Rule 58 public final RuleChain mAllRules = RuleChain 59 .outerRule(mTestWatcher) 60 .around(mPerfStatusReporter) 61 .around(mActivityRule); 62 63 private final int mLayoutId; 64 AbstractAutofillPerfTestCase(int layoutId)65 protected AbstractAutofillPerfTestCase(int layoutId) { 66 mLayoutId = layoutId; 67 } 68 69 @BeforeClass disableDefaultAugmentedService()70 public static void disableDefaultAugmentedService() { 71 Log.v(TAG, "@BeforeClass: disableDefaultAugmentedService()"); 72 setDefaultAugmentedAutofillServiceEnabled(false); 73 } 74 75 @AfterClass enableDefaultAugmentedService()76 public static void enableDefaultAugmentedService() { 77 Log.v(TAG, "@AfterClass: enableDefaultAugmentedService()"); 78 setDefaultAugmentedAutofillServiceEnabled(true); 79 } 80 81 /** 82 * Enables / disables the default augmented autofill service. 83 */ setDefaultAugmentedAutofillServiceEnabled(boolean enabled)84 private static void setDefaultAugmentedAutofillServiceEnabled(boolean enabled) { 85 Log.d(TAG, "setDefaultAugmentedAutofillServiceEnabled(): " + enabled); 86 runShellCommand("cmd autofill set default-augmented-service-enabled 0 %s", 87 Boolean.toString(enabled)); 88 } 89 90 /** 91 * Prepares the activity so that by the time the test is run it has reference to its fields. 92 */ 93 @Before prepareActivity()94 public void prepareActivity() throws Throwable { 95 mActivityRule.runOnUiThread(() -> { 96 assertTrue("We should be running on the main thread", 97 Looper.getMainLooper().getThread() == Thread.currentThread()); 98 assertTrue("We should be running on the main thread", 99 Looper.myLooper() == Looper.getMainLooper()); 100 PerfTestActivity activity = mActivityRule.getActivity(); 101 activity.setContentView(mLayoutId); 102 onCreate(activity); 103 }); 104 } 105 106 /** 107 * Initializes the {@link PerfTestActivity} after it was launched. 108 */ onCreate(PerfTestActivity activity)109 protected abstract void onCreate(PerfTestActivity activity); 110 } 111