Merge "Add developer settings link to WebView DevTools" into main
This commit is contained in:
committed by
Android (Google) Code Review
commit
6fc2502a7b
@@ -136,6 +136,9 @@
|
||||
android:dialogTitle="@string/select_webview_provider_dialog_title"
|
||||
android:fragment="com.android.settings.webview.WebViewAppPicker" />
|
||||
|
||||
<Preference android:key="webview_launch_devtools"
|
||||
android:title="@string/webview_launch_devtools_title" />
|
||||
|
||||
<SwitchPreferenceCompat
|
||||
android:key="color_temperature"
|
||||
android:title="@string/color_temperature"
|
||||
|
@@ -718,6 +718,7 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
|
||||
controllers.add(new Enable16kPagesPreferenceController(context, fragment));
|
||||
controllers.add(new PictureColorModePreferenceController(context, lifecycle));
|
||||
controllers.add(new WebViewAppPreferenceController(context));
|
||||
controllers.add(new WebViewDevUiPreferenceController(context));
|
||||
controllers.add(new CoolColorTemperaturePreferenceController(context));
|
||||
controllers.add(new DisableAutomaticUpdatesPreferenceController(context));
|
||||
controllers.add(new SelectDSUPreferenceController(context));
|
||||
|
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright (C) 2025 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.development;
|
||||
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settings.webview.WebViewUpdateServiceWrapper;
|
||||
import com.android.settingslib.development.DeveloperOptionsPreferenceController;
|
||||
|
||||
public class WebViewDevUiPreferenceController extends DeveloperOptionsPreferenceController
|
||||
implements PreferenceControllerMixin {
|
||||
|
||||
private static final String TAG = "WebViewDevUiPrefCtrl";
|
||||
private static final String WEBVIEW_APP_KEY = "webview_launch_devtools";
|
||||
|
||||
@NonNull
|
||||
private final WebViewUpdateServiceWrapper mWebViewUpdateServiceWrapper;
|
||||
|
||||
@VisibleForTesting
|
||||
public WebViewDevUiPreferenceController(@NonNull Context context,
|
||||
@NonNull WebViewUpdateServiceWrapper webViewUpdateServiceWrapper) {
|
||||
super(context);
|
||||
mWebViewUpdateServiceWrapper = webViewUpdateServiceWrapper;
|
||||
}
|
||||
|
||||
public WebViewDevUiPreferenceController(@NonNull Context context) {
|
||||
this(context, new WebViewUpdateServiceWrapper());
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public String getPreferenceKey() {
|
||||
return WEBVIEW_APP_KEY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handlePreferenceTreeClick(@NonNull Preference preference) {
|
||||
if (!WEBVIEW_APP_KEY.equals(preference.getKey())) {
|
||||
return false;
|
||||
}
|
||||
launchWebViewDevUi();
|
||||
return true;
|
||||
}
|
||||
|
||||
private void launchWebViewDevUi() {
|
||||
PackageInfo currentWebViewPackage =
|
||||
mWebViewUpdateServiceWrapper.getCurrentWebViewPackage();
|
||||
if (currentWebViewPackage == null) {
|
||||
Log.e(TAG, "Couldn't find current WebView package");
|
||||
Toast.makeText(
|
||||
mContext,
|
||||
mContext.getString(
|
||||
com.android.settingslib.R.string.webview_launch_devtools_no_package),
|
||||
Toast.LENGTH_LONG).show();
|
||||
return;
|
||||
}
|
||||
String currentWebViewPackageName = currentWebViewPackage.packageName;
|
||||
Intent intent = new Intent("com.android.webview.SHOW_DEV_UI");
|
||||
intent.setPackage(currentWebViewPackageName);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
|
||||
try {
|
||||
mContext.startActivity(intent);
|
||||
} catch (ActivityNotFoundException e) {
|
||||
Log.e(
|
||||
TAG,
|
||||
"Couldn't launch developer UI from current WebView package: "
|
||||
+ currentWebViewPackage);
|
||||
Toast.makeText(
|
||||
mContext,
|
||||
mContext.getString(
|
||||
com.android.settingslib.R.string.webview_launch_devtools_no_activity),
|
||||
Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* Copyright (C) 2025 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.development;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageInfo;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.android.settings.webview.WebViewUpdateServiceWrapper;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentMatcher;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadows.ShadowToast;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(
|
||||
shadows = {
|
||||
ShadowToast.class,
|
||||
})
|
||||
public class WebViewDevUiPreferenceControllerTest {
|
||||
private static final String CURRENT_WEBVIEW_PROVIDER = "current.webview.provider";
|
||||
private static final String DEVTOOLS_ACTION = "com.android.webview.SHOW_DEV_UI";
|
||||
|
||||
@Mock
|
||||
private PreferenceScreen mPreferenceScreen;
|
||||
@Mock
|
||||
private Preference mPreference;
|
||||
@Mock
|
||||
private WebViewUpdateServiceWrapper mWebViewUpdateServiceWrapper;
|
||||
|
||||
private Context mContext;
|
||||
private WebViewDevUiPreferenceController mController;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = spy(ApplicationProvider.getApplicationContext());
|
||||
mController = new WebViewDevUiPreferenceController(
|
||||
mContext, mWebViewUpdateServiceWrapper);
|
||||
when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
|
||||
.thenReturn(mPreference);
|
||||
mController.displayPreference(mPreferenceScreen);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlePreferenceTreeClick_notWebViewDevUiPreference_shouldReturnFalse() {
|
||||
when(mPreference.getKey()).thenReturn("Some random key");
|
||||
|
||||
final boolean isHandled = mController.handlePreferenceTreeClick(mPreference);
|
||||
|
||||
assertThat(isHandled).isFalse();
|
||||
assertThat(ShadowToast.shownToastCount()).isEqualTo(0);
|
||||
verify(mContext, never()).startActivity(argThat(expectedIntent()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlePreferenceTreeClick_webViewDevUiAvailable_startsActivity() {
|
||||
final String preferenceKey = mController.getPreferenceKey();
|
||||
when(mPreference.getKey()).thenReturn(preferenceKey);
|
||||
PackageInfo packageInfo = webviewProviderPackage();
|
||||
when(mWebViewUpdateServiceWrapper.getCurrentWebViewPackage()).thenReturn(packageInfo);
|
||||
doNothing().when(mContext).startActivity(argThat(expectedIntent()));
|
||||
|
||||
final boolean isHandled = mController.handlePreferenceTreeClick(mPreference);
|
||||
|
||||
assertThat(isHandled).isTrue();
|
||||
assertThat(ShadowToast.shownToastCount()).isEqualTo(0);
|
||||
verify(mContext).startActivity(argThat(expectedIntent()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlePreferenceTreeClick_noPackage_showsToast() {
|
||||
final String preferenceKey = mController.getPreferenceKey();
|
||||
when(mPreference.getKey()).thenReturn(preferenceKey);
|
||||
when(mWebViewUpdateServiceWrapper.getCurrentWebViewPackage()).thenReturn(null);
|
||||
|
||||
final boolean isHandled = mController.handlePreferenceTreeClick(mPreference);
|
||||
|
||||
assertThat(isHandled).isTrue();
|
||||
assertThat(ShadowToast.shownToastCount()).isEqualTo(1);
|
||||
assertThat(ShadowToast.showedToast(
|
||||
mContext.getString(
|
||||
com.android.settingslib.R.string.webview_launch_devtools_no_package)))
|
||||
.isTrue();
|
||||
verify(mContext, never()).startActivity(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlePreferenceTreeClick_resolveFails_showsToast() {
|
||||
final String preferenceKey = mController.getPreferenceKey();
|
||||
when(mPreference.getKey()).thenReturn(preferenceKey);
|
||||
PackageInfo packageInfo = webviewProviderPackage();
|
||||
when(mWebViewUpdateServiceWrapper.getCurrentWebViewPackage()).thenReturn(packageInfo);
|
||||
doThrow(new ActivityNotFoundException()).when(mContext)
|
||||
.startActivity(argThat(expectedIntent()));
|
||||
|
||||
final boolean isHandled = mController.handlePreferenceTreeClick(mPreference);
|
||||
|
||||
assertThat(isHandled).isTrue();
|
||||
verify(mContext).startActivity(any());
|
||||
assertThat(ShadowToast.shownToastCount()).isEqualTo(1);
|
||||
assertThat(ShadowToast.showedToast(
|
||||
mContext.getString(
|
||||
com.android.settingslib.R.string.webview_launch_devtools_no_activity)))
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
private PackageInfo webviewProviderPackage() {
|
||||
PackageInfo packageInfo = new PackageInfo();
|
||||
packageInfo.packageName = CURRENT_WEBVIEW_PROVIDER;
|
||||
return packageInfo;
|
||||
}
|
||||
|
||||
private ArgumentMatcher<Intent> expectedIntent() {
|
||||
return intent -> CURRENT_WEBVIEW_PROVIDER.equals(intent.getPackage())
|
||||
&& DEVTOOLS_ACTION.equals(intent.getAction())
|
||||
&& ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
== Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user