| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/instant/instant_test_utils.h" | 5 #include "chrome/browser/instant/instant_test_utils.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/search_engines/template_url_service.h" | 10 #include "chrome/browser/search_engines/template_url_service.h" |
| 11 #include "chrome/browser/search_engines/template_url_service_factory.h" | 11 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 12 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
| 13 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "chrome/common/chrome_switches.h" |
| 12 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/test/base/interactive_test_utils.h" | 16 #include "chrome/test/base/interactive_test_utils.h" |
| 14 #include "chrome/test/base/ui_test_utils.h" | 17 #include "chrome/test/base/ui_test_utils.h" |
| 18 #include "content/public/browser/notification_service.h" |
| 15 #include "content/public/browser/render_process_host.h" | 19 #include "content/public/browser/render_process_host.h" |
| 16 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/common/result_codes.h" | 21 #include "content/public/common/result_codes.h" |
| 18 #include "content/public/test/browser_test_utils.h" | 22 #include "content/public/test/browser_test_utils.h" |
| 19 | 23 |
| 20 namespace { | 24 namespace { |
| 21 | 25 |
| 22 std::string WrapScript(const std::string& script) { | 26 std::string WrapScript(const std::string& script) { |
| 23 return "domAutomationController.send(" + script + ")"; | 27 return "domAutomationController.send(" + script + ")"; |
| 24 } | 28 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 43 run_loop_.Run(); | 47 run_loop_.Run(); |
| 44 } | 48 } |
| 45 | 49 |
| 46 void InstantTestModelObserver::PreviewStateChanged(const InstantModel& model) { | 50 void InstantTestModelObserver::PreviewStateChanged(const InstantModel& model) { |
| 47 if (model.mode().mode == desired_mode_type_) | 51 if (model.mode().mode == desired_mode_type_) |
| 48 run_loop_.Quit(); | 52 run_loop_.Quit(); |
| 49 } | 53 } |
| 50 | 54 |
| 51 // InstantTestBase ----------------------------------------------------------- | 55 // InstantTestBase ----------------------------------------------------------- |
| 52 | 56 |
| 53 void InstantTestBase::SetupInstant() { | 57 void InstantTestBase::SetupInstant(Browser* browser) { |
| 58 browser_ = browser; |
| 54 TemplateURLService* service = | 59 TemplateURLService* service = |
| 55 TemplateURLServiceFactory::GetForProfile(browser()->profile()); | 60 TemplateURLServiceFactory::GetForProfile(browser_->profile()); |
| 56 ui_test_utils::WaitForTemplateURLServiceToLoad(service); | 61 ui_test_utils::WaitForTemplateURLServiceToLoad(service); |
| 57 | 62 |
| 58 TemplateURLData data; | 63 TemplateURLData data; |
| 59 // Necessary to use exact URL for both the main URL and the alternate URL for | 64 // Necessary to use exact URL for both the main URL and the alternate URL for |
| 60 // search term extraction to work in InstantExtended. | 65 // search term extraction to work in InstantExtended. |
| 61 data.SetURL(instant_url_.spec() + "q={searchTerms}"); | 66 data.SetURL(instant_url_.spec() + "q={searchTerms}"); |
| 62 data.instant_url = instant_url_.spec(); | 67 data.instant_url = instant_url_.spec(); |
| 63 data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}"); | 68 data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}"); |
| 64 data.search_terms_replacement_key = "strk"; | 69 data.search_terms_replacement_key = "strk"; |
| 65 | 70 |
| 66 TemplateURL* template_url = new TemplateURL(browser()->profile(), data); | 71 TemplateURL* template_url = new TemplateURL(browser_->profile(), data); |
| 67 service->Add(template_url); // Takes ownership of |template_url|. | 72 service->Add(template_url); // Takes ownership of |template_url|. |
| 68 service->SetDefaultSearchProvider(template_url); | 73 service->SetDefaultSearchProvider(template_url); |
| 69 | 74 |
| 70 browser()->profile()->GetPrefs()->SetBoolean(prefs::kInstantEnabled, true); | 75 browser_->profile()->GetPrefs()->SetBoolean(prefs::kInstantEnabled, true); |
| 71 | 76 |
| 72 // TODO(shishir): Fix this ugly hack. | 77 // TODO(shishir): Fix this ugly hack. |
| 73 instant()->SetInstantEnabled(false, true); | 78 instant()->SetInstantEnabled(false, true); |
| 74 instant()->SetInstantEnabled(true, false); | 79 instant()->SetInstantEnabled(true, false); |
| 75 } | 80 } |
| 76 | 81 |
| 82 void InstantTestBase::Init(const GURL& instant_url) { |
| 83 instant_url_ = instant_url; |
| 84 } |
| 85 |
| 77 void InstantTestBase::KillInstantRenderView() { | 86 void InstantTestBase::KillInstantRenderView() { |
| 78 base::KillProcess( | 87 base::KillProcess( |
| 79 instant()->GetPreviewContents()->GetRenderProcessHost()->GetHandle(), | 88 instant()->GetPreviewContents()->GetRenderProcessHost()->GetHandle(), |
| 80 content::RESULT_CODE_KILLED, | 89 content::RESULT_CODE_KILLED, |
| 81 false); | 90 false); |
| 82 } | 91 } |
| 83 | 92 |
| 84 void InstantTestBase::FocusOmnibox() { | 93 void InstantTestBase::FocusOmnibox() { |
| 85 // If the omnibox already has focus, just notify Instant. | 94 // If the omnibox already has focus, just notify Instant. |
| 86 if (omnibox()->model()->has_focus()) { | 95 if (omnibox()->model()->has_focus()) { |
| 87 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, | 96 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, |
| 88 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); | 97 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); |
| 89 } else { | 98 } else { |
| 90 browser()->window()->GetLocationBar()->FocusLocation(false); | 99 browser_->window()->GetLocationBar()->FocusLocation(false); |
| 91 } | 100 } |
| 92 } | 101 } |
| 93 | 102 |
| 103 void InstantTestBase::FocusOmniboxAndWaitForInstantSupport() { |
| 104 content::WindowedNotificationObserver observer( |
| 105 chrome::NOTIFICATION_INSTANT_OVERLAY_SUPPORT_DETERMINED, |
| 106 content::NotificationService::AllSources()); |
| 107 FocusOmnibox(); |
| 108 observer.Wait(); |
| 109 } |
| 110 |
| 111 void InstantTestBase::FocusOmniboxAndWaitForInstantExtendedSupport() { |
| 112 content::WindowedNotificationObserver ntp_observer( |
| 113 chrome::NOTIFICATION_INSTANT_NTP_SUPPORT_DETERMINED, |
| 114 content::NotificationService::AllSources()); |
| 115 content::WindowedNotificationObserver overlay_observer( |
| 116 chrome::NOTIFICATION_INSTANT_OVERLAY_SUPPORT_DETERMINED, |
| 117 content::NotificationService::AllSources()); |
| 118 FocusOmnibox(); |
| 119 ntp_observer.Wait(); |
| 120 overlay_observer.Wait(); |
| 121 } |
| 122 |
| 94 void InstantTestBase::SetOmniboxText(const std::string& text) { | 123 void InstantTestBase::SetOmniboxText(const std::string& text) { |
| 95 FocusOmnibox(); | 124 FocusOmnibox(); |
| 96 omnibox()->SetUserText(UTF8ToUTF16(text)); | 125 omnibox()->SetUserText(UTF8ToUTF16(text)); |
| 97 } | 126 } |
| 98 | 127 |
| 99 void InstantTestBase::SetOmniboxTextAndWaitForInstantToShow( | 128 void InstantTestBase::SetOmniboxTextAndWaitForInstantToShow( |
| 100 const std::string& text) { | 129 const std::string& text) { |
| 101 InstantTestModelObserver observer( | 130 InstantTestModelObserver observer( |
| 102 instant()->model(), chrome::search::Mode::MODE_SEARCH_SUGGESTIONS); | 131 instant()->model(), chrome::search::Mode::MODE_SEARCH_SUGGESTIONS); |
| 103 SetOmniboxText(text); | 132 SetOmniboxText(text); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 actual == expected; | 167 actual == expected; |
| 139 } | 168 } |
| 140 | 169 |
| 141 bool InstantTestBase::HasUserInputInProgress() { | 170 bool InstantTestBase::HasUserInputInProgress() { |
| 142 return omnibox()->model()->user_input_in_progress_; | 171 return omnibox()->model()->user_input_in_progress_; |
| 143 } | 172 } |
| 144 | 173 |
| 145 bool InstantTestBase::HasTemporaryText() { | 174 bool InstantTestBase::HasTemporaryText() { |
| 146 return omnibox()->model()->has_temporary_text_; | 175 return omnibox()->model()->has_temporary_text_; |
| 147 } | 176 } |
| 177 |
| 178 bool InstantTestBase::LoadImage(content::RenderViewHost* rvh, |
| 179 const std::string& image, |
| 180 bool* loaded) { |
| 181 std::string js_chrome = |
| 182 "var img = document.createElement('img');" |
| 183 "img.onerror = function() { domAutomationController.send(false); };" |
| 184 "img.onload = function() { domAutomationController.send(true); };" |
| 185 "img.src = '" + image + "';"; |
| 186 return content::ExecuteScriptAndExtractBool(rvh, js_chrome, loaded); |
| 187 } |
| OLD | NEW |