OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #import "chrome/browser/ui/cocoa/options/preferences_window_controller.h" | |
6 | |
7 #import <Cocoa/Cocoa.h> | |
8 | |
9 #import "base/scoped_nsobject.h" | |
10 #include "chrome/browser/prefs/browser_prefs.h" | |
11 #include "chrome/browser/ui/cocoa/browser_test_helper.h" | |
12 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" | |
13 #import "chrome/browser/ui/cocoa/options/custom_home_pages_model.h" | |
14 #include "chrome/browser/ui/options/options_window.h" | |
15 #include "chrome/common/pref_names.h" | |
16 #include "chrome/test/testing_browser_process.h" | |
17 #include "chrome/test/testing_pref_service.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 #import "testing/gtest_mac.h" | |
20 #include "testing/platform_test.h" | |
21 | |
22 // Helper Objective-C object that sets a BOOL when we get a particular | |
23 // callback from the prefs window. | |
24 @interface PrefsClosedObserver : NSObject { | |
25 @public | |
26 BOOL gotNotification_; | |
27 } | |
28 - (void)prefsWindowClosed:(NSNotification*)notify; | |
29 @end | |
30 | |
31 @implementation PrefsClosedObserver | |
32 - (void)prefsWindowClosed:(NSNotification*)notify { | |
33 gotNotification_ = YES; | |
34 } | |
35 @end | |
36 | |
37 namespace { | |
38 | |
39 class PrefsControllerTest : public CocoaTest { | |
40 public: | |
41 virtual void SetUp() { | |
42 CocoaTest::SetUp(); | |
43 browser::RegisterLocalState(&local_state_); | |
44 local_state_.RegisterBooleanPref(prefs::kMetricsReportingEnabled, false); | |
45 browser_process_.get()->SetPrefService(&local_state_); | |
46 | |
47 pref_controller_ = [[PreferencesWindowController alloc] | |
48 initWithProfile:browser_helper_.profile() | |
49 initialPage:OPTIONS_PAGE_DEFAULT]; | |
50 EXPECT_TRUE(pref_controller_); | |
51 } | |
52 | |
53 virtual void TearDown() { | |
54 [pref_controller_ close]; | |
55 CocoaTest::TearDown(); | |
56 } | |
57 | |
58 ScopedTestingBrowserProcess browser_process_; | |
59 TestingPrefService local_state_; | |
60 BrowserTestHelper browser_helper_; | |
61 PreferencesWindowController* pref_controller_; | |
62 }; | |
63 | |
64 // Test showing the preferences window and making sure it's visible, then | |
65 // making sure we get the notification when it's closed. | |
66 TEST_F(PrefsControllerTest, ShowAndClose) { | |
67 [pref_controller_ showPreferences:nil]; | |
68 EXPECT_TRUE([[pref_controller_ window] isVisible]); | |
69 | |
70 scoped_nsobject<PrefsClosedObserver> observer( | |
71 [[PrefsClosedObserver alloc] init]); | |
72 NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter]; | |
73 [defaultCenter addObserver:observer.get() | |
74 selector:@selector(prefsWindowClosed:) | |
75 name:NSWindowWillCloseNotification | |
76 object:[pref_controller_ window]]; | |
77 [[pref_controller_ window] performClose:observer]; | |
78 EXPECT_TRUE(observer.get()->gotNotification_); | |
79 [defaultCenter removeObserver:observer.get()]; | |
80 | |
81 // Prevent pref_controller_ from being closed again in TearDown() | |
82 pref_controller_ = nil; | |
83 } | |
84 | |
85 TEST_F(PrefsControllerTest, ValidateCustomHomePagesTable) { | |
86 // First, insert two valid URLs into the CustomHomePagesModel. | |
87 GURL url1("http://www.google.com/"); | |
88 GURL url2("http://maps.google.com/"); | |
89 std::vector<GURL> urls; | |
90 urls.push_back(url1); | |
91 urls.push_back(url2); | |
92 [[pref_controller_ customPagesSource] setURLs:urls]; | |
93 EXPECT_EQ(2U, [[pref_controller_ customPagesSource] countOfCustomHomePages]); | |
94 | |
95 // Now insert a bad (empty) URL into the model. | |
96 [[pref_controller_ customPagesSource] setURLStringEmptyAt:1]; | |
97 | |
98 // Send a notification to simulate the end of editing on a cell in the table | |
99 // which should trigger validation. | |
100 [pref_controller_ controlTextDidEndEditing:[NSNotification | |
101 notificationWithName:NSControlTextDidEndEditingNotification | |
102 object:nil]]; | |
103 EXPECT_EQ(1U, [[pref_controller_ customPagesSource] countOfCustomHomePages]); | |
104 } | |
105 | |
106 TEST_F(PrefsControllerTest, NormalizePage) { | |
107 EXPECT_EQ(OPTIONS_PAGE_GENERAL, | |
108 [pref_controller_ normalizePage:OPTIONS_PAGE_GENERAL]); | |
109 EXPECT_EQ(OPTIONS_PAGE_CONTENT, | |
110 [pref_controller_ normalizePage:OPTIONS_PAGE_CONTENT]); | |
111 EXPECT_EQ(OPTIONS_PAGE_ADVANCED, | |
112 [pref_controller_ normalizePage:OPTIONS_PAGE_ADVANCED]); | |
113 | |
114 [pref_controller_ lastSelectedPage]->SetValue(OPTIONS_PAGE_ADVANCED); | |
115 EXPECT_EQ(OPTIONS_PAGE_ADVANCED, | |
116 [pref_controller_ normalizePage:OPTIONS_PAGE_DEFAULT]); | |
117 | |
118 [pref_controller_ lastSelectedPage]->SetValue(OPTIONS_PAGE_DEFAULT); | |
119 EXPECT_EQ(OPTIONS_PAGE_GENERAL, | |
120 [pref_controller_ normalizePage:OPTIONS_PAGE_DEFAULT]); | |
121 } | |
122 | |
123 TEST_F(PrefsControllerTest, GetToolbarItemForPage) { | |
124 // Trigger awakeFromNib. | |
125 [pref_controller_ window]; | |
126 | |
127 NSArray* toolbarItems = [[pref_controller_ toolbar] items]; | |
128 EXPECT_EQ([toolbarItems objectAtIndex:0], | |
129 [pref_controller_ getToolbarItemForPage:OPTIONS_PAGE_GENERAL]); | |
130 EXPECT_EQ([toolbarItems objectAtIndex:1], | |
131 [pref_controller_ getToolbarItemForPage:OPTIONS_PAGE_CONTENT]); | |
132 EXPECT_EQ([toolbarItems objectAtIndex:2], | |
133 [pref_controller_ getToolbarItemForPage:OPTIONS_PAGE_ADVANCED]); | |
134 | |
135 [pref_controller_ lastSelectedPage]->SetValue(OPTIONS_PAGE_ADVANCED); | |
136 EXPECT_EQ([toolbarItems objectAtIndex:2], | |
137 [pref_controller_ getToolbarItemForPage:OPTIONS_PAGE_DEFAULT]); | |
138 | |
139 // Out-of-range argument. | |
140 EXPECT_EQ([toolbarItems objectAtIndex:0], | |
141 [pref_controller_ getToolbarItemForPage:OPTIONS_PAGE_COUNT]); | |
142 } | |
143 | |
144 TEST_F(PrefsControllerTest, GetPageForToolbarItem) { | |
145 scoped_nsobject<NSToolbarItem> toolbarItem( | |
146 [[NSToolbarItem alloc] initWithItemIdentifier:@""]); | |
147 [toolbarItem setTag:0]; | |
148 EXPECT_EQ(OPTIONS_PAGE_GENERAL, | |
149 [pref_controller_ getPageForToolbarItem:toolbarItem]); | |
150 [toolbarItem setTag:1]; | |
151 EXPECT_EQ(OPTIONS_PAGE_CONTENT, | |
152 [pref_controller_ getPageForToolbarItem:toolbarItem]); | |
153 [toolbarItem setTag:2]; | |
154 EXPECT_EQ(OPTIONS_PAGE_ADVANCED, | |
155 [pref_controller_ getPageForToolbarItem:toolbarItem]); | |
156 | |
157 // Out-of-range argument. | |
158 [toolbarItem setTag:3]; | |
159 EXPECT_EQ(OPTIONS_PAGE_GENERAL, | |
160 [pref_controller_ getPageForToolbarItem:toolbarItem]); | |
161 } | |
162 | |
163 TEST_F(PrefsControllerTest, GetPrefsViewForPage) { | |
164 // Trigger awakeFromNib. | |
165 [pref_controller_ window]; | |
166 | |
167 EXPECT_EQ([pref_controller_ basicsView], | |
168 [pref_controller_ getPrefsViewForPage:OPTIONS_PAGE_GENERAL]); | |
169 EXPECT_EQ([pref_controller_ personalStuffView], | |
170 [pref_controller_ getPrefsViewForPage:OPTIONS_PAGE_CONTENT]); | |
171 EXPECT_EQ([pref_controller_ underTheHoodView], | |
172 [pref_controller_ getPrefsViewForPage:OPTIONS_PAGE_ADVANCED]); | |
173 | |
174 [pref_controller_ lastSelectedPage]->SetValue(OPTIONS_PAGE_ADVANCED); | |
175 EXPECT_EQ([pref_controller_ underTheHoodView], | |
176 [pref_controller_ getPrefsViewForPage:OPTIONS_PAGE_DEFAULT]); | |
177 } | |
178 | |
179 TEST_F(PrefsControllerTest, SwitchToPage) { | |
180 // Trigger awakeFromNib. | |
181 NSWindow* window = [pref_controller_ window]; | |
182 | |
183 NSView* contentView = [window contentView]; | |
184 NSView* basicsView = [pref_controller_ basicsView]; | |
185 NSView* personalStuffView = [pref_controller_ personalStuffView]; | |
186 NSView* underTheHoodView = [pref_controller_ underTheHoodView]; | |
187 NSToolbar* toolbar = [pref_controller_ toolbar]; | |
188 NSToolbarItem* basicsToolbarItem = | |
189 [pref_controller_ getToolbarItemForPage:OPTIONS_PAGE_GENERAL]; | |
190 NSToolbarItem* personalStuffToolbarItem = | |
191 [pref_controller_ getToolbarItemForPage:OPTIONS_PAGE_CONTENT]; | |
192 NSToolbarItem* underTheHoodToolbarItem = | |
193 [pref_controller_ getToolbarItemForPage:OPTIONS_PAGE_ADVANCED]; | |
194 NSString* basicsIdentifier = [basicsToolbarItem itemIdentifier]; | |
195 NSString* personalStuffIdentifier = [personalStuffToolbarItem itemIdentifier]; | |
196 NSString* underTheHoodIdentifier = [underTheHoodToolbarItem itemIdentifier]; | |
197 IntegerPrefMember* lastSelectedPage = [pref_controller_ lastSelectedPage]; | |
198 | |
199 // Test without animation. | |
200 | |
201 [pref_controller_ switchToPage:OPTIONS_PAGE_GENERAL animate:NO]; | |
202 EXPECT_TRUE([basicsView isDescendantOf:contentView]); | |
203 EXPECT_FALSE([personalStuffView isDescendantOf:contentView]); | |
204 EXPECT_FALSE([underTheHoodView isDescendantOf:contentView]); | |
205 EXPECT_NSEQ(basicsIdentifier, [toolbar selectedItemIdentifier]); | |
206 EXPECT_EQ(OPTIONS_PAGE_GENERAL, lastSelectedPage->GetValue()); | |
207 EXPECT_NSEQ([basicsToolbarItem label], [window title]); | |
208 | |
209 [pref_controller_ switchToPage:OPTIONS_PAGE_CONTENT animate:NO]; | |
210 EXPECT_FALSE([basicsView isDescendantOf:contentView]); | |
211 EXPECT_TRUE([personalStuffView isDescendantOf:contentView]); | |
212 EXPECT_FALSE([underTheHoodView isDescendantOf:contentView]); | |
213 EXPECT_NSEQ([toolbar selectedItemIdentifier], personalStuffIdentifier); | |
214 EXPECT_EQ(OPTIONS_PAGE_CONTENT, lastSelectedPage->GetValue()); | |
215 EXPECT_NSEQ([personalStuffToolbarItem label], [window title]); | |
216 | |
217 [pref_controller_ switchToPage:OPTIONS_PAGE_ADVANCED animate:NO]; | |
218 EXPECT_FALSE([basicsView isDescendantOf:contentView]); | |
219 EXPECT_FALSE([personalStuffView isDescendantOf:contentView]); | |
220 EXPECT_TRUE([underTheHoodView isDescendantOf:contentView]); | |
221 EXPECT_NSEQ([toolbar selectedItemIdentifier], underTheHoodIdentifier); | |
222 EXPECT_EQ(OPTIONS_PAGE_ADVANCED, lastSelectedPage->GetValue()); | |
223 EXPECT_NSEQ([underTheHoodToolbarItem label], [window title]); | |
224 | |
225 // Test OPTIONS_PAGE_DEFAULT. | |
226 | |
227 lastSelectedPage->SetValue(OPTIONS_PAGE_CONTENT); | |
228 [pref_controller_ switchToPage:OPTIONS_PAGE_DEFAULT animate:NO]; | |
229 EXPECT_FALSE([basicsView isDescendantOf:contentView]); | |
230 EXPECT_TRUE([personalStuffView isDescendantOf:contentView]); | |
231 EXPECT_FALSE([underTheHoodView isDescendantOf:contentView]); | |
232 EXPECT_NSEQ(personalStuffIdentifier, [toolbar selectedItemIdentifier]); | |
233 EXPECT_EQ(OPTIONS_PAGE_CONTENT, lastSelectedPage->GetValue()); | |
234 EXPECT_NSEQ([personalStuffToolbarItem label], [window title]); | |
235 | |
236 // TODO(akalin): Figure out how to test animation; we'll need everything | |
237 // to stick around until the animation finishes. | |
238 } | |
239 | |
240 // TODO(akalin): Figure out how to test sync controls. | |
241 // TODO(akalin): Figure out how to test that sync controls are not shown | |
242 // when there isn't a sync service. | |
243 | |
244 } // namespace | |
OLD | NEW |