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.
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 "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/content_settings/content_settings_usages_state.h"
13 #include "chrome/browser/content_settings/host_content_settings_map.h"
14 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
15 #include "chrome/browser/infobars/confirm_infobar_delegate.h"
16 #include "chrome/browser/infobars/infobar_service.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/browser_commands.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/common/chrome_paths.h"
22 #include "chrome/common/content_settings_pattern.h"
23 #include "chrome/test/base/in_process_browser_test.h"
24 #include "chrome/test/base/ui_test_utils.h"
25 #include "components/infobars/core/infobar.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/test/browser_test_utils.h"
33 #include "net/base/net_util.h"
34 #include "net/test/embedded_test_server/embedded_test_server.h"
36 using content::DomOperationNotificationDetails
;
37 using content::NavigationController
;
38 using content::WebContents
;
42 // IFrameLoader ---------------------------------------------------------------
44 // Used to block until an iframe is loaded via a javascript call.
45 // Note: NavigateToURLBlockUntilNavigationsComplete doesn't seem to work for
46 // multiple embedded iframes, as notifications seem to be 'batched'. Instead, we
47 // load and wait one single frame here by calling a javascript function.
48 class IFrameLoader
: public content::NotificationObserver
{
50 IFrameLoader(Browser
* browser
, int iframe_id
, const GURL
& url
);
51 virtual ~IFrameLoader();
53 // content::NotificationObserver:
54 virtual void Observe(int type
,
55 const content::NotificationSource
& source
,
56 const content::NotificationDetails
& details
) OVERRIDE
;
58 const GURL
& iframe_url() const { return iframe_url_
; }
61 content::NotificationRegistrar registrar_
;
63 // If true the navigation has completed.
64 bool navigation_completed_
;
66 // If true the javascript call has completed.
67 bool javascript_completed_
;
69 std::string javascript_response_
;
71 // The URL for the iframe we just loaded.
74 DISALLOW_COPY_AND_ASSIGN(IFrameLoader
);
77 IFrameLoader::IFrameLoader(Browser
* browser
, int iframe_id
, const GURL
& url
)
78 : navigation_completed_(false),
79 javascript_completed_(false) {
80 WebContents
* web_contents
=
81 browser
->tab_strip_model()->GetActiveWebContents();
82 NavigationController
* controller
= &web_contents
->GetController();
83 registrar_
.Add(this, content::NOTIFICATION_LOAD_STOP
,
84 content::Source
<NavigationController
>(controller
));
85 registrar_
.Add(this, content::NOTIFICATION_DOM_OPERATION_RESPONSE
,
86 content::NotificationService::AllSources());
87 std::string
script(base::StringPrintf(
88 "window.domAutomationController.setAutomationId(0);"
89 "window.domAutomationController.send(addIFrame(%d, \"%s\"));",
90 iframe_id
, url
.spec().c_str()));
91 web_contents
->GetMainFrame()->ExecuteJavaScript(base::UTF8ToUTF16(script
));
92 content::RunMessageLoop();
94 EXPECT_EQ(base::StringPrintf("\"%d\"", iframe_id
), javascript_response_
);
95 registrar_
.RemoveAll();
96 // Now that we loaded the iframe, let's fetch its src.
97 script
= base::StringPrintf(
98 "window.domAutomationController.send(getIFrameSrc(%d))", iframe_id
);
99 std::string iframe_src
;
100 EXPECT_TRUE(content::ExecuteScriptAndExtractString(web_contents
, script
,
102 iframe_url_
= GURL(iframe_src
);
105 IFrameLoader::~IFrameLoader() {
108 void IFrameLoader::Observe(int type
,
109 const content::NotificationSource
& source
,
110 const content::NotificationDetails
& details
) {
111 if (type
== content::NOTIFICATION_LOAD_STOP
) {
112 navigation_completed_
= true;
113 } else if (type
== content::NOTIFICATION_DOM_OPERATION_RESPONSE
) {
114 content::Details
<DomOperationNotificationDetails
> dom_op_details(details
);
115 javascript_response_
= dom_op_details
->json
;
116 javascript_completed_
= true;
118 if (javascript_completed_
&& navigation_completed_
)
119 base::MessageLoopForUI::current()->Quit();
122 // GeolocationNotificationObserver --------------------------------------------
124 class GeolocationNotificationObserver
: public content::NotificationObserver
{
126 // If |wait_for_infobar| is true, AddWatchAndWaitForNotification will block
127 // until the infobar has been displayed; otherwise it will block until the
128 // navigation is completed.
129 explicit GeolocationNotificationObserver(bool wait_for_infobar
);
130 virtual ~GeolocationNotificationObserver();
132 // content::NotificationObserver:
133 virtual void Observe(int type
,
134 const content::NotificationSource
& source
,
135 const content::NotificationDetails
& details
) OVERRIDE
;
137 void AddWatchAndWaitForNotification(
138 content::RenderFrameHost
* render_frame_host
);
140 bool has_infobar() const { return !!infobar_
; }
141 infobars::InfoBar
* infobar() { return infobar_
; }
144 content::NotificationRegistrar registrar_
;
145 bool wait_for_infobar_
;
146 infobars::InfoBar
* infobar_
;
147 bool navigation_started_
;
148 bool navigation_completed_
;
149 std::string javascript_response_
;
151 DISALLOW_COPY_AND_ASSIGN(GeolocationNotificationObserver
);
154 GeolocationNotificationObserver::GeolocationNotificationObserver(
155 bool wait_for_infobar
)
156 : wait_for_infobar_(wait_for_infobar
),
158 navigation_started_(false),
159 navigation_completed_(false) {
160 registrar_
.Add(this, content::NOTIFICATION_DOM_OPERATION_RESPONSE
,
161 content::NotificationService::AllSources());
162 if (wait_for_infobar
) {
163 registrar_
.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
,
164 content::NotificationService::AllSources());
166 registrar_
.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
167 content::NotificationService::AllSources());
168 registrar_
.Add(this, content::NOTIFICATION_LOAD_START
,
169 content::NotificationService::AllSources());
170 registrar_
.Add(this, content::NOTIFICATION_LOAD_STOP
,
171 content::NotificationService::AllSources());
175 GeolocationNotificationObserver::~GeolocationNotificationObserver() {
178 void GeolocationNotificationObserver::Observe(
180 const content::NotificationSource
& source
,
181 const content::NotificationDetails
& details
) {
182 if (type
== chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
) {
183 infobar_
= content::Details
<infobars::InfoBar::AddedDetails
>(details
).ptr();
184 ASSERT_FALSE(infobar_
->delegate()->GetIcon().IsEmpty());
185 ASSERT_TRUE(infobar_
->delegate()->AsConfirmInfoBarDelegate());
186 } else if (type
== content::NOTIFICATION_DOM_OPERATION_RESPONSE
) {
187 content::Details
<DomOperationNotificationDetails
> dom_op_details(details
);
188 javascript_response_
= dom_op_details
->json
;
189 LOG(WARNING
) << "javascript_response " << javascript_response_
;
190 } else if ((type
== content::NOTIFICATION_NAV_ENTRY_COMMITTED
) ||
191 (type
== content::NOTIFICATION_LOAD_START
)) {
192 navigation_started_
= true;
193 } else if ((type
== content::NOTIFICATION_LOAD_STOP
) && navigation_started_
) {
194 navigation_started_
= false;
195 navigation_completed_
= true;
198 // We're either waiting for just the infobar, or for both a javascript
199 // prompt and response.
200 if ((wait_for_infobar_
&& infobar_
) ||
201 (navigation_completed_
&& !javascript_response_
.empty()))
202 base::MessageLoopForUI::current()->Quit();
205 void GeolocationNotificationObserver::AddWatchAndWaitForNotification(
206 content::RenderFrameHost
* render_frame_host
) {
207 LOG(WARNING
) << "will add geolocation watch";
209 "window.domAutomationController.setAutomationId(0);"
210 "window.domAutomationController.send(geoStart());");
211 render_frame_host
->ExecuteJavaScript(base::UTF8ToUTF16(script
));
212 content::RunMessageLoop();
213 registrar_
.RemoveAll();
214 LOG(WARNING
) << "got geolocation watch" << javascript_response_
;
215 EXPECT_NE("\"0\"", javascript_response_
);
216 EXPECT_TRUE(wait_for_infobar_
? (infobar_
!= NULL
) : navigation_completed_
);
222 // GeolocationBrowserTest -----------------------------------------------------
224 // This is a browser test for Geolocation.
225 // It exercises various integration points from javascript <-> browser:
226 // 1. Infobar is displayed when a geolocation is requested from an unauthorized
228 // 2. Denying the infobar triggers the correct error callback.
229 // 3. Allowing the infobar does not trigger an error, and allow a geoposition to
230 // be passed to javascript.
231 // 4. Permissions persisted in disk are respected.
232 // 5. Incognito profiles don't use saved permissions.
233 class GeolocationBrowserTest
: public InProcessBrowserTest
{
235 enum InitializationOptions
{
237 INITIALIZATION_OFFTHERECORD
,
238 INITIALIZATION_NEWTAB
,
239 INITIALIZATION_IFRAMES
,
242 GeolocationBrowserTest();
243 virtual ~GeolocationBrowserTest();
245 // InProcessBrowserTest:
246 virtual void SetUpOnMainThread() OVERRIDE
;
247 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE
;
249 Browser
* current_browser() { return current_browser_
; }
250 void set_html_for_tests(const std::string
& html_for_tests
) {
251 html_for_tests_
= html_for_tests
;
253 content::RenderFrameHost
* frame_host() const { return render_frame_host_
; }
254 const GURL
& current_url() const { return current_url_
; }
255 const GURL
& iframe_url(size_t i
) const { return iframe_urls_
[i
]; }
256 double fake_latitude() const { return fake_latitude_
; }
257 double fake_longitude() const { return fake_longitude_
; }
259 // Initializes the test server and navigates to the initial url.
260 bool Initialize(InitializationOptions options
) WARN_UNUSED_RESULT
;
262 // Loads the specified number of iframes.
263 void LoadIFrames(int number_iframes
);
265 // Specifies which frame is to be used for JavaScript calls.
266 void SetFrameHost(const std::string
& frame_name
);
268 // Start watching for geolocation notifications. If |wait_for_infobar| is
269 // true, wait for the infobar to be displayed. Otherwise wait for a javascript
271 void AddGeolocationWatch(bool wait_for_infobar
);
273 // Checks that no errors have been received in javascript, and checks that the
274 // position most recently received in javascript matches |latitude| and
276 void CheckGeoposition(double latitude
, double longitude
);
278 // For |requesting_url| if |allowed| is true accept the infobar. Otherwise
280 void SetInfoBarResponse(const GURL
& requesting_url
, bool allowed
);
282 // Executes |function| in |render_frame_host| and checks that the return value
283 // matches |expected|.
284 void CheckStringValueFromJavascriptForFrame(
285 const std::string
& expected
,
286 const std::string
& function
,
287 content::RenderFrameHost
* render_frame_host
);
289 // Executes |function| and checks that the return value matches |expected|.
290 void CheckStringValueFromJavascript(const std::string
& expected
,
291 const std::string
& function
);
293 // Sets a new position and sends a notification with the new position.
294 void NotifyGeoposition(double latitude
, double longitude
);
297 infobars::InfoBar
* infobar_
;
298 Browser
* current_browser_
;
299 // path element of a URL referencing the html content for this test.
300 std::string html_for_tests_
;
301 // This member defines the frame where the JavaScript calls will run.
302 content::RenderFrameHost
* render_frame_host_
;
303 // The current url for the top level page.
305 // If not empty, the GURLs for the iframes loaded by LoadIFrames().
306 std::vector
<GURL
> iframe_urls_
;
307 double fake_latitude_
;
308 double fake_longitude_
;
310 DISALLOW_COPY_AND_ASSIGN(GeolocationBrowserTest
);
313 GeolocationBrowserTest::GeolocationBrowserTest()
315 current_browser_(NULL
),
316 html_for_tests_("/geolocation/simple.html"),
317 render_frame_host_(NULL
),
318 fake_latitude_(1.23),
319 fake_longitude_(4.56) {
322 GeolocationBrowserTest::~GeolocationBrowserTest() {
325 void GeolocationBrowserTest::SetUpOnMainThread() {
326 ui_test_utils::OverrideGeolocation(fake_latitude_
, fake_longitude_
);
329 void GeolocationBrowserTest::TearDownInProcessBrowserTestFixture() {
330 LOG(WARNING
) << "TearDownInProcessBrowserTestFixture. Test Finished.";
333 bool GeolocationBrowserTest::Initialize(InitializationOptions options
) {
334 if (!embedded_test_server()->Started() &&
335 !embedded_test_server()->InitializeAndWaitUntilReady()) {
336 ADD_FAILURE() << "Test server failed to start.";
340 current_url_
= embedded_test_server()->GetURL(html_for_tests_
);
341 LOG(WARNING
) << "before navigate";
342 if (options
== INITIALIZATION_OFFTHERECORD
) {
343 current_browser_
= ui_test_utils::OpenURLOffTheRecord(
344 browser()->profile(), current_url_
);
346 current_browser_
= browser();
347 if (options
== INITIALIZATION_NEWTAB
)
348 chrome::NewTab(current_browser_
);
349 ui_test_utils::NavigateToURL(current_browser_
, current_url_
);
351 LOG(WARNING
) << "after navigate";
353 EXPECT_TRUE(current_browser_
);
354 return !!current_browser_
;
357 void GeolocationBrowserTest::LoadIFrames(int number_iframes
) {
358 // Limit to 3 iframes.
359 DCHECK_LT(0, number_iframes
);
360 DCHECK_LE(number_iframes
, 3);
361 iframe_urls_
.resize(number_iframes
);
362 for (int i
= 0; i
< number_iframes
; ++i
) {
363 IFrameLoader
loader(current_browser_
, i
, GURL());
364 iframe_urls_
[i
] = loader
.iframe_url();
368 void GeolocationBrowserTest::SetFrameHost(const std::string
& frame_name
) {
369 WebContents
* web_contents
=
370 current_browser_
->tab_strip_model()->GetActiveWebContents();
371 render_frame_host_
= NULL
;
373 if (frame_name
.empty()) {
374 render_frame_host_
= web_contents
->GetMainFrame();
376 render_frame_host_
= content::FrameMatchingPredicate(
377 web_contents
, base::Bind(&content::FrameMatchesName
, frame_name
));
379 DCHECK(render_frame_host_
);
382 void GeolocationBrowserTest::AddGeolocationWatch(bool wait_for_infobar
) {
383 GeolocationNotificationObserver
notification_observer(wait_for_infobar
);
384 notification_observer
.AddWatchAndWaitForNotification(render_frame_host_
);
385 if (wait_for_infobar
) {
386 EXPECT_TRUE(notification_observer
.has_infobar());
387 infobar_
= notification_observer
.infobar();
391 void GeolocationBrowserTest::CheckGeoposition(double latitude
,
393 // Checks we have no error.
394 CheckStringValueFromJavascript("0", "geoGetLastError()");
395 CheckStringValueFromJavascript(base::DoubleToString(latitude
),
396 "geoGetLastPositionLatitude()");
397 CheckStringValueFromJavascript(base::DoubleToString(longitude
),
398 "geoGetLastPositionLongitude()");
401 void GeolocationBrowserTest::SetInfoBarResponse(const GURL
& requesting_url
,
403 WebContents
* web_contents
=
404 current_browser_
->tab_strip_model()->GetActiveWebContents();
405 TabSpecificContentSettings
* content_settings
=
406 TabSpecificContentSettings::FromWebContents(web_contents
);
407 const ContentSettingsUsagesState
& usages_state
=
408 content_settings
->geolocation_usages_state();
409 size_t state_map_size
= usages_state
.state_map().size();
410 ASSERT_TRUE(infobar_
);
411 LOG(WARNING
) << "will set infobar response";
413 content::WindowedNotificationObserver
observer(
414 content::NOTIFICATION_LOAD_STOP
,
415 content::Source
<NavigationController
>(&web_contents
->GetController()));
417 infobar_
->delegate()->AsConfirmInfoBarDelegate()->Accept();
419 infobar_
->delegate()->AsConfirmInfoBarDelegate()->Cancel();
423 InfoBarService
* infobar_service
=
424 InfoBarService::FromWebContents(web_contents
);
425 infobar_service
->RemoveInfoBar(infobar_
);
426 LOG(WARNING
) << "infobar response set";
428 EXPECT_GT(usages_state
.state_map().size(), state_map_size
);
429 GURL
requesting_origin(requesting_url
.GetOrigin());
430 EXPECT_EQ(1U, usages_state
.state_map().count(requesting_origin
));
431 ContentSetting expected_setting
=
432 allowed
? CONTENT_SETTING_ALLOW
: CONTENT_SETTING_BLOCK
;
433 EXPECT_EQ(expected_setting
,
434 usages_state
.state_map().find(requesting_origin
)->second
);
437 void GeolocationBrowserTest::CheckStringValueFromJavascriptForFrame(
438 const std::string
& expected
,
439 const std::string
& function
,
440 content::RenderFrameHost
* render_frame_host
) {
441 std::string
script(base::StringPrintf(
442 "window.domAutomationController.send(%s)", function
.c_str()));
444 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
445 render_frame_host
, script
, &result
));
446 EXPECT_EQ(expected
, result
);
449 void GeolocationBrowserTest::CheckStringValueFromJavascript(
450 const std::string
& expected
,
451 const std::string
& function
) {
452 CheckStringValueFromJavascriptForFrame(
453 expected
, function
, render_frame_host_
);
456 void GeolocationBrowserTest::NotifyGeoposition(double latitude
,
458 fake_latitude_
= latitude
;
459 fake_longitude_
= longitude
;
460 ui_test_utils::OverrideGeolocation(latitude
, longitude
);
461 LOG(WARNING
) << "MockLocationProvider listeners updated";
465 // Tests ----------------------------------------------------------------------
467 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest
, DisplaysPermissionBar
) {
468 ASSERT_TRUE(Initialize(INITIALIZATION_NONE
));
470 AddGeolocationWatch(true);
473 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest
, Geoposition
) {
474 ASSERT_TRUE(Initialize(INITIALIZATION_NONE
));
476 AddGeolocationWatch(true);
477 SetInfoBarResponse(current_url(), true);
478 CheckGeoposition(fake_latitude(), fake_longitude());
481 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest
,
482 ErrorOnPermissionDenied
) {
483 ASSERT_TRUE(Initialize(INITIALIZATION_NONE
));
485 AddGeolocationWatch(true);
486 // Infobar was displayed, deny access and check for error code.
487 SetInfoBarResponse(current_url(), false);
488 CheckStringValueFromJavascript("1", "geoGetLastError()");
491 // See http://crbug.com/308358
492 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest
, DISABLED_NoInfobarForSecondTab
) {
493 ASSERT_TRUE(Initialize(INITIALIZATION_NONE
));
495 AddGeolocationWatch(true);
496 SetInfoBarResponse(current_url(), true);
497 // Disables further prompts from this tab.
498 CheckStringValueFromJavascript("0", "geoSetMaxNavigateCount(0)");
500 // Checks infobar will not be created in a second tab.
501 ASSERT_TRUE(Initialize(INITIALIZATION_NEWTAB
));
503 AddGeolocationWatch(false);
504 CheckGeoposition(fake_latitude(), fake_longitude());
507 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest
, NoInfobarForDeniedOrigin
) {
508 ASSERT_TRUE(Initialize(INITIALIZATION_NONE
));
509 current_browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
510 ContentSettingsPattern::FromURLNoWildcard(current_url()),
511 ContentSettingsPattern::FromURLNoWildcard(current_url()),
512 CONTENT_SETTINGS_TYPE_GEOLOCATION
, std::string(), CONTENT_SETTING_BLOCK
);
514 AddGeolocationWatch(false);
515 // Checks we have an error for this denied origin.
516 CheckStringValueFromJavascript("1", "geoGetLastError()");
517 // Checks infobar will not be created a second tab.
518 ASSERT_TRUE(Initialize(INITIALIZATION_NEWTAB
));
520 AddGeolocationWatch(false);
521 CheckStringValueFromJavascript("1", "geoGetLastError()");
524 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest
, NoInfobarForAllowedOrigin
) {
525 ASSERT_TRUE(Initialize(INITIALIZATION_NONE
));
526 current_browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
527 ContentSettingsPattern::FromURLNoWildcard(current_url()),
528 ContentSettingsPattern::FromURLNoWildcard(current_url()),
529 CONTENT_SETTINGS_TYPE_GEOLOCATION
, std::string(), CONTENT_SETTING_ALLOW
);
530 // Checks no infobar will be created and there's no error callback.
532 AddGeolocationWatch(false);
533 CheckGeoposition(fake_latitude(), fake_longitude());
536 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest
, NoInfobarForOffTheRecord
) {
537 // First, check infobar will be created for regular profile
538 ASSERT_TRUE(Initialize(INITIALIZATION_NONE
));
540 AddGeolocationWatch(true);
541 // Response will be persisted.
542 SetInfoBarResponse(current_url(), true);
543 CheckGeoposition(fake_latitude(), fake_longitude());
544 // Disables further prompts from this tab.
545 CheckStringValueFromJavascript("0", "geoSetMaxNavigateCount(0)");
546 // Go incognito, and checks no infobar will be created.
547 ASSERT_TRUE(Initialize(INITIALIZATION_OFFTHERECORD
));
549 AddGeolocationWatch(false);
550 CheckGeoposition(fake_latitude(), fake_longitude());
553 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest
, NoLeakFromOffTheRecord
) {
554 // First, check infobar will be created for incognito profile.
555 ASSERT_TRUE(Initialize(INITIALIZATION_OFFTHERECORD
));
557 AddGeolocationWatch(true);
558 // Response won't be persisted.
559 SetInfoBarResponse(current_url(), true);
560 CheckGeoposition(fake_latitude(), fake_longitude());
561 // Disables further prompts from this tab.
562 CheckStringValueFromJavascript("0", "geoSetMaxNavigateCount(0)");
563 // Go to the regular profile, infobar will be created.
564 ASSERT_TRUE(Initialize(INITIALIZATION_NONE
));
566 AddGeolocationWatch(true);
567 SetInfoBarResponse(current_url(), false);
568 CheckStringValueFromJavascript("1", "geoGetLastError()");
571 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest
, IFramesWithFreshPosition
) {
572 set_html_for_tests("/geolocation/iframes_different_origin.html");
573 ASSERT_TRUE(Initialize(INITIALIZATION_IFRAMES
));
575 LOG(WARNING
) << "frames loaded";
577 SetFrameHost("iframe_0");
578 AddGeolocationWatch(true);
579 SetInfoBarResponse(iframe_url(0), true);
580 CheckGeoposition(fake_latitude(), fake_longitude());
581 // Disables further prompts from this iframe.
582 CheckStringValueFromJavascript("0", "geoSetMaxNavigateCount(0)");
584 // Test second iframe from a different origin with a cached geoposition will
585 // create the infobar.
586 SetFrameHost("iframe_1");
587 AddGeolocationWatch(true);
589 // Back to the first frame, enable navigation and refresh geoposition.
590 SetFrameHost("iframe_0");
591 CheckStringValueFromJavascript("1", "geoSetMaxNavigateCount(1)");
592 double fresh_position_latitude
= 3.17;
593 double fresh_position_longitude
= 4.23;
594 content::WindowedNotificationObserver
observer(
595 content::NOTIFICATION_LOAD_STOP
,
596 content::Source
<NavigationController
>(
597 ¤t_browser()->tab_strip_model()->GetActiveWebContents()->
599 NotifyGeoposition(fresh_position_latitude
, fresh_position_longitude
);
601 CheckGeoposition(fresh_position_latitude
, fresh_position_longitude
);
603 // Disable navigation for this frame.
604 CheckStringValueFromJavascript("0", "geoSetMaxNavigateCount(0)");
606 // Now go ahead an authorize the second frame.
607 SetFrameHost("iframe_1");
608 // Infobar was displayed, allow access and check there's no error code.
609 SetInfoBarResponse(iframe_url(1), true);
610 LOG(WARNING
) << "Checking position...";
611 CheckGeoposition(fresh_position_latitude
, fresh_position_longitude
);
612 LOG(WARNING
) << "...done.";
615 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest
,
616 IFramesWithCachedPosition
) {
617 set_html_for_tests("/geolocation/iframes_different_origin.html");
618 ASSERT_TRUE(Initialize(INITIALIZATION_IFRAMES
));
621 SetFrameHost("iframe_0");
622 AddGeolocationWatch(true);
623 SetInfoBarResponse(iframe_url(0), true);
624 CheckGeoposition(fake_latitude(), fake_longitude());
626 // Refresh geoposition, but let's not yet create the watch on the second frame
627 // so that it'll fetch from cache.
628 double cached_position_latitude
= 5.67;
629 double cached_position_lognitude
= 8.09;
630 content::WindowedNotificationObserver
observer(
631 content::NOTIFICATION_LOAD_STOP
,
632 content::Source
<NavigationController
>(
633 ¤t_browser()->tab_strip_model()->GetActiveWebContents()->
635 NotifyGeoposition(cached_position_latitude
, cached_position_lognitude
);
637 CheckGeoposition(cached_position_latitude
, cached_position_lognitude
);
639 // Disable navigation for this frame.
640 CheckStringValueFromJavascript("0", "geoSetMaxNavigateCount(0)");
642 // Now go ahead and authorize the second frame.
643 SetFrameHost("iframe_1");
644 AddGeolocationWatch(true);
645 // WebKit will use its cache, but we also broadcast a position shortly
646 // afterwards. We're only interested in the first navigation for the success
647 // callback from the cached position.
648 CheckStringValueFromJavascript("1", "geoSetMaxNavigateCount(1)");
649 SetInfoBarResponse(iframe_url(1), true);
650 CheckGeoposition(cached_position_latitude
, cached_position_lognitude
);
653 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest
, CancelPermissionForFrame
) {
654 set_html_for_tests("/geolocation/iframes_different_origin.html");
655 ASSERT_TRUE(Initialize(INITIALIZATION_IFRAMES
));
657 LOG(WARNING
) << "frames loaded";
659 SetFrameHost("iframe_0");
660 AddGeolocationWatch(true);
661 SetInfoBarResponse(iframe_url(0), true);
662 CheckGeoposition(fake_latitude(), fake_longitude());
663 // Disables further prompts from this iframe.
664 CheckStringValueFromJavascript("0", "geoSetMaxNavigateCount(0)");
666 // Test second iframe from a different origin with a cached geoposition will
667 // create the infobar.
668 SetFrameHost("iframe_1");
669 AddGeolocationWatch(true);
671 InfoBarService
* infobar_service
= InfoBarService::FromWebContents(
672 current_browser()->tab_strip_model()->GetActiveWebContents());
673 size_t num_infobars_before_cancel
= infobar_service
->infobar_count();
674 // Change the iframe, and ensure the infobar is gone.
675 IFrameLoader
change_iframe_1(current_browser(), 1, current_url());
676 size_t num_infobars_after_cancel
= infobar_service
->infobar_count();
677 EXPECT_EQ(num_infobars_before_cancel
, num_infobars_after_cancel
+ 1);
680 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest
, InvalidUrlRequest
) {
681 // Tests that an invalid URL (e.g. from a popup window) is rejected
682 // correctly. Also acts as a regression test for http://crbug.com/40478
683 set_html_for_tests("/geolocation/invalid_request_url.html");
684 ASSERT_TRUE(Initialize(INITIALIZATION_NONE
));
687 WebContents
* original_tab
=
688 current_browser()->tab_strip_model()->GetActiveWebContents();
689 CheckStringValueFromJavascript("1", "requestGeolocationFromInvalidUrl()");
690 CheckStringValueFromJavascriptForFrame("1", "isAlive()",
691 original_tab
->GetMainFrame());
694 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest
, NoInfoBarBeforeStart
) {
695 // See http://crbug.com/42789
696 set_html_for_tests("/geolocation/iframes_different_origin.html");
697 ASSERT_TRUE(Initialize(INITIALIZATION_IFRAMES
));
699 LOG(WARNING
) << "frames loaded";
701 // Access navigator.geolocation, but ensure it won't request permission.
702 SetFrameHost("iframe_1");
703 CheckStringValueFromJavascript("object", "geoAccessNavigatorGeolocation()");
705 SetFrameHost("iframe_0");
706 AddGeolocationWatch(true);
707 SetInfoBarResponse(iframe_url(0), true);
708 CheckGeoposition(fake_latitude(), fake_longitude());
709 CheckStringValueFromJavascript("0", "geoSetMaxNavigateCount(0)");
711 // Permission should be requested after adding a watch.
712 SetFrameHost("iframe_1");
713 AddGeolocationWatch(true);
714 SetInfoBarResponse(iframe_url(1), true);
715 CheckGeoposition(fake_latitude(), fake_longitude());
718 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest
, TwoWatchesInOneFrame
) {
719 set_html_for_tests("/geolocation/two_watches.html");
720 ASSERT_TRUE(Initialize(INITIALIZATION_NONE
));
722 // First, set the JavaScript to navigate when it receives |final_position|.
723 double final_position_latitude
= 3.17;
724 double final_position_longitude
= 4.23;
725 std::string script
= base::StringPrintf(
726 "window.domAutomationController.send(geoSetFinalPosition(%f, %f))",
727 final_position_latitude
, final_position_longitude
);
728 std::string js_result
;
729 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
730 current_browser()->tab_strip_model()->GetActiveWebContents(), script
,
732 EXPECT_EQ(js_result
, "ok");
734 // Send a position which both geolocation watches will receive.
736 AddGeolocationWatch(true);
737 SetInfoBarResponse(current_url(), true);
738 CheckGeoposition(fake_latitude(), fake_longitude());
740 // The second watch will now have cancelled. Ensure an update still makes
741 // its way through to the first watcher.
742 content::WindowedNotificationObserver
observer(
743 content::NOTIFICATION_LOAD_STOP
,
744 content::Source
<NavigationController
>(
745 ¤t_browser()->tab_strip_model()->GetActiveWebContents()->
747 NotifyGeoposition(final_position_latitude
, final_position_longitude
);
749 CheckGeoposition(final_position_latitude
, final_position_longitude
);
752 IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest
, TabDestroyed
) {
753 set_html_for_tests("/geolocation/tab_destroyed.html");
754 ASSERT_TRUE(Initialize(INITIALIZATION_IFRAMES
));
757 SetFrameHost("iframe_0");
758 AddGeolocationWatch(true);
760 SetFrameHost("iframe_1");
761 AddGeolocationWatch(false);
763 SetFrameHost("iframe_2");
764 AddGeolocationWatch(false);
767 "window.domAutomationController.send(window.close());";
768 bool result
= content::ExecuteScript(
769 current_browser()->tab_strip_model()->GetActiveWebContents(), script
);
770 EXPECT_EQ(result
, true);