Chromium Code Reviews| Index: chrome/browser/ui/views/search_view_controller_browsertest.cc |
| diff --git a/chrome/browser/ui/views/search_view_controller_browsertest.cc b/chrome/browser/ui/views/search_view_controller_browsertest.cc |
| index 2b37c16e8a0969ddd5c646c46c0604c4864085ac..6b4d0e0ecd0da6b9f097df517f84118f48a76ce7 100644 |
| --- a/chrome/browser/ui/views/search_view_controller_browsertest.cc |
| +++ b/chrome/browser/ui/views/search_view_controller_browsertest.cc |
| @@ -5,13 +5,28 @@ |
| #include "chrome/browser/ui/views/search_view_controller.h" |
| #include "base/command_line.h" |
| +#include "base/string16.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/browser/bookmarks/bookmark_model.h" |
| +#include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| +#include "chrome/browser/bookmarks/bookmark_utils.h" |
| +#include "chrome/browser/instant/instant_controller.h" |
| +#include "chrome/browser/prefs/pref_service.h" |
| +#include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_commands.h" |
| #include "chrome/browser/ui/search/search.h" |
| #include "chrome/browser/ui/search/search_model.h" |
| +#include "chrome/browser/ui/search/search_model_observer.h" |
| #include "chrome/browser/ui/search/search_types.h" |
| #include "chrome/browser/ui/views/frame/browser_view.h" |
| +#include "chrome/browser/ui/views/frame/contents_container.h" |
| +#include "chrome/browser/ui/view_ids.h" |
| #include "chrome/common/chrome_switches.h" |
| +#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.
|
| +#include "chrome/common/pref_names.h" |
| #include "chrome/test/base/in_process_browser_test.h" |
| +#include "chrome/test/base/ui_test_utils.h" |
| // Currently, the features under test are only enabled under Aura. |
| #if defined(USE_AURA) |
| @@ -22,11 +37,16 @@ |
| // Avoid tests on branded Chrome where channel is set to CHANNEL_STABLE. |
| #define AVOID_TEST_ON_BRANDED_CHROME() { \ |
| - if (!chrome::search::IsInstantExtendedAPIEnabled(browser()->profile())) \ |
| + if (!IsInstantExtendedAPIEnabled(browser()->profile())) \ |
| return; \ |
| } |
| -class SearchViewControllerTest : public InProcessBrowserTest { |
| +namespace chrome { |
| +namespace search { |
| + |
| +class SearchViewControllerTest : public InProcessBrowserTest, |
| + public content::NotificationObserver, |
| + public SearchModelObserver { |
| public: |
| SearchViewControllerTest() {} |
| virtual ~SearchViewControllerTest() {} |
| @@ -35,7 +55,26 @@ class SearchViewControllerTest : public InProcessBrowserTest { |
| command_line->AppendSwitch(switches::kEnableInstantExtendedAPI); |
| } |
| - SearchViewController* controller() { |
| + virtual void SetUpOnMainThread() OVERRIDE { |
| +#if defined(USE_AURA) |
| + const ViewID kOmniboxFocusViewID = VIEW_ID_OMNIBOX; |
| +#else |
| + const ViewID kOmniboxFocusViewID = VIEW_ID_LOCATION_BAR; |
| +#endif |
| + |
| + ASSERT_NO_FATAL_FAILURE(SetUpOmniboxData()); |
| + chrome::FocusLocationBar(browser()); |
| + ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), kOmniboxFocusViewID)); |
| + |
| + // Record search model changes. |
| + browser()->search_model()->AddObserver(this); |
| + } |
| + |
| + virtual void CleanUpOnMainThread() OVERRIDE { |
| + browser()->search_model()->RemoveObserver(this); |
| + } |
| + |
| + SearchViewController* controller() const { |
| #if defined(USE_AURA) |
| const BrowserView* browser_view = |
| static_cast<BrowserView*>(browser()->window()); |
| @@ -45,7 +84,7 @@ class SearchViewControllerTest : public InProcessBrowserTest { |
| #endif // USE_AURA |
| } |
| - bool controller_state_is_ntp() { |
| + bool controller_state_is_ntp() const { |
| #if defined(USE_AURA) |
| return SearchViewController::is_ntp_state(controller()->state()); |
| #else |
| @@ -53,7 +92,75 @@ class SearchViewControllerTest : public InProcessBrowserTest { |
| #endif // USE_AURA |
| } |
| + bool controller_state_is_suggestions() const { |
| + return controller()->state() == SearchViewController::STATE_SUGGESTIONS; |
| + } |
| + |
| + bool controller_ntp_container_is_visible() const { |
| + return controller()->ntp_container_->visible(); |
| + } |
| + |
| + bool controller_contents_has_overlay() const { |
| + return controller()->contents_container_->overlay(); |
| + } |
| + |
| + bool controller_contents_overlay_visible() const { |
| + return controller()->contents_container_->overlay()->visible(); |
| + } |
| + |
| + bool controller_contents_has_preview() const { |
| + return controller()->contents_container_->preview(); |
| + } |
| + |
| + // Returns last mode change observed. |
| + Mode last_mode() const { return last_mode_; } |
| + |
| private: |
| + // Populates bookmark model with simple data set so that omnibox suggestions |
| + // are non-empty. |
| + void SetUpOmniboxData() { |
| + BookmarkModel* bookmark_model = |
| + BookmarkModelFactory::GetForProfile(browser()->profile()); |
| + ASSERT_NE(static_cast<BookmarkModel*>(NULL), bookmark_model); |
| + if (!bookmark_model->IsLoaded()) { |
| + content::NotificationRegistrar registrar; |
| + registrar.Add(this, chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED, |
| + content::Source<Profile>(browser()->profile())); |
| + content::RunMessageLoop(); |
| + } |
| + bookmark_utils::AddIfNotBookmarked(bookmark_model, |
| + GURL("http://www.farmall.com"), |
| + ASCIIToUTF16("dirt")); |
| + bookmark_utils::AddIfNotBookmarked(bookmark_model, |
| + GURL("http://www.johndeere.com"), |
| + ASCIIToUTF16("dirty")); |
| + bookmark_utils::AddIfNotBookmarked(bookmark_model, |
| + GURL("http://www.kubota.com"), |
| + ASCIIToUTF16("dirtier")); |
| + } |
| + |
| + // content::NotificationObserver overrides: |
| + virtual void Observe(int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) OVERRIDE { |
| + switch (type) { |
| + case chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED: |
| + break; |
| + default: |
| + FAIL() << "Unexpected notification type."; |
| + } |
| + MessageLoop::current()->Quit(); |
| + } |
| + |
| + // SearchModelObserver overrides: |
| + virtual void ModeChanged(const Mode& old_mode, |
| + const Mode& new_mode) OVERRIDE { |
| + last_mode_ = new_mode; |
| + } |
| + |
| + // Recorded when |ModeChanged| is observed. |
| + Mode last_mode_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(SearchViewControllerTest); |
| }; |
| @@ -68,3 +175,71 @@ IN_PROC_BROWSER_TEST_F(SearchViewControllerTest, MAYBE(StartToNTP)) { |
| EXPECT_TRUE(browser()->search_model()->mode().is_ntp()); |
| EXPECT_TRUE(controller_state_is_ntp()); |
| } |
| + |
| +IN_PROC_BROWSER_TEST_F(SearchViewControllerTest, |
| + MAYBE(NTPToNativeSuggestions)) { |
| + AVOID_TEST_ON_BRANDED_CHROME(); |
| + |
| + // Initial state. |
| + EXPECT_TRUE(browser()->search_model()->mode().is_default()); |
| + ASSERT_NE(static_cast<SearchViewController*>(NULL), controller()); |
| + |
| + // Get to NTP. |
| + AddTabAtIndex(0, GURL("chrome://newtab"), content::PAGE_TRANSITION_LINK); |
| + EXPECT_TRUE(browser()->search_model()->mode().is_ntp()); |
| + EXPECT_TRUE(controller_state_is_ntp()); |
| + |
| + // Type into the omnibox. |
| + ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_D, |
| + false, false, false, false)); |
| + // Expect it to change mode to show suggestions. |
| + EXPECT_EQ(Mode(Mode::MODE_SEARCH_SUGGESTIONS, true), last_mode()); |
| + |
| + // Expect that the controller state ends at |STATE_SUGGESTIONS|. |
| + EXPECT_TRUE(controller_state_is_suggestions()); |
| + |
| + // Expect that the |ntp_container_| layer is hidden. |
| + EXPECT_FALSE(controller_ntp_container_is_visible()); |
| + |
| + // Expect that the |contents_container_| has the overlay set, but no preview. |
| + ASSERT_TRUE(controller_contents_has_overlay()); |
| + EXPECT_TRUE(controller_contents_overlay_visible()); |
| + EXPECT_FALSE(controller_contents_has_preview()); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(SearchViewControllerTest, |
| + MAYBE(NTPToInstantSuggestions)) { |
| + AVOID_TEST_ON_BRANDED_CHROME(); |
| + |
| + PrefService* service = browser()->profile()->GetPrefs(); |
| + service->SetBoolean(prefs::kInstantEnabled, true); |
| + EXPECT_TRUE(InstantController::IsInstantEnabled(browser()->profile())); |
| + |
| + // Initial state. |
| + EXPECT_TRUE(browser()->search_model()->mode().is_default()); |
| + ASSERT_NE(static_cast<SearchViewController*>(NULL), controller()); |
| + |
| + // Get to NTP. |
| + AddTabAtIndex(0, GURL("chrome://newtab"), content::PAGE_TRANSITION_LINK); |
| + EXPECT_TRUE(browser()->search_model()->mode().is_ntp()); |
| + EXPECT_TRUE(controller_state_is_ntp()); |
| + |
| + // Type into the omnibox. |
| + ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_D, |
| + false, false, false, false)); |
| + // Expect it to change mode to show suggestions. |
| + EXPECT_EQ(Mode(Mode::MODE_SEARCH_SUGGESTIONS, true), last_mode()); |
| + |
| + // Expect that the controller state ends at |STATE_SUGGESTIONS|. |
| + EXPECT_TRUE(controller_state_is_suggestions()); |
| + |
| + // Expect that the |ntp_container_| layer is hidden. |
| + EXPECT_FALSE(controller_ntp_container_is_visible()); |
| + |
| + // Expect that the |contents_container_| overlay exists and but is invisible. |
| + ASSERT_TRUE(controller_contents_has_overlay()); |
| + EXPECT_FALSE(controller_contents_overlay_visible()); |
| +} |
| + |
| +} // namespace search |
| +} // namespace chrome |