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