Home | History | Annotate | Download | only in search
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/ui/search/search_model.h"
      6 
      7 #include "base/command_line.h"
      8 #include "chrome/browser/ui/search/search_model_observer.h"
      9 #include "chrome/browser/ui/search/search_tab_helper.h"
     10 #include "chrome/common/chrome_switches.h"
     11 #include "chrome/common/search_types.h"
     12 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
     13 
     14 namespace {
     15 
     16 class MockSearchModelObserver : public SearchModelObserver {
     17  public:
     18   MockSearchModelObserver();
     19   virtual ~MockSearchModelObserver();
     20 
     21   virtual void ModelChanged(const SearchModel::State& old_state,
     22                             const SearchModel::State& new_state) OVERRIDE;
     23 
     24   void VerifySearchModelStates(const SearchModel::State& expected_old_state,
     25                                const SearchModel::State& expected_new_state);
     26 
     27   void VerifyNotificationCount(int expected_count);
     28 
     29  private:
     30   // How many times we've seen search model changed notifications.
     31   int modelchanged_notification_count_;
     32 
     33   SearchModel::State actual_old_state_;
     34   SearchModel::State actual_new_state_;
     35 
     36   DISALLOW_COPY_AND_ASSIGN(MockSearchModelObserver);
     37 };
     38 
     39 MockSearchModelObserver::MockSearchModelObserver()
     40     : modelchanged_notification_count_(0) {
     41 }
     42 
     43 MockSearchModelObserver::~MockSearchModelObserver() {
     44 }
     45 
     46 void MockSearchModelObserver::ModelChanged(
     47     const SearchModel::State& old_state,
     48     const SearchModel::State& new_state) {
     49   actual_old_state_ = old_state;
     50   actual_new_state_ = new_state;
     51   modelchanged_notification_count_++;
     52 }
     53 
     54 void MockSearchModelObserver::VerifySearchModelStates(
     55     const SearchModel::State& expected_old_state,
     56     const SearchModel::State& expected_new_state) {
     57   EXPECT_TRUE(actual_old_state_ == expected_old_state);
     58   EXPECT_TRUE(actual_new_state_ == expected_new_state);
     59 }
     60 
     61 void MockSearchModelObserver::VerifyNotificationCount(int expected_count) {
     62   EXPECT_EQ(modelchanged_notification_count_, expected_count);
     63 }
     64 
     65 }  // namespace
     66 
     67 class SearchModelTest : public ChromeRenderViewHostTestHarness {
     68  public:
     69   virtual void SetUp() OVERRIDE;
     70   virtual void TearDown() OVERRIDE;
     71 
     72   MockSearchModelObserver mock_observer;
     73   SearchModel* model;
     74 };
     75 
     76 void SearchModelTest::SetUp() {
     77   CommandLine::ForCurrentProcess()->AppendSwitch(
     78       switches::kEnableInstantExtendedAPI);
     79   ChromeRenderViewHostTestHarness::SetUp();
     80   SearchTabHelper::CreateForWebContents(web_contents());
     81   SearchTabHelper* search_tab_helper =
     82       SearchTabHelper::FromWebContents(web_contents());
     83   ASSERT_TRUE(search_tab_helper != NULL);
     84   model = search_tab_helper->model();
     85   model->AddObserver(&mock_observer);
     86 }
     87 
     88 void SearchModelTest::TearDown() {
     89   model->RemoveObserver(&mock_observer);
     90   ChromeRenderViewHostTestHarness::TearDown();
     91 }
     92 
     93 TEST_F(SearchModelTest, UpdateSearchModelInstantSupport) {
     94   mock_observer.VerifyNotificationCount(0);
     95   EXPECT_TRUE(model->instant_support() == INSTANT_SUPPORT_UNKNOWN);
     96   SearchModel::State expected_old_state = model->state();
     97   SearchModel::State expected_new_state(model->state());
     98   expected_new_state.instant_support = INSTANT_SUPPORT_YES;
     99 
    100   model->SetInstantSupportState(INSTANT_SUPPORT_YES);
    101   mock_observer.VerifySearchModelStates(expected_old_state, expected_new_state);
    102   mock_observer.VerifyNotificationCount(1);
    103   EXPECT_TRUE(model->instant_support() == INSTANT_SUPPORT_YES);
    104 
    105   expected_old_state = expected_new_state;
    106   expected_new_state.instant_support = INSTANT_SUPPORT_NO;
    107   model->SetInstantSupportState(INSTANT_SUPPORT_NO);
    108   mock_observer.VerifySearchModelStates(expected_old_state, expected_new_state);
    109   mock_observer.VerifyNotificationCount(2);
    110 
    111   // Notify the observer only if the search model state is changed.
    112   model->SetInstantSupportState(INSTANT_SUPPORT_NO);
    113   EXPECT_TRUE(model->state() == expected_new_state);
    114   EXPECT_TRUE(model->instant_support() == INSTANT_SUPPORT_NO);
    115   mock_observer.VerifyNotificationCount(2);
    116 }
    117 
    118 TEST_F(SearchModelTest, UpdateSearchModelMode) {
    119   mock_observer.VerifyNotificationCount(0);
    120   SearchMode search_mode(SearchMode::MODE_NTP, SearchMode::ORIGIN_NTP);
    121   SearchModel::State expected_old_state = model->state();
    122   SearchModel::State expected_new_state(model->state());
    123   expected_new_state.mode = search_mode;
    124 
    125   model->SetMode(search_mode);
    126   mock_observer.VerifySearchModelStates(expected_old_state, expected_new_state);
    127   mock_observer.VerifyNotificationCount(1);
    128 
    129   search_mode.mode = SearchMode::MODE_SEARCH_RESULTS;
    130   expected_old_state = expected_new_state;
    131   expected_new_state.mode = search_mode;
    132   model->SetMode(search_mode);
    133   mock_observer.VerifySearchModelStates(expected_old_state, expected_new_state);
    134   mock_observer.VerifyNotificationCount(2);
    135   EXPECT_TRUE(model->state() == expected_new_state);
    136 }
    137 
    138 TEST_F(SearchModelTest, UpdateSearchModelState) {
    139   SearchModel::State expected_new_state(model->state());
    140   expected_new_state.instant_support = INSTANT_SUPPORT_NO;
    141   EXPECT_FALSE(model->state() == expected_new_state);
    142   model->SetState(expected_new_state);
    143   mock_observer.VerifyNotificationCount(1);
    144   EXPECT_TRUE(model->state() == expected_new_state);
    145 }
    146 
    147 TEST_F(SearchModelTest, UpdateVoiceSearchSupported) {
    148   mock_observer.VerifyNotificationCount(0);
    149   EXPECT_FALSE(model->voice_search_supported());
    150 
    151   SearchModel::State expected_old_state = model->state();
    152   SearchModel::State expected_new_state(model->state());
    153   expected_new_state.voice_search_supported = true;
    154 
    155   model->SetVoiceSearchSupported(true);
    156   mock_observer.VerifySearchModelStates(expected_old_state, expected_new_state);
    157   mock_observer.VerifyNotificationCount(1);
    158   EXPECT_TRUE(model->voice_search_supported());
    159 }
    160