Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / geolocation / geolocation_browsertest.cc
blob0417d40053fed8a78e03b15f64e6159c8b1f296c
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <string>
7 #include "base/compiler_specific.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/test/simple_test_clock.h"
12 #include "base/time/clock.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
15 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_commands.h"
19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
21 #include "chrome/test/base/in_process_browser_test.h"
22 #include "chrome/test/base/ui_test_utils.h"
23 #include "components/content_settings/core/browser/content_settings_usages_state.h"
24 #include "components/content_settings/core/browser/host_content_settings_map.h"
25 #include "components/content_settings/core/common/content_settings_pattern.h"
26 #include "content/public/browser/dom_operation_notification_details.h"
27 #include "content/public/browser/navigation_controller.h"
28 #include "content/public/browser/notification_details.h"
29 #include "content/public/browser/notification_service.h"
30 #include "content/public/browser/render_frame_host.h"
31 #include "content/public/browser/web_contents.h"
32 #include "content/public/common/geoposition.h"
33 #include "content/public/test/browser_test_utils.h"
34 #include "net/base/net_util.h"
35 #include "net/test/embedded_test_server/embedded_test_server.h"
37 namespace {
39 std::string GetErrorCodePermissionDenied() {
40 return base::IntToString(content::Geoposition::ERROR_CODE_PERMISSION_DENIED);
43 std::string RunScript(content::RenderFrameHost* render_frame_host,
44 const std::string& script) {
45 std::string result;
46 EXPECT_TRUE(content::ExecuteScriptAndExtractString(render_frame_host, script,
47 &result));
48 return result;
51 // IFrameLoader ---------------------------------------------------------------
53 // Used to block until an iframe is loaded via a javascript call.
54 // Note: NavigateToURLBlockUntilNavigationsComplete doesn't seem to work for
55 // multiple embedded iframes, as notifications seem to be 'batched'. Instead, we
56 // load and wait one single frame here by calling a javascript function.
57 class IFrameLoader : public content::NotificationObserver {
58 public:
59 IFrameLoader(Browser* browser, int iframe_id, const GURL& url);
60 ~IFrameLoader() override;
62 // content::NotificationObserver:
63 void Observe(int type,
64 const content::NotificationSource& source,
65 const content::NotificationDetails& details) override;
67 const GURL& iframe_url() const { return iframe_url_; }
69 private:
70 content::NotificationRegistrar registrar_;
72 // If true the navigation has completed.
73 bool navigation_completed_;
75 // If true the javascript call has completed.
76 bool javascript_completed_;
78 std::string javascript_response_;
80 // The URL for the iframe we just loaded.
81 GURL iframe_url_;
83 DISALLOW_COPY_AND_ASSIGN(IFrameLoader);
86 IFrameLoader::IFrameLoader(Browser* browser, int iframe_id, const GURL& url)
87 : navigation_completed_(false),
88 javascript_completed_(false) {
89 content::WebContents* web_contents =
90 browser->tab_strip_model()->GetActiveWebContents();
91 content::NavigationController* controller = &web_contents->GetController();
92 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP,
93 content::Source<content::NavigationController>(controller));
94 registrar_.Add(this, content::NOTIFICATION_DOM_OPERATION_RESPONSE,
95 content::NotificationService::AllSources());
96 std::string script(base::StringPrintf(
97 "window.domAutomationController.setAutomationId(0);"
98 "window.domAutomationController.send(addIFrame(%d, \"%s\"));",
99 iframe_id, url.spec().c_str()));
100 web_contents->GetMainFrame()->ExecuteJavaScriptForTests(
101 base::UTF8ToUTF16(script));
102 content::RunMessageLoop();
104 EXPECT_EQ(base::StringPrintf("\"%d\"", iframe_id), javascript_response_);
105 registrar_.RemoveAll();
106 // Now that we loaded the iframe, let's fetch its src.
107 script = base::StringPrintf(
108 "window.domAutomationController.send(getIFrameSrc(%d))", iframe_id);
109 iframe_url_ = GURL(RunScript(web_contents->GetMainFrame(), script));
112 IFrameLoader::~IFrameLoader() {
115 void IFrameLoader::Observe(int type,
116 const content::NotificationSource& source,
117 const content::NotificationDetails& details) {
118 if (type == content::NOTIFICATION_LOAD_STOP) {
119 navigation_completed_ = true;
120 } else if (type == content::NOTIFICATION_DOM_OPERATION_RESPONSE) {
121 content::Details<content::DomOperationNotificationDetails> dom_op_details(
122 details);
123 javascript_response_ = dom_op_details->json;
124 javascript_completed_ = true;
126 if (javascript_completed_ && navigation_completed_)
127 base::MessageLoopForUI::current()->Quit();
130 // PermissionBubbleObserver ---------------------------------------------------
132 // Used to observe the creation of a single permission bubble without
133 // responding.
134 class PermissionBubbleObserver : public PermissionBubbleManager::Observer {
135 public:
136 explicit PermissionBubbleObserver(content::WebContents* web_contents)
137 : bubble_manager_(PermissionBubbleManager::FromWebContents(web_contents)),
138 request_shown_(false),
139 message_loop_runner_(new content::MessageLoopRunner) {
140 bubble_manager_->AddObserver(this);
142 ~PermissionBubbleObserver() override {
143 // Safe to remove twice if it happens.
144 bubble_manager_->RemoveObserver(this);
147 void Wait() { message_loop_runner_->Run(); }
149 bool request_shown() { return request_shown_; }
151 private:
152 // PermissionBubbleManager::Observer
153 void OnBubbleAdded() override {
154 request_shown_ = true;
155 bubble_manager_->RemoveObserver(this);
156 message_loop_runner_->Quit();
159 PermissionBubbleManager* bubble_manager_;
160 bool request_shown_;
161 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
163 DISALLOW_COPY_AND_ASSIGN(PermissionBubbleObserver);
166 } // namespace
169 // GeolocationBrowserTest -----------------------------------------------------
171 // This is a browser test for Geolocation.
172 // It exercises various integration points from javascript <-> browser:
173 // 1. The user is prompted when a position is requested from an unauthorized
174 // origin.
175 // 2. Denying the request triggers the correct error callback.
176 // 3. Granting permission does not trigger an error, and allows a position to
177 // be passed to javascript.
178 // 4. Permissions persisted in disk are respected.
179 // 5. Incognito profiles don't persist permissions on disk, but they do inherit
180 // them from their regular parent profile.
181 class GeolocationBrowserTest : public InProcessBrowserTest {
182 public:
183 enum InitializationOptions {
184 // The default profile and browser window will be used.
185 INITIALIZATION_DEFAULT,
187 // An incognito profile and browser window will be used.
188 INITIALIZATION_OFFTHERECORD,
190 // A new tab will be created using the default profile and browser window.
191 INITIALIZATION_NEWTAB,
194 GeolocationBrowserTest();
195 ~GeolocationBrowserTest() override;
197 // InProcessBrowserTest:
198 void SetUpOnMainThread() override;
199 void TearDownInProcessBrowserTestFixture() override;
201 Browser* current_browser() { return current_browser_; }
202 void set_html_for_tests(const std::string& html_for_tests) {
203 html_for_tests_ = html_for_tests;
205 const GURL& current_url() const { return current_url_; }
206 const GURL& iframe_url(size_t i) const { return iframe_urls_[i]; }
207 double fake_latitude() const { return fake_latitude_; }
208 double fake_longitude() const { return fake_longitude_; }
210 // Initializes the test server and navigates to the initial url.
211 void Initialize(InitializationOptions options);
213 // Loads two iframes with different origins: http://127.0.0.1 and
214 // http://localhost.
215 void LoadIFrames();
217 // Specifies which frame to use for executing JavaScript.
218 void SetFrameForScriptExecution(const std::string& frame_name);
220 // Gets the HostContentSettingsMap for the current profile.
221 HostContentSettingsMap* GetHostContentSettingsMap();
223 // Calls watchPosition in JavaScript and accepts or denies the resulting
224 // permission bubble. Returns |true| if the expected behavior happened.
225 bool WatchPositionAndGrantPermission() WARN_UNUSED_RESULT;
226 bool WatchPositionAndDenyPermission() WARN_UNUSED_RESULT;
228 // Calls watchPosition in JavaScript and observes whether the permission
229 // bubble is shown without interacting with it. Callers should set
230 // |bubble_should_display| to |true| if they expect a bubble to display.
231 void WatchPositionAndObservePermissionBubble(bool bubble_should_display);
233 // Checks that no errors have been received in JavaScript, and checks that the
234 // position most recently received matches |latitude| and |longitude|.
235 void ExpectPosition(double latitude, double longitude);
237 // Executes |function| in |render_frame_host| and checks that the return value
238 // matches |expected|.
239 void ExpectValueFromScriptForFrame(
240 const std::string& expected,
241 const std::string& function,
242 content::RenderFrameHost* render_frame_host);
244 // Executes |function| and checks that the return value matches |expected|.
245 void ExpectValueFromScript(const std::string& expected,
246 const std::string& function);
248 // Sets a new (second) position and runs all callbacks currently registered
249 // with the Geolocation system. Returns |true| if the new position is updated
250 // successfully in JavaScript.
251 bool SetPositionAndWaitUntilUpdated(double latitude, double longitude);
253 // Convenience method to look up the number of queued permission bubbles.
254 int GetBubbleQueueSize(PermissionBubbleManager* manager);
256 private:
257 // Calls watchPosition() in JavaScript and accepts or denies the resulting
258 // permission bubble. Returns the JavaScript response.
259 std::string WatchPositionAndRespondToPermissionBubble(
260 PermissionBubbleManager::AutoResponseType bubble_response);
262 // The current Browser as set in Initialize. May be for an incognito profile.
263 Browser* current_browser_ = nullptr;
265 // The path element of a URL referencing the html content for this test.
266 std::string html_for_tests_ = "/geolocation/simple.html";
268 // The frame where the JavaScript calls will run.
269 content::RenderFrameHost* render_frame_host_ = nullptr;
271 // The current url for the top level page.
272 GURL current_url_;
274 // The urls for the iframes loaded by LoadIFrames.
275 std::vector<GURL> iframe_urls_;
277 // The values used for the position override.
278 double fake_latitude_ = 1.23;
279 double fake_longitude_ = 4.56;
281 DISALLOW_COPY_AND_ASSIGN(GeolocationBrowserTest);
284 GeolocationBrowserTest::GeolocationBrowserTest() {
287 GeolocationBrowserTest::~GeolocationBrowserTest() {
290 void GeolocationBrowserTest::SetUpOnMainThread() {
291 ui_test_utils::OverrideGeolocation(fake_latitude_, fake_longitude_);
294 void GeolocationBrowserTest::TearDownInProcessBrowserTestFixture() {
295 LOG(WARNING) << "TearDownInProcessBrowserTestFixture. Test Finished.";
298 void GeolocationBrowserTest::Initialize(InitializationOptions options) {
299 if (!embedded_test_server()->Started() &&
300 !embedded_test_server()->InitializeAndWaitUntilReady()) {
301 ADD_FAILURE() << "Test server failed to start.";
302 return;
305 current_url_ = embedded_test_server()->GetURL(html_for_tests_);
306 if (options == INITIALIZATION_OFFTHERECORD) {
307 current_browser_ = OpenURLOffTheRecord(browser()->profile(), current_url_);
308 } else {
309 current_browser_ = browser();
310 if (options == INITIALIZATION_NEWTAB)
311 chrome::NewTab(current_browser_);
313 ASSERT_TRUE(current_browser_);
314 if (options != INITIALIZATION_OFFTHERECORD)
315 ui_test_utils::NavigateToURL(current_browser_, current_url_);
317 // By default the main frame is used for JavaScript execution.
318 SetFrameForScriptExecution("");
321 void GeolocationBrowserTest::LoadIFrames() {
322 int number_iframes = 2;
323 iframe_urls_.resize(number_iframes);
324 for (int i = 0; i < number_iframes; ++i) {
325 IFrameLoader loader(current_browser_, i, GURL());
326 iframe_urls_[i] = loader.iframe_url();
330 void GeolocationBrowserTest::SetFrameForScriptExecution(
331 const std::string& frame_name) {
332 content::WebContents* web_contents =
333 current_browser_->tab_strip_model()->GetActiveWebContents();
334 render_frame_host_ = nullptr;
336 if (frame_name.empty()) {
337 render_frame_host_ = web_contents->GetMainFrame();
338 } else {
339 render_frame_host_ = content::FrameMatchingPredicate(
340 web_contents, base::Bind(&content::FrameMatchesName, frame_name));
342 DCHECK(render_frame_host_);
345 HostContentSettingsMap* GeolocationBrowserTest::GetHostContentSettingsMap() {
346 return HostContentSettingsMapFactory::GetForProfile(
347 current_browser()->profile());
350 bool GeolocationBrowserTest::WatchPositionAndGrantPermission() {
351 std::string result = WatchPositionAndRespondToPermissionBubble(
352 PermissionBubbleManager::ACCEPT_ALL);
353 return "request-callback-success" == result;
356 bool GeolocationBrowserTest::WatchPositionAndDenyPermission() {
357 std::string result = WatchPositionAndRespondToPermissionBubble(
358 PermissionBubbleManager::DENY_ALL);
359 return "request-callback-error" == result;
362 std::string GeolocationBrowserTest::WatchPositionAndRespondToPermissionBubble(
363 PermissionBubbleManager::AutoResponseType bubble_response) {
364 PermissionBubbleManager::FromWebContents(
365 current_browser_->tab_strip_model()->GetActiveWebContents())
366 ->set_auto_response_for_test(bubble_response);
367 return RunScript(render_frame_host_, "geoStartWithAsyncResponse()");
370 void GeolocationBrowserTest::WatchPositionAndObservePermissionBubble(
371 bool bubble_should_display) {
372 PermissionBubbleObserver observer(
373 current_browser_->tab_strip_model()->GetActiveWebContents());
374 if (bubble_should_display) {
375 // Control will return as soon as the API call is made, and then the
376 // observer will wait for the bubble to display.
377 RunScript(render_frame_host_, "geoStartWithSyncResponse()");
378 observer.Wait();
379 } else {
380 // Control will return once one of the callbacks fires.
381 RunScript(render_frame_host_, "geoStartWithAsyncResponse()");
383 EXPECT_EQ(bubble_should_display, observer.request_shown());
386 void GeolocationBrowserTest::ExpectPosition(double latitude, double longitude) {
387 // Checks we have no error.
388 ExpectValueFromScript("0", "geoGetLastError()");
389 ExpectValueFromScript(base::DoubleToString(latitude),
390 "geoGetLastPositionLatitude()");
391 ExpectValueFromScript(base::DoubleToString(longitude),
392 "geoGetLastPositionLongitude()");
395 void GeolocationBrowserTest::ExpectValueFromScriptForFrame(
396 const std::string& expected,
397 const std::string& function,
398 content::RenderFrameHost* render_frame_host) {
399 std::string script(base::StringPrintf(
400 "window.domAutomationController.send(%s)", function.c_str()));
401 EXPECT_EQ(expected, RunScript(render_frame_host, script));
404 void GeolocationBrowserTest::ExpectValueFromScript(
405 const std::string& expected,
406 const std::string& function) {
407 ExpectValueFromScriptForFrame(expected, function, render_frame_host_);
410 bool GeolocationBrowserTest::SetPositionAndWaitUntilUpdated(double latitude,
411 double longitude) {
412 fake_latitude_ = latitude;
413 fake_longitude_ = longitude;
414 ui_test_utils::OverrideGeolocation(latitude, longitude);
416 // Now wait until the new position gets to the script.
417 // Control will return (a) if the update has already been received, or (b)
418 // when the update is received. This will hang if the position is never
419 // updated. Currently this expects the position to be updated once; if your
420 // test updates it repeatedly, |position_updated| (JS) needs to change to an
421 // int to count how often it's been updated.
422 std::string result =
423 RunScript(render_frame_host_, "checkIfGeopositionUpdated()");
424 return result == "geoposition-updated";
427 int GeolocationBrowserTest::GetBubbleQueueSize(
428 PermissionBubbleManager* manager) {
429 return static_cast<int>(manager->requests_.size());
432 // Tests ----------------------------------------------------------------------
434 #if defined(OS_LINUX)
435 // http://crbug.com/527437
436 #define MAYBE_DisplaysPrompt DISABLED_DisplaysPrompt
437 #else
438 #define MAYBE_DisplaysPrompt DisplaysPrompt
439 #endif
440 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_DisplaysPrompt) {
441 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
442 ASSERT_TRUE(WatchPositionAndGrantPermission());
445 #if defined(OS_LINUX)
446 // http://crbug.com/527437
447 #define MAYBE_Geoposition DISABLED_Geoposition
448 #else
449 #define MAYBE_Geoposition Geoposition
450 #endif
451 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_Geoposition) {
452 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
453 ASSERT_TRUE(WatchPositionAndGrantPermission());
454 ExpectPosition(fake_latitude(), fake_longitude());
457 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, ErrorOnPermissionDenied) {
458 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
459 EXPECT_TRUE(WatchPositionAndDenyPermission());
460 ExpectValueFromScript(GetErrorCodePermissionDenied(), "geoGetLastError()");
463 #if defined(OS_LINUX)
464 // http://crbug.com/527437
465 #define MAYBE_NoPromptForSecondTab DISABLED_NoPromptForSecondTab
466 #else
467 #define MAYBE_NoPromptForSecondTab NoPromptForSecondTab
468 #endif
469 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_NoPromptForSecondTab) {
470 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
471 ASSERT_TRUE(WatchPositionAndGrantPermission());
473 // Checks bubble is not needed in a second tab.
474 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_NEWTAB));
475 WatchPositionAndObservePermissionBubble(false);
476 ExpectPosition(fake_latitude(), fake_longitude());
479 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, NoPromptForDeniedOrigin) {
480 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
481 GetHostContentSettingsMap()->SetContentSetting(
482 ContentSettingsPattern::FromURLNoWildcard(current_url()),
483 ContentSettingsPattern::FromURLNoWildcard(current_url()),
484 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), CONTENT_SETTING_BLOCK);
486 // Check that the bubble wasn't shown but we get an error for this origin.
487 WatchPositionAndObservePermissionBubble(false);
488 ExpectValueFromScript(GetErrorCodePermissionDenied(), "geoGetLastError()");
490 // Checks prompt will not be created a second tab.
491 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_NEWTAB));
492 WatchPositionAndObservePermissionBubble(false);
493 ExpectValueFromScript(GetErrorCodePermissionDenied(), "geoGetLastError()");
496 #if defined(OS_LINUX)
497 // http://crbug.com/527437
498 #define MAYBE_NoPromptForAllowedOrigin DISABLED_NoPromptForAllowedOrigin
499 #else
500 #define MAYBE_NoPromptForAllowedOrigin NoPromptForAllowedOrigin
501 #endif
502 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_NoPromptForAllowedOrigin) {
503 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
504 GetHostContentSettingsMap()->SetContentSetting(
505 ContentSettingsPattern::FromURLNoWildcard(current_url()),
506 ContentSettingsPattern::FromURLNoWildcard(current_url()),
507 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), CONTENT_SETTING_ALLOW);
508 // The bubble is not shown, there is no error, and the position gets to the
509 // script.
510 WatchPositionAndObservePermissionBubble(false);
511 ExpectPosition(fake_latitude(), fake_longitude());
514 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, NoPromptForOffTheRecord) {
515 // For a regular profile the user is prompted, and when granted the position
516 // gets to the script.
517 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
518 ASSERT_TRUE(WatchPositionAndGrantPermission());
519 ExpectPosition(fake_latitude(), fake_longitude());
521 // The permission is persisted for the regular profile, and inherited by its
522 // incognito profile. Go incognito, and check that the user is not prompted
523 // again and the position gets to the script.
524 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_OFFTHERECORD));
525 WatchPositionAndObservePermissionBubble(false);
526 ExpectPosition(fake_latitude(), fake_longitude());
529 // http://crbug.com/523387
530 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest,
531 DISABLED_NoLeakFromOffTheRecord) {
532 // The user is prompted in a fresh incognito profile, and when granted the
533 // position gets to the script.
534 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_OFFTHERECORD));
535 ASSERT_TRUE(WatchPositionAndGrantPermission());
536 ExpectPosition(fake_latitude(), fake_longitude());
538 // The regular profile knows nothing of what happened in incognito. It is
539 // prompted and when granted the position gets to the script.
540 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
541 ASSERT_TRUE(WatchPositionAndGrantPermission());
542 ExpectPosition(fake_latitude(), fake_longitude());
545 #if defined(OS_LINUX)
546 // http://crbug.com/527437
547 #define MAYBE_IFramesWithFreshPosition DISABLED_IFramesWithFreshPosition
548 #else
549 #define MAYBE_IFramesWithFreshPosition IFramesWithFreshPosition
550 #endif
551 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_IFramesWithFreshPosition) {
552 set_html_for_tests("/geolocation/two_iframes.html");
553 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
554 LoadIFrames();
556 // Grant permission in the first frame, the position gets to the script.
557 SetFrameForScriptExecution("iframe_0");
558 ASSERT_TRUE(WatchPositionAndGrantPermission());
559 ExpectPosition(fake_latitude(), fake_longitude());
561 // In a second iframe from a different origin with a cached position the user
562 // is prompted.
563 SetFrameForScriptExecution("iframe_1");
564 WatchPositionAndObservePermissionBubble(true);
566 // Back to the first frame, enable navigation and refresh geoposition.
567 SetFrameForScriptExecution("iframe_0");
568 double fresh_position_latitude = 3.17;
569 double fresh_position_longitude = 4.23;
570 ASSERT_TRUE(SetPositionAndWaitUntilUpdated(fresh_position_latitude,
571 fresh_position_longitude));
572 ExpectPosition(fresh_position_latitude, fresh_position_longitude);
574 // When permission is granted to the second iframe the fresh position gets to
575 // the script.
576 SetFrameForScriptExecution("iframe_1");
577 ASSERT_TRUE(WatchPositionAndGrantPermission());
578 ExpectPosition(fresh_position_latitude, fresh_position_longitude);
581 #if defined(OS_LINUX)
582 // http://crbug.com/527437
583 #define MAYBE_IFramesWithCachedPosition DISABLED_IFramesWithCachedPosition
584 #else
585 #define MAYBE_IFramesWithCachedPosition IFramesWithCachedPosition
586 #endif
587 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest,
588 MAYBE_IFramesWithCachedPosition) {
589 set_html_for_tests("/geolocation/two_iframes.html");
590 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
591 LoadIFrames();
593 // Grant permission in the first frame, the position gets to the script.
594 SetFrameForScriptExecution("iframe_0");
595 ASSERT_TRUE(WatchPositionAndGrantPermission());
596 ExpectPosition(fake_latitude(), fake_longitude());
598 // Refresh the position, but let's not yet create the watch on the second
599 // frame so that it'll fetch from cache.
600 double cached_position_latitude = 5.67;
601 double cached_position_lognitude = 8.09;
602 ASSERT_TRUE(SetPositionAndWaitUntilUpdated(cached_position_latitude,
603 cached_position_lognitude));
604 ExpectPosition(cached_position_latitude, cached_position_lognitude);
606 // Now check the second frame gets cached values as well.
607 SetFrameForScriptExecution("iframe_1");
608 ASSERT_TRUE(WatchPositionAndGrantPermission());
609 ExpectPosition(cached_position_latitude, cached_position_lognitude);
612 // http://crbug.com/523387
613 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest,
614 DISABLED_CancelPermissionForFrame) {
615 set_html_for_tests("/geolocation/two_iframes.html");
616 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
617 LoadIFrames();
619 SetFrameForScriptExecution("iframe_0");
620 ASSERT_TRUE(WatchPositionAndGrantPermission());
621 ExpectPosition(fake_latitude(), fake_longitude());
623 // Test second iframe from a different origin with a cached position will
624 // create the prompt.
625 SetFrameForScriptExecution("iframe_1");
626 WatchPositionAndObservePermissionBubble(true);
628 // Navigate the iframe, and ensure the prompt is gone.
629 content::WebContents* web_contents =
630 current_browser()->tab_strip_model()->GetActiveWebContents();
631 IFrameLoader change_iframe_1(current_browser(), 1, current_url());
632 int num_bubbles_after_cancel = GetBubbleQueueSize(
633 PermissionBubbleManager::FromWebContents(web_contents));
634 EXPECT_EQ(0, num_bubbles_after_cancel);
637 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, InvalidUrlRequest) {
638 // Tests that an invalid URL (e.g. from a popup window) is rejected
639 // correctly. Also acts as a regression test for http://crbug.com/40478
640 set_html_for_tests("/geolocation/invalid_request_url.html");
641 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
643 content::WebContents* original_tab =
644 current_browser()->tab_strip_model()->GetActiveWebContents();
645 ExpectValueFromScript(GetErrorCodePermissionDenied(),
646 "requestGeolocationFromInvalidUrl()");
647 ExpectValueFromScriptForFrame("1", "isAlive()", original_tab->GetMainFrame());
650 #if defined(OS_LINUX)
651 // http://crbug.com/527437
652 #define MAYBE_NoPromptBeforeStart DISABLED_NoPromptBeforeStart
653 #else
654 #define MAYBE_NoPromptBeforeStart NoPromptBeforeStart
655 #endif
656 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_NoPromptBeforeStart) {
657 // See http://crbug.com/42789
658 set_html_for_tests("/geolocation/two_iframes.html");
659 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
660 LoadIFrames();
662 // In the second iframe, access the navigator.geolocation object, but don't
663 // call any methods yet so it won't request permission yet.
664 SetFrameForScriptExecution("iframe_1");
665 ExpectValueFromScript("object", "geoAccessNavigatorGeolocation()");
667 // In the first iframe, call watchPosition, grant permission, and verify that
668 // the position gets to the script.
669 SetFrameForScriptExecution("iframe_0");
670 ASSERT_TRUE(WatchPositionAndGrantPermission());
671 ExpectPosition(fake_latitude(), fake_longitude());
673 // Back to the second frame. The user is prompted now (it has a different
674 // origin). When permission is granted the position gets to the script.
675 SetFrameForScriptExecution("iframe_1");
676 ASSERT_TRUE(WatchPositionAndGrantPermission());
677 ExpectPosition(fake_latitude(), fake_longitude());
680 #if defined(OS_LINUX)
681 // http://crbug.com/527437
682 #define MAYBE_TwoWatchesInOneFrame DISABLED_TwoWatchesInOneFrame
683 #else
684 #define MAYBE_TwoWatchesInOneFrame TwoWatchesInOneFrame
685 #endif
686 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_TwoWatchesInOneFrame) {
687 set_html_for_tests("/geolocation/two_watches.html");
688 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
690 // Tell the script what to expect as the final coordinates.
691 double final_position_latitude = 3.17;
692 double final_position_longitude = 4.23;
693 std::string script =
694 base::StringPrintf("geoSetFinalPosition(%f, %f)", final_position_latitude,
695 final_position_longitude);
696 ExpectValueFromScript("ok", script);
698 // Request permission and set two watches for the initial success callback.
699 ASSERT_TRUE(WatchPositionAndGrantPermission());
700 ExpectPosition(fake_latitude(), fake_longitude());
702 // The second watch will now have cancelled. Ensure an update still makes
703 // its way through to the first watcher.
704 ASSERT_TRUE(SetPositionAndWaitUntilUpdated(final_position_latitude,
705 final_position_longitude));
706 ExpectPosition(final_position_latitude, final_position_longitude);
709 // TODO(felt): Disabled because the second permission request hangs.
710 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, DISABLED_PendingChildFrames) {
711 set_html_for_tests("/geolocation/two_iframes.html");
712 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
713 LoadIFrames();
715 SetFrameForScriptExecution("iframe_0");
716 WatchPositionAndObservePermissionBubble(true);
718 SetFrameForScriptExecution("iframe_1");
719 WatchPositionAndObservePermissionBubble(true);
722 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, TabDestroyed) {
723 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
724 WatchPositionAndObservePermissionBubble(true);
726 // TODO(mvanouwerkerk): Can't close a window you did not open. Maybe this was
727 // valid when the test was written, but now it just prints "Scripts may close
728 // only the windows that were opened by it."
729 std::string script = "window.domAutomationController.send(window.close())";
730 ASSERT_TRUE(content::ExecuteScript(
731 current_browser()->tab_strip_model()->GetActiveWebContents(), script));
734 #if defined(OS_LINUX)
735 // http://crbug.com/527437
736 #define MAYBE_LastUsageUpdated DISABLED_LastUsageUpdated
737 #else
738 #define MAYBE_LastUsageUpdated LastUsageUpdated
739 #endif
740 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_LastUsageUpdated) {
741 ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
742 base::SimpleTestClock* clock_ = new base::SimpleTestClock();
743 GetHostContentSettingsMap()->SetPrefClockForTesting(
744 scoped_ptr<base::Clock>(clock_));
745 clock_->SetNow(base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(10));
747 // Setting the permission should trigger the last usage.
748 GetHostContentSettingsMap()->SetContentSetting(
749 ContentSettingsPattern::FromURLNoWildcard(current_url()),
750 ContentSettingsPattern::FromURLNoWildcard(current_url()),
751 CONTENT_SETTINGS_TYPE_GEOLOCATION,
752 std::string(),
753 CONTENT_SETTING_ALLOW);
755 // Permission has been used at the starting time.
756 EXPECT_EQ(GetHostContentSettingsMap()->GetLastUsage(
757 current_url().GetOrigin(),
758 current_url().GetOrigin(),
759 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), 10);
761 clock_->Advance(base::TimeDelta::FromSeconds(3));
763 // Calling watchPosition should trigger the last usage update.
764 WatchPositionAndObservePermissionBubble(false);
765 ExpectPosition(fake_latitude(), fake_longitude());
767 // Last usage has been updated.
768 EXPECT_EQ(GetHostContentSettingsMap()->GetLastUsage(
769 current_url().GetOrigin(),
770 current_url().GetOrigin(),
771 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), 13);