<commit_before>// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/string_util.h"
#include "base/waitable_event.h"
#include "chrome/browser/app_modal_dialog.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/dom_operation_notification_details.h"
#include "chrome/browser/geolocation/geolocation_content_settings_map.h"
#include "chrome/browser/geolocation/location_arbitrator.h"
#include "chrome/browser/geolocation/location_provider.h"
#include "chrome/browser/geolocation/mock_location_provider.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/geoposition.h"
#include "chrome/common/notification_details.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"
#include "chrome/common/render_messages.h"
#include "chrome/test/in_process_browser_test.h"
#include "chrome/test/ui_test_utils.h"
#include "net/base/net_util.h"

// Used to block until an iframe is loaded via a javascript call.
// Note: NavigateToURLBlockUntilNavigationsComplete doesn't seem to work for
// multiple embedded iframes, as notifications seem to be 'batched'. Instead, we
// load and wait one single frame here by calling a javascript function.
class IFrameLoader : public NotificationObserver {
 public:
   IFrameLoader(Browser* browser, int iframe_id)
      : navigation_completed_(false),
        javascript_completed_(false) {
    NavigationController* controller =
        &browser->GetSelectedTabContents()->controller();
    registrar_.Add(this, NotificationType::LOAD_STOP,
                   Source<NavigationController>(controller));
    registrar_.Add(this, NotificationType::DOM_OPERATION_RESPONSE,
                   NotificationService::AllSources());
    std::string script = StringPrintf(
        "window.domAutomationController.setAutomationId(0);"
        "window.domAutomationController.send(addIFrame(%d));",
        iframe_id);
    browser->GetSelectedTabContents()->render_view_host()->
        ExecuteJavascriptInWebFrame(L"", UTF8ToWide(script));
    ui_test_utils::RunMessageLoop();

    EXPECT_EQ(StringPrintf("\"%d\"", iframe_id), javascript_response_);
    registrar_.RemoveAll();
    // Now that we loaded the iframe, let's fetch its src.
    script = StringPrintf(
        "window.domAutomationController.send(getIFrameSrc(%d))", iframe_id);
    std::string iframe_src;
    ui_test_utils::ExecuteJavaScriptAndExtractString(
        browser->GetSelectedTabContents()->render_view_host(),
        L"", UTF8ToWide(script), &iframe_src);
    iframe_url_ = GURL(iframe_src);
  }

  GURL iframe_url() const { return iframe_url_; }

  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details) {
    if (type == NotificationType::LOAD_STOP) {
      navigation_completed_ = true;
    } else if (type == NotificationType::DOM_OPERATION_RESPONSE) {
      Details<DomOperationNotificationDetails> dom_op_details(details);
      javascript_response_ = dom_op_details->json();
      javascript_completed_ = true;
    }
    if (javascript_completed_ && navigation_completed_)
      MessageLoopForUI::current()->Quit();
  }

 private:
  NotificationRegistrar registrar_;

  // If true the navigation has completed.
  bool navigation_completed_;

  // If true the javascript call has completed.
  bool javascript_completed_;

  std::string javascript_response_;

  // The URL for the iframe we just loaded.
  GURL iframe_url_;

  DISALLOW_COPY_AND_ASSIGN(IFrameLoader);
};

class GeolocationNotificationObserver : public NotificationObserver {
 public:
  // If |wait_for_infobar| is true, AddWatchAndWaitForNotification will block
  // until the inforbar has been displayed; otherwise it will block until the
  // javascript alert box is displayed.
  explicit GeolocationNotificationObserver(bool wait_for_infobar)
    : wait_for_infobar_(wait_for_infobar),
      infobar_(NULL),
      js_prompt_(NULL) {
    registrar_.Add(this, NotificationType::DOM_OPERATION_RESPONSE,
                   NotificationService::AllSources());
    if (wait_for_infobar) {
      registrar_.Add(this, NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
                     NotificationService::AllSources());
    } else {
      registrar_.Add(this, NotificationType::APP_MODAL_DIALOG_SHOWN,
                     NotificationService::AllSources());
    }
  }

  void AddWatchAndWaitForNotification(RenderViewHost* render_view_host,
                                      const std::wstring& iframe_xpath) {
    LOG(WARNING) << "will add geolocation watch";
    std::string script =
        "window.domAutomationController.setAutomationId(0);"
        "window.domAutomationController.send(geoStart());";
    render_view_host->ExecuteJavascriptInWebFrame(iframe_xpath,
                                                  UTF8ToWide(script));
    ui_test_utils::RunMessageLoop();
    registrar_.RemoveAll();
    LOG(WARNING) << "got geolocation watch" << javascript_response_;
    EXPECT_NE("\"0\"", javascript_response_);
    if (wait_for_infobar_) {
      EXPECT_TRUE(infobar_);
    } else {
      EXPECT_TRUE(js_prompt_);
      js_prompt_->CloseModalDialog();
    }
  }

  // NotificationObserver
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details) {
    if (type.value == NotificationType::TAB_CONTENTS_INFOBAR_ADDED) {
      infobar_ = Details<InfoBarDelegate>(details).ptr();
      ASSERT_TRUE(infobar_->GetIcon());
      ASSERT_TRUE(infobar_->AsConfirmInfoBarDelegate());
    } else if (type == NotificationType::DOM_OPERATION_RESPONSE) {
      Details<DomOperationNotificationDetails> dom_op_details(details);
      javascript_response_ = dom_op_details->json();
      LOG(WARNING) << "javascript_response " << javascript_response_;
    } else if (type == NotificationType::APP_MODAL_DIALOG_SHOWN) {
      js_prompt_ = Source<AppModalDialog>(source).ptr();
    }
    // We're either waiting for just the inforbar, or for both a javascript
    // prompt and response.
    if (wait_for_infobar_ && infobar_)
      MessageLoopForUI::current()->Quit();
    else if (js_prompt_ && !javascript_response_.empty())
      MessageLoopForUI::current()->Quit();
  }

  NotificationRegistrar registrar_;
  bool wait_for_infobar_;
  InfoBarDelegate* infobar_;
  AppModalDialog* js_prompt_;
  std::string javascript_response_;
};

void NotifyGeopositionOnIOThread(const Geoposition& geoposition) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
  DCHECK(MockLocationProvider::instance_);
  MockLocationProvider::instance_->position_ = geoposition;
  MockLocationProvider::instance_->UpdateListeners();
  LOG(WARNING) << "MockLocationProvider listeners updated";
}

// This is a browser test for Geolocation.
// It exercises various integration points from javascript <-> browser:
// 1. Infobar is displayed when a geolocation is requested from an unauthorized
// origin.
// 2. Denying the infobar triggers the correct error callback.
// 3. Allowing the infobar does not trigger an error, and allow a geoposition to
// be passed to javascript.
// 4. Permissions persisted in disk are respected.
// 5. Off the record profiles don't use saved permissions.
class GeolocationBrowserTest : public InProcessBrowserTest {
 public:
  GeolocationBrowserTest()
    : infobar_(NULL),
      current_browser_(NULL),
      html_for_tests_("files/geolocation/simple.html") {
    EnableDOMAutomation();
  }

  enum InitializationOptions {
    INITIALIZATION_NONE,
    INITIALIZATION_OFFTHERECORD,
    INITIALIZATION_NEWTAB,
    INITIALIZATION_IFRAMES,
  };

  void Initialize(InitializationOptions options) {
    GeolocationArbitrator::SetProviderFactoryForTest(
        &NewAutoSuccessMockNetworkLocationProvider);
    if (!server_.get())
      server_ = StartHTTPServer();

    current_url_ = server_->TestServerPage(html_for_tests_);
    LOG(WARNING) << "before navigate";
    if (options == INITIALIZATION_OFFTHERECORD) {
      ui_test_utils::OpenURLOffTheRecord(browser()->profile(), current_url_);
      current_browser_ = BrowserList::FindBrowserWithType(
          browser()->profile()->GetOffTheRecordProfile(), Browser::TYPE_NORMAL,
          false);
    } else if (options == INITIALIZATION_NEWTAB) {
      current_browser_ = browser();
      current_browser_->NewTab();
      ui_test_utils::NavigateToURL(current_browser_, current_url_);
    } else if (options == INITIALIZATION_IFRAMES) {
      current_browser_ = browser();
      ui_test_utils::NavigateToURL(current_browser_, current_url_);
      IFrameLoader iframe0(current_browser_, 0);
      iframe0_url_ = iframe0.iframe_url();

      IFrameLoader iframe1(current_browser_, 1);
      iframe1_url_ = iframe1.iframe_url();
    } else {
      current_browser_ = browser();
      ui_test_utils::NavigateToURL(current_browser_, current_url_);
    }
    EXPECT_TRUE(current_browser_);
    LOG(WARNING) << "after navigate";
  }

  void AddGeolocationWatch(bool wait_for_infobar) {
    GeolocationNotificationObserver notification_observer(wait_for_infobar);
    notification_observer.AddWatchAndWaitForNotification(
        current_browser_->GetSelectedTabContents()->render_view_host(),
        iframe_xpath_);
    if (wait_for_infobar) {
      EXPECT_TRUE(notification_observer.infobar_);
      infobar_ = notification_observer.infobar_;
    }
  }

  Geoposition GeopositionFromLatLong(double latitude, double longitude) {
    Geoposition geoposition;
    geoposition.latitude = latitude;
    geoposition.longitude = longitude;
    geoposition.accuracy = 0;
    geoposition.error_code = Geoposition::ERROR_CODE_NONE;
    // Webkit compares the timestamp to wall clock time, so we need
    // it to be contemporary.
    geoposition.timestamp = base::Time::Now();
    EXPECT_TRUE(geoposition.IsValidFix());
    return geoposition;
  }

  void CheckGeoposition(const Geoposition& geoposition) {
    // Checks we have no error.
    CheckStringValueFromJavascript("0", "geoGetLastError()");
    CheckStringValueFromJavascript(
        DoubleToString(geoposition.latitude), "geoGetLastPositionLatitude()");
    CheckStringValueFromJavascript(
        DoubleToString(geoposition.longitude), "geoGetLastPositionLongitude()");
  }

  void SetInfobarResponse(const GURL& requesting_url, bool allowed) {
    TabContents* tab_contents = current_browser_->GetSelectedTabContents();
    TabContents::GeolocationContentSettings content_settings =
        tab_contents->geolocation_content_settings();
    size_t settings_size = content_settings.size();
    ASSERT_TRUE(infobar_);
    LOG(WARNING) << "will set infobar response";
    if (allowed)
      infobar_->AsConfirmInfoBarDelegate()->Accept();
    else
      infobar_->AsConfirmInfoBarDelegate()->Cancel();
    WaitForJSPrompt();
    LOG(WARNING) << "infobar response set";
    infobar_ = NULL;
    content_settings = tab_contents->geolocation_content_settings();
    EXPECT_GT(content_settings.size(), settings_size);
    EXPECT_EQ(1U, content_settings.count(requesting_url));
    ContentSetting expected_setting =
          allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
    EXPECT_EQ(expected_setting, content_settings[requesting_url]);
  }

  void WaitForJSPrompt() {
    LOG(WARNING) << "will block for JS prompt";
    AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
    LOG(WARNING) << "JS prompt received";
    ASSERT_TRUE(alert);
    LOG(WARNING) << "will close JS prompt";
    alert->CloseModalDialog();
    LOG(WARNING) << "closed JS prompt";
  }

  void CheckStringValueFromJavascript(
      const std::string& expected, const std::string& function) {
    std::string script = StringPrintf(
        "window.domAutomationController.send(%s)", function.c_str());
    std::string result;
    ui_test_utils::ExecuteJavaScriptAndExtractString(
        current_browser_->GetSelectedTabContents()->render_view_host(),
        iframe_xpath_, UTF8ToWide(script), &result);
    EXPECT_EQ(expected.c_str(), result);
  }

  scoped_refptr<HTTPTestServer> server_;
  InfoBarDelegate* infobar_;
  Browser* current_browser_;
  // path element of a URL referencing the html content for this test.
  std::string html_for_tests_;
  // This member defines the iframe (or top-level page, if empty) where the
  // javascript calls will run.
  std::wstring iframe_xpath_;
  // The current url for the top level page.
  GURL current_url_;
  // If not empty, the GURL for the first iframe.
  GURL iframe0_url_;
  // If not empty, the GURL for the second iframe.
  GURL iframe1_url_;
};

#if defined(OS_MACOSX)
// TODO(bulach): investigate why this fails on mac. It may be related to:
// http://crbug.com/29424
#define MAYBE_DisplaysPermissionBar DISABLED_DisplaysPermissionBar
#else
#define MAYBE_DisplaysPermissionBar DisplaysPermissionBar
#endif

IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_DisplaysPermissionBar) {
  Initialize(INITIALIZATION_NONE);
  AddGeolocationWatch(true);
}

#if defined(OS_MACOSX)
// TODO(bulach): investigate why this fails on mac. It may be related to:
// http://crbug.com/29424
#define MAYBE_Geoposition DISABLED_Geoposition
#else
#define MAYBE_Geoposition Geoposition
#endif

IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_Geoposition) {
  Initialize(INITIALIZATION_NONE);
  AddGeolocationWatch(true);
  SetInfobarResponse(current_url_, true);
  CheckGeoposition(MockLocationProvider::instance_->position_);
}

#if defined(OS_MACOSX)
// TODO(bulach): investigate why this fails on mac. It may be related to:
// http://crbug.com/29424
#define MAYBE_ErrorOnPermissionDenied DISABLED_ErrorOnPermissionDenied
#else
#define MAYBE_ErrorOnPermissionDenied ErrorOnPermissionDenied
#endif

IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_ErrorOnPermissionDenied) {
  Initialize(INITIALIZATION_NONE);
  AddGeolocationWatch(true);
  // Infobar was displayed, deny access and check for error code.
  SetInfobarResponse(current_url_, false);
  CheckStringValueFromJavascript("1", "geoGetLastError()");
}

#if defined(OS_MACOSX)
// TODO(bulach): investigate why this fails on mac. It may be related to:
// http://crbug.com/29424
#define MAYBE_NoInfobarForSecondTab DISABLED_NoInfobarForSecondTab
#else
#define MAYBE_NoInfobarForSecondTab NoInfobarForSecondTab
#endif

IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_NoInfobarForSecondTab) {
  Initialize(INITIALIZATION_NONE);
  AddGeolocationWatch(true);
  SetInfobarResponse(current_url_, true);

  // Checks infobar will not be created a second tab.
  Initialize(INITIALIZATION_NEWTAB);
  AddGeolocationWatch(false);
  CheckGeoposition(MockLocationProvider::instance_->position_);
}

#if defined(OS_MACOSX)
// TODO(bulach): investigate why this fails on mac. It may be related to:
// http://crbug.com/29424<commit_msg>Workaround for 40099 - disable failing test<commit_after>// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/string_util.h"
#include "base/waitable_event.h"
#include "chrome/browser/app_modal_dialog.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/dom_operation_notification_details.h"
#include "chrome/browser/geolocation/geolocation_content_settings_map.h"
#include "chrome/browser/geolocation/location_arbitrator.h"
#include "chrome/browser/geolocation/location_provider.h"
#include "chrome/browser/geolocation/mock_location_provider.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/geoposition.h"
#include "chrome/common/notification_details.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"
#include "chrome/common/render_messages.h"
#include "chrome/test/in_process_browser_test.h"
#include "chrome/test/ui_test_utils.h"
#include "net/base/net_util.h"

// Used to block until an iframe is loaded via a javascript call.
// Note: NavigateToURLBlockUntilNavigationsComplete doesn't seem to work for
// multiple embedded iframes, as notifications seem to be 'batched'. Instead, we
// load and wait one single frame here by calling a javascript function.
class IFrameLoader : public NotificationObserver {
 public:
   IFrameLoader(Browser* browser, int iframe_id)
      : navigation_completed_(false),
        javascript_completed_(false) {
    NavigationController* controller =
        &browser->GetSelectedTabContents()->controller();
    registrar_.Add(this, NotificationType::LOAD_STOP,
                   Source<NavigationController>(controller));
    registrar_.Add(this, NotificationType::DOM_OPERATION_RESPONSE,
                   NotificationService::AllSources());
    std::string script = StringPrintf(
        "window.domAutomationController.setAutomationId(0);"
        "window.domAutomationController.send(addIFrame(%d));",
        iframe_id);
    browser->GetSelectedTabContents()->render_view_host()->
        ExecuteJavascriptInWebFrame(L"", UTF8ToWide(script));
    ui_test_utils::RunMessageLoop();

    EXPECT_EQ(StringPrintf("\"%d\"", iframe_id), javascript_response_);
    registrar_.RemoveAll();
    // Now that we loaded the iframe, let's fetch its src.
    script = StringPrintf(
        "window.domAutomationController.send(getIFrameSrc(%d))", iframe_id);
    std::string iframe_src;
    ui_test_utils::ExecuteJavaScriptAndExtractString(
        browser->GetSelectedTabContents()->render_view_host(),
        L"", UTF8ToWide(script), &iframe_src);
    iframe_url_ = GURL(iframe_src);
  }

  GURL iframe_url() const { return iframe_url_; }

  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details) {
    if (type == NotificationType::LOAD_STOP) {
      navigation_completed_ = true;
    } else if (type == NotificationType::DOM_OPERATION_RESPONSE) {
      Details<DomOperationNotificationDetails> dom_op_details(details);
      javascript_response_ = dom_op_details->json();
      javascript_completed_ = true;
    }
    if (javascript_completed_ && navigation_completed_)
      MessageLoopForUI::current()->Quit();
  }

 private:
  NotificationRegistrar registrar_;

  // If true the navigation has completed.
  bool navigation_completed_;

  // If true the javascript call has completed.
  bool javascript_completed_;

  std::string javascript_response_;

  // The URL for the iframe we just loaded.
  GURL iframe_url_;

  DISALLOW_COPY_AND_ASSIGN(IFrameLoader);
};

class GeolocationNotificationObserver : public NotificationObserver {
 public:
  // If |wait_for_infobar| is true, AddWatchAndWaitForNotification will block
  // until the inforbar has been displayed; otherwise it will block until the
  // javascript alert box is displayed.
  explicit GeolocationNotificationObserver(bool wait_for_infobar)
    : wait_for_infobar_(wait_for_infobar),
      infobar_(NULL),
      js_prompt_(NULL) {
    registrar_.Add(this, NotificationType::DOM_OPERATION_RESPONSE,
                   NotificationService::AllSources());
    if (wait_for_infobar) {
      registrar_.Add(this, NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
                     NotificationService::AllSources());
    } else {
      registrar_.Add(this, NotificationType::APP_MODAL_DIALOG_SHOWN,
                     NotificationService::AllSources());
    }
  }

  void AddWatchAndWaitForNotification(RenderViewHost* render_view_host,
                                      const std::wstring& iframe_xpath) {
    LOG(WARNING) << "will add geolocation watch";
    std::string script =
        "window.domAutomationController.setAutomationId(0);"
        "window.domAutomationController.send(geoStart());";
    render_view_host->ExecuteJavascriptInWebFrame(iframe_xpath,
                                                  UTF8ToWide(script));
    ui_test_utils::RunMessageLoop();
    registrar_.RemoveAll();
    LOG(WARNING) << "got geolocation watch" << javascript_response_;
    EXPECT_NE("\"0\"", javascript_response_);
    if (wait_for_infobar_) {
      EXPECT_TRUE(infobar_);
    } else {
      EXPECT_TRUE(js_prompt_);
      js_prompt_->CloseModalDialog();
    }
  }

  // NotificationObserver
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details) {
    if (type.value == NotificationType::TAB_CONTENTS_INFOBAR_ADDED) {
      infobar_ = Details<InfoBarDelegate>(details).ptr();
      ASSERT_TRUE(infobar_->GetIcon());
      ASSERT_TRUE(infobar_->AsConfirmInfoBarDelegate());
    } else if (type == NotificationType::DOM_OPERATION_RESPONSE) {
      Details<DomOperationNotificationDetails> dom_op_details(details);
      javascript_response_ = dom_op_details->json();
      LOG(WARNING) << "javascript_response " << javascript_response_;
    } else if (type == NotificationType::APP_MODAL_DIALOG_SHOWN) {
      js_prompt_ = Source<AppModalDialog>(source).ptr();
    }
    // We're either waiting for just the inforbar, or for both a javascript
    // prompt and response.
    if (wait_for_infobar_ && infobar_)
      MessageLoopForUI::current()->Quit();
    else if (js_prompt_ && !javascript_response_.empty())
      MessageLoopForUI::current()->Quit();
  }

  NotificationRegistrar registrar_;
  bool wait_for_infobar_;
  InfoBarDelegate* infobar_;
  AppModalDialog* js_prompt_;
  std::string javascript_response_;
};

void NotifyGeopositionOnIOThread(const Geoposition& geoposition) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
  DCHECK(MockLocationProvider::instance_);
  MockLocationProvider::instance_->position_ = geoposition;
  MockLocationProvider::instance_->UpdateListeners();
  LOG(WARNING) << "MockLocationProvider listeners updated";
}

// This is a browser test for Geolocation.
// It exercises various integration points from javascript <-> browser:
// 1. Infobar is displayed when a geolocation is requested from an unauthorized
// origin.
// 2. Denying the infobar triggers the correct error callback.
// 3. Allowing the infobar does not trigger an error, and allow a geoposition to
// be passed to javascript.
// 4. Permissions persisted in disk are respected.
// 5. Off the record profiles don't use saved permissions.
class GeolocationBrowserTest : public InProcessBrowserTest {
 public:
  GeolocationBrowserTest()
    : infobar_(NULL),
      current_browser_(NULL),
      html_for_tests_("files/geolocation/simple.html") {
    EnableDOMAutomation();
  }

  enum InitializationOptions {
    INITIALIZATION_NONE,
    INITIALIZATION_OFFTHERECORD,
    INITIALIZATION_NEWTAB,
    INITIALIZATION_IFRAMES,
  };

  void Initialize(InitializationOptions options) {
    GeolocationArbitrator::SetProviderFactoryForTest(
        &NewAutoSuccessMockNetworkLocationProvider);
    if (!server_.get())
      server_ = StartHTTPServer();

    current_url_ = server_->TestServerPage(html_for_tests_);
    LOG(WARNING) << "before navigate";
    if (options == INITIALIZATION_OFFTHERECORD) {
      ui_test_utils::OpenURLOffTheRecord(browser()->profile(), current_url_);
      current_browser_ = BrowserList::FindBrowserWithType(
          browser()->profile()->GetOffTheRecordProfile(), Browser::TYPE_NORMAL,
          false);
    } else if (options == INITIALIZATION_NEWTAB) {
      current_browser_ = browser();
      current_browser_->NewTab();
      ui_test_utils::NavigateToURL(current_browser_, current_url_);
    } else if (options == INITIALIZATION_IFRAMES) {
      current_browser_ = browser();
      ui_test_utils::NavigateToURL(current_browser_, current_url_);
      IFrameLoader iframe0(current_browser_, 0);
      iframe0_url_ = iframe0.iframe_url();

      IFrameLoader iframe1(current_browser_, 1);
      iframe1_url_ = iframe1.iframe_url();
    } else {
      current_browser_ = browser();
      ui_test_utils::NavigateToURL(current_browser_, current_url_);
    }
    EXPECT_TRUE(current_browser_);
    LOG(WARNING) << "after navigate";
  }

  void AddGeolocationWatch(bool wait_for_infobar) {
    GeolocationNotificationObserver notification_observer(wait_for_infobar);
    notification_observer.AddWatchAndWaitForNotification(
        current_browser_->GetSelectedTabContents()->render_view_host(),
        iframe_xpath_);
    if (wait_for_infobar) {
      EXPECT_TRUE(notification_observer.infobar_);
      infobar_ = notification_observer.infobar_;
    }
  }

  Geoposition GeopositionFromLatLong(double latitude, double longitude) {
    Geoposition geoposition;
    geoposition.latitude = latitude;
    geoposition.longitude = longitude;
    geoposition.accuracy = 0;
    geoposition.error_code = Geoposition::ERROR_CODE_NONE;
    // Webkit compares the timestamp to wall clock time, so we need
    // it to be contemporary.
    geoposition.timestamp = base::Time::Now();
    EXPECT_TRUE(geoposition.IsValidFix());
    return geoposition;
  }

  void CheckGeoposition(const Geoposition& geoposition) {
    // Checks we have no error.
    CheckStringValueFromJavascript("0", "geoGetLastError()");
    CheckStringValueFromJavascript(
        DoubleToString(geoposition.latitude), "geoGetLastPositionLatitude()");
    CheckStringValueFromJavascript(
        DoubleToString(geoposition.longitude), "geoGetLastPositionLongitude()");
  }

  void SetInfobarResponse(const GURL& requesting_url, bool allowed) {
    TabContents* tab_contents = current_browser_->GetSelectedTabContents();
    TabContents::GeolocationContentSettings content_settings =
        tab_contents->geolocation_content_settings();
    size_t settings_size = content_settings.size();
    ASSERT_TRUE(infobar_);
    LOG(WARNING) << "will set infobar response";
    if (allowed)
      infobar_->AsConfirmInfoBarDelegate()->Accept();
    else
      infobar_->AsConfirmInfoBarDelegate()->Cancel();
    WaitForJSPrompt();
    LOG(WARNING) << "infobar response set";
    infobar_ = NULL;
    content_settings = tab_contents->geolocation_content_settings();
    EXPECT_GT(content_settings.size(), settings_size);
    EXPECT_EQ(1U, content_settings.count(requesting_url));
    ContentSetting expected_setting =
          allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
    EXPECT_EQ(expected_setting, content_settings[requesting_url]);
  }

  void WaitForJSPrompt() {
    LOG(WARNING) << "will block for JS prompt";
    AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
    LOG(WARNING) << "JS prompt received";
    ASSERT_TRUE(alert);
    LOG(WARNING) << "will close JS prompt";
    alert->CloseModalDialog();
    LOG(WARNING) << "closed JS prompt";
  }

  void CheckStringValueFromJavascript(
      const std::string& expected, const std::string& function) {
    std::string script = StringPrintf(
        "window.domAutomationController.send(%s)", function.c_str());
    std::string result;
    ui_test_utils::ExecuteJavaScriptAndExtractString(
        current_browser_->GetSelectedTabContents()->render_view_host(),
        iframe_xpath_, UTF8ToWide(script), &result);
    EXPECT_EQ(expected.c_str(), result);
  }

  scoped_refptr<HTTPTestServer> server_;
  InfoBarDelegate* infobar_;
  Browser* current_browser_;
  // path element of a URL referencing the html content for this test.
  std::string html_for_tests_;
  // This member defines the iframe (or top-level page, if empty) where the
  // javascript calls will run.
  std::wstring iframe_xpath_;
  // The current url for the top level page.
  GURL current_url_;
  // If not empty, the GURL for the first iframe.
  GURL iframe0_url_;
  // If not empty, the GURL for the second iframe.
  GURL iframe1_url_;
};

#if defined(OS_MACOSX)
// TODO(bulach): investigate why this fails on mac. It may be related to:
// http://crbug.com/29424
#define MAYBE_DisplaysPermissionBar DISABLED_DisplaysPermissionBar
#else
#define MAYBE_DisplaysPermissionBar DisplaysPermissionBar
#endif

IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_DisplaysPermissionBar) {
  Initialize(INITIALIZATION_NONE);
  AddGeolocationWatch(true);
}

#if defined(OS_MACOSX)
// TODO(bulach): investigate why this fails on mac. It may be related to:
// http://crbug.com/29424
#define MAYBE_Geoposition DISABLED_Geoposition
#else
#define MAYBE_Geoposition Geoposition
#endif

IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_Geoposition) {
  Initialize(INITIALIZATION_NONE);
  AddGeolocationWatch(true);
  SetInfobarResponse(current_url_, true);
  CheckGeoposition(MockLocationProvider::instance_->position_);
}

#if defined(OS_MACOSX)
// TODO(bulach): investigate why this fails on mac. It may be related to:
// http://crbug.com/29424
#define MAYBE_ErrorOnPermissionDenied DISABLED_ErrorOnPermissionDenied
#else
#define MAYBE_ErrorOnPermissionDenied ErrorOnPermissionDenied
#endif

IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_ErrorOnPermissionDenied) {
  Initialize(INITIALIZATION_NONE);
  AddGeolocationWatch(true);
  // Infobar was displayed, deny access and check for error code.
  SetInfobarResponse(current_url_, false);
  CheckStringValueFromJavascript("1", "geoGetLastError()");
}

#if defined(OS_MACOSX)
// TODO(bulach): investigate why this fails on mac. It may be related to:
// http://crbug.com/29424
#define MAYBE_NoInfobarForSecondTab DISABLED_NoInfobarForSecondTab
#else
#define MAYBE_NoInfobarForSecondTab NoInfobarForSecondTab
#endif

// TODO(joth): Fix test. http://crbug.com/40099
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, DISABLED_NoInfobarForSecondTab) {
  Initialize(INITIALIZATION_NONE);
  AddGeolocationWatch(true);
  SetInfobarResponse(current_url_, true);

  // Checks infobar will not be created a second tab.
  Initialize(INITIALIZATION_NEWTAB);
  AddGeolocationWatch(false);
  CheckGeoposition(MockLocationProvider::instance_->position_);
}

#if defined(OS_MACOSX)
// TODO(bulach): investigate why this fails on mac. It may be related to:
// http://crbug.com/29424
{'fraction_non_alphanumeric': 0.09945945945945946, 'fraction_numerical': 0.003931203931203931, 'mean_word_length': 4.480430879712747, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5007503', 'n_tokens_mistral': 9535, 'n_tokens_neox': 8946, 'n_words': 1872}