• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package android.view.textclassifier;
17 
18 import android.content.Context;
19 import android.perftests.utils.BenchmarkState;
20 import android.perftests.utils.PerfStatusReporter;
21 import android.provider.DeviceConfig;
22 
23 import androidx.test.InstrumentationRegistry;
24 import androidx.test.filters.LargeTest;
25 
26 import org.junit.After;
27 import org.junit.AfterClass;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Rule;
31 import org.junit.Test;
32 
33 @LargeTest
34 public class TextClassificationManagerPerfTest {
35     private static final String WRITE_DEVICE_CONFIG_PERMISSION =
36             "android.permission.WRITE_DEVICE_CONFIG";
37 
38     @Rule
39     public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
40 
41     private String mOriginalSystemTextclassifierStatus;
42 
43     @BeforeClass
setUpClass()44     public static void setUpClass() {
45         InstrumentationRegistry.getInstrumentation().getUiAutomation()
46                 .adoptShellPermissionIdentity(
47                         WRITE_DEVICE_CONFIG_PERMISSION);
48     }
49 
50     @AfterClass
tearDownClass()51     public static void tearDownClass() {
52         InstrumentationRegistry
53                 .getInstrumentation()
54                 .getUiAutomation()
55                 .dropShellPermissionIdentity();
56     }
57 
58     @Before
setUp()59     public void setUp() {
60         // Saves config original value.
61         mOriginalSystemTextclassifierStatus = DeviceConfig.getProperty(
62                 DeviceConfig.NAMESPACE_TEXTCLASSIFIER, "system_textclassifier_enabled");
63     }
64 
65     @After
tearDown()66     public void tearDown() {
67         // Restores config original value.
68         enableSystemTextclassifier(mOriginalSystemTextclassifierStatus);
69     }
70 
71     @Test
testGetTextClassifier_systemTextClassifierDisabled()72     public void testGetTextClassifier_systemTextClassifierDisabled() {
73         Context context = InstrumentationRegistry.getTargetContext();
74         enableSystemTextclassifier(String.valueOf(false));
75         TextClassificationManager textClassificationManager =
76                 context.getSystemService(TextClassificationManager.class);
77         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
78         while (state.keepRunning()) {
79             textClassificationManager.getTextClassifier();
80         }
81     }
82 
83     @Test
testGetTextClassifier_systemTextClassifierEnabled()84     public void testGetTextClassifier_systemTextClassifierEnabled() {
85         Context context = InstrumentationRegistry.getTargetContext();
86         enableSystemTextclassifier(String.valueOf(true));
87         TextClassificationManager textClassificationManager =
88                 context.getSystemService(TextClassificationManager.class);
89         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
90         while (state.keepRunning()) {
91             textClassificationManager.getTextClassifier();
92         }
93     }
94 
enableSystemTextclassifier(String enabled)95     private void enableSystemTextclassifier(String enabled) {
96         DeviceConfig.setProperty(DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
97                 "system_textclassifier_enabled", enabled, /* makeDefault */ false);
98     }
99 }
100