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