OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 "base/mac/scoped_nsautorelease_pool.h" | |
6 #include "base/scoped_nsobject.h" | |
7 #include "chrome/browser/search_engines/template_url.h" | |
8 #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/cocoa_test_helper.h" | |
11 #import "chrome/browser/ui/cocoa/keyword_editor_cocoa_controller.h" | |
12 #include "chrome/test/testing_profile.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 #include "testing/platform_test.h" | |
15 | |
16 @interface FakeKeywordEditorController : KeywordEditorCocoaController { | |
17 @public | |
18 BOOL modelChanged_; | |
19 } | |
20 - (void)modelChanged; | |
21 - (BOOL)hasModelChanged; | |
22 - (KeywordEditorModelObserver*)observer; | |
23 @end | |
24 | |
25 @implementation FakeKeywordEditorController | |
26 | |
27 - (void)modelChanged { | |
28 modelChanged_ = YES; | |
29 } | |
30 | |
31 - (BOOL)hasModelChanged { | |
32 return modelChanged_; | |
33 } | |
34 | |
35 - (KeywordEditorModelObserver*)observer { | |
36 return observer_.get(); | |
37 } | |
38 | |
39 - (NSTableView*)tableView { | |
40 return tableView_; | |
41 } | |
42 | |
43 @end | |
44 | |
45 // TODO(rsesek): Figure out a good way to test this class (crbug.com/21640). | |
46 | |
47 namespace { | |
48 | |
49 class KeywordEditorCocoaControllerTest : public CocoaTest { | |
50 public: | |
51 virtual void SetUp() { | |
52 CocoaTest::SetUp(); | |
53 TestingProfile* profile = | |
54 static_cast<TestingProfile*>(browser_helper_.profile()); | |
55 profile->CreateTemplateURLModel(); | |
56 | |
57 controller_ = | |
58 [[FakeKeywordEditorController alloc] initWithProfile:profile]; | |
59 } | |
60 | |
61 virtual void TearDown() { | |
62 // Force the window to load so we hit |-awakeFromNib| to register as the | |
63 // window's delegate so that the controller can clean itself up in | |
64 // |-windowWillClose:|. | |
65 ASSERT_TRUE([controller_ window]); | |
66 | |
67 [controller_ close]; | |
68 CocoaTest::TearDown(); | |
69 } | |
70 | |
71 // Helper to count the keyword editors. | |
72 NSUInteger CountKeywordEditors() { | |
73 base::mac::ScopedNSAutoreleasePool pool; | |
74 NSUInteger count = 0; | |
75 for (NSWindow* window in [NSApp windows]) { | |
76 id controller = [window windowController]; | |
77 if ([controller isKindOfClass:[KeywordEditorCocoaController class]]) { | |
78 ++count; | |
79 } | |
80 } | |
81 return count; | |
82 } | |
83 | |
84 BrowserTestHelper browser_helper_; | |
85 FakeKeywordEditorController* controller_; | |
86 }; | |
87 | |
88 TEST_F(KeywordEditorCocoaControllerTest, TestModelChanged) { | |
89 EXPECT_FALSE([controller_ hasModelChanged]); | |
90 KeywordEditorModelObserver* observer = [controller_ observer]; | |
91 observer->OnTemplateURLModelChanged(); | |
92 EXPECT_TRUE([controller_ hasModelChanged]); | |
93 } | |
94 | |
95 // Test that +showKeywordEditor brings up the existing editor and | |
96 // creates one if needed. | |
97 TEST_F(KeywordEditorCocoaControllerTest, ShowKeywordEditor) { | |
98 // No outstanding editors. | |
99 Profile* profile(browser_helper_.profile()); | |
100 KeywordEditorCocoaController* sharedInstance = | |
101 [KeywordEditorCocoaController sharedInstanceForProfile:profile]; | |
102 EXPECT_TRUE(nil == sharedInstance); | |
103 EXPECT_EQ(CountKeywordEditors(), 0U); | |
104 | |
105 const NSUInteger initial_window_count([[NSApp windows] count]); | |
106 | |
107 // The window unwinds using -autorelease, so we need to introduce an | |
108 // autorelease pool to really test whether it went away or not. | |
109 { | |
110 base::mac::ScopedNSAutoreleasePool pool; | |
111 | |
112 // +showKeywordEditor: creates a new controller. | |
113 [KeywordEditorCocoaController showKeywordEditor:profile]; | |
114 sharedInstance = | |
115 [KeywordEditorCocoaController sharedInstanceForProfile:profile]; | |
116 EXPECT_TRUE(sharedInstance); | |
117 EXPECT_EQ(CountKeywordEditors(), 1U); | |
118 | |
119 // Another call doesn't create another controller. | |
120 [KeywordEditorCocoaController showKeywordEditor:profile]; | |
121 EXPECT_TRUE(sharedInstance == | |
122 [KeywordEditorCocoaController sharedInstanceForProfile:profile]); | |
123 EXPECT_EQ(CountKeywordEditors(), 1U); | |
124 | |
125 [sharedInstance close]; | |
126 } | |
127 | |
128 // No outstanding editors. | |
129 sharedInstance = | |
130 [KeywordEditorCocoaController sharedInstanceForProfile:profile]; | |
131 EXPECT_TRUE(nil == sharedInstance); | |
132 EXPECT_EQ(CountKeywordEditors(), 0U); | |
133 | |
134 // Windows we created should be gone. | |
135 EXPECT_EQ([[NSApp windows] count], initial_window_count); | |
136 | |
137 // Get a new editor, should be different from the previous one. | |
138 [KeywordEditorCocoaController showKeywordEditor:profile]; | |
139 KeywordEditorCocoaController* newSharedInstance = | |
140 [KeywordEditorCocoaController sharedInstanceForProfile:profile]; | |
141 EXPECT_TRUE(sharedInstance != newSharedInstance); | |
142 EXPECT_EQ(CountKeywordEditors(), 1U); | |
143 [newSharedInstance close]; | |
144 } | |
145 | |
146 TEST_F(KeywordEditorCocoaControllerTest, IndexInModelForRowMixed) { | |
147 [controller_ window]; // Force |-awakeFromNib|. | |
148 TemplateURLModel* template_model = [controller_ controller]->url_model(); | |
149 | |
150 // Add a default engine. | |
151 TemplateURL* t_url = new TemplateURL(); | |
152 t_url->SetURL("http://test1/{searchTerms}", 0, 0); | |
153 t_url->set_keyword(L"test1"); | |
154 t_url->set_short_name(L"Test1"); | |
155 t_url->set_show_in_default_list(true); | |
156 template_model->Add(t_url); | |
157 | |
158 // Add a non-default engine. | |
159 t_url = new TemplateURL(); | |
160 t_url->SetURL("http://test2/{searchTerms}", 0, 0); | |
161 t_url->set_keyword(L"test2"); | |
162 t_url->set_short_name(L"Test2"); | |
163 t_url->set_show_in_default_list(false); | |
164 template_model->Add(t_url); | |
165 | |
166 // Two headers with a single row underneath each. | |
167 NSTableView* table = [controller_ tableView]; | |
168 [table reloadData]; | |
169 ASSERT_EQ(4, [[controller_ tableView] numberOfRows]); | |
170 | |
171 // Index 0 is the group header, index 1 should be the first engine. | |
172 ASSERT_EQ(0, [controller_ indexInModelForRow:1]); | |
173 | |
174 // Index 2 should be the group header, so index 3 should be the non-default | |
175 // engine. | |
176 ASSERT_EQ(1, [controller_ indexInModelForRow:3]); | |
177 | |
178 ASSERT_TRUE([controller_ tableView:table isGroupRow:0]); | |
179 ASSERT_FALSE([controller_ tableView:table isGroupRow:1]); | |
180 ASSERT_TRUE([controller_ tableView:table isGroupRow:2]); | |
181 ASSERT_FALSE([controller_ tableView:table isGroupRow:3]); | |
182 | |
183 ASSERT_FALSE([controller_ tableView:table shouldSelectRow:0]); | |
184 ASSERT_TRUE([controller_ tableView:table shouldSelectRow:1]); | |
185 ASSERT_FALSE([controller_ tableView:table shouldSelectRow:2]); | |
186 ASSERT_TRUE([controller_ tableView:table shouldSelectRow:3]); | |
187 } | |
188 | |
189 TEST_F(KeywordEditorCocoaControllerTest, IndexInModelForDefault) { | |
190 [controller_ window]; // Force |-awakeFromNib|. | |
191 TemplateURLModel* template_model = [controller_ controller]->url_model(); | |
192 | |
193 // Add 2 default engines. | |
194 TemplateURL* t_url = new TemplateURL(); | |
195 t_url->SetURL("http://test1/{searchTerms}", 0, 0); | |
196 t_url->set_keyword(L"test1"); | |
197 t_url->set_short_name(L"Test1"); | |
198 t_url->set_show_in_default_list(true); | |
199 template_model->Add(t_url); | |
200 | |
201 t_url = new TemplateURL(); | |
202 t_url->SetURL("http://test2/{searchTerms}", 0, 0); | |
203 t_url->set_keyword(L"test2"); | |
204 t_url->set_short_name(L"Test2"); | |
205 t_url->set_show_in_default_list(true); | |
206 template_model->Add(t_url); | |
207 | |
208 // One header and two rows. | |
209 NSTableView* table = [controller_ tableView]; | |
210 [table reloadData]; | |
211 ASSERT_EQ(3, [[controller_ tableView] numberOfRows]); | |
212 | |
213 // Index 0 is the group header, index 1 should be the first engine. | |
214 ASSERT_EQ(0, [controller_ indexInModelForRow:1]); | |
215 ASSERT_EQ(1, [controller_ indexInModelForRow:2]); | |
216 | |
217 ASSERT_TRUE([controller_ tableView:table isGroupRow:0]); | |
218 ASSERT_FALSE([controller_ tableView:table isGroupRow:1]); | |
219 ASSERT_FALSE([controller_ tableView:table isGroupRow:2]); | |
220 | |
221 ASSERT_FALSE([controller_ tableView:table shouldSelectRow:0]); | |
222 ASSERT_TRUE([controller_ tableView:table shouldSelectRow:1]); | |
223 ASSERT_TRUE([controller_ tableView:table shouldSelectRow:2]); | |
224 } | |
225 | |
226 } // namespace | |
OLD | NEW |