blob: f61ea85492364adbe08bb0455ab1830c2695e228 [file] [log] [blame]
Tony Mak751afc92019-04-02 18:23:54 +01001/*
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 */
16package android.view.textclassifier;
17
18import android.content.Context;
19import android.perftests.utils.BenchmarkState;
20import android.perftests.utils.PerfStatusReporter;
Joanne Chungff33c162020-02-05 16:50:17 +080021import android.provider.DeviceConfig;
Tony Mak751afc92019-04-02 18:23:54 +010022
23import androidx.test.InstrumentationRegistry;
24import androidx.test.filters.LargeTest;
25
26import org.junit.After;
Joanne Chungff33c162020-02-05 16:50:17 +080027import org.junit.AfterClass;
28import org.junit.Before;
29import org.junit.BeforeClass;
Tony Mak751afc92019-04-02 18:23:54 +010030import org.junit.Rule;
31import org.junit.Test;
32
33@LargeTest
34public class TextClassificationManagerPerfTest {
Joanne Chungff33c162020-02-05 16:50:17 +080035 private static final String WRITE_DEVICE_CONFIG_PERMISSION =
36 "android.permission.WRITE_DEVICE_CONFIG";
Tony Mak751afc92019-04-02 18:23:54 +010037
38 @Rule
39 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
40
Joanne Chungff33c162020-02-05 16:50:17 +080041 private String mOriginalSystemTextclassifierStatus;
42
43 @BeforeClass
44 public static void setUpClass() {
45 InstrumentationRegistry.getInstrumentation().getUiAutomation()
46 .adoptShellPermissionIdentity(
47 WRITE_DEVICE_CONFIG_PERMISSION);
48 }
49
50 @AfterClass
51 public static void tearDownClass() {
52 InstrumentationRegistry
53 .getInstrumentation()
54 .getUiAutomation()
55 .dropShellPermissionIdentity();
56 }
57
58 @Before
59 public void setUp() {
60 // Saves config original value.
61 mOriginalSystemTextclassifierStatus = DeviceConfig.getProperty(
62 DeviceConfig.NAMESPACE_TEXTCLASSIFIER, "system_textclassifier_enabled");
63 }
64
Tony Mak751afc92019-04-02 18:23:54 +010065 @After
66 public void tearDown() {
Joanne Chungff33c162020-02-05 16:50:17 +080067 // Restores config original value.
68 enableSystemTextclassifier(mOriginalSystemTextclassifierStatus);
Tony Mak751afc92019-04-02 18:23:54 +010069 }
70
71 @Test
72 public void testGetTextClassifier_systemTextClassifierDisabled() {
73 Context context = InstrumentationRegistry.getTargetContext();
Joanne Chungff33c162020-02-05 16:50:17 +080074 enableSystemTextclassifier(String.valueOf(false));
Tony Mak751afc92019-04-02 18:23:54 +010075 TextClassificationManager textClassificationManager =
76 context.getSystemService(TextClassificationManager.class);
77 BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
78 while (state.keepRunning()) {
79 textClassificationManager.getTextClassifier();
Abodunrinwa Toki0634af32019-04-04 13:10:59 +010080 textClassificationManager.invalidateForTesting();
Tony Mak751afc92019-04-02 18:23:54 +010081 }
82 }
83
84 @Test
85 public void testGetTextClassifier_systemTextClassifierEnabled() {
86 Context context = InstrumentationRegistry.getTargetContext();
Joanne Chungff33c162020-02-05 16:50:17 +080087 enableSystemTextclassifier(String.valueOf(true));
Tony Mak751afc92019-04-02 18:23:54 +010088 TextClassificationManager textClassificationManager =
89 context.getSystemService(TextClassificationManager.class);
90 BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
91 while (state.keepRunning()) {
92 textClassificationManager.getTextClassifier();
Abodunrinwa Toki0634af32019-04-04 13:10:59 +010093 textClassificationManager.invalidateForTesting();
Tony Mak751afc92019-04-02 18:23:54 +010094 }
95 }
Joanne Chungff33c162020-02-05 16:50:17 +080096
97 private void enableSystemTextclassifier(String enabled) {
98 DeviceConfig.setProperty(DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
99 "system_textclassifier_enabled", enabled, /* makeDefault */ false);
100 }
Tony Mak751afc92019-04-02 18:23:54 +0100101}