Skip to content

Commit

Permalink
Added test
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhong committed May 1, 2024
1 parent 7fcde86 commit 55e3e34
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
24 changes: 24 additions & 0 deletions browser/ui/geolocation/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (c) 2024 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.

assert(!is_android)

source_set("browser_tests") {
testonly = true
defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ]

sources = [ "brave_geolocation_browsertest.cc" ]
deps = [
"//base",
"//brave/components/constants",
"//chrome/browser",
"//chrome/browser/ui",
"//chrome/common:constants",
"//chrome/test:test_support_ui",
"//content/test:test_support",
"//net:test_support",
"//url",
]
}
80 changes: 80 additions & 0 deletions browser/ui/geolocation/brave_geolocation_browsertest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* Copyright (c) 2024 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include <string>

#include "base/path_service.h"
#include "brave/browser/ui/geolocation/brave_geolocation_permission_tab_helper.h"
#include "brave/components/constants/brave_paths.h"
#include "chrome/browser/ssl/cert_verifier_browser_test.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/common/chrome_paths.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "net/dns/mock_host_resolver.h"
#include "url/gurl.h"

class GeolocationPermissionRequestBrowserTest : public CertVerifierBrowserTest {
public:
GeolocationPermissionRequestBrowserTest()
: https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {}

void SetUpOnMainThread() override {
CertVerifierBrowserTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1");
brave::RegisterPathProvider();
base::FilePath test_data_dir =
base::PathService::CheckedGet(brave::DIR_TEST_DATA);
https_server_.ServeFilesFromDirectory(test_data_dir);
mock_cert_verifier()->set_default_result(net::OK);

ASSERT_TRUE(https_server_.Start());
}

content::WebContents* active_contents() {
return browser()->tab_strip_model()->GetActiveWebContents();
}

protected:
net::EmbeddedTestServer https_server_;
};

IN_PROC_BROWSER_TEST_F(GeolocationPermissionRequestBrowserTest,
SetEnableHighAccuracyTest) {
GURL url = https_server_.GetURL("a.com", "/simple.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));

auto* tab_helper =
BraveGeolocationPermissionTabHelper::FromWebContents(active_contents());
EXPECT_FALSE(tab_helper->enable_high_accuracy());

const std::string get_current_position_js_with_high =
"navigator.geolocation.getCurrentPosition(() => {}, () => {}, { "
"enableHighAccuracy : true })";
ASSERT_EQ(nullptr, content::EvalJs(active_contents(),
get_current_position_js_with_high));
content::RunAllTasksUntilIdle();
EXPECT_TRUE(tab_helper->enable_high_accuracy());

// Reload clear high_accuracy bit from tab helper.
content::TestNavigationObserver observer(active_contents());
chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB);
observer.Wait();
EXPECT_FALSE(tab_helper->enable_high_accuracy());

const std::string get_current_position_js_without_high =
"navigator.geolocation.getCurrentPosition(() => {}, () => {}, { "
"enableHighAccuracy : false })";
ASSERT_EQ(nullptr, content::EvalJs(active_contents(),
get_current_position_js_without_high));
content::RunAllTasksUntilIdle();
EXPECT_FALSE(tab_helper->enable_high_accuracy());
}
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,7 @@ test("brave_browser_tests") {
"//brave/app/theme:brave_theme_resources_grit",
"//brave/app/theme:brave_unscaled_resources_grit",
"//brave/browser/sharing_hub:browser_tests",
"//brave/browser/ui/geolocation:browser_tests",
"//brave/browser/ui/whats_new:browser_test",
"//brave/components/brave_wallet/browser:test_support",
"//chrome/browser/apps/app_service:app_service",
Expand Down

0 comments on commit 55e3e34

Please sign in to comment.