| Index: chrome/browser/instant/instant_test_utils.cc
|
| diff --git a/chrome/browser/instant/instant_test_utils.cc b/chrome/browser/instant/instant_test_utils.cc
|
| index 01de31c91480a72b60ecedfebc8805bd6bbe4611..9a4112a528a0023aa6bda80ec7219b6d05b76d56 100644
|
| --- a/chrome/browser/instant/instant_test_utils.cc
|
| +++ b/chrome/browser/instant/instant_test_utils.cc
|
| @@ -4,18 +4,25 @@
|
|
|
| #include "chrome/browser/instant/instant_test_utils.h"
|
|
|
| -#include "base/command_line.h"
|
| -#include "base/prefs/pref_service.h"
|
| -#include "chrome/browser/profiles/profile.h"
|
| +#include "base/path_service.h"
|
| +#include "chrome/browser/instant/instant_controller.h"
|
| +#include "chrome/browser/instant/instant_overlay_model.h"
|
| +#include "chrome/browser/instant/instant_overlay_model_observer.h"
|
| +#include "chrome/browser/instant/instant_preloader.h"
|
| +#include "chrome/browser/instant/instant_service.h"
|
| +#include "chrome/browser/instant/instant_service_factory.h"
|
| #include "chrome/browser/search_engines/template_url_service.h"
|
| #include "chrome/browser/search_engines/template_url_service_factory.h"
|
| +#include "chrome/browser/ui/browser.h"
|
| +#include "chrome/browser/ui/browser_instant_controller.h"
|
| +#include "chrome/browser/ui/browser_window.h"
|
| +#include "chrome/browser/ui/omnibox/location_bar.h"
|
| #include "chrome/browser/ui/omnibox/omnibox_view.h"
|
| +#include "chrome/common/chrome_constants.h"
|
| #include "chrome/common/chrome_notification_types.h"
|
| -#include "chrome/common/chrome_switches.h"
|
| -#include "chrome/common/pref_names.h"
|
| -#include "chrome/test/base/interactive_test_utils.h"
|
| +#include "chrome/common/chrome_paths.h"
|
| +#include "chrome/test/base/testing_profile.h"
|
| #include "chrome/test/base/ui_test_utils.h"
|
| -#include "content/public/browser/notification_service.h"
|
| #include "content/public/browser/render_process_host.h"
|
| #include "content/public/browser/web_contents.h"
|
| #include "content/public/common/result_codes.h"
|
| @@ -27,68 +34,121 @@ std::string WrapScript(const std::string& script) {
|
| return "domAutomationController.send(" + script + ")";
|
| }
|
|
|
| -} // namespace
|
| +class InstantTestOverlayModelObserver : public InstantOverlayModelObserver {
|
| + public:
|
| + InstantTestOverlayModelObserver(InstantController* controller,
|
| + bool expected_to_show);
|
| + virtual ~InstantTestOverlayModelObserver();
|
| +
|
| + void WaitForExpectedOverlayState();
|
| +
|
| + private:
|
| + // Overridden from InstantOverlayModelObserver:
|
| + virtual void OverlayStateChanged(const InstantOverlayModel& model) OVERRIDE;
|
|
|
| -// InstantTestModelObserver --------------------------------------------------
|
| + bool IsExpectedState() const;
|
|
|
| -InstantTestModelObserver::InstantTestModelObserver(
|
| - InstantOverlayModel* model,
|
| - chrome::search::Mode::Type desired_mode_type)
|
| - : model_(model),
|
| - desired_mode_type_(desired_mode_type) {
|
| - model_->AddObserver(this);
|
| + InstantController* const controller_;
|
| + const bool expected_to_show_;
|
| + base::RunLoop run_loop_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(InstantTestOverlayModelObserver);
|
| +};
|
| +
|
| +InstantTestOverlayModelObserver::InstantTestOverlayModelObserver(
|
| + InstantController* controller,
|
| + bool expected_to_show)
|
| + : controller_(controller),
|
| + expected_to_show_(expected_to_show) {
|
| + controller_->AddOverlayModelObserver(this);
|
| + if (IsExpectedState())
|
| + run_loop_.Quit();
|
| }
|
|
|
| -InstantTestModelObserver::~InstantTestModelObserver() {
|
| - model_->RemoveObserver(this);
|
| +InstantTestOverlayModelObserver::~InstantTestOverlayModelObserver() {
|
| + controller_->RemoveOverlayModelObserver(this);
|
| }
|
|
|
| -void InstantTestModelObserver::WaitForDesiredOverlayState() {
|
| +void InstantTestOverlayModelObserver::WaitForExpectedOverlayState() {
|
| run_loop_.Run();
|
| }
|
|
|
| -void InstantTestModelObserver::OverlayStateChanged(
|
| - const InstantOverlayModel& model) {
|
| - if (model.mode().mode == desired_mode_type_)
|
| +void InstantTestOverlayModelObserver::OverlayStateChanged(
|
| + const InstantOverlayModel& /* model */) {
|
| + if (IsExpectedState())
|
| run_loop_.Quit();
|
| }
|
|
|
| -// InstantTestBase -----------------------------------------------------------
|
| +bool InstantTestOverlayModelObserver::IsExpectedState() const {
|
| + return expected_to_show_ == (controller_->model()->contents() != NULL);
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +// InstantTestBase ------------------------------------------------------------
|
| +
|
| +InstantTestBase::InstantTestBase()
|
| + : https_test_server_(
|
| + net::TestServer::TYPE_HTTPS,
|
| + net::BaseTestServer::SSLOptions(),
|
| + base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))),
|
| + browser_(NULL) {
|
| +}
|
|
|
| -void InstantTestBase::SetupInstant(Browser* browser) {
|
| - browser_ = browser;
|
| +InstantTestBase::~InstantTestBase() {
|
| +}
|
| +
|
| +// Set up the profile dir to explicitly null out any Instant URL. Without this,
|
| +// the dir ends up with a default "google.com" Instant URL which we might try
|
| +// to load at startup.
|
| +bool InstantTestBase::DisableInstantOnStartup() {
|
| + base::FilePath profile_dir;
|
| + if (!PathService::Get(chrome::DIR_USER_DATA, &profile_dir))
|
| + return false;
|
| +
|
| + profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir);
|
| + if (!file_util::CreateDirectory(profile_dir))
|
| + return false;
|
| +
|
| + base::FilePath source_dir;
|
| + if (!PathService::Get(chrome::DIR_TEST_DATA, &source_dir))
|
| + return false;
|
| +
|
| + source_dir = source_dir.AppendASCII("profiles").AppendASCII("instant");
|
| +
|
| + if (!file_util::CopyFile(source_dir.AppendASCII("Preferences"),
|
| + profile_dir.Append(chrome::kPreferencesFilename)))
|
| + return false;
|
| +
|
| + if (!file_util::CopyFile(source_dir.AppendASCII("Web Data"),
|
| + profile_dir.Append(chrome::kWebDataFilename)))
|
| + return false;
|
| +
|
| + return true;
|
| +}
|
| +
|
| +void InstantTestBase::SetupDefaultSearchProvider(
|
| + const TemplateURLData& template_data) {
|
| TemplateURLService* service =
|
| TemplateURLServiceFactory::GetForProfile(browser_->profile());
|
| ui_test_utils::WaitForTemplateURLServiceToLoad(service);
|
|
|
| - TemplateURLData data;
|
| - // Necessary to use exact URL for both the main URL and the alternate URL for
|
| - // search term extraction to work in InstantExtended.
|
| - data.SetURL(instant_url_.spec() + "q={searchTerms}");
|
| - data.instant_url = instant_url_.spec();
|
| - data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}");
|
| - data.search_terms_replacement_key = "strk";
|
| -
|
| - TemplateURL* template_url = new TemplateURL(browser_->profile(), data);
|
| + TemplateURL* template_url =
|
| + new TemplateURL(browser_->profile(), template_data);
|
| service->Add(template_url); // Takes ownership of |template_url|.
|
| service->SetDefaultSearchProvider(template_url);
|
| +}
|
|
|
| - browser_->profile()->GetPrefs()->SetBoolean(prefs::kInstantEnabled, true);
|
| -
|
| - // TODO(shishir): Fix this ugly hack.
|
| - instant()->SetInstantEnabled(false, true);
|
| - instant()->SetInstantEnabled(true, false);
|
| +InstantService* InstantTestBase::service() const {
|
| + return InstantServiceFactory::GetForProfile(browser_->profile());
|
| }
|
|
|
| -void InstantTestBase::Init(const GURL& instant_url) {
|
| - instant_url_ = instant_url;
|
| +InstantController* InstantTestBase::instant() const {
|
| + return browser_->instant_controller()->instant();
|
| }
|
|
|
| -void InstantTestBase::KillInstantRenderView() {
|
| - base::KillProcess(
|
| - instant()->GetOverlayContents()->GetRenderProcessHost()->GetHandle(),
|
| - content::RESULT_CODE_KILLED,
|
| - false);
|
| +OmniboxView* InstantTestBase::omnibox() const {
|
| + return browser_->window()->GetLocationBar()->GetLocationEntry();
|
| }
|
|
|
| void InstantTestBase::FocusOmnibox() {
|
| @@ -101,37 +161,38 @@ void InstantTestBase::FocusOmnibox() {
|
| }
|
| }
|
|
|
| -void InstantTestBase::FocusOmniboxAndWaitForInstantSupport() {
|
| - content::WindowedNotificationObserver observer(
|
| - chrome::NOTIFICATION_INSTANT_OVERLAY_SUPPORT_DETERMINED,
|
| - content::NotificationService::AllSources());
|
| - FocusOmnibox();
|
| - observer.Wait();
|
| +void InstantTestBase::WaitForInstantSupport() {
|
| + if (!service()->preloader()->supports_instant()) {
|
| + content::WindowedNotificationObserver(
|
| + chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED,
|
| + content::NotificationService::AllSources()).
|
| + Wait();
|
| + }
|
| }
|
|
|
| -void InstantTestBase::FocusOmniboxAndWaitForInstantExtendedSupport() {
|
| - content::WindowedNotificationObserver ntp_observer(
|
| - chrome::NOTIFICATION_INSTANT_NTP_SUPPORT_DETERMINED,
|
| - content::NotificationService::AllSources());
|
| - content::WindowedNotificationObserver overlay_observer(
|
| - chrome::NOTIFICATION_INSTANT_OVERLAY_SUPPORT_DETERMINED,
|
| - content::NotificationService::AllSources());
|
| +void InstantTestBase::FocusOmniboxAndWaitForInstantSupport() {
|
| FocusOmnibox();
|
| - ntp_observer.Wait();
|
| - overlay_observer.Wait();
|
| + WaitForInstantSupport();
|
| }
|
|
|
| void InstantTestBase::SetOmniboxText(const std::string& text) {
|
| - FocusOmnibox();
|
| omnibox()->SetUserText(UTF8ToUTF16(text));
|
| }
|
|
|
| +void InstantTestBase::WaitForOverlayToShow() {
|
| + InstantTestOverlayModelObserver(instant(), true).
|
| + WaitForExpectedOverlayState();
|
| +}
|
| +
|
| +void InstantTestBase::WaitForOverlayToHide() {
|
| + InstantTestOverlayModelObserver(instant(), false).
|
| + WaitForExpectedOverlayState();
|
| +}
|
| +
|
| void InstantTestBase::SetOmniboxTextAndWaitForOverlayToShow(
|
| const std::string& text) {
|
| - InstantTestModelObserver observer(
|
| - instant()->model(), chrome::search::Mode::MODE_SEARCH_SUGGESTIONS);
|
| SetOmniboxText(text);
|
| - observer.WaitForDesiredOverlayState();
|
| + WaitForOverlayToShow();
|
| }
|
|
|
| bool InstantTestBase::GetBoolFromJS(content::WebContents* contents,
|
| @@ -155,10 +216,6 @@ bool InstantTestBase::GetStringFromJS(content::WebContents* contents,
|
| contents, WrapScript(script), result);
|
| }
|
|
|
| -bool InstantTestBase::ExecuteScript(const std::string& script) {
|
| - return content::ExecuteScript(instant()->GetOverlayContents(), script);
|
| -}
|
| -
|
| bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents,
|
| bool expected) {
|
| bool actual = !expected; // Purposely start with a mis-match.
|
| @@ -168,21 +225,20 @@ bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents,
|
| actual == expected;
|
| }
|
|
|
| -bool InstantTestBase::HasUserInputInProgress() {
|
| - return omnibox()->model()->user_input_in_progress_;
|
| -}
|
| -
|
| -bool InstantTestBase::HasTemporaryText() {
|
| - return omnibox()->model()->has_temporary_text_;
|
| +void InstantTestBase::KillOverlayRenderView() {
|
| + base::KillProcess(
|
| + instant()->GetOverlayContents()->GetRenderProcessHost()->GetHandle(),
|
| + content::RESULT_CODE_KILLED,
|
| + false);
|
| }
|
|
|
| -bool InstantTestBase::LoadImage(content::RenderViewHost* rvh,
|
| - const std::string& image,
|
| +bool InstantTestBase::LoadImage(content::WebContents* contents,
|
| + const std::string& image_url,
|
| bool* loaded) {
|
| - std::string js_chrome =
|
| + std::string script =
|
| "var img = document.createElement('img');"
|
| "img.onerror = function() { domAutomationController.send(false); };"
|
| "img.onload = function() { domAutomationController.send(true); };"
|
| - "img.src = '" + image + "';";
|
| - return content::ExecuteScriptAndExtractBool(rvh, js_chrome, loaded);
|
| + "img.src = '" + image_url + "';";
|
| + return content::ExecuteScriptAndExtractBool(contents, script, loaded);
|
| }
|
|
|