summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/search_engine_list_model_unittest.mm
blob: 683d4540874907a6ec520089881eb7196446880c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/scoped_nsobject.h"
#include "chrome/browser/cocoa/browser_test_helper.h"
#import "chrome/browser/cocoa/search_engine_list_model.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_model.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"

// A helper for NSNotifications. Makes a note that it's been called back.
@interface SearchEngineListHelper : NSObject {
 @public
  BOOL sawNotification_;
}
@end

@implementation SearchEngineListHelper
- (void)entryChanged:(NSNotification*)notify {
  sawNotification_ = YES;
}
@end

class SearchEngineListModelTest : public PlatformTest {
 public:
  SearchEngineListModelTest() {
    // Build a fake set of template urls.
    template_model_.reset(new TemplateURLModel(helper_.profile()));
    TemplateURL* t_url = new TemplateURL();
    t_url->SetURL("http://www.google.com/?q={searchTerms}", 0, 0);
    t_url->set_keyword(L"keyword");
    t_url->set_short_name(L"google");
    t_url->set_show_in_default_list(true);
    template_model_->Add(t_url);
    t_url = new TemplateURL();
    t_url->SetURL("http://www.google2.com/?q={searchTerms}", 0, 0);
    t_url->set_keyword(L"keyword2");
    t_url->set_short_name(L"google2");
    t_url->set_show_in_default_list(true);
    template_model_->Add(t_url);
    EXPECT_EQ(template_model_->GetTemplateURLs().size(), 2U);

    model_.reset([[SearchEngineListModel alloc]
                    initWithModel:template_model_.get()]);
    notification_helper_.reset([[SearchEngineListHelper alloc] init]);
    [[NSNotificationCenter defaultCenter]
        addObserver:notification_helper_.get()
           selector:@selector(entryChanged:)
              name:kSearchEngineListModelChangedNotification
            object:nil];
   }
  ~SearchEngineListModelTest() {
    [[NSNotificationCenter defaultCenter]
        removeObserver:notification_helper_.get()];
  }

  BrowserTestHelper helper_;
  scoped_ptr<TemplateURLModel> template_model_;
  scoped_nsobject<SearchEngineListModel> model_;
  scoped_nsobject<SearchEngineListHelper> notification_helper_;
};

TEST_F(SearchEngineListModelTest, Init) {
  scoped_nsobject<SearchEngineListModel> model(
      [[SearchEngineListModel alloc] initWithModel:template_model_.get()]);
}

TEST_F(SearchEngineListModelTest, Engines) {
  NSArray* engines = [model_ searchEngines];
  EXPECT_EQ([engines count], 2U);
}

TEST_F(SearchEngineListModelTest, Default) {
  EXPECT_EQ([model_ defaultIndex], 0U);

  [model_ setDefaultIndex:1];
  EXPECT_EQ([model_ defaultIndex], 1U);

  // Add two more URLs, neither of which are shown in the default list.
  TemplateURL* t_url = new TemplateURL();
  t_url->SetURL("http://www.google3.com/?q={searchTerms}", 0, 0);
  t_url->set_keyword(L"keyword3");
  t_url->set_short_name(L"google3 not eligible");
  t_url->set_show_in_default_list(false);
  template_model_->Add(t_url);
  t_url = new TemplateURL();
  t_url->SetURL("http://www.google4.com/?q={searchTerms}", 0, 0);
  t_url->set_keyword(L"keyword4");
  t_url->set_short_name(L"google4");
  t_url->set_show_in_default_list(false);
  template_model_->Add(t_url);

  // Still should only have 2 engines and not these newly added ones.
  EXPECT_EQ([[model_ searchEngines] count], 2U);

  // Since keyword3 is not in the default list, the 2nd index in the default
  // keyword list should be keyword4. Test for http://crbug.com/21898.
  template_model_->SetDefaultSearchProvider(t_url);
  EXPECT_EQ([[model_ searchEngines] count], 3U);
  EXPECT_EQ([model_ defaultIndex], 2U);

  NSString* defaultString = [[model_ searchEngines] objectAtIndex:2];
  EXPECT_NSEQ(@"google4", defaultString);
}

// Make sure that when the back-end model changes that we get a notification.
TEST_F(SearchEngineListModelTest, Notification) {
  // Add one more item to force a notification.
  TemplateURL* t_url = new TemplateURL();
  t_url->SetURL("http://www.google3.com/foo/bar", 0, 0);
  t_url->set_keyword(L"keyword3");
  t_url->set_short_name(L"google3");
  t_url->set_show_in_default_list(true);
  template_model_->Add(t_url);

  EXPECT_TRUE(notification_helper_.get()->sawNotification_);
}