Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / geolocation / geolocation_browsertest.cc
blob2517e83a40fa296145751ff2107d925ea6b81cb7
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/command_line.h"
8 #include "base/compiler_specific.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/test/simple_test_clock.h"
13 #include "base/time/clock.h"
14 #include "chrome/browser/chrome_notification_types.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/common/chrome_paths.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/test/base/in_process_browser_test.h"
24 #include "chrome/test/base/ui_test_utils.h"
25 #include "components/content_settings/core/browser/content_settings_usages_state.h"
26 #include "components/content_settings/core/browser/host_content_settings_map.h"
27 #include "components/content_settings/core/common/content_settings_pattern.h"
28 #include "content/public/browser/dom_operation_notification_details.h"
29 #include "content/public/browser/navigation_controller.h"
30 #include "content/public/browser/notification_details.h"
31 #include "content/public/browser/notification_service.h"
32 #include "content/public/browser/render_frame_host.h"
33 #include "content/public/browser/web_contents.h"
34 #include "content/public/test/browser_test_utils.h"
35 #include "net/base/net_util.h"
36 #include "net/test/embedded_test_server/embedded_test_server.h"
38 using content::DomOperationNotificationDetails;
39 using content::NavigationController;
40 using content::WebContents;
42 namespace {
44 // IFrameLoader ---------------------------------------------------------------
46 // Used to block until an iframe is loaded via a javascript call.
47 // Note: NavigateToURLBlockUntilNavigationsComplete doesn't seem to work for
48 // multiple embedded iframes, as notifications seem to be 'batched'. Instead, we
49 // load and wait one single frame here by calling a javascript function.
50 class IFrameLoader : public content::NotificationObserver {
51 public:
52 IFrameLoader(Browser* browser, int iframe_id, const GURL& url);
53 ~IFrameLoader() override;
55 // content::NotificationObserver:
56 void Observe(int type,
57 const content::NotificationSource& source,
58 const content::NotificationDetails& details) override;
60 const GURL& iframe_url() const { return iframe_url_; }
62 private:
63 content::NotificationRegistrar registrar_;
65 // If true the navigation has completed.
66 bool navigation_completed_;
68 // If true the javascript call has completed.
69 bool javascript_completed_;
71 std::string javascript_response_;
73 // The URL for the iframe we just loaded.
74 GURL iframe_url_;
76 DISALLOW_COPY_AND_ASSIGN(IFrameLoader);
79 IFrameLoader::IFrameLoader(Browser* browser, int iframe_id, const GURL& url)
80 : navigation_completed_(false),
81 javascript_completed_(false) {
82 WebContents* web_contents =
83 browser->tab_strip_model()->GetActiveWebContents();
84 NavigationController* controller = &web_contents->GetController();
85 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP,
86 content::Source<NavigationController>(controller));
87 registrar_.Add(this, content::NOTIFICATION_DOM_OPERATION_RESPONSE,
88 content::NotificationService::AllSources());
89 std::string script(base::StringPrintf(
90 "window.domAutomationController.setAutomationId(0);"
91 "window.domAutomationController.send(addIFrame(%d, \"%s\"));",
92 iframe_id, url.spec().c_str()));
93 web_contents->GetMainFrame()->ExecuteJavaScriptForTests(
94 base::UTF8ToUTF16(script));
95 content::RunMessageLoop();
97 EXPECT_EQ(base::StringPrintf("\"%d\"", iframe_id), javascript_response_);
98 registrar_.RemoveAll();
99 // Now that we loaded the iframe, let's fetch its src.
100 script = base::StringPrintf(
101 "window.domAutomationController.send(getIFrameSrc(%d))", iframe_id);
102 std::string iframe_src;
103 EXPECT_TRUE(content::ExecuteScriptAndExtractString(web_contents, script,
104 &iframe_src));
105 iframe_url_ = GURL(iframe_src);
108 IFrameLoader::~IFrameLoader() {
111 void IFrameLoader::Observe(int type,
112 const content::NotificationSource& source,
113 const content::NotificationDetails& details) {
114 if (type == content::NOTIFICATION_LOAD_STOP) {
115 navigation_completed_ = true;
116 } else if (type == content::NOTIFICATION_DOM_OPERATION_RESPONSE) {
117 content::Details<DomOperationNotificationDetails> dom_op_details(details);
118 javascript_response_ = dom_op_details->json;
119 javascript_completed_ = true;
121 if (javascript_completed_ && navigation_completed_)
122 base::MessageLoopForUI::current()->Quit();
125 // PermissionRequestObserver ---------------------------------------------------
127 // Used to observe the creation of permission prompt without responding.
128 class PermissionRequestObserver : public PermissionBubbleManager::Observer {
129 public:
130 explicit PermissionRequestObserver(content::WebContents* web_contents)
131 : bubble_manager_(PermissionBubbleManager::FromWebContents(web_contents)),
132 request_shown_(false),
133 message_loop_runner_(new content::MessageLoopRunner) {
134 bubble_manager_->AddObserver(this);
136 ~PermissionRequestObserver() override {
137 // Safe to remove twice if it happens.
138 bubble_manager_->RemoveObserver(this);
141 void Wait() { message_loop_runner_->Run(); }
143 bool request_shown() { return request_shown_; }
145 private:
146 // PermissionBubbleManager::Observer
147 void OnBubbleAdded() override {
148 request_shown_ = true;
149 bubble_manager_->RemoveObserver(this);
150 message_loop_runner_->Quit();
153 PermissionBubbleManager* bubble_manager_;
154 bool request_shown_;
155 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
157 DISALLOW_COPY_AND_ASSIGN(PermissionRequestObserver);
160 } // namespace
163 // GeolocationBrowserTest -----------------------------------------------------
165 // This is a browser test for Geolocation.
166 // It exercises various integration points from javascript <-> browser:
167 // 1. Prompt is displayed when a geolocation is requested from an unauthorized
168 // origin.
169 // 2. Denying the request triggers the correct error callback.
170 // 3. Allowing the request does not trigger an error, and allow a geoposition to
171 // be passed to javascript.
172 // 4. Permissions persisted in disk are respected.
173 // 5. Incognito profiles don't use saved permissions.
174 class GeolocationBrowserTest : public InProcessBrowserTest {
175 public:
176 enum InitializationOptions {
177 INITIALIZATION_NONE,
178 INITIALIZATION_OFFTHERECORD,
179 INITIALIZATION_NEWTAB,
180 INITIALIZATION_IFRAMES,
183 GeolocationBrowserTest();
184 ~GeolocationBrowserTest() override;
186 // InProcessBrowserTest:
187 void SetUpOnMainThread() override;
188 void TearDownInProcessBrowserTestFixture() override;
190 Browser* current_browser() { return current_browser_; }
191 void set_html_for_tests(const std::string& html_for_tests) {
192 html_for_tests_ = html_for_tests;
194 content::RenderFrameHost* frame_host() const { return render_frame_host_; }
195 const GURL& current_url() const { return current_url_; }
196 const GURL& iframe_url(size_t i) const { return iframe_urls_[i]; }
197 double fake_latitude() const { return fake_latitude_; }
198 double fake_longitude() const { return fake_longitude_; }
200 // Initializes the test server and navigates to the initial url.
201 bool Initialize(InitializationOptions options) WARN_UNUSED_RESULT;
203 // Loads the specified number of iframes.
204 void LoadIFrames(int number_iframes);
206 // Specifies which frame is to be used for JavaScript calls.
207 void SetFrameHost(const std::string& frame_name);
209 // Check geolocation and accept or deny the resulting permission bubble.
210 // Returns |true| if the expected behavior happened.
211 bool RequestAndAcceptPermission() WARN_UNUSED_RESULT;
212 bool RequestAndDenyPermission() WARN_UNUSED_RESULT;
214 // Check geolocation and observe whether the permission bubble was shown.
215 // Callers should set |bubble_should_display| to |true| if they expect a
216 // bubble to display.
217 void RequestPermissionAndObserve(bool bubble_should_display);
219 // Checks that no errors have been received in javascript, and checks that the
220 // position most recently received in javascript matches |latitude| and
221 // |longitude|.
222 void CheckGeoposition(double latitude, double longitude);
224 // Checks whether a coordinate change has been registered yet (and waits if
225 // it hasn't). After sending new geoposition coordinates, call this before
226 // CheckGeoposition to avoid a race condition.
227 bool CheckGeopositionUpdated() WARN_UNUSED_RESULT;
229 // Executes |function| in |render_frame_host| and checks that the return value
230 // matches |expected|.
231 void CheckStringValueFromJavascriptForFrame(
232 const std::string& expected,
233 const std::string& function,
234 content::RenderFrameHost* render_frame_host);
236 // Executes |function| and checks that the return value matches |expected|.
237 void CheckStringValueFromJavascript(const std::string& expected,
238 const std::string& function);
240 // Sets a new position and sends a notification with the new position. Call
241 // CheckGeopositionReadyCallback after this to make sure that the value has
242 // been received by the JavaScript.
243 void NotifyGeoposition(double latitude, double longitude);
245 // Convenience method to look up the number of queued permission bubbles.
246 int GetBubblesQueueSize(PermissionBubbleManager* mgr);
248 private:
249 std::string RequestAndRespondToPermission(
250 PermissionBubbleManager::AutoResponseType bubble_response);
252 Browser* current_browser_;
253 // path element of a URL referencing the html content for this test.
254 std::string html_for_tests_;
255 // This member defines the frame where the JavaScript calls will run.
256 content::RenderFrameHost* render_frame_host_;
257 // The current url for the top level page.
258 GURL current_url_;
259 // If not empty, the GURLs for the iframes loaded by LoadIFrames().
260 std::vector<GURL> iframe_urls_;
261 double fake_latitude_;
262 double fake_longitude_;
264 DISALLOW_COPY_AND_ASSIGN(GeolocationBrowserTest);
267 GeolocationBrowserTest::GeolocationBrowserTest()
268 : current_browser_(nullptr),
269 html_for_tests_("/geolocation/simple.html"),
270 render_frame_host_(nullptr),
271 fake_latitude_(1.23),
272 fake_longitude_(4.56) {
275 GeolocationBrowserTest::~GeolocationBrowserTest() {
278 void GeolocationBrowserTest::SetUpOnMainThread() {
279 ui_test_utils::OverrideGeolocation(fake_latitude_, fake_longitude_);
282 void GeolocationBrowserTest::TearDownInProcessBrowserTestFixture() {
283 LOG(WARNING) << "TearDownInProcessBrowserTestFixture. Test Finished.";
286 bool GeolocationBrowserTest::Initialize(InitializationOptions options) {
287 if (!embedded_test_server()->Started() &&
288 !embedded_test_server()->InitializeAndWaitUntilReady()) {
289 ADD_FAILURE() << "Test server failed to start.";
290 return false;
293 current_url_ = embedded_test_server()->GetURL(html_for_tests_);
294 if (options == INITIALIZATION_OFFTHERECORD) {
295 current_browser_ = OpenURLOffTheRecord(browser()->profile(), current_url_);
296 } else {
297 current_browser_ = browser();
298 if (options == INITIALIZATION_NEWTAB)
299 chrome::NewTab(current_browser_);
301 if (options != INITIALIZATION_OFFTHERECORD)
302 ui_test_utils::NavigateToURL(current_browser_, current_url_);
304 EXPECT_TRUE(current_browser_);
305 return !!current_browser_;
308 void GeolocationBrowserTest::LoadIFrames(int number_iframes) {
309 // Limit to 3 iframes.
310 DCHECK_LT(0, number_iframes);
311 DCHECK_LE(number_iframes, 3);
312 iframe_urls_.resize(number_iframes);
313 for (int i = 0; i < number_iframes; ++i) {
314 IFrameLoader loader(current_browser_, i, GURL());
315 iframe_urls_[i] = loader.iframe_url();
319 void GeolocationBrowserTest::SetFrameHost(const std::string& frame_name) {
320 WebContents* web_contents =
321 current_browser_->tab_strip_model()->GetActiveWebContents();
322 render_frame_host_ = nullptr;
324 if (frame_name.empty()) {
325 render_frame_host_ = web_contents->GetMainFrame();
326 } else {
327 render_frame_host_ = content::FrameMatchingPredicate(
328 web_contents, base::Bind(&content::FrameMatchesName, frame_name));
330 DCHECK(render_frame_host_);
333 bool GeolocationBrowserTest::RequestAndAcceptPermission() {
334 std::string result =
335 RequestAndRespondToPermission(PermissionBubbleManager::ACCEPT_ALL);
336 return "request-callback-success" == result;
339 bool GeolocationBrowserTest::RequestAndDenyPermission() {
340 std::string result =
341 RequestAndRespondToPermission(PermissionBubbleManager::DENY_ALL);
342 return "request-callback-error" == result;
345 std::string GeolocationBrowserTest::RequestAndRespondToPermission(
346 PermissionBubbleManager::AutoResponseType bubble_response) {
347 std::string result;
348 PermissionBubbleManager::FromWebContents(
349 current_browser_->tab_strip_model()->GetActiveWebContents())
350 ->set_auto_response_for_test(bubble_response);
351 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
352 render_frame_host_, "geoStartWithAsyncResponse();", &result));
353 return result;
356 void GeolocationBrowserTest::RequestPermissionAndObserve(
357 bool bubble_should_display) {
358 std::string result;
359 PermissionRequestObserver observer(
360 current_browser_->tab_strip_model()->GetActiveWebContents());
361 if (bubble_should_display) {
362 // Control will return as soon as the API call is made, and then the
363 // observer will wait for the bubble to display.
364 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
365 render_frame_host_, "geoStartWithSyncResponse()", &result));
366 observer.Wait();
367 } else {
368 // Control will return once one of the callbacks fires.
369 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
370 render_frame_host_, "geoStartWithAsyncResponse()", &result));
372 EXPECT_EQ(bubble_should_display, observer.request_shown());
375 void GeolocationBrowserTest::CheckGeoposition(double latitude,
376 double longitude) {
377 // Checks we have no error.
378 CheckStringValueFromJavascript("0", "geoGetLastError()");
379 CheckStringValueFromJavascript(base::DoubleToString(latitude),
380 "geoGetLastPositionLatitude()");
381 CheckStringValueFromJavascript(base::DoubleToString(longitude),
382 "geoGetLastPositionLongitude()");
385 bool GeolocationBrowserTest::CheckGeopositionUpdated() {
386 // Control will return (a) if the update has already been received, or (b)
387 // when the update is received. This will hang if the geolocation is never
388 // updated. Currently this expects geoposition to be updated once; if your
389 // test updates geoposition repeatedly, |position_updated| (JS) needs to
390 // change to an int to count how often it's been updated.
391 std::string result;
392 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
393 render_frame_host_, "checkIfGeopositionUpdated();", &result));
394 return result == "geoposition-updated";
397 void GeolocationBrowserTest::CheckStringValueFromJavascriptForFrame(
398 const std::string& expected,
399 const std::string& function,
400 content::RenderFrameHost* render_frame_host) {
401 std::string script(base::StringPrintf(
402 "window.domAutomationController.send(%s)", function.c_str()));
403 std::string result;
404 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
405 render_frame_host, script, &result));
406 EXPECT_EQ(expected, result);
409 void GeolocationBrowserTest::CheckStringValueFromJavascript(
410 const std::string& expected,
411 const std::string& function) {
412 CheckStringValueFromJavascriptForFrame(
413 expected, function, render_frame_host_);
416 void GeolocationBrowserTest::NotifyGeoposition(double latitude,
417 double longitude) {
418 fake_latitude_ = latitude;
419 fake_longitude_ = longitude;
420 ui_test_utils::OverrideGeolocation(latitude, longitude);
423 int GeolocationBrowserTest::GetBubblesQueueSize(PermissionBubbleManager* mgr) {
424 return static_cast<int>(mgr->requests_.size());
427 // Tests ----------------------------------------------------------------------
429 #if defined(OS_LINUX)
430 // http://crbug.com/527437
431 #define MAYBE_DisplaysPrompt DISABLED_DisplaysPrompt
432 #else
433 #define MAYBE_DisplaysPrompt DisplaysPrompt
434 #endif
435 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_DisplaysPrompt) {
436 ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
437 SetFrameHost("");
438 ASSERT_TRUE(RequestAndAcceptPermission());
441 #if defined(OS_LINUX)
442 // http://crbug.com/527437
443 #define MAYBE_Geoposition DISABLED_Geoposition
444 #else
445 #define MAYBE_Geoposition Geoposition
446 #endif
447 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_Geoposition) {
448 ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
449 SetFrameHost("");
450 ASSERT_TRUE(RequestAndAcceptPermission());
451 CheckGeoposition(fake_latitude(), fake_longitude());
454 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, ErrorOnPermissionDenied) {
455 ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
456 SetFrameHost("");
457 EXPECT_TRUE(RequestAndDenyPermission());
458 CheckStringValueFromJavascript("1", "geoGetLastError()");
461 #if defined(OS_LINUX)
462 // http://crbug.com/527437
463 #define MAYBE_NoPromptForSecondTab DISABLED_NoPromptForSecondTab
464 #else
465 #define MAYBE_NoPromptForSecondTab NoPromptForSecondTab
466 #endif
467 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_NoPromptForSecondTab) {
468 ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
469 SetFrameHost("");
470 ASSERT_TRUE(RequestAndAcceptPermission());
472 // Checks bubble is not needed in a second tab.
473 ASSERT_TRUE(Initialize(INITIALIZATION_NEWTAB));
474 SetFrameHost("");
475 RequestPermissionAndObserve(false);
476 CheckGeoposition(fake_latitude(), fake_longitude());
479 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, NoPromptForDeniedOrigin) {
480 ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
481 current_browser()->profile()->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 SetFrameHost("");
488 RequestPermissionAndObserve(false);
489 CheckStringValueFromJavascript("1", "geoGetLastError()");
491 // Checks prompt will not be created a second tab.
492 ASSERT_TRUE(Initialize(INITIALIZATION_NEWTAB));
493 SetFrameHost("");
494 RequestPermissionAndObserve(false);
495 CheckStringValueFromJavascript("1", "geoGetLastError()");
498 #if defined(OS_LINUX)
499 // http://crbug.com/527437
500 #define MAYBE_NoPromptForAllowedOrigin DISABLED_NoPromptForAllowedOrigin
501 #else
502 #define MAYBE_NoPromptForAllowedOrigin NoPromptForAllowedOrigin
503 #endif
504 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_NoPromptForAllowedOrigin) {
505 ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
506 current_browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
507 ContentSettingsPattern::FromURLNoWildcard(current_url()),
508 ContentSettingsPattern::FromURLNoWildcard(current_url()),
509 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), CONTENT_SETTING_ALLOW);
510 // Checks no prompt will be created and there's no error callback.
511 SetFrameHost("");
512 RequestPermissionAndObserve(false);
513 CheckGeoposition(fake_latitude(), fake_longitude());
516 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, NoPromptForOffTheRecord) {
517 // Check prompt will be created and persisted for regular profile.
518 ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
519 SetFrameHost("");
520 ASSERT_TRUE(RequestAndAcceptPermission());
521 CheckGeoposition(fake_latitude(), fake_longitude());
523 // Go incognito, and check no prompt will be created.
524 ASSERT_TRUE(Initialize(INITIALIZATION_OFFTHERECORD));
525 SetFrameHost("");
526 RequestPermissionAndObserve(false);
527 CheckGeoposition(fake_latitude(), fake_longitude());
530 // http://crbug.com/523387
531 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest,
532 DISABLED_NoLeakFromOffTheRecord) {
533 // Check prompt will be created for incognito profile.
534 ASSERT_TRUE(Initialize(INITIALIZATION_OFFTHERECORD));
535 SetFrameHost("");
536 ASSERT_TRUE(RequestAndAcceptPermission());
537 CheckGeoposition(fake_latitude(), fake_longitude());
539 // Check prompt will be created for the regular profile.
540 ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
541 SetFrameHost("");
542 ASSERT_TRUE(RequestAndAcceptPermission());
543 CheckGeoposition(fake_latitude(), fake_longitude());
546 #if defined(OS_LINUX)
547 // http://crbug.com/527437
548 #define MAYBE_IFramesWithFreshPosition DISABLED_IFramesWithFreshPosition
549 #else
550 #define MAYBE_IFramesWithFreshPosition IFramesWithFreshPosition
551 #endif
552 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_IFramesWithFreshPosition) {
553 set_html_for_tests("/geolocation/two_iframes.html");
554 ASSERT_TRUE(Initialize(INITIALIZATION_IFRAMES));
555 LoadIFrames(2);
557 // Request permission from the first frame.
558 SetFrameHost("iframe_0");
559 ASSERT_TRUE(RequestAndAcceptPermission());
560 CheckGeoposition(fake_latitude(), fake_longitude());
562 // Test second iframe from a different origin with a cached geoposition will
563 // create the prompt.
564 SetFrameHost("iframe_1");
565 RequestPermissionAndObserve(true);
567 // Back to the first frame, enable navigation and refresh geoposition.
568 SetFrameHost("iframe_0");
569 double fresh_position_latitude = 3.17;
570 double fresh_position_longitude = 4.23;
571 NotifyGeoposition(fresh_position_latitude, fresh_position_longitude);
572 ASSERT_TRUE(CheckGeopositionUpdated());
573 CheckGeoposition(fresh_position_latitude, fresh_position_longitude);
575 // Authorize the second frame and check it works.
576 SetFrameHost("iframe_1");
577 ASSERT_TRUE(RequestAndAcceptPermission());
578 CheckGeoposition(fake_latitude(), fake_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_TRUE(Initialize(INITIALIZATION_IFRAMES));
591 LoadIFrames(2);
593 // Request permission from the first frame.
594 SetFrameHost("iframe_0");
595 ASSERT_TRUE(RequestAndAcceptPermission());
596 CheckGeoposition(fake_latitude(), fake_longitude());
598 // Refresh geoposition, but let's not yet create the watch on the second frame
599 // so that it'll fetch from cache.
600 double cached_position_latitude = 5.67;
601 double cached_position_lognitude = 8.09;
602 NotifyGeoposition(cached_position_latitude, cached_position_lognitude);
603 ASSERT_TRUE(CheckGeopositionUpdated());
604 CheckGeoposition(cached_position_latitude, cached_position_lognitude);
606 // Now check the second frame gets cached values as well.
607 SetFrameHost("iframe_1");
608 ASSERT_TRUE(RequestAndAcceptPermission());
609 CheckGeoposition(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_TRUE(Initialize(INITIALIZATION_IFRAMES));
617 LoadIFrames(2);
619 SetFrameHost("iframe_0");
620 ASSERT_TRUE(RequestAndAcceptPermission());
621 CheckGeoposition(fake_latitude(), fake_longitude());
623 // Test second iframe from a different origin with a cached geoposition will
624 // create the prompt.
625 SetFrameHost("iframe_1");
626 RequestPermissionAndObserve(true);
628 // Navigate the iframe, and ensure the prompt is gone.
629 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 = GetBubblesQueueSize(
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_TRUE(Initialize(INITIALIZATION_NONE));
643 SetFrameHost("");
644 WebContents* original_tab =
645 current_browser()->tab_strip_model()->GetActiveWebContents();
646 CheckStringValueFromJavascript("1", "requestGeolocationFromInvalidUrl()");
647 CheckStringValueFromJavascriptForFrame("1", "isAlive()",
648 original_tab->GetMainFrame());
651 #if defined(OS_LINUX)
652 // http://crbug.com/527437
653 #define MAYBE_NoPromptBeforeStart DISABLED_NoPromptBeforeStart
654 #else
655 #define MAYBE_NoPromptBeforeStart NoPromptBeforeStart
656 #endif
657 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_NoPromptBeforeStart) {
658 // See http://crbug.com/42789
659 set_html_for_tests("/geolocation/two_iframes.html");
660 ASSERT_TRUE(Initialize(INITIALIZATION_IFRAMES));
661 LoadIFrames(2);
663 // Access navigator.geolocation, but ensure it won't request permission.
664 SetFrameHost("iframe_1");
665 CheckStringValueFromJavascript("object", "geoAccessNavigatorGeolocation()");
667 SetFrameHost("iframe_0");
668 ASSERT_TRUE(RequestAndAcceptPermission());
669 CheckGeoposition(fake_latitude(), fake_longitude());
671 // Permission should be requested after adding a watch.
672 SetFrameHost("iframe_1");
673 ASSERT_TRUE(RequestAndAcceptPermission());
674 CheckGeoposition(fake_latitude(), fake_longitude());
677 #if defined(OS_LINUX)
678 // http://crbug.com/527437
679 #define MAYBE_TwoWatchesInOneFrame DISABLED_TwoWatchesInOneFrame
680 #else
681 #define MAYBE_TwoWatchesInOneFrame TwoWatchesInOneFrame
682 #endif
683 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_TwoWatchesInOneFrame) {
684 set_html_for_tests("/geolocation/two_watches.html");
685 ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
686 SetFrameHost("");
688 // Tell the JS what to expect as the final coordinates.
689 double final_position_latitude = 3.17;
690 double final_position_longitude = 4.23;
691 std::string script =
692 base::StringPrintf("geoSetFinalPosition(%f, %f)", final_position_latitude,
693 final_position_longitude);
694 CheckStringValueFromJavascript("ok", script);
696 // Request permission and set two watches for the initial success callback.
697 ASSERT_TRUE(RequestAndAcceptPermission());
698 CheckGeoposition(fake_latitude(), fake_longitude());
700 // The second watch will now have cancelled. Ensure an update still makes
701 // its way through to the first watcher.
702 NotifyGeoposition(final_position_latitude, final_position_longitude);
703 ASSERT_TRUE(CheckGeopositionUpdated());
704 CheckGeoposition(final_position_latitude, final_position_longitude);
707 // TODO(felt): Disabled because the second permission request hangs.
708 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, DISABLED_PendingChildFrames) {
709 set_html_for_tests("/geolocation/two_iframes.html");
710 ASSERT_TRUE(Initialize(INITIALIZATION_IFRAMES));
711 LoadIFrames(2);
713 SetFrameHost("iframe_0");
714 RequestPermissionAndObserve(true);
716 SetFrameHost("iframe_1");
717 RequestPermissionAndObserve(true);
720 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, TabDestroyed) {
721 ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
722 SetFrameHost("");
723 RequestPermissionAndObserve(true);
725 std::string script =
726 "window.domAutomationController.send(window.close());";
727 bool result = content::ExecuteScript(
728 current_browser()->tab_strip_model()->GetActiveWebContents(), script);
729 EXPECT_EQ(result, true);
732 #if defined(OS_LINUX)
733 // http://crbug.com/527437
734 #define MAYBE_LastUsageUpdated DISABLED_LastUsageUpdated
735 #else
736 #define MAYBE_LastUsageUpdated LastUsageUpdated
737 #endif
738 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_LastUsageUpdated) {
739 ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
740 base::SimpleTestClock* clock_ = new base::SimpleTestClock();
741 current_browser()
742 ->profile()
743 ->GetHostContentSettingsMap()
744 ->SetPrefClockForTesting(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 current_browser()->profile()->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(current_browser()
757 ->profile()
758 ->GetHostContentSettingsMap()
759 ->GetLastUsage(current_url().GetOrigin(),
760 current_url().GetOrigin(),
761 CONTENT_SETTINGS_TYPE_GEOLOCATION)
762 .ToDoubleT(),
763 10);
765 clock_->Advance(base::TimeDelta::FromSeconds(3));
767 // Watching should trigger the last usage update.
768 SetFrameHost("");
769 RequestPermissionAndObserve(false);
770 CheckGeoposition(fake_latitude(), fake_longitude());
772 // Last usage has been updated.
773 EXPECT_EQ(current_browser()
774 ->profile()
775 ->GetHostContentSettingsMap()
776 ->GetLastUsage(current_url().GetOrigin(),
777 current_url().GetOrigin(),
778 CONTENT_SETTINGS_TYPE_GEOLOCATION)
779 .ToDoubleT(),
780 13);