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