| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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/scoped_nsobject.h" | |
| 6 #include "chrome/browser/prefs/session_startup_pref.h" | |
| 7 #include "chrome/browser/ui/cocoa/browser_test_helper.h" | |
| 8 #import "chrome/browser/ui/cocoa/options/custom_home_pages_model.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 #import "testing/gtest_mac.h" | |
| 11 #include "testing/platform_test.h" | |
| 12 | |
| 13 // A helper for KVO and NSNotifications. Makes a note that it's been called | |
| 14 // back. | |
| 15 @interface CustomHomePageHelper : NSObject { | |
| 16 @public | |
| 17 BOOL sawNotification_; | |
| 18 } | |
| 19 @end | |
| 20 | |
| 21 @implementation CustomHomePageHelper | |
| 22 - (void)observeValueForKeyPath:(NSString*)keyPath | |
| 23 ofObject:(id)object | |
| 24 change:(NSDictionary*)change | |
| 25 context:(void*)context { | |
| 26 sawNotification_ = YES; | |
| 27 } | |
| 28 | |
| 29 - (void)entryChanged:(NSNotification*)notify { | |
| 30 sawNotification_ = YES; | |
| 31 } | |
| 32 @end | |
| 33 | |
| 34 @interface NSObject () | |
| 35 - (void)setURL:(NSString*)url; | |
| 36 @end | |
| 37 | |
| 38 namespace { | |
| 39 | |
| 40 // Helper that creates an autoreleased entry. | |
| 41 CustomHomePageEntry* MakeEntry(NSString* url) { | |
| 42 CustomHomePageEntry* entry = [[[CustomHomePageEntry alloc] init] autorelease]; | |
| 43 [entry setURL:url]; | |
| 44 return entry; | |
| 45 } | |
| 46 | |
| 47 // Helper that casts from |id| to the Entry type and returns the URL string. | |
| 48 NSString* EntryURL(id entry) { | |
| 49 return [static_cast<CustomHomePageEntry*>(entry) URL]; | |
| 50 } | |
| 51 | |
| 52 class CustomHomePagesModelTest : public PlatformTest { | |
| 53 public: | |
| 54 CustomHomePagesModelTest() { | |
| 55 model_.reset([[CustomHomePagesModel alloc] | |
| 56 initWithProfile:helper_.profile()]); | |
| 57 } | |
| 58 ~CustomHomePagesModelTest() { } | |
| 59 | |
| 60 BrowserTestHelper helper_; | |
| 61 scoped_nsobject<CustomHomePagesModel> model_; | |
| 62 }; | |
| 63 | |
| 64 TEST_F(CustomHomePagesModelTest, Init) { | |
| 65 scoped_nsobject<CustomHomePagesModel> model( | |
| 66 [[CustomHomePagesModel alloc] initWithProfile:helper_.profile()]); | |
| 67 } | |
| 68 | |
| 69 TEST_F(CustomHomePagesModelTest, GetSetURLs) { | |
| 70 // Basic test. | |
| 71 std::vector<GURL> urls; | |
| 72 urls.push_back(GURL("http://www.google.com")); | |
| 73 [model_ setURLs:urls]; | |
| 74 std::vector<GURL> received_urls = [model_.get() URLs]; | |
| 75 EXPECT_EQ(received_urls.size(), 1U); | |
| 76 EXPECT_TRUE(urls[0] == received_urls[0]); | |
| 77 | |
| 78 // Set an empty list, make sure we get back an empty list. | |
| 79 std::vector<GURL> empty; | |
| 80 [model_ setURLs:empty]; | |
| 81 received_urls = [model_.get() URLs]; | |
| 82 EXPECT_EQ(received_urls.size(), 0U); | |
| 83 | |
| 84 // Give it a list with not well-formed URLs and make sure we get back. | |
| 85 // only the good ones. | |
| 86 std::vector<GURL> poorly_formed; | |
| 87 poorly_formed.push_back(GURL("http://www.google.com")); // good | |
| 88 poorly_formed.push_back(GURL("www.google.com")); // bad | |
| 89 poorly_formed.push_back(GURL("www.yahoo.")); // bad | |
| 90 poorly_formed.push_back(GURL("http://www.yahoo.com")); // good | |
| 91 [model_ setURLs:poorly_formed]; | |
| 92 received_urls = [model_.get() URLs]; | |
| 93 EXPECT_EQ(received_urls.size(), 2U); | |
| 94 } | |
| 95 | |
| 96 // Test that we get a KVO notification when called setURLs. | |
| 97 TEST_F(CustomHomePagesModelTest, KVOObserveWhenListChanges) { | |
| 98 scoped_nsobject<CustomHomePageHelper> kvo_helper( | |
| 99 [[CustomHomePageHelper alloc] init]); | |
| 100 [model_ addObserver:kvo_helper | |
| 101 forKeyPath:@"customHomePages" | |
| 102 options:0L | |
| 103 context:NULL]; | |
| 104 EXPECT_FALSE(kvo_helper.get()->sawNotification_); | |
| 105 | |
| 106 std::vector<GURL> urls; | |
| 107 urls.push_back(GURL("http://www.google.com")); | |
| 108 [model_ setURLs:urls]; // Should send kvo change notification. | |
| 109 EXPECT_TRUE(kvo_helper.get()->sawNotification_); | |
| 110 | |
| 111 [model_ removeObserver:kvo_helper forKeyPath:@"customHomePages"]; | |
| 112 } | |
| 113 | |
| 114 // Test the KVO "to-many" bindings for |customHomePages| and the KVO | |
| 115 // notifiation when items are added to and removed from the list. | |
| 116 TEST_F(CustomHomePagesModelTest, KVO) { | |
| 117 EXPECT_EQ([model_ countOfCustomHomePages], 0U); | |
| 118 | |
| 119 scoped_nsobject<CustomHomePageHelper> kvo_helper( | |
| 120 [[CustomHomePageHelper alloc] init]); | |
| 121 [model_ addObserver:kvo_helper | |
| 122 forKeyPath:@"customHomePages" | |
| 123 options:0L | |
| 124 context:NULL]; | |
| 125 EXPECT_FALSE(kvo_helper.get()->sawNotification_); | |
| 126 | |
| 127 // Cheat and insert NSString objects into the array. As long as we don't | |
| 128 // call -URLs, we'll be ok. | |
| 129 [model_ insertObject:MakeEntry(@"www.google.com") inCustomHomePagesAtIndex:0]; | |
| 130 EXPECT_TRUE(kvo_helper.get()->sawNotification_); | |
| 131 [model_ insertObject:MakeEntry(@"www.yahoo.com") inCustomHomePagesAtIndex:1]; | |
| 132 [model_ insertObject:MakeEntry(@"dev.chromium.org") | |
| 133 inCustomHomePagesAtIndex:2]; | |
| 134 EXPECT_EQ([model_ countOfCustomHomePages], 3U); | |
| 135 | |
| 136 EXPECT_NSEQ(@"http://www.yahoo.com/", | |
| 137 EntryURL([model_ objectInCustomHomePagesAtIndex:1])); | |
| 138 | |
| 139 kvo_helper.get()->sawNotification_ = NO; | |
| 140 [model_ removeObjectFromCustomHomePagesAtIndex:1]; | |
| 141 EXPECT_TRUE(kvo_helper.get()->sawNotification_); | |
| 142 EXPECT_EQ([model_ countOfCustomHomePages], 2U); | |
| 143 EXPECT_NSEQ(@"http://dev.chromium.org/", | |
| 144 EntryURL([model_ objectInCustomHomePagesAtIndex:1])); | |
| 145 EXPECT_NSEQ(@"http://www.google.com/", | |
| 146 EntryURL([model_ objectInCustomHomePagesAtIndex:0])); | |
| 147 | |
| 148 [model_ removeObserver:kvo_helper forKeyPath:@"customHomePages"]; | |
| 149 } | |
| 150 | |
| 151 // Test that when individual items are changed that they broadcast a message. | |
| 152 TEST_F(CustomHomePagesModelTest, ModelChangedNotification) { | |
| 153 scoped_nsobject<CustomHomePageHelper> kvo_helper( | |
| 154 [[CustomHomePageHelper alloc] init]); | |
| 155 [[NSNotificationCenter defaultCenter] | |
| 156 addObserver:kvo_helper | |
| 157 selector:@selector(entryChanged:) | |
| 158 name:kHomepageEntryChangedNotification | |
| 159 object:nil]; | |
| 160 | |
| 161 std::vector<GURL> urls; | |
| 162 urls.push_back(GURL("http://www.google.com")); | |
| 163 [model_ setURLs:urls]; | |
| 164 NSObject* entry = [model_ objectInCustomHomePagesAtIndex:0]; | |
| 165 [entry setURL:@"http://www.foo.bar"]; | |
| 166 EXPECT_TRUE(kvo_helper.get()->sawNotification_); | |
| 167 [[NSNotificationCenter defaultCenter] removeObserver:kvo_helper]; | |
| 168 } | |
| 169 | |
| 170 TEST_F(CustomHomePagesModelTest, ReloadURLs) { | |
| 171 scoped_nsobject<CustomHomePageHelper> kvo_helper( | |
| 172 [[CustomHomePageHelper alloc] init]); | |
| 173 [model_ addObserver:kvo_helper | |
| 174 forKeyPath:@"customHomePages" | |
| 175 options:0L | |
| 176 context:NULL]; | |
| 177 EXPECT_FALSE(kvo_helper.get()->sawNotification_); | |
| 178 EXPECT_EQ([model_ countOfCustomHomePages], 0U); | |
| 179 | |
| 180 std::vector<GURL> urls; | |
| 181 urls.push_back(GURL("http://www.google.com")); | |
| 182 SessionStartupPref pref; | |
| 183 pref.urls = urls; | |
| 184 SessionStartupPref::SetStartupPref(helper_.profile(), pref); | |
| 185 | |
| 186 [model_ reloadURLs]; | |
| 187 | |
| 188 EXPECT_TRUE(kvo_helper.get()->sawNotification_); | |
| 189 EXPECT_EQ([model_ countOfCustomHomePages], 1U); | |
| 190 EXPECT_NSEQ(@"http://www.google.com/", | |
| 191 EntryURL([model_ objectInCustomHomePagesAtIndex:0])); | |
| 192 | |
| 193 [model_ removeObserver:kvo_helper.get() forKeyPath:@"customHomePages"]; | |
| 194 } | |
| 195 | |
| 196 } // namespace | |
| OLD | NEW |