Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/ui/views/search_view_controller.h" | 5 #include "chrome/browser/ui/views/search_view_controller.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/string16.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "chrome/browser/bookmarks/bookmark_model.h" | |
| 11 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | |
| 12 #include "chrome/browser/bookmarks/bookmark_utils.h" | |
| 13 #include "chrome/browser/instant/instant_controller.h" | |
| 14 #include "chrome/browser/prefs/pref_service.h" | |
| 15 #include "chrome/browser/profiles/profile.h" | |
| 8 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/browser_commands.h" | |
| 9 #include "chrome/browser/ui/search/search.h" | 18 #include "chrome/browser/ui/search/search.h" |
| 10 #include "chrome/browser/ui/search/search_model.h" | 19 #include "chrome/browser/ui/search/search_model.h" |
| 20 #include "chrome/browser/ui/search/search_model_observer.h" | |
| 11 #include "chrome/browser/ui/search/search_types.h" | 21 #include "chrome/browser/ui/search/search_types.h" |
| 12 #include "chrome/browser/ui/views/frame/browser_view.h" | 22 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 23 #include "chrome/browser/ui/views/frame/contents_container.h" | |
| 24 #include "chrome/browser/ui/view_ids.h" | |
| 13 #include "chrome/common/chrome_switches.h" | 25 #include "chrome/common/chrome_switches.h" |
| 26 #include "chrome/common/chrome_notification_types.h" | |
|
kuan
2012/09/12 18:27:43
nit: this shld be b4 chrome_switches.h
dhollowa
2012/09/12 18:49:23
Done.
| |
| 27 #include "chrome/common/pref_names.h" | |
| 14 #include "chrome/test/base/in_process_browser_test.h" | 28 #include "chrome/test/base/in_process_browser_test.h" |
| 29 #include "chrome/test/base/ui_test_utils.h" | |
| 15 | 30 |
| 16 // Currently, the features under test are only enabled under Aura. | 31 // Currently, the features under test are only enabled under Aura. |
| 17 #if defined(USE_AURA) | 32 #if defined(USE_AURA) |
| 18 #define MAYBE(x) x | 33 #define MAYBE(x) x |
| 19 #else | 34 #else |
| 20 #define MAYBE(x) DISABLED_##x | 35 #define MAYBE(x) DISABLED_##x |
| 21 #endif | 36 #endif |
| 22 | 37 |
| 23 // Avoid tests on branded Chrome where channel is set to CHANNEL_STABLE. | 38 // Avoid tests on branded Chrome where channel is set to CHANNEL_STABLE. |
| 24 #define AVOID_TEST_ON_BRANDED_CHROME() { \ | 39 #define AVOID_TEST_ON_BRANDED_CHROME() { \ |
| 25 if (!chrome::search::IsInstantExtendedAPIEnabled(browser()->profile())) \ | 40 if (!IsInstantExtendedAPIEnabled(browser()->profile())) \ |
| 26 return; \ | 41 return; \ |
| 27 } | 42 } |
| 28 | 43 |
| 29 class SearchViewControllerTest : public InProcessBrowserTest { | 44 namespace chrome { |
| 45 namespace search { | |
| 46 | |
| 47 class SearchViewControllerTest : public InProcessBrowserTest, | |
| 48 public content::NotificationObserver, | |
| 49 public SearchModelObserver { | |
| 30 public: | 50 public: |
| 31 SearchViewControllerTest() {} | 51 SearchViewControllerTest() {} |
| 32 virtual ~SearchViewControllerTest() {} | 52 virtual ~SearchViewControllerTest() {} |
| 33 | 53 |
| 34 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 54 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 35 command_line->AppendSwitch(switches::kEnableInstantExtendedAPI); | 55 command_line->AppendSwitch(switches::kEnableInstantExtendedAPI); |
| 36 } | 56 } |
| 37 | 57 |
| 38 SearchViewController* controller() { | 58 virtual void SetUpOnMainThread() OVERRIDE { |
| 59 #if defined(USE_AURA) | |
| 60 const ViewID kOmniboxFocusViewID = VIEW_ID_OMNIBOX; | |
| 61 #else | |
| 62 const ViewID kOmniboxFocusViewID = VIEW_ID_LOCATION_BAR; | |
| 63 #endif | |
| 64 | |
| 65 ASSERT_NO_FATAL_FAILURE(SetUpOmniboxData()); | |
| 66 chrome::FocusLocationBar(browser()); | |
| 67 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), kOmniboxFocusViewID)); | |
| 68 | |
| 69 // Record search model changes. | |
| 70 browser()->search_model()->AddObserver(this); | |
| 71 } | |
| 72 | |
| 73 virtual void CleanUpOnMainThread() OVERRIDE { | |
| 74 browser()->search_model()->RemoveObserver(this); | |
| 75 } | |
| 76 | |
| 77 SearchViewController* controller() const { | |
| 39 #if defined(USE_AURA) | 78 #if defined(USE_AURA) |
| 40 const BrowserView* browser_view = | 79 const BrowserView* browser_view = |
| 41 static_cast<BrowserView*>(browser()->window()); | 80 static_cast<BrowserView*>(browser()->window()); |
| 42 return browser_view->search_view_controller(); | 81 return browser_view->search_view_controller(); |
| 43 #else | 82 #else |
| 44 return NULL; | 83 return NULL; |
| 45 #endif // USE_AURA | 84 #endif // USE_AURA |
| 46 } | 85 } |
| 47 | 86 |
| 48 bool controller_state_is_ntp() { | 87 bool controller_state_is_ntp() const { |
| 49 #if defined(USE_AURA) | 88 #if defined(USE_AURA) |
| 50 return SearchViewController::is_ntp_state(controller()->state()); | 89 return SearchViewController::is_ntp_state(controller()->state()); |
| 51 #else | 90 #else |
| 52 return false; | 91 return false; |
| 53 #endif // USE_AURA | 92 #endif // USE_AURA |
| 54 } | 93 } |
| 55 | 94 |
| 95 bool controller_state_is_suggestions() const { | |
| 96 return controller()->state() == SearchViewController::STATE_SUGGESTIONS; | |
| 97 } | |
| 98 | |
| 99 bool controller_ntp_container_is_visible() const { | |
| 100 return controller()->ntp_container_->visible(); | |
| 101 } | |
| 102 | |
| 103 bool controller_contents_has_overlay() const { | |
| 104 return controller()->contents_container_->overlay(); | |
| 105 } | |
| 106 | |
| 107 bool controller_contents_overlay_visible() const { | |
| 108 return controller()->contents_container_->overlay()->visible(); | |
| 109 } | |
| 110 | |
| 111 bool controller_contents_has_preview() const { | |
| 112 return controller()->contents_container_->preview(); | |
| 113 } | |
| 114 | |
| 115 // Returns last mode change observed. | |
| 116 Mode last_mode() const { return last_mode_; } | |
| 117 | |
| 56 private: | 118 private: |
| 119 // Populates bookmark model with simple data set so that omnibox suggestions | |
| 120 // are non-empty. | |
| 121 void SetUpOmniboxData() { | |
| 122 BookmarkModel* bookmark_model = | |
| 123 BookmarkModelFactory::GetForProfile(browser()->profile()); | |
| 124 ASSERT_NE(static_cast<BookmarkModel*>(NULL), bookmark_model); | |
| 125 if (!bookmark_model->IsLoaded()) { | |
| 126 content::NotificationRegistrar registrar; | |
| 127 registrar.Add(this, chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED, | |
| 128 content::Source<Profile>(browser()->profile())); | |
| 129 content::RunMessageLoop(); | |
| 130 } | |
| 131 bookmark_utils::AddIfNotBookmarked(bookmark_model, | |
| 132 GURL("http://www.farmall.com"), | |
| 133 ASCIIToUTF16("dirt")); | |
| 134 bookmark_utils::AddIfNotBookmarked(bookmark_model, | |
| 135 GURL("http://www.johndeere.com"), | |
| 136 ASCIIToUTF16("dirty")); | |
| 137 bookmark_utils::AddIfNotBookmarked(bookmark_model, | |
| 138 GURL("http://www.kubota.com"), | |
| 139 ASCIIToUTF16("dirtier")); | |
| 140 } | |
| 141 | |
| 142 // content::NotificationObserver overrides: | |
| 143 virtual void Observe(int type, | |
| 144 const content::NotificationSource& source, | |
| 145 const content::NotificationDetails& details) OVERRIDE { | |
| 146 switch (type) { | |
| 147 case chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED: | |
| 148 break; | |
| 149 default: | |
| 150 FAIL() << "Unexpected notification type."; | |
| 151 } | |
| 152 MessageLoop::current()->Quit(); | |
| 153 } | |
| 154 | |
| 155 // SearchModelObserver overrides: | |
| 156 virtual void ModeChanged(const Mode& old_mode, | |
| 157 const Mode& new_mode) OVERRIDE { | |
| 158 last_mode_ = new_mode; | |
| 159 } | |
| 160 | |
| 161 // Recorded when |ModeChanged| is observed. | |
| 162 Mode last_mode_; | |
| 163 | |
| 57 DISALLOW_COPY_AND_ASSIGN(SearchViewControllerTest); | 164 DISALLOW_COPY_AND_ASSIGN(SearchViewControllerTest); |
| 58 }; | 165 }; |
| 59 | 166 |
| 60 IN_PROC_BROWSER_TEST_F(SearchViewControllerTest, MAYBE(StartToNTP)) { | 167 IN_PROC_BROWSER_TEST_F(SearchViewControllerTest, MAYBE(StartToNTP)) { |
| 61 AVOID_TEST_ON_BRANDED_CHROME(); | 168 AVOID_TEST_ON_BRANDED_CHROME(); |
| 62 | 169 |
| 63 // Initial state. | 170 // Initial state. |
| 64 EXPECT_TRUE(browser()->search_model()->mode().is_default()); | 171 EXPECT_TRUE(browser()->search_model()->mode().is_default()); |
| 65 ASSERT_NE(static_cast<SearchViewController*>(NULL), controller()); | 172 ASSERT_NE(static_cast<SearchViewController*>(NULL), controller()); |
| 66 | 173 |
| 67 AddTabAtIndex(0, GURL("chrome://newtab"), content::PAGE_TRANSITION_LINK); | 174 AddTabAtIndex(0, GURL("chrome://newtab"), content::PAGE_TRANSITION_LINK); |
| 68 EXPECT_TRUE(browser()->search_model()->mode().is_ntp()); | 175 EXPECT_TRUE(browser()->search_model()->mode().is_ntp()); |
| 69 EXPECT_TRUE(controller_state_is_ntp()); | 176 EXPECT_TRUE(controller_state_is_ntp()); |
| 70 } | 177 } |
| 178 | |
| 179 IN_PROC_BROWSER_TEST_F(SearchViewControllerTest, | |
| 180 MAYBE(NTPToNativeSuggestions)) { | |
| 181 AVOID_TEST_ON_BRANDED_CHROME(); | |
| 182 | |
| 183 // Initial state. | |
| 184 EXPECT_TRUE(browser()->search_model()->mode().is_default()); | |
| 185 ASSERT_NE(static_cast<SearchViewController*>(NULL), controller()); | |
| 186 | |
| 187 // Get to NTP. | |
| 188 AddTabAtIndex(0, GURL("chrome://newtab"), content::PAGE_TRANSITION_LINK); | |
| 189 EXPECT_TRUE(browser()->search_model()->mode().is_ntp()); | |
| 190 EXPECT_TRUE(controller_state_is_ntp()); | |
| 191 | |
| 192 // Type into the omnibox. | |
| 193 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_D, | |
| 194 false, false, false, false)); | |
| 195 // Expect it to change mode to show suggestions. | |
| 196 EXPECT_EQ(Mode(Mode::MODE_SEARCH_SUGGESTIONS, true), last_mode()); | |
| 197 | |
| 198 // Expect that the controller state ends at |STATE_SUGGESTIONS|. | |
| 199 EXPECT_TRUE(controller_state_is_suggestions()); | |
| 200 | |
| 201 // Expect that the |ntp_container_| layer is hidden. | |
| 202 EXPECT_FALSE(controller_ntp_container_is_visible()); | |
| 203 | |
| 204 // Expect that the |contents_container_| has the overlay set, but no preview. | |
| 205 ASSERT_TRUE(controller_contents_has_overlay()); | |
| 206 EXPECT_TRUE(controller_contents_overlay_visible()); | |
| 207 EXPECT_FALSE(controller_contents_has_preview()); | |
| 208 } | |
| 209 | |
| 210 IN_PROC_BROWSER_TEST_F(SearchViewControllerTest, | |
| 211 MAYBE(NTPToInstantSuggestions)) { | |
| 212 AVOID_TEST_ON_BRANDED_CHROME(); | |
| 213 | |
| 214 PrefService* service = browser()->profile()->GetPrefs(); | |
| 215 service->SetBoolean(prefs::kInstantEnabled, true); | |
| 216 EXPECT_TRUE(InstantController::IsInstantEnabled(browser()->profile())); | |
| 217 | |
| 218 // Initial state. | |
| 219 EXPECT_TRUE(browser()->search_model()->mode().is_default()); | |
| 220 ASSERT_NE(static_cast<SearchViewController*>(NULL), controller()); | |
| 221 | |
| 222 // Get to NTP. | |
| 223 AddTabAtIndex(0, GURL("chrome://newtab"), content::PAGE_TRANSITION_LINK); | |
| 224 EXPECT_TRUE(browser()->search_model()->mode().is_ntp()); | |
| 225 EXPECT_TRUE(controller_state_is_ntp()); | |
| 226 | |
| 227 // Type into the omnibox. | |
| 228 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_D, | |
| 229 false, false, false, false)); | |
| 230 // Expect it to change mode to show suggestions. | |
| 231 EXPECT_EQ(Mode(Mode::MODE_SEARCH_SUGGESTIONS, true), last_mode()); | |
| 232 | |
| 233 // Expect that the controller state ends at |STATE_SUGGESTIONS|. | |
| 234 EXPECT_TRUE(controller_state_is_suggestions()); | |
| 235 | |
| 236 // Expect that the |ntp_container_| layer is hidden. | |
| 237 EXPECT_FALSE(controller_ntp_container_is_visible()); | |
| 238 | |
| 239 // Expect that the |contents_container_| overlay exists and but is invisible. | |
| 240 ASSERT_TRUE(controller_contents_has_overlay()); | |
| 241 EXPECT_FALSE(controller_contents_overlay_visible()); | |
| 242 } | |
| 243 | |
| 244 } // namespace search | |
| 245 } // namespace chrome | |
| OLD | NEW |