#include <stddef.h>
#include <string>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/escape.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/simple_test_clock.h"
#include "base/time/clock.h"
#include "build/build_config.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/content_settings/browser/page_specific_content_settings.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/permissions/features.h"
#include "components/permissions/permission_request_manager.h"
#include "components/permissions/test/permission_request_observer.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/isolated_world_ids.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/prerender_test_util.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "services/device/public/cpp/test/scoped_geolocation_overrider.h"
#include "services/device/public/mojom/geoposition.mojom.h"
namespace {
std::string GetErrorCodePermissionDenied() {
return base::NumberToString(
static_cast<int>(device::mojom::GeopositionErrorCode::kPermissionDenied));
}
std::string RunScript(content::RenderFrameHost* render_frame_host,
const std::string& script) {
return content::EvalJs(render_frame_host, script).ExtractString();
}
class IFrameLoader : public content::WebContentsObserver {
public:
IFrameLoader(Browser* browser, int iframe_id, const GURL& url);
IFrameLoader(const IFrameLoader&) = delete;
IFrameLoader& operator=(const IFrameLoader&) = delete;
~IFrameLoader() override;
void DidStopLoading() override;
void DomOperationResponse(content::RenderFrameHost* render_frame_host,
const std::string& json_string) override;
const GURL& iframe_url() const { return iframe_url_; }
private:
bool navigation_completed_;
bool javascript_completed_;
std::string javascript_response_;
GURL iframe_url_;
base::RunLoop run_loop;
base::OnceClosure quit_closure_;
};
IFrameLoader::IFrameLoader(Browser* browser, int iframe_id, const GURL& url)
: navigation_completed_(false),
javascript_completed_(false) {
content::WebContents* web_contents =
browser->tab_strip_model()->GetActiveWebContents();
content::WebContentsObserver::Observe(web_contents);
std::string script(base::StringPrintf(
"window.domAutomationController.send(addIFrame(%d, \"%s\"));",
iframe_id, url.spec().c_str()));
web_contents->GetPrimaryMainFrame()->ExecuteJavaScriptForTests(
base::UTF8ToUTF16(script), base::NullCallback(),
content::ISOLATED_WORLD_ID_GLOBAL);
quit_closure_ = run_loop.QuitWhenIdleClosure();
run_loop.Run();
EXPECT_EQ(base::StringPrintf("\"%d\"", iframe_id), javascript_response_);
content::WebContentsObserver::Observe(nullptr);
script = base::StringPrintf("getIFrameSrc(%d)", iframe_id);
iframe_url_ = GURL(RunScript(web_contents->GetPrimaryMainFrame(), script));
}
IFrameLoader::~IFrameLoader() = default;
void IFrameLoader::DidStopLoading() {
navigation_completed_ = true;
if (javascript_completed_ && navigation_completed_)
std::move(quit_closure_).Run();
}
void IFrameLoader::DomOperationResponse(
content::RenderFrameHost* render_frame_host,
const std::string& json_string) {
javascript_response_ = json_string;
javascript_completed_ = true;
if (javascript_completed_ && navigation_completed_)
std::move(quit_closure_).Run();
}
}
class GeolocationBrowserTest : public InProcessBrowserTest {
public:
enum InitializationOptions {
INITIALIZATION_DEFAULT,
INITIALIZATION_OFFTHERECORD,
INITIALIZATION_NEWTAB,
};
GeolocationBrowserTest();
GeolocationBrowserTest(const GeolocationBrowserTest&) = delete;
GeolocationBrowserTest& operator=(const GeolocationBrowserTest&) = delete;
~GeolocationBrowserTest() override = default;
void SetUpOnMainThread() override;
void TearDownInProcessBrowserTestFixture() override;
Browser* current_browser() { return current_browser_; }
void set_html_for_tests(const std::string& html_for_tests) {
html_for_tests_ = html_for_tests;
}
std::string html_for_tests() { return html_for_tests_; }
const GURL& iframe_url(size_t i) const { return iframe_urls_[i]; }
double fake_latitude() const { return fake_latitude_; }
double fake_longitude() const { return fake_longitude_; }
GURL GetTestURL() const {
return https_test_server_.GetURL(html_for_tests_);
}
GURL GetTestURLForHostname(std::string hostname) const {
return https_test_server_.GetURL(hostname, html_for_tests_);
}
content::WebContents* web_contents() {
return current_browser()->tab_strip_model()->GetActiveWebContents();
}
void Initialize(InitializationOptions options, GURL target);
void Initialize(InitializationOptions options);
void LoadIFrames();
void SetFrameForScriptExecution(const std::string& frame_name);
HostContentSettingsMap* GetHostContentSettingsMap();
[[nodiscard]] bool WatchPositionAndGrantPermission();
[[nodiscard]] bool WatchPositionAndDenyPermission();
void WatchPositionAndObservePermissionRequest(bool request_should_display);
void ExpectPosition(double latitude, double longitude);
void ExpectValueFromScriptForFrame(
const std::string& expected,
const std::string& function,
content::RenderFrameHost* render_frame_host);
void ExpectValueFromScript(const std::string& expected,
const std::string& function);
bool SetPositionAndWaitUntilUpdated(double latitude, double longitude);
protected:
double fake_latitude_ = 1.23;
double fake_longitude_ = 4.56;
std::unique_ptr<device::ScopedGeolocationOverrider> geolocation_overrider_;
raw_ptr<Browser, AcrossTasksDanglingUntriaged> current_browser_ = nullptr;
net::EmbeddedTestServer https_test_server_{
net::EmbeddedTestServer::TYPE_HTTPS};
private:
std::string WatchPositionAndRespondToPermissionRequest(
permissions::PermissionRequestManager::AutoResponseType request_response);
net::test_server::EmbeddedTestServerHandle test_server_handle_;
std::string html_for_tests_ = "/geolocation/simple.html";
raw_ptr<content::RenderFrameHost, AcrossTasksDanglingUntriaged>
render_frame_host_ = nullptr;
std::vector<GURL> iframe_urls_;
};
GeolocationBrowserTest::GeolocationBrowserTest()
: geolocation_overrider_(
std::make_unique<device::ScopedGeolocationOverrider>(
fake_latitude_,
fake_longitude_)) {}
void GeolocationBrowserTest::SetUpOnMainThread() {
current_browser_ = browser();
host_resolver()->AddRule("*", "127.0.0.1");
https_test_server_.SetCertHostnames({"a.test", "b.test", "localhost"});
https_test_server_.AddDefaultHandlers(GetChromeTestDataDir());
ASSERT_TRUE(test_server_handle_ = https_test_server_.StartAndReturnHandle());
}
void GeolocationBrowserTest::TearDownInProcessBrowserTestFixture() {
LOG(WARNING) << "TearDownInProcessBrowserTestFixture. Test Finished.";
}
void GeolocationBrowserTest::Initialize(InitializationOptions options) {
Initialize(options, GetTestURL());
}
void GeolocationBrowserTest::Initialize(InitializationOptions options,
GURL target) {
if (options == INITIALIZATION_OFFTHERECORD) {
current_browser_ = OpenURLOffTheRecord(browser()->profile(), target);
} else {
current_browser_ = browser();
if (options == INITIALIZATION_NEWTAB)
chrome::NewTab(current_browser_);
}
ASSERT_TRUE(current_browser_);
if (options != INITIALIZATION_OFFTHERECORD)
ASSERT_TRUE(ui_test_utils::NavigateToURL(current_browser_, target));
SetFrameForScriptExecution("");
}
void GeolocationBrowserTest::LoadIFrames() {
int number_iframes = 2;
iframe_urls_.resize(number_iframes);
for (int i = 0; i < number_iframes; ++i) {
IFrameLoader loader(current_browser_, i, GURL());
iframe_urls_[i] = loader.iframe_url();
}
}
void GeolocationBrowserTest::SetFrameForScriptExecution(
const std::string& frame_name) {
render_frame_host_ = nullptr;
if (frame_name.empty()) {
render_frame_host_ = web_contents()->GetPrimaryMainFrame();
} else {
render_frame_host_ = content::FrameMatchingPredicate(
web_contents()->GetPrimaryPage(),
base::BindRepeating(&content::FrameMatchesName, frame_name));
}
DCHECK(render_frame_host_);
}
HostContentSettingsMap* GeolocationBrowserTest::GetHostContentSettingsMap() {
return HostContentSettingsMapFactory::GetForProfile(browser()->profile());
}
bool GeolocationBrowserTest::WatchPositionAndGrantPermission() {
std::string result = WatchPositionAndRespondToPermissionRequest(
permissions::PermissionRequestManager::ACCEPT_ALL);
return "request-callback-success" == result;
}
bool GeolocationBrowserTest::WatchPositionAndDenyPermission() {
std::string result = WatchPositionAndRespondToPermissionRequest(
permissions::PermissionRequestManager::DENY_ALL);
return "request-callback-error" == result;
}
std::string GeolocationBrowserTest::WatchPositionAndRespondToPermissionRequest(
permissions::PermissionRequestManager::AutoResponseType request_response) {
permissions::PermissionRequestManager::FromWebContents(web_contents())
->set_auto_response_for_test(request_response);
return RunScript(render_frame_host_, "geoStartWithAsyncResponse()");
}
void GeolocationBrowserTest::WatchPositionAndObservePermissionRequest(
bool request_should_display) {
permissions::PermissionRequestObserver observer(web_contents());
if (request_should_display) {
RunScript(render_frame_host_, "geoStartWithSyncResponse()");
observer.Wait();
} else {
RunScript(render_frame_host_, "geoStartWithAsyncResponse()");
}
EXPECT_EQ(request_should_display, observer.request_shown());
}
void GeolocationBrowserTest::ExpectPosition(double latitude, double longitude) {
ExpectValueFromScript("0", "geoGetLastError()");
ExpectValueFromScript(base::NumberToString(latitude),
"geoGetLastPositionLatitude()");
ExpectValueFromScript(base::NumberToString(longitude),
"geoGetLastPositionLongitude()");
}
void GeolocationBrowserTest::ExpectValueFromScriptForFrame(
const std::string& expected,
const std::string& function,
content::RenderFrameHost* render_frame_host) {
EXPECT_EQ(expected, RunScript(render_frame_host, function));
}
void GeolocationBrowserTest::ExpectValueFromScript(
const std::string& expected,
const std::string& function) {
ExpectValueFromScriptForFrame(expected, function, render_frame_host_);
}
bool GeolocationBrowserTest::SetPositionAndWaitUntilUpdated(double latitude,
double longitude) {
fake_latitude_ = latitude;
fake_longitude_ = longitude;
geolocation_overrider_->UpdateLocation(fake_latitude_, fake_longitude_);
return content::EvalJs(render_frame_host_, "geopositionUpdates.pop();")
.ExtractString() == "geoposition-updated";
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, DisplaysPrompt) {
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
ASSERT_TRUE(WatchPositionAndGrantPermission());
EXPECT_EQ(CONTENT_SETTING_ALLOW,
GetHostContentSettingsMap()->GetContentSetting(
GetTestURL(), GetTestURL(), ContentSettingsType::GEOLOCATION));
WatchPositionAndObservePermissionRequest(false);
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, Geoposition) {
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
ASSERT_TRUE(WatchPositionAndGrantPermission());
ExpectPosition(fake_latitude(), fake_longitude());
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, ErrorOnPermissionDenied) {
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
EXPECT_TRUE(WatchPositionAndDenyPermission());
ExpectValueFromScript(GetErrorCodePermissionDenied(), "geoGetLastError()");
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetHostContentSettingsMap()->GetContentSetting(
GetTestURL(), GetTestURL(), ContentSettingsType::GEOLOCATION));
WatchPositionAndObservePermissionRequest(false);
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, NoPromptForSecondTab) {
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
ASSERT_TRUE(WatchPositionAndGrantPermission());
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_NEWTAB));
WatchPositionAndObservePermissionRequest(false);
ExpectPosition(fake_latitude(), fake_longitude());
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, NoPromptForDeniedOrigin) {
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
GetTestURL(), GetTestURL(), ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_BLOCK);
WatchPositionAndObservePermissionRequest(false);
ExpectValueFromScript(GetErrorCodePermissionDenied(), "geoGetLastError()");
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_NEWTAB));
WatchPositionAndObservePermissionRequest(false);
ExpectValueFromScript(GetErrorCodePermissionDenied(), "geoGetLastError()");
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, NoPromptForAllowedOrigin) {
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
GetTestURL(), GetTestURL(), ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_ALLOW);
WatchPositionAndObservePermissionRequest(false);
ExpectPosition(fake_latitude(), fake_longitude());
}
#if BUILDFLAG(IS_WIN)
#define MAYBE_PromptForOffTheRecord DISABLED_PromptForOffTheRecord
#else
#define MAYBE_PromptForOffTheRecord PromptForOffTheRecord
#endif
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_PromptForOffTheRecord) {
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
ASSERT_TRUE(WatchPositionAndGrantPermission());
ExpectPosition(fake_latitude(), fake_longitude());
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_OFFTHERECORD));
ASSERT_TRUE(WatchPositionAndGrantPermission());
ExpectPosition(fake_latitude(), fake_longitude());
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, NoLeakFromOffTheRecord) {
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_OFFTHERECORD));
ASSERT_TRUE(WatchPositionAndGrantPermission());
ExpectPosition(fake_latitude(), fake_longitude());
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
ASSERT_TRUE(WatchPositionAndGrantPermission());
ExpectPosition(fake_latitude(), fake_longitude());
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, IFramesWithCachedPosition) {
set_html_for_tests("/geolocation/two_iframes.html");
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
LoadIFrames();
SetFrameForScriptExecution("iframe_0");
ASSERT_TRUE(WatchPositionAndGrantPermission());
ExpectPosition(fake_latitude(), fake_longitude());
double cached_position_latitude = 5.67;
double cached_position_lognitude = 8.09;
ASSERT_TRUE(SetPositionAndWaitUntilUpdated(cached_position_latitude,
cached_position_lognitude));
ExpectPosition(cached_position_latitude, cached_position_lognitude);
SetFrameForScriptExecution("iframe_1");
ASSERT_TRUE(WatchPositionAndGrantPermission());
ExpectPosition(cached_position_latitude, cached_position_lognitude);
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, InvalidUrlRequest) {
set_html_for_tests("/geolocation/invalid_request_url.html");
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
content::WebContents* original_tab = web_contents();
ExpectValueFromScript(GetErrorCodePermissionDenied(),
"requestGeolocationFromInvalidUrl()");
ExpectValueFromScriptForFrame("1", "isAlive()",
original_tab->GetPrimaryMainFrame());
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, NoPromptBeforeStart) {
set_html_for_tests("/geolocation/two_iframes.html");
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
LoadIFrames();
SetFrameForScriptExecution("iframe_1");
ExpectValueFromScript("object", "geoAccessNavigatorGeolocation()");
SetFrameForScriptExecution("iframe_0");
ASSERT_TRUE(WatchPositionAndGrantPermission());
ExpectPosition(fake_latitude(), fake_longitude());
SetFrameForScriptExecution("iframe_1");
ASSERT_TRUE(WatchPositionAndGrantPermission());
ExpectPosition(fake_latitude(), fake_longitude());
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, TwoWatchesInOneFrame) {
set_html_for_tests("/geolocation/two_watches.html");
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
double final_position_latitude = 3.17;
double final_position_longitude = 4.23;
std::string script =
base::StringPrintf("geoSetFinalPosition(%f, %f)", final_position_latitude,
final_position_longitude);
ExpectValueFromScript("ok", script);
ASSERT_TRUE(WatchPositionAndGrantPermission());
ExpectPosition(fake_latitude(), fake_longitude());
ASSERT_TRUE(SetPositionAndWaitUntilUpdated(final_position_latitude,
final_position_longitude));
ExpectPosition(final_position_latitude, final_position_longitude);
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, DISABLED_PendingChildFrames) {
set_html_for_tests("/geolocation/two_iframes.html");
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
LoadIFrames();
SetFrameForScriptExecution("iframe_0");
WatchPositionAndObservePermissionRequest(true);
SetFrameForScriptExecution("iframe_1");
WatchPositionAndObservePermissionRequest(true);
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, TabDestroyed) {
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
WatchPositionAndObservePermissionRequest(true);
std::string script = "window.domAutomationController.send(window.close())";
ASSERT_TRUE(content::ExecJs(web_contents(), script));
}
class GeolocationPrerenderBrowserTest : public GeolocationBrowserTest {
public:
GeolocationPrerenderBrowserTest()
: prerender_helper_(
base::BindRepeating(&GeolocationPrerenderBrowserTest::web_contents,
base::Unretained(this))) {}
~GeolocationPrerenderBrowserTest() override = default;
void SetUp() override {
prerender_helper_.RegisterServerRequestMonitor(&https_test_server_);
GeolocationBrowserTest::SetUp();
}
protected:
content::test::PrerenderTestHelper prerender_helper_;
};
IN_PROC_BROWSER_TEST_F(GeolocationPrerenderBrowserTest,
DeferredBeforePrerenderActivation) {
ASSERT_TRUE(ui_test_utils::NavigateToURL(
current_browser(), https_test_server_.GetURL("/empty.html")));
content::FrameTreeNodeId host_id =
prerender_helper_.AddPrerender(GetTestURL());
content::test::PrerenderHostObserver prerender_observer(*web_contents(),
host_id);
ASSERT_TRUE(prerender_helper_.GetHostForUrl(GetTestURL()));
permissions::PermissionRequestObserver observer(web_contents());
content::RenderFrameHost* prerender_rfh =
prerender_helper_.GetPrerenderedMainFrameHost(host_id);
ExecuteScriptAsync(prerender_rfh, "geoStartWithAsyncResponse()");
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(observer.request_shown());
prerender_helper_.NavigatePrimaryPage(GetTestURL());
ASSERT_TRUE(prerender_observer.was_activated());
observer.Wait();
EXPECT_TRUE(observer.request_shown());
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest,
GrantToDenyStopsGeolocationWatch) {
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
ASSERT_TRUE(WatchPositionAndGrantPermission());
ExpectPosition(fake_latitude(), fake_longitude());
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
GetTestURL(), GetTestURL(), ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_BLOCK);
EXPECT_TRUE(SetPositionAndWaitUntilUpdated(1, 2));
ExpectValueFromScript(GetErrorCodePermissionDenied(), "geoGetLastError()");
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest,
GrantToRevokeStopsGeolocationWatch) {
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
ASSERT_TRUE(WatchPositionAndGrantPermission());
ExpectPosition(fake_latitude(), fake_longitude());
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
GetTestURL(), GetTestURL(), ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_ASK);
EXPECT_TRUE(SetPositionAndWaitUntilUpdated(1, 2));
ExpectValueFromScript(GetErrorCodePermissionDenied(), "geoGetLastError()");
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest,
GrantToDenyToGrantDoesNotRemainBlocked) {
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
ASSERT_TRUE(WatchPositionAndGrantPermission());
ExpectPosition(fake_latitude(), fake_longitude());
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
GetTestURL(), GetTestURL(), ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_BLOCK);
EXPECT_TRUE(SetPositionAndWaitUntilUpdated(1, 2));
ExpectValueFromScript(GetErrorCodePermissionDenied(), "geoGetLastError()");
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
GetTestURL(), GetTestURL(), ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_ALLOW);
ASSERT_TRUE(WatchPositionAndGrantPermission());
ExpectPosition(fake_latitude(), fake_longitude());
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest,
ToggleToDenyDoesNotLeakCrossOrigin) {
GURL a_test_gurl = GetTestURLForHostname("a.test");
GURL b_test_gurl = GetTestURLForHostname("b.test");
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT, a_test_gurl));
ASSERT_TRUE(WatchPositionAndGrantPermission());
ExpectPosition(fake_latitude(), fake_longitude());
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
a_test_gurl, a_test_gurl, ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_BLOCK);
EXPECT_TRUE(SetPositionAndWaitUntilUpdated(1, 2));
ExpectValueFromScript(GetErrorCodePermissionDenied(), "geoGetLastError()");
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT, b_test_gurl));
ASSERT_TRUE(WatchPositionAndGrantPermission());
ExpectPosition(fake_latitude(), fake_longitude());
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest,
ToggleToDenyDoesNotOverrideGrantOnOtherOrigin) {
GURL a_test_gurl = GetTestURLForHostname("a.test");
GURL b_test_gurl = GetTestURLForHostname("b.test");
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
b_test_gurl, b_test_gurl, ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_ALLOW);
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT, a_test_gurl));
ASSERT_TRUE(WatchPositionAndGrantPermission());
ExpectPosition(fake_latitude(), fake_longitude());
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
a_test_gurl, a_test_gurl, ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_BLOCK);
EXPECT_TRUE(SetPositionAndWaitUntilUpdated(1, 2));
ExpectValueFromScript(GetErrorCodePermissionDenied(), "geoGetLastError()");
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT, b_test_gurl));
WatchPositionAndObservePermissionRequest(false);
ExpectPosition(fake_latitude(), fake_longitude());
}