Add getTextClassifier perf test

This helps us to evaluate the fix.

BUG: 129695635

Test: frameworks/base/apct-tests/perftests/textclassifier/run.sh

Change-Id: I7d2d019faecb3721c044a43e24d86d98a08be64b
diff --git a/apct-tests/perftests/textclassifier/run.sh b/apct-tests/perftests/textclassifier/run.sh
index c6782d1..8660d26 100755
--- a/apct-tests/perftests/textclassifier/run.sh
+++ b/apct-tests/perftests/textclassifier/run.sh
@@ -1,4 +1,8 @@
 set -e
-make TextClassifierPerfTests
+make TextClassifierPerfTests perf-setup.sh
+adb install ${OUT}/testcases/TextClassifierPerfTests/arm64/TextClassifierPerfTests.apk
 adb shell cmd package compile -m speed -f com.android.perftests.textclassifier
-adb shell am instrument -w -e class android.view.textclassifier.TextClassifierPerfTest com.android.perftests.textclassifier/androidx.test.runner.AndroidJUnitRunner
+adb push ${OUT}/obj/EXECUTABLES/perf-setup.sh_intermediates/perf-setup.sh /data/local/tmp/
+adb shell chmod +x /data/local/tmp/perf-setup.sh
+adb shell /data/local/tmp/perf-setup.sh
+adb shell am instrument -w -e package android.view.textclassifier com.android.perftests.textclassifier/androidx.test.runner.AndroidJUnitRunner
\ No newline at end of file
diff --git a/apct-tests/perftests/textclassifier/src/android/view/textclassifier/TextClassificationManagerPerfTest.java b/apct-tests/perftests/textclassifier/src/android/view/textclassifier/TextClassificationManagerPerfTest.java
new file mode 100644
index 0000000..c149195
--- /dev/null
+++ b/apct-tests/perftests/textclassifier/src/android/view/textclassifier/TextClassificationManagerPerfTest.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package android.view.textclassifier;
+
+import android.content.Context;
+import android.perftests.utils.BenchmarkState;
+import android.perftests.utils.PerfStatusReporter;
+import android.perftests.utils.SettingsHelper;
+import android.provider.Settings;
+
+import androidx.test.InstrumentationRegistry;
+import androidx.test.filters.LargeTest;
+
+import org.junit.After;
+import org.junit.Rule;
+import org.junit.Test;
+
+@LargeTest
+public class TextClassificationManagerPerfTest {
+
+    @Rule
+    public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
+
+    @After
+    public void tearDown() {
+        SettingsHelper.delete(
+                SettingsHelper.NAMESPACE_GLOBAL, Settings.Global.TEXT_CLASSIFIER_CONSTANTS);
+    }
+
+    @Test
+    public void testGetTextClassifier_systemTextClassifierDisabled() {
+        Context context = InstrumentationRegistry.getTargetContext();
+        SettingsHelper.set(
+                SettingsHelper.NAMESPACE_GLOBAL,
+                Settings.Global.TEXT_CLASSIFIER_CONSTANTS,
+                "system_textclassifier_enabled=false");
+        TextClassificationManager textClassificationManager =
+                context.getSystemService(TextClassificationManager.class);
+        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
+        while (state.keepRunning()) {
+            textClassificationManager.getTextClassifier();
+            textClassificationManager.invalidate();
+        }
+    }
+
+    @Test
+    public void testGetTextClassifier_systemTextClassifierEnabled() {
+        Context context = InstrumentationRegistry.getTargetContext();
+        SettingsHelper.set(
+                SettingsHelper.NAMESPACE_GLOBAL,
+                Settings.Global.TEXT_CLASSIFIER_CONSTANTS,
+                "system_textclassifier_enabled=true");
+        TextClassificationManager textClassificationManager =
+                context.getSystemService(TextClassificationManager.class);
+        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
+        while (state.keepRunning()) {
+            textClassificationManager.getTextClassifier();
+            textClassificationManager.invalidate();
+        }
+    }
+}
diff --git a/apct-tests/perftests/utils/src/android/perftests/utils/SettingsHelper.java b/apct-tests/perftests/utils/src/android/perftests/utils/SettingsHelper.java
index d7d1d6b..9835886 100644
--- a/apct-tests/perftests/utils/src/android/perftests/utils/SettingsHelper.java
+++ b/apct-tests/perftests/utils/src/android/perftests/utils/SettingsHelper.java
@@ -18,19 +18,19 @@
 
 import android.content.Context;
 import android.provider.Settings;
-import android.text.TextUtils;
-
-import java.util.Objects;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 
+import java.util.Objects;
+
 /**
  * Provides utilities to interact with the device's {@link Settings}.
  */
 public final class SettingsHelper {
 
     public static final String NAMESPACE_SECURE = "secure";
+    public static final String NAMESPACE_GLOBAL = "global";
 
     private static int DEFAULT_TIMEOUT_MS = 5000;
 
diff --git a/core/java/android/view/textclassifier/TextClassificationManager.java b/core/java/android/view/textclassifier/TextClassificationManager.java
index fa898c3..417c6e7 100644
--- a/core/java/android/view/textclassifier/TextClassificationManager.java
+++ b/core/java/android/view/textclassifier/TextClassificationManager.java
@@ -30,6 +30,7 @@
 import android.view.textclassifier.TextClassifier.TextClassifierType;
 
 import com.android.internal.annotations.GuardedBy;
+import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.internal.util.Preconditions;
 
@@ -247,7 +248,9 @@
                 && TextClassifierService.getServiceComponentName(mContext) != null;
     }
 
-    private void invalidate() {
+    /** @hide */
+    @VisibleForTesting
+    public void invalidate() {
         synchronized (mLock) {
             mSettings = null;
             mLocalTextClassifier = null;