Tony Mak | 751afc9 | 2019-04-02 18:23:54 +0100 | [diff] [blame] | 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; |
Joanne Chung | ff33c16 | 2020-02-05 16:50:17 +0800 | [diff] [blame^] | 21 | import android.provider.DeviceConfig; |
Tony Mak | 751afc9 | 2019-04-02 18:23:54 +0100 | [diff] [blame] | 22 | |
| 23 | import androidx.test.InstrumentationRegistry; |
| 24 | import androidx.test.filters.LargeTest; |
| 25 | |
| 26 | import org.junit.After; |
Joanne Chung | ff33c16 | 2020-02-05 16:50:17 +0800 | [diff] [blame^] | 27 | import org.junit.AfterClass; |
| 28 | import org.junit.Before; |
| 29 | import org.junit.BeforeClass; |
Tony Mak | 751afc9 | 2019-04-02 18:23:54 +0100 | [diff] [blame] | 30 | import org.junit.Rule; |
| 31 | import org.junit.Test; |
| 32 | |
| 33 | @LargeTest |
| 34 | public class TextClassificationManagerPerfTest { |
Joanne Chung | ff33c16 | 2020-02-05 16:50:17 +0800 | [diff] [blame^] | 35 | private static final String WRITE_DEVICE_CONFIG_PERMISSION = |
| 36 | "android.permission.WRITE_DEVICE_CONFIG"; |
Tony Mak | 751afc9 | 2019-04-02 18:23:54 +0100 | [diff] [blame] | 37 | |
| 38 | @Rule |
| 39 | public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); |
| 40 | |
Joanne Chung | ff33c16 | 2020-02-05 16:50:17 +0800 | [diff] [blame^] | 41 | 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 Mak | 751afc9 | 2019-04-02 18:23:54 +0100 | [diff] [blame] | 65 | @After |
| 66 | public void tearDown() { |
Joanne Chung | ff33c16 | 2020-02-05 16:50:17 +0800 | [diff] [blame^] | 67 | // Restores config original value. |
| 68 | enableSystemTextclassifier(mOriginalSystemTextclassifierStatus); |
Tony Mak | 751afc9 | 2019-04-02 18:23:54 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void testGetTextClassifier_systemTextClassifierDisabled() { |
| 73 | Context context = InstrumentationRegistry.getTargetContext(); |
Joanne Chung | ff33c16 | 2020-02-05 16:50:17 +0800 | [diff] [blame^] | 74 | enableSystemTextclassifier(String.valueOf(false)); |
Tony Mak | 751afc9 | 2019-04-02 18:23:54 +0100 | [diff] [blame] | 75 | TextClassificationManager textClassificationManager = |
| 76 | context.getSystemService(TextClassificationManager.class); |
| 77 | BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 78 | while (state.keepRunning()) { |
| 79 | textClassificationManager.getTextClassifier(); |
Abodunrinwa Toki | 0634af3 | 2019-04-04 13:10:59 +0100 | [diff] [blame] | 80 | textClassificationManager.invalidateForTesting(); |
Tony Mak | 751afc9 | 2019-04-02 18:23:54 +0100 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testGetTextClassifier_systemTextClassifierEnabled() { |
| 86 | Context context = InstrumentationRegistry.getTargetContext(); |
Joanne Chung | ff33c16 | 2020-02-05 16:50:17 +0800 | [diff] [blame^] | 87 | enableSystemTextclassifier(String.valueOf(true)); |
Tony Mak | 751afc9 | 2019-04-02 18:23:54 +0100 | [diff] [blame] | 88 | TextClassificationManager textClassificationManager = |
| 89 | context.getSystemService(TextClassificationManager.class); |
| 90 | BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 91 | while (state.keepRunning()) { |
| 92 | textClassificationManager.getTextClassifier(); |
Abodunrinwa Toki | 0634af3 | 2019-04-04 13:10:59 +0100 | [diff] [blame] | 93 | textClassificationManager.invalidateForTesting(); |
Tony Mak | 751afc9 | 2019-04-02 18:23:54 +0100 | [diff] [blame] | 94 | } |
| 95 | } |
Joanne Chung | ff33c16 | 2020-02-05 16:50:17 +0800 | [diff] [blame^] | 96 | |
| 97 | private void enableSystemTextclassifier(String enabled) { |
| 98 | DeviceConfig.setProperty(DeviceConfig.NAMESPACE_TEXTCLASSIFIER, |
| 99 | "system_textclassifier_enabled", enabled, /* makeDefault */ false); |
| 100 | } |
Tony Mak | 751afc9 | 2019-04-02 18:23:54 +0100 | [diff] [blame] | 101 | } |