Skip to content

Commit

Permalink
[WebLayer] Switch geolocation tests to use permission prompt
Browse files Browse the repository at this point in the history
These tests were previously using the fake permission manager which
always grants permissions. Now we can use the real permission manager,
and the fake permission manager can be removed.

Fixes a strict mode violation in permission dialogs (similar to fixes
from https://crrev.com/c/2108603).

Bug: 1025625, 1025609
Change-Id: I8b756c61ee213151e53d68a375ffa4d3f8fd7643
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2111061
Commit-Queue: Clark DuVall <cduvall@chromium.org>
Reviewed-by: John Abd-El-Malek <jam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#752494}
  • Loading branch information
clarkduvall authored and Commit Bot committed Mar 23, 2020
1 parent 0442890 commit 6966bd5
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import androidx.core.widget.TextViewCompat;

import org.chromium.base.StrictModeContext;
import org.chromium.components.browser_ui.modaldialog.R;
import org.chromium.ui.modaldialog.ModalDialogProperties;
import org.chromium.ui.modelutil.PropertyModel;
Expand All @@ -24,8 +25,7 @@ public static PropertyModel getModel(
ModalDialogProperties.Controller controller, PermissionDialogDelegate delegate) {
Context context = delegate.getWindow().getContext().get();
assert context != null;
LayoutInflater inflater = LayoutInflater.from(context);
View customView = inflater.inflate(R.layout.permission_dialog, null);
View customView = loadDialogView(context);

String messageText = delegate.getMessageText();
assert !TextUtils.isEmpty(messageText);
Expand All @@ -44,4 +44,12 @@ public static PropertyModel getModel(
.with(ModalDialogProperties.FILTER_TOUCH_FOR_SECURITY, true)
.build();
}

private static View loadDialogView(Context context) {
// LayoutInflater may access the disk.
try (StrictModeContext ignored = StrictModeContext.allowDiskReads()) {
LayoutInflater inflater = LayoutInflater.from(context);
return inflater.inflate(R.layout.permission_dialog, null);
}
}
}
2 changes: 0 additions & 2 deletions weblayer/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ source_set("weblayer_lib_base") {
"browser/download_impl.h",
"browser/download_manager_delegate_impl.cc",
"browser/download_manager_delegate_impl.h",
"browser/fake_permission_controller_delegate.cc",
"browser/fake_permission_controller_delegate.h",
"browser/feature_list_creator.cc",
"browser/feature_list_creator.h",
"browser/file_select_helper.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.net.test.util.TestWebServer;
Expand All @@ -27,7 +26,6 @@
* Tests that Geolocation Web API works as expected.
*/
@RunWith(WebLayerJUnit4ClassRunner.class)
@CommandLineFlags.Add("weblayer-fake-permissions")
public final class GeolocationTest {
@Rule
public InstrumentationActivityTestRule mActivityTestRule =
Expand Down Expand Up @@ -94,6 +92,8 @@ public void tearDown() throws Throwable {
@MediumTest
public void testGeolocation_getPosition() throws Throwable {
mActivityTestRule.executeScriptSync("initiate_getCurrentPosition();", false);
waitForDialog();
mTestWebLayer.clickPermissionDialogButton(true);
waitForCountEqual("positionCount", 1);
mActivityTestRule.executeScriptSync("initiate_getCurrentPosition();", false);
waitForCountEqual("positionCount", 2);
Expand All @@ -107,6 +107,8 @@ public void testGeolocation_getPosition() throws Throwable {
@MediumTest
public void testGeolocation_watchPosition() throws Throwable {
mActivityTestRule.executeScriptSync("initiate_watchPosition();", false);
waitForDialog();
mTestWebLayer.clickPermissionDialogButton(true);
waitForCountGreaterThan("positionCount", 1);
ensureGeolocationIsRunning(true);
Assert.assertEquals(0, getCountFromJS("errorCount"));
Expand All @@ -119,6 +121,8 @@ public void testGeolocation_watchPosition() throws Throwable {
@MediumTest
public void testGeolocation_destroyTabStopsGeolocation() throws Throwable {
mActivityTestRule.executeScriptSync("initiate_watchPosition();", false);
waitForDialog();
mTestWebLayer.clickPermissionDialogButton(true);
ensureGeolocationIsRunning(true);
TestThreadUtils.runOnUiThreadBlocking(() -> {
Browser browser = mActivity.getBrowser();
Expand All @@ -143,13 +147,28 @@ public void testGeolocation_denyOnInsecureOrigins() throws Throwable {
Assert.assertEquals(0, getCountFromJS("positionCount"));
}

@Test
@MediumTest
public void testGeolocation_denyFromPrompt() throws Throwable {
mActivityTestRule.executeScriptSync("initiate_watchPosition();", false);
waitForDialog();
mTestWebLayer.clickPermissionDialogButton(false);
waitForCountEqual("errorCount", 1);
Assert.assertEquals(0, getCountFromJS("positionCount"));
}

// helper methods

private void waitForCountEqual(String variableName, int count) {
CriteriaHelper.pollInstrumentationThread(
() -> { return getCountFromJS(variableName) == count; });
}

private void waitForDialog() {
CriteriaHelper.pollInstrumentationThread(
() -> { return mTestWebLayer.isPermissionDialogShown(); });
}

private void waitForCountGreaterThan(String variableName, int count) {
CriteriaHelper.pollInstrumentationThread(
() -> { return getCountFromJS(variableName) > count; });
Expand Down
9 changes: 0 additions & 9 deletions weblayer/browser/browser_context_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "content/public/browser/device_service.h"
#include "content/public/browser/download_request_utils.h"
#include "content/public/browser/resource_context.h"
#include "weblayer/browser/fake_permission_controller_delegate.h"
#include "weblayer/browser/permissions/permission_manager_factory.h"
#include "weblayer/browser/stateful_ssl_host_state_delegate_factory.h"
#include "weblayer/public/common/switches.h"
Expand Down Expand Up @@ -153,14 +152,6 @@ content::SSLHostStateDelegate* BrowserContextImpl::GetSSLHostStateDelegate() {

content::PermissionControllerDelegate*
BrowserContextImpl::GetPermissionControllerDelegate() {
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kWebLayerFakePermissions)) {
if (!permission_controller_delegate_) {
permission_controller_delegate_ =
std::make_unique<FakePermissionControllerDelegate>();
}
return permission_controller_delegate_.get();
}
return PermissionManagerFactory::GetForBrowserContext(this);
}

Expand Down
2 changes: 0 additions & 2 deletions weblayer/browser/browser_context_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ class BrowserContextImpl : public content::BrowserContext {
resource_context_;
DownloadManagerDelegateImpl download_delegate_;
std::unique_ptr<PrefService> user_pref_service_;
std::unique_ptr<content::PermissionControllerDelegate>
permission_controller_delegate_;
std::unique_ptr<WebLayerVariationsClient> weblayer_variations_client_;
};
} // namespace weblayer
Expand Down
9 changes: 0 additions & 9 deletions weblayer/browser/content_browser_client_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,6 @@ ContentBrowserClientImpl::CreateBrowserMainParts(
return browser_main_parts;
}

void ContentBrowserClientImpl::AppendExtraCommandLineSwitches(
base::CommandLine* command_line,
int child_process_id) {
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kWebLayerFakePermissions)) {
command_line->AppendSwitch(switches::kWebLayerFakePermissions);
}
}

std::string ContentBrowserClientImpl::GetApplicationLocale() {
return i18n::GetApplicationLocale();
}
Expand Down
2 changes: 0 additions & 2 deletions weblayer/browser/content_browser_client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class ContentBrowserClientImpl : public content::ContentBrowserClient {
// ContentBrowserClient overrides.
std::unique_ptr<content::BrowserMainParts> CreateBrowserMainParts(
const content::MainFunctionParams& parameters) override;
void AppendExtraCommandLineSwitches(base::CommandLine* command_line,
int child_process_id) override;
std::string GetApplicationLocale() override;
std::string GetAcceptLangs(content::BrowserContext* context) override;
content::WebContentsViewDelegate* GetWebContentsViewDelegate(
Expand Down
77 changes: 0 additions & 77 deletions weblayer/browser/fake_permission_controller_delegate.cc

This file was deleted.

63 changes: 0 additions & 63 deletions weblayer/browser/fake_permission_controller_delegate.h

This file was deleted.

3 changes: 3 additions & 0 deletions weblayer/browser/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,12 @@ android_library("test_java") {
sources = [ "org/chromium/weblayer_private/test/TestWebLayerImpl.java" ]
deps = [
":weblayer_test_resources",
"//components/permissions/android:java",
"//content/public/test/android:content_java_test_support",
"//net/android:net_java",
"//services/device/public/java:geolocation_java",
"//services/device/public/java:geolocation_java_test_support",
"//ui/android:ui_full_java",
]
srcjar_deps = [ ":test_aidl" ]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
import android.os.IBinder;

import org.chromium.base.annotations.UsedByReflection;
import org.chromium.components.permissions.PermissionDialogController;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.device.geolocation.LocationProviderOverrider;
import org.chromium.device.geolocation.MockLocationProvider;
import org.chromium.net.NetworkChangeNotifier;
import org.chromium.ui.modaldialog.ModalDialogProperties;
import org.chromium.weblayer_private.test_interfaces.ITestWebLayer;

import java.util.concurrent.ExecutionException;

/**
* Root implementation class for TestWebLayer.
*/
Expand Down Expand Up @@ -46,4 +51,24 @@ public void setMockLocationProvider(boolean enable) {
public boolean isMockLocationProviderRunning() {
return mMockLocationProvider.isRunning();
}
}

@Override
public boolean isPermissionDialogShown() {
try {
return TestThreadUtils.runOnUiThreadBlocking(() -> {
return PermissionDialogController.getInstance().isDialogShownForTest();
});
} catch (ExecutionException e) {
return false;
}
}

@Override
public void clickPermissionDialogButton(boolean allow) {
TestThreadUtils.runOnUiThreadBlocking(() -> {
PermissionDialogController.getInstance().clickButtonForTest(allow
? ModalDialogProperties.ButtonType.POSITIVE
: ModalDialogProperties.ButtonType.NEGATIVE);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ interface ITestWebLayer {
// set mock location provider
void setMockLocationProvider(in boolean enable) = 2;
boolean isMockLocationProviderRunning() = 3;

// Whether or not a permission dialog is currently showing.
boolean isPermissionDialogShown() = 4;

// Clicks a button on the permission dialog.
void clickPermissionDialogButton(in boolean allow) = 5;
}
4 changes: 0 additions & 4 deletions weblayer/public/common/switches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,4 @@ namespace switches {
// Makes WebLayer Shell use the given path for its data directory.
const char kWebLayerUserDataDir[] = "weblayer-user-data-dir";

// Makes WebLayer use a fake permission controller delegate which grants all
// permissions.
const char kWebLayerFakePermissions[] = "weblayer-fake-permissions";

} // namespace switches
1 change: 0 additions & 1 deletion weblayer/public/common/switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace switches {

extern const char kWebLayerUserDataDir[];
extern const char kWebLayerFakePermissions[];

} // namespace switches

Expand Down
Loading

0 comments on commit 6966bd5

Please sign in to comment.