OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/scoped_nsobject.h" | 5 #include "base/scoped_nsobject.h" |
| 6 #include "base/utf_string_conversions.h" |
6 #include "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
7 #include "chrome/browser/search_engines/template_url.h" | 8 #include "chrome/browser/search_engines/template_url.h" |
8 #include "chrome/browser/search_engines/template_url_model.h" | 9 #include "chrome/browser/search_engines/template_url_model.h" |
9 #include "chrome/browser/ui/cocoa/browser_test_helper.h" | 10 #include "chrome/browser/ui/cocoa/browser_test_helper.h" |
10 #import "chrome/browser/ui/cocoa/search_engine_list_model.h" | 11 #import "chrome/browser/ui/cocoa/search_engine_list_model.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 #import "testing/gtest_mac.h" | 13 #import "testing/gtest_mac.h" |
13 #include "testing/platform_test.h" | 14 #include "testing/platform_test.h" |
14 | 15 |
15 // A helper for NSNotifications. Makes a note that it's been called back. | 16 // A helper for NSNotifications. Makes a note that it's been called back. |
16 @interface SearchEngineListHelper : NSObject { | 17 @interface SearchEngineListHelper : NSObject { |
17 @public | 18 @public |
18 BOOL sawNotification_; | 19 BOOL sawNotification_; |
19 } | 20 } |
20 @end | 21 @end |
21 | 22 |
22 @implementation SearchEngineListHelper | 23 @implementation SearchEngineListHelper |
23 - (void)entryChanged:(NSNotification*)notify { | 24 - (void)entryChanged:(NSNotification*)notify { |
24 sawNotification_ = YES; | 25 sawNotification_ = YES; |
25 } | 26 } |
26 @end | 27 @end |
27 | 28 |
28 class SearchEngineListModelTest : public PlatformTest { | 29 class SearchEngineListModelTest : public PlatformTest { |
29 public: | 30 public: |
30 SearchEngineListModelTest() { | 31 SearchEngineListModelTest() { |
31 // Build a fake set of template urls. | 32 // Build a fake set of template urls. |
32 template_model_.reset(new TemplateURLModel(helper_.profile())); | 33 template_model_.reset(new TemplateURLModel(helper_.profile())); |
33 TemplateURL* t_url = new TemplateURL(); | 34 TemplateURL* t_url = new TemplateURL(); |
34 t_url->SetURL("http://www.google.com/?q={searchTerms}", 0, 0); | 35 t_url->SetURL("http://www.google.com/?q={searchTerms}", 0, 0); |
35 t_url->set_keyword(L"keyword"); | 36 t_url->set_keyword(ASCIIToUTF16("keyword")); |
36 t_url->set_short_name(L"google"); | 37 t_url->set_short_name(ASCIIToUTF16("google")); |
37 t_url->set_show_in_default_list(true); | 38 t_url->set_show_in_default_list(true); |
38 template_model_->Add(t_url); | 39 template_model_->Add(t_url); |
39 t_url = new TemplateURL(); | 40 t_url = new TemplateURL(); |
40 t_url->SetURL("http://www.google2.com/?q={searchTerms}", 0, 0); | 41 t_url->SetURL("http://www.google2.com/?q={searchTerms}", 0, 0); |
41 t_url->set_keyword(L"keyword2"); | 42 t_url->set_keyword(ASCIIToUTF16("keyword2")); |
42 t_url->set_short_name(L"google2"); | 43 t_url->set_short_name(ASCIIToUTF16("google2")); |
43 t_url->set_show_in_default_list(true); | 44 t_url->set_show_in_default_list(true); |
44 template_model_->Add(t_url); | 45 template_model_->Add(t_url); |
45 EXPECT_EQ(template_model_->GetTemplateURLs().size(), 2U); | 46 EXPECT_EQ(template_model_->GetTemplateURLs().size(), 2U); |
46 | 47 |
47 model_.reset([[SearchEngineListModel alloc] | 48 model_.reset([[SearchEngineListModel alloc] |
48 initWithModel:template_model_.get()]); | 49 initWithModel:template_model_.get()]); |
49 notification_helper_.reset([[SearchEngineListHelper alloc] init]); | 50 notification_helper_.reset([[SearchEngineListHelper alloc] init]); |
50 [[NSNotificationCenter defaultCenter] | 51 [[NSNotificationCenter defaultCenter] |
51 addObserver:notification_helper_.get() | 52 addObserver:notification_helper_.get() |
52 selector:@selector(entryChanged:) | 53 selector:@selector(entryChanged:) |
(...skipping 23 matching lines...) Expand all Loading... |
76 | 77 |
77 TEST_F(SearchEngineListModelTest, Default) { | 78 TEST_F(SearchEngineListModelTest, Default) { |
78 EXPECT_EQ([model_ defaultIndex], -1); | 79 EXPECT_EQ([model_ defaultIndex], -1); |
79 | 80 |
80 [model_ setDefaultIndex:1]; | 81 [model_ setDefaultIndex:1]; |
81 EXPECT_EQ([model_ defaultIndex], 1); | 82 EXPECT_EQ([model_ defaultIndex], 1); |
82 | 83 |
83 // Add two more URLs, neither of which are shown in the default list. | 84 // Add two more URLs, neither of which are shown in the default list. |
84 TemplateURL* t_url = new TemplateURL(); | 85 TemplateURL* t_url = new TemplateURL(); |
85 t_url->SetURL("http://www.google3.com/?q={searchTerms}", 0, 0); | 86 t_url->SetURL("http://www.google3.com/?q={searchTerms}", 0, 0); |
86 t_url->set_keyword(L"keyword3"); | 87 t_url->set_keyword(ASCIIToUTF16("keyword3")); |
87 t_url->set_short_name(L"google3 not eligible"); | 88 t_url->set_short_name(ASCIIToUTF16("google3 not eligible")); |
88 t_url->set_show_in_default_list(false); | 89 t_url->set_show_in_default_list(false); |
89 template_model_->Add(t_url); | 90 template_model_->Add(t_url); |
90 t_url = new TemplateURL(); | 91 t_url = new TemplateURL(); |
91 t_url->SetURL("http://www.google4.com/?q={searchTerms}", 0, 0); | 92 t_url->SetURL("http://www.google4.com/?q={searchTerms}", 0, 0); |
92 t_url->set_keyword(L"keyword4"); | 93 t_url->set_keyword(ASCIIToUTF16("keyword4")); |
93 t_url->set_short_name(L"google4"); | 94 t_url->set_short_name(ASCIIToUTF16("google4")); |
94 t_url->set_show_in_default_list(false); | 95 t_url->set_show_in_default_list(false); |
95 template_model_->Add(t_url); | 96 template_model_->Add(t_url); |
96 | 97 |
97 // Still should only have 2 engines and not these newly added ones. | 98 // Still should only have 2 engines and not these newly added ones. |
98 EXPECT_EQ([[model_ searchEngines] count], 2U); | 99 EXPECT_EQ([[model_ searchEngines] count], 2U); |
99 | 100 |
100 // Since keyword3 is not in the default list, the 2nd index in the default | 101 // Since keyword3 is not in the default list, the 2nd index in the default |
101 // keyword list should be keyword4. Test for http://crbug.com/21898. | 102 // keyword list should be keyword4. Test for http://crbug.com/21898. |
102 template_model_->SetDefaultSearchProvider(t_url); | 103 template_model_->SetDefaultSearchProvider(t_url); |
103 EXPECT_EQ([[model_ searchEngines] count], 3U); | 104 EXPECT_EQ([[model_ searchEngines] count], 3U); |
104 EXPECT_EQ([model_ defaultIndex], 2); | 105 EXPECT_EQ([model_ defaultIndex], 2); |
105 | 106 |
106 NSString* defaultString = [[model_ searchEngines] objectAtIndex:2]; | 107 NSString* defaultString = [[model_ searchEngines] objectAtIndex:2]; |
107 EXPECT_NSEQ(@"google4", defaultString); | 108 EXPECT_NSEQ(@"google4", defaultString); |
108 } | 109 } |
109 | 110 |
110 TEST_F(SearchEngineListModelTest, DefaultChosenFromUI) { | 111 TEST_F(SearchEngineListModelTest, DefaultChosenFromUI) { |
111 EXPECT_EQ([model_ defaultIndex], -1); | 112 EXPECT_EQ([model_ defaultIndex], -1); |
112 | 113 |
113 [model_ setDefaultIndex:1]; | 114 [model_ setDefaultIndex:1]; |
114 EXPECT_EQ([model_ defaultIndex], 1); | 115 EXPECT_EQ([model_ defaultIndex], 1); |
115 | 116 |
116 // Add two more URLs, the first one not shown in the default list. | 117 // Add two more URLs, the first one not shown in the default list. |
117 TemplateURL* t_url = new TemplateURL(); | 118 TemplateURL* t_url = new TemplateURL(); |
118 t_url->SetURL("http://www.google3.com/?q={searchTerms}", 0, 0); | 119 t_url->SetURL("http://www.google3.com/?q={searchTerms}", 0, 0); |
119 t_url->set_keyword(L"keyword3"); | 120 t_url->set_keyword(ASCIIToUTF16("keyword3")); |
120 t_url->set_short_name(L"google3 not eligible"); | 121 t_url->set_short_name(ASCIIToUTF16("google3 not eligible")); |
121 t_url->set_show_in_default_list(false); | 122 t_url->set_show_in_default_list(false); |
122 template_model_->Add(t_url); | 123 template_model_->Add(t_url); |
123 t_url = new TemplateURL(); | 124 t_url = new TemplateURL(); |
124 t_url->SetURL("http://www.google4.com/?q={searchTerms}", 0, 0); | 125 t_url->SetURL("http://www.google4.com/?q={searchTerms}", 0, 0); |
125 t_url->set_keyword(L"keyword4"); | 126 t_url->set_keyword(ASCIIToUTF16("keyword4")); |
126 t_url->set_short_name(L"google4"); | 127 t_url->set_short_name(ASCIIToUTF16("google4")); |
127 t_url->set_show_in_default_list(true); | 128 t_url->set_show_in_default_list(true); |
128 template_model_->Add(t_url); | 129 template_model_->Add(t_url); |
129 | 130 |
130 // We should have 3 engines. | 131 // We should have 3 engines. |
131 EXPECT_EQ([[model_ searchEngines] count], 3U); | 132 EXPECT_EQ([[model_ searchEngines] count], 3U); |
132 | 133 |
133 // Simulate the UI setting the default to the third entry. | 134 // Simulate the UI setting the default to the third entry. |
134 [model_ setDefaultIndex:2]; | 135 [model_ setDefaultIndex:2]; |
135 EXPECT_EQ([model_ defaultIndex], 2); | 136 EXPECT_EQ([model_ defaultIndex], 2); |
136 | 137 |
137 // The default search provider should be google4. | 138 // The default search provider should be google4. |
138 EXPECT_EQ(template_model_->GetDefaultSearchProvider(), t_url); | 139 EXPECT_EQ(template_model_->GetDefaultSearchProvider(), t_url); |
139 } | 140 } |
140 | 141 |
141 // Make sure that when the back-end model changes that we get a notification. | 142 // Make sure that when the back-end model changes that we get a notification. |
142 TEST_F(SearchEngineListModelTest, Notification) { | 143 TEST_F(SearchEngineListModelTest, Notification) { |
143 // Add one more item to force a notification. | 144 // Add one more item to force a notification. |
144 TemplateURL* t_url = new TemplateURL(); | 145 TemplateURL* t_url = new TemplateURL(); |
145 t_url->SetURL("http://www.google3.com/foo/bar", 0, 0); | 146 t_url->SetURL("http://www.google3.com/foo/bar", 0, 0); |
146 t_url->set_keyword(L"keyword3"); | 147 t_url->set_keyword(ASCIIToUTF16("keyword3")); |
147 t_url->set_short_name(L"google3"); | 148 t_url->set_short_name(ASCIIToUTF16("google3")); |
148 t_url->set_show_in_default_list(true); | 149 t_url->set_show_in_default_list(true); |
149 template_model_->Add(t_url); | 150 template_model_->Add(t_url); |
150 | 151 |
151 EXPECT_TRUE(notification_helper_.get()->sawNotification_); | 152 EXPECT_TRUE(notification_helper_.get()->sawNotification_); |
152 } | 153 } |
OLD | NEW |