Merge "Remove duplicate search result when search "autofill service"" into rvc-dev
diff --git a/src/com/android/settings/network/TetherProvisioningActivity.java b/src/com/android/settings/network/TetherProvisioningActivity.java
index 230cfe0..09efe48 100644
--- a/src/com/android/settings/network/TetherProvisioningActivity.java
+++ b/src/com/android/settings/network/TetherProvisioningActivity.java
@@ -21,12 +21,16 @@
 import android.content.pm.PackageManager;
 import android.content.res.Resources;
 import android.net.ConnectivityManager;
+import android.net.TetheringConstants;
+import android.net.TetheringManager;
 import android.os.Bundle;
 import android.os.ResultReceiver;
 import android.os.UserHandle;
 import android.telephony.SubscriptionManager;
 import android.util.Log;
 
+import androidx.annotation.VisibleForTesting;
+
 import com.android.settings.Utils;
 
 /**
@@ -36,17 +40,18 @@
  * with {@link android.permission.TETHER_PRIVILEGED}.
  */
 public class TetherProvisioningActivity extends Activity {
-    private static final int PROVISION_REQUEST = 0;
     private static final String TAG = "TetherProvisioningAct";
     private static final String EXTRA_TETHER_TYPE = "TETHER_TYPE";
-    private static final String EXTRA_SUBID = "subId";
     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
     private ResultReceiver mResultReceiver;
+    @VisibleForTesting
+    static final int PROVISION_REQUEST = 0;
+    @VisibleForTesting
+    static final String EXTRA_SUBID = "subId";
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-
         mResultReceiver = (ResultReceiver)getIntent().getParcelableExtra(
                 ConnectivityManager.EXTRA_PROVISION_CALLBACK);
 
@@ -58,16 +63,22 @@
         final int subId = SubscriptionManager.getActiveDataSubscriptionId();
         if (tetherSubId != subId) {
             Log.e(TAG, "This Provisioning request is outdated, current subId: " + subId);
+            mResultReceiver.send(TetheringManager.TETHER_ERROR_PROVISIONING_FAILED, null);
+            finish();
             return;
         }
-        final Resources res = Utils.getResourcesForSubId(this, subId);
-        final String[] provisionApp = res.getStringArray(
-                com.android.internal.R.array.config_mobile_hotspot_provision_app);
-
+        String[] provisionApp = getIntent().getStringArrayExtra(
+                TetheringConstants.EXTRA_RUN_PROVISION);
+        if (provisionApp == null || provisionApp.length < 2) {
+            final Resources res = Utils.getResourcesForSubId(this, subId);
+            provisionApp = res.getStringArray(
+                    com.android.internal.R.array.config_mobile_hotspot_provision_app);
+        }
         final Intent intent = new Intent(Intent.ACTION_MAIN);
         intent.setClassName(provisionApp[0], provisionApp[1]);
         intent.putExtra(EXTRA_TETHER_TYPE, tetherType);
         intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subId);
+        intent.putExtra(ConnectivityManager.EXTRA_PROVISION_CALLBACK, mResultReceiver);
         if (DEBUG) {
             Log.d(TAG, "Starting provisioning app: " + provisionApp[0] + "." + provisionApp[1]);
         }
@@ -75,7 +86,7 @@
         if (getPackageManager().queryIntentActivities(intent,
                 PackageManager.MATCH_DEFAULT_ONLY).isEmpty()) {
             Log.e(TAG, "Provisioning app is configured, but not available.");
-            mResultReceiver.send(ConnectivityManager.TETHER_ERROR_PROVISION_FAILED, null);
+            mResultReceiver.send(TetheringManager.TETHER_ERROR_PROVISIONING_FAILED, null);
             finish();
             return;
         }
@@ -89,8 +100,8 @@
         if (requestCode == PROVISION_REQUEST) {
             if (DEBUG) Log.d(TAG, "Got result from app: " + resultCode);
             int result = resultCode == Activity.RESULT_OK ?
-                    ConnectivityManager.TETHER_ERROR_NO_ERROR :
-                    ConnectivityManager.TETHER_ERROR_PROVISION_FAILED;
+                    TetheringManager.TETHER_ERROR_NO_ERROR :
+                    TetheringManager.TETHER_ERROR_PROVISIONING_FAILED;
             mResultReceiver.send(result, null);
             finish();
         }
diff --git a/src/com/android/settings/panel/PanelFragment.java b/src/com/android/settings/panel/PanelFragment.java
index 9de4139..69d9e64c 100644
--- a/src/com/android/settings/panel/PanelFragment.java
+++ b/src/com/android/settings/panel/PanelFragment.java
@@ -200,15 +200,17 @@
         loadAllSlices();
 
         final IconCompat icon = mPanel.getIcon();
+        final CharSequence title = mPanel.getTitle();
         if (icon == null) {
             mTitleView.setVisibility(View.VISIBLE);
             mPanelHeader.setVisibility(View.GONE);
-            mTitleView.setText(mPanel.getTitle());
+            mTitleView.setText(title);
         } else {
             mTitleView.setVisibility(View.GONE);
             mPanelHeader.setVisibility(View.VISIBLE);
+            mPanelHeader.setAccessibilityPaneTitle(title);
             mTitleIcon.setImageIcon(icon.toIcon(getContext()));
-            mHeaderTitle.setText(mPanel.getTitle());
+            mHeaderTitle.setText(title);
             mHeaderSubtitle.setText(mPanel.getSubTitle());
             if (mPanel.getHeaderIconIntent() != null) {
                 mTitleIcon.setOnClickListener(getHeaderIconListener());
diff --git a/tests/unit/Android.bp b/tests/unit/Android.bp
index 43bf02c..583b46e 100644
--- a/tests/unit/Android.bp
+++ b/tests/unit/Android.bp
@@ -12,6 +12,7 @@
     ],
 
     static_libs: [
+        "androidx.test.core",
         "androidx.test.rules",
         "androidx.test.espresso.core",
         "androidx.test.espresso.contrib-nodeps",
diff --git a/tests/unit/AndroidTest.xml b/tests/unit/AndroidTest.xml
index b9321a3..9ce7657 100644
--- a/tests/unit/AndroidTest.xml
+++ b/tests/unit/AndroidTest.xml
@@ -19,6 +19,7 @@
     <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
         <option name="cleanup-apks" value="true" />
         <option name="test-file-name" value="SettingsUnitTests.apk" />
+        <option name="test-file-name" value="InstrumentedEntitlementApp.apk" />
     </target_preparer>
 
     <option name="test-tag" value="SettingsUnitTests" />
diff --git a/tests/unit/InstrumentedEntitlementApp/Android.bp b/tests/unit/InstrumentedEntitlementApp/Android.bp
new file mode 100644
index 0000000..dacd32a
--- /dev/null
+++ b/tests/unit/InstrumentedEntitlementApp/Android.bp
@@ -0,0 +1,19 @@
+// Copyright (C) 2020 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.
+
+android_test {
+    name: "InstrumentedEntitlementApp",
+    srcs: ["src/**/*.java"],
+    test_suites: ["device-tests"],
+}
diff --git a/tests/unit/InstrumentedEntitlementApp/AndroidManifest.xml b/tests/unit/InstrumentedEntitlementApp/AndroidManifest.xml
new file mode 100644
index 0000000..b123079
--- /dev/null
+++ b/tests/unit/InstrumentedEntitlementApp/AndroidManifest.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ * Copyright (C) 2020 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.
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="android.test.entitlement">
+
+    <application>
+        <activity android:name=".InstrumentedEntitlementActivity"
+                  android:label="InstrumentedEntitlementActivity"
+                  android:exported="true">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity>
+    </application>
+</manifest>
diff --git a/tests/unit/InstrumentedEntitlementApp/src/android/test/entitlement/InstrumentedEntitlementActivity.java b/tests/unit/InstrumentedEntitlementApp/src/android/test/entitlement/InstrumentedEntitlementActivity.java
new file mode 100644
index 0000000..2439746
--- /dev/null
+++ b/tests/unit/InstrumentedEntitlementApp/src/android/test/entitlement/InstrumentedEntitlementActivity.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2020 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.test.entitlement;
+
+import android.app.Activity;
+
+// Instrumented activity for TetherProvisioningActivityTest
+public class InstrumentedEntitlementActivity extends Activity {
+}
diff --git a/tests/unit/src/com/android/settings/network/TetherProvisioningActivityTest.java b/tests/unit/src/com/android/settings/network/TetherProvisioningActivityTest.java
new file mode 100644
index 0000000..6246d9f
--- /dev/null
+++ b/tests/unit/src/com/android/settings/network/TetherProvisioningActivityTest.java
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2020 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 com.android.settings.network;
+
+import static android.net.TetheringConstants.EXTRA_ADD_TETHER_TYPE;
+import static android.net.TetheringConstants.EXTRA_PROVISION_CALLBACK;
+import static android.net.TetheringConstants.EXTRA_RUN_PROVISION;
+import static android.net.TetheringManager.TETHERING_WIFI;
+
+import static com.android.settings.network.TetherProvisioningActivity.PROVISION_REQUEST;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import android.app.Activity;
+import android.content.Intent;
+import android.net.TetheringManager;
+import android.os.Bundle;
+import android.os.ResultReceiver;
+import android.provider.Settings;
+import android.telephony.SubscriptionManager;
+
+import androidx.lifecycle.Lifecycle;
+import androidx.test.core.app.ActivityScenario;
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+
+@RunWith(AndroidJUnit4.class)
+public class TetherProvisioningActivityTest {
+    private static class WrappedReceiver extends ResultReceiver {
+        private final CompletableFuture<Integer> mFuture = new CompletableFuture<>();
+
+        WrappedReceiver() {
+            super(null /* handler */);
+        }
+
+        @Override
+        protected void onReceiveResult(int resultCode, Bundle resultData) {
+            mFuture.complete(resultCode);
+        }
+
+        public int get() throws Exception {
+            return mFuture.get(10_000L, TimeUnit.MILLISECONDS);
+        }
+    }
+
+    @Test
+    public void testOnCreate_FinishWithNonActiveDataSubId() throws Exception {
+        final WrappedReceiver receiver = new WrappedReceiver();
+        try (ActivityScenario<TetherProvisioningActivity> scenario = ActivityScenario.launch(
+                new Intent(Settings.ACTION_TETHER_PROVISIONING_UI)
+                        .putExtra(EXTRA_ADD_TETHER_TYPE, TETHERING_WIFI)
+                        .putExtra(EXTRA_PROVISION_CALLBACK, receiver)
+                        .putExtra(TetherProvisioningActivity.EXTRA_SUBID, 10000))) {
+            assertEquals(TetheringManager.TETHER_ERROR_PROVISIONING_FAILED, receiver.get());
+            assertEquals(Lifecycle.State.DESTROYED, scenario.getState());
+        }
+    }
+
+    @Test
+    public void testOnCreate_FinishWithUnavailableProvisioningApp() throws Exception {
+        final WrappedReceiver receiver = new WrappedReceiver();
+        final int subId = SubscriptionManager.getActiveDataSubscriptionId();
+        try (ActivityScenario<TetherProvisioningActivity> scenario = ActivityScenario.launch(
+                new Intent(Settings.ACTION_TETHER_PROVISIONING_UI)
+                        .putExtra(EXTRA_ADD_TETHER_TYPE, TETHERING_WIFI)
+                        .putExtra(EXTRA_PROVISION_CALLBACK, receiver)
+                        .putExtra(TetherProvisioningActivity.EXTRA_SUBID, subId)
+                        .putExtra(EXTRA_RUN_PROVISION, new String[] { "", "" }))) {
+            assertEquals(TetheringManager.TETHER_ERROR_PROVISIONING_FAILED, receiver.get());
+            assertEquals(Lifecycle.State.DESTROYED, scenario.getState());
+        }
+    }
+
+    @Test
+    public void testOnCreate_startActivityForResult() {
+        final WrappedReceiver receiver = new WrappedReceiver();
+        final int subId = SubscriptionManager.getActiveDataSubscriptionId();
+        final String[] provisionApp = new String[] {
+                "android.test.entitlement",
+                "android.test.entitlement.InstrumentedEntitlementActivity"
+        };
+        try (ActivityScenario<TetherProvisioningActivity> scenario = ActivityScenario.launch(
+                new Intent(Settings.ACTION_TETHER_PROVISIONING_UI)
+                        .putExtra(EXTRA_ADD_TETHER_TYPE, TETHERING_WIFI)
+                        .putExtra(EXTRA_PROVISION_CALLBACK, receiver)
+                        .putExtra(TetherProvisioningActivity.EXTRA_SUBID, subId)
+                        .putExtra(EXTRA_RUN_PROVISION, provisionApp))) {
+            scenario.onActivity(activity -> {
+                assertFalse(activity.isFinishing());
+                activity.onActivityResult(PROVISION_REQUEST, Activity.RESULT_OK, null /* intent */);
+                try {
+                    assertEquals(TetheringManager.TETHER_ERROR_NO_ERROR, receiver.get());
+                } catch (Exception e) {
+                    // ActivityAction#perform() doesn't throw the exception. Just catch the
+                    // exception and call fail() here.
+                    fail("Can not get result after 10s.");
+                }
+                assertTrue(activity.isFinishing());
+            });
+        }
+    }
+}