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 #import <Cocoa/Cocoa.h> | |
6 | |
7 #import "base/scoped_nsobject.h" | |
8 #include "base/scoped_ptr.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "chrome/browser/browsing_data_remover.h" | |
11 #include "chrome/browser/cookies_tree_model.h" | |
12 #include "chrome/browser/mock_browsing_data_appcache_helper.h" | |
13 #include "chrome/browser/mock_browsing_data_database_helper.h" | |
14 #include "chrome/browser/mock_browsing_data_indexed_db_helper.h" | |
15 #include "chrome/browser/mock_browsing_data_local_storage_helper.h" | |
16 #include "chrome/browser/profiles/profile.h" | |
17 #include "chrome/browser/ui/cocoa/browser_test_helper.h" | |
18 #include "chrome/browser/ui/cocoa/clear_browsing_data_controller.h" | |
19 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" | |
20 #import "chrome/browser/ui/cocoa/options/cookies_window_controller.h" | |
21 #include "chrome/common/net/url_request_context_getter.h" | |
22 #include "chrome/test/testing_profile.h" | |
23 #include "googleurl/src/gurl.h" | |
24 #include "grit/generated_resources.h" | |
25 #include "net/url_request/url_request_context.h" | |
26 #include "testing/gtest/include/gtest/gtest.h" | |
27 #import "testing/gtest_mac.h" | |
28 #include "testing/platform_test.h" | |
29 #import "third_party/ocmock/OCMock/OCMock.h" | |
30 #include "ui/base/l10n/l10n_util_mac.h" | |
31 #include "ui/base/models/tree_model.h" | |
32 | |
33 // Used to test FindCocoaNode. This only sets the title and node, without | |
34 // initializing any other members. | |
35 @interface FakeCocoaCookieTreeNode : CocoaCookieTreeNode { | |
36 ui::TreeModelNode* testNode_; | |
37 } | |
38 - (id)initWithTreeNode:(ui::TreeModelNode*)node; | |
39 @end | |
40 @implementation FakeCocoaCookieTreeNode | |
41 - (id)initWithTreeNode:(ui::TreeModelNode*)node { | |
42 if ((self = [super init])) { | |
43 testNode_ = node; | |
44 children_.reset([[NSMutableArray alloc] init]); | |
45 } | |
46 return self; | |
47 } | |
48 - (ui::TreeModelNode*)treeNode { | |
49 return testNode_; | |
50 } | |
51 @end | |
52 | |
53 namespace { | |
54 | |
55 class CookiesWindowControllerTest : public CocoaTest { | |
56 public: | |
57 | |
58 virtual void SetUp() { | |
59 CocoaTest::SetUp(); | |
60 TestingProfile* profile = browser_helper_.profile(); | |
61 profile->CreateRequestContext(); | |
62 database_helper_ = new MockBrowsingDataDatabaseHelper(profile); | |
63 local_storage_helper_ = new MockBrowsingDataLocalStorageHelper(profile); | |
64 appcache_helper_ = new MockBrowsingDataAppCacheHelper(profile); | |
65 indexed_db_helper_ = new MockBrowsingDataIndexedDBHelper(profile); | |
66 controller_.reset( | |
67 [[CookiesWindowController alloc] initWithProfile:profile | |
68 databaseHelper:database_helper_ | |
69 storageHelper:local_storage_helper_ | |
70 appcacheHelper:appcache_helper_ | |
71 indexedDBHelper:indexed_db_helper_] | |
72 ); | |
73 } | |
74 | |
75 virtual void TearDown() { | |
76 CocoaTest::TearDown(); | |
77 } | |
78 | |
79 CocoaCookieTreeNode* CocoaNodeFromTreeNode(ui::TreeModelNode* node) { | |
80 return [controller_ modelObserver]->CocoaNodeFromTreeNode(node); | |
81 } | |
82 | |
83 CocoaCookieTreeNode* FindCocoaNode(ui::TreeModelNode* node, | |
84 CocoaCookieTreeNode* start) { | |
85 return [controller_ modelObserver]->FindCocoaNode(node, start); | |
86 } | |
87 | |
88 protected: | |
89 BrowserTestHelper browser_helper_; | |
90 scoped_nsobject<CookiesWindowController> controller_; | |
91 MockBrowsingDataDatabaseHelper* database_helper_; | |
92 MockBrowsingDataLocalStorageHelper* local_storage_helper_; | |
93 MockBrowsingDataAppCacheHelper* appcache_helper_; | |
94 MockBrowsingDataIndexedDBHelper* indexed_db_helper_; | |
95 }; | |
96 | |
97 TEST_F(CookiesWindowControllerTest, Construction) { | |
98 std::vector<SkBitmap> skia_icons; | |
99 [controller_ treeModel]->GetIcons(&skia_icons); | |
100 | |
101 EXPECT_EQ([[controller_ icons] count], skia_icons.size() + 1U); | |
102 } | |
103 | |
104 TEST_F(CookiesWindowControllerTest, FindCocoaNodeRoot) { | |
105 scoped_ptr< ui::TreeNodeWithValue<int> > search( | |
106 new ui::TreeNodeWithValue<int>(42)); | |
107 scoped_nsobject<FakeCocoaCookieTreeNode> node( | |
108 [[FakeCocoaCookieTreeNode alloc] initWithTreeNode:search.get()]); | |
109 EXPECT_EQ(node.get(), FindCocoaNode(search.get(), node.get())); | |
110 } | |
111 | |
112 TEST_F(CookiesWindowControllerTest, FindCocoaNodeImmediateChild) { | |
113 scoped_ptr< ui::TreeNodeWithValue<int> > parent( | |
114 new ui::TreeNodeWithValue<int>(100)); | |
115 scoped_ptr< ui::TreeNodeWithValue<int> > child1( | |
116 new ui::TreeNodeWithValue<int>(10)); | |
117 scoped_ptr< ui::TreeNodeWithValue<int> > child2( | |
118 new ui::TreeNodeWithValue<int>(20)); | |
119 scoped_nsobject<FakeCocoaCookieTreeNode> cocoaParent( | |
120 [[FakeCocoaCookieTreeNode alloc] initWithTreeNode:parent.get()]); | |
121 scoped_nsobject<FakeCocoaCookieTreeNode> cocoaChild1( | |
122 [[FakeCocoaCookieTreeNode alloc] initWithTreeNode:child1.get()]); | |
123 scoped_nsobject<FakeCocoaCookieTreeNode> cocoaChild2( | |
124 [[FakeCocoaCookieTreeNode alloc] initWithTreeNode:child2.get()]); | |
125 [[cocoaParent mutableChildren] addObject:cocoaChild1.get()]; | |
126 [[cocoaParent mutableChildren] addObject:cocoaChild2.get()]; | |
127 | |
128 EXPECT_EQ(cocoaChild2.get(), FindCocoaNode(child2.get(), cocoaParent.get())); | |
129 } | |
130 | |
131 TEST_F(CookiesWindowControllerTest, FindCocoaNodeRecursive) { | |
132 scoped_ptr< ui::TreeNodeWithValue<int> > parent( | |
133 new ui::TreeNodeWithValue<int>(100)); | |
134 scoped_ptr< ui::TreeNodeWithValue<int> > child1( | |
135 new ui::TreeNodeWithValue<int>(10)); | |
136 scoped_ptr< ui::TreeNodeWithValue<int> > child2( | |
137 new ui::TreeNodeWithValue<int>(20)); | |
138 scoped_nsobject<FakeCocoaCookieTreeNode> cocoaParent( | |
139 [[FakeCocoaCookieTreeNode alloc] initWithTreeNode:parent.get()]); | |
140 scoped_nsobject<FakeCocoaCookieTreeNode> cocoaChild1( | |
141 [[FakeCocoaCookieTreeNode alloc] initWithTreeNode:child1.get()]); | |
142 scoped_nsobject<FakeCocoaCookieTreeNode> cocoaChild2( | |
143 [[FakeCocoaCookieTreeNode alloc] initWithTreeNode:child2.get()]); | |
144 [[cocoaParent mutableChildren] addObject:cocoaChild1.get()]; | |
145 [[cocoaChild1 mutableChildren] addObject:cocoaChild2.get()]; | |
146 | |
147 EXPECT_EQ(cocoaChild2.get(), FindCocoaNode(child2.get(), cocoaParent.get())); | |
148 } | |
149 | |
150 TEST_F(CookiesWindowControllerTest, CocoaNodeFromTreeNodeCookie) { | |
151 net::CookieMonster* cm = browser_helper_.profile()->GetCookieMonster(); | |
152 cm->SetCookie(GURL("http://foo.com"), "A=B"); | |
153 CookiesTreeModel model(cm, database_helper_, local_storage_helper_, nil, nil, | |
154 nil); | |
155 | |
156 // Root --> foo.com --> Cookies --> A. Create node for 'A'. | |
157 ui::TreeModelNode* node = | |
158 model.GetRoot()->GetChild(0)->GetChild(0)->GetChild(0); | |
159 CocoaCookieTreeNode* cookie = CocoaNodeFromTreeNode(node); | |
160 | |
161 CocoaCookieDetails* details = [cookie details]; | |
162 EXPECT_NSEQ(@"B", [details content]); | |
163 EXPECT_NSEQ(l10n_util::GetNSString(IDS_COOKIES_COOKIE_EXPIRES_SESSION), | |
164 [details expires]); | |
165 EXPECT_NSEQ(l10n_util::GetNSString(IDS_COOKIES_COOKIE_SENDFOR_ANY), | |
166 [details sendFor]); | |
167 EXPECT_NSEQ(@"A", [cookie title]); | |
168 EXPECT_NSEQ(@"A", [details name]); | |
169 EXPECT_NSEQ(@"/", [details path]); | |
170 EXPECT_EQ(0U, [[cookie children] count]); | |
171 EXPECT_TRUE([details created]); | |
172 EXPECT_TRUE([cookie isLeaf]); | |
173 EXPECT_EQ(node, [cookie treeNode]); | |
174 } | |
175 | |
176 TEST_F(CookiesWindowControllerTest, CocoaNodeFromTreeNodeRecursive) { | |
177 net::CookieMonster* cm = browser_helper_.profile()->GetCookieMonster(); | |
178 cm->SetCookie(GURL("http://foo.com"), "A=B"); | |
179 CookiesTreeModel model(cm, database_helper_, local_storage_helper_, nil, nil, | |
180 nil); | |
181 | |
182 // Root --> foo.com --> Cookies --> A. Create node for 'foo.com'. | |
183 CookieTreeNode* node = model.GetRoot()->GetChild(0); | |
184 CocoaCookieTreeNode* domain = CocoaNodeFromTreeNode(node); | |
185 CocoaCookieTreeNode* cookies = [[domain children] objectAtIndex:0]; | |
186 CocoaCookieTreeNode* cookie = [[cookies children] objectAtIndex:0]; | |
187 | |
188 // Test domain-level node. | |
189 EXPECT_NSEQ(@"foo.com", [domain title]); | |
190 | |
191 EXPECT_FALSE([domain isLeaf]); | |
192 EXPECT_EQ(1U, [[domain children] count]); | |
193 EXPECT_EQ(node, [domain treeNode]); | |
194 | |
195 // Test "Cookies" folder node. | |
196 EXPECT_NSEQ(l10n_util::GetNSString(IDS_COOKIES_COOKIES), [cookies title]); | |
197 EXPECT_FALSE([cookies isLeaf]); | |
198 EXPECT_EQ(1U, [[cookies children] count]); | |
199 EXPECT_EQ(node->GetChild(0), [cookies treeNode]); | |
200 | |
201 // Test cookie node. This is the same as CocoaNodeFromTreeNodeCookie. | |
202 CocoaCookieDetails* details = [cookie details]; | |
203 EXPECT_NSEQ(@"B", [details content]); | |
204 EXPECT_NSEQ(l10n_util::GetNSString(IDS_COOKIES_COOKIE_EXPIRES_SESSION), | |
205 [details expires]); | |
206 EXPECT_NSEQ(l10n_util::GetNSString(IDS_COOKIES_COOKIE_SENDFOR_ANY), | |
207 [details sendFor]); | |
208 EXPECT_NSEQ(@"A", [cookie title]); | |
209 EXPECT_NSEQ(@"A", [details name]); | |
210 EXPECT_NSEQ(@"/", [details path]); | |
211 EXPECT_NSEQ(@"foo.com", [details domain]); | |
212 EXPECT_EQ(0U, [[cookie children] count]); | |
213 EXPECT_TRUE([details created]); | |
214 EXPECT_TRUE([cookie isLeaf]); | |
215 EXPECT_EQ(node->GetChild(0)->GetChild(0), [cookie treeNode]); | |
216 } | |
217 | |
218 TEST_F(CookiesWindowControllerTest, TreeNodesAdded) { | |
219 const GURL url = GURL("http://foo.com"); | |
220 TestingProfile* profile = browser_helper_.profile(); | |
221 net::CookieMonster* cm = profile->GetCookieMonster(); | |
222 cm->SetCookie(url, "A=B"); | |
223 | |
224 controller_.reset( | |
225 [[CookiesWindowController alloc] initWithProfile:profile | |
226 databaseHelper:database_helper_ | |
227 storageHelper:local_storage_helper_ | |
228 appcacheHelper:appcache_helper_ | |
229 indexedDBHelper:indexed_db_helper_]); | |
230 | |
231 // Root --> foo.com --> Cookies. | |
232 NSMutableArray* cocoa_children = | |
233 [[[[[[controller_ cocoaTreeModel] children] objectAtIndex:0] | |
234 children] objectAtIndex:0] mutableChildren]; | |
235 EXPECT_EQ(1U, [cocoa_children count]); | |
236 | |
237 // Create some cookies. | |
238 cm->SetCookie(url, "C=D"); | |
239 cm->SetCookie(url, "E=F"); | |
240 | |
241 net::CookieList list = cm->GetAllCookies(); | |
242 CookiesTreeModel* model = [controller_ treeModel]; | |
243 // Root --> foo.com --> Cookies. | |
244 CookieTreeNode* parent = model->GetRoot()->GetChild(0)->GetChild(0); | |
245 | |
246 ASSERT_EQ(3U, list.size()); | |
247 | |
248 // Add the cookie nodes. | |
249 CookieTreeCookieNode* cnode = new CookieTreeCookieNode(&list[1]); | |
250 parent->Add(1, cnode); // |parent| takes ownership. | |
251 cnode = new CookieTreeCookieNode(&list[2]); | |
252 parent->Add(2, cnode); | |
253 | |
254 // Manually notify the observer. | |
255 [controller_ modelObserver]->TreeNodesAdded(model, parent, 1, 2); | |
256 | |
257 // Check that we have created 2 more Cocoa nodes. | |
258 EXPECT_EQ(3U, [cocoa_children count]); | |
259 } | |
260 | |
261 TEST_F(CookiesWindowControllerTest, TreeNodesRemoved) { | |
262 const GURL url = GURL("http://foo.com"); | |
263 TestingProfile* profile = browser_helper_.profile(); | |
264 net::CookieMonster* cm = profile->GetCookieMonster(); | |
265 cm->SetCookie(url, "A=B"); | |
266 cm->SetCookie(url, "C=D"); | |
267 cm->SetCookie(url, "E=F"); | |
268 | |
269 controller_.reset( | |
270 [[CookiesWindowController alloc] initWithProfile:profile | |
271 databaseHelper:database_helper_ | |
272 storageHelper:local_storage_helper_ | |
273 appcacheHelper:appcache_helper_ | |
274 indexedDBHelper:indexed_db_helper_]); | |
275 | |
276 // Root --> foo.com --> Cookies. | |
277 NSMutableArray* cocoa_children = | |
278 [[[[[[controller_ cocoaTreeModel] children] objectAtIndex:0] | |
279 children] objectAtIndex:0] mutableChildren]; | |
280 EXPECT_EQ(3U, [cocoa_children count]); | |
281 | |
282 CookiesTreeModel* model = [controller_ treeModel]; | |
283 // Root --> foo.com --> Cookies. | |
284 CookieTreeNode* parent = model->GetRoot()->GetChild(0)->GetChild(0); | |
285 | |
286 // Pretend to remove the nodes. | |
287 [controller_ modelObserver]->TreeNodesRemoved(model, parent, 1, 2); | |
288 | |
289 EXPECT_EQ(1U, [cocoa_children count]); | |
290 | |
291 NSString* title = [[[cocoa_children objectAtIndex:0] details] name]; | |
292 EXPECT_NSEQ(@"A", title); | |
293 } | |
294 | |
295 TEST_F(CookiesWindowControllerTest, TreeNodeChanged) { | |
296 const GURL url = GURL("http://foo.com"); | |
297 TestingProfile* profile = browser_helper_.profile(); | |
298 net::CookieMonster* cm = profile->GetCookieMonster(); | |
299 cm->SetCookie(url, "A=B"); | |
300 | |
301 controller_.reset( | |
302 [[CookiesWindowController alloc] initWithProfile:profile | |
303 databaseHelper:database_helper_ | |
304 storageHelper:local_storage_helper_ | |
305 appcacheHelper:appcache_helper_ | |
306 indexedDBHelper:indexed_db_helper_]); | |
307 | |
308 CookiesTreeModel* model = [controller_ treeModel]; | |
309 // Root --> foo.com --> Cookies. | |
310 CookieTreeNode* node = model->GetRoot()->GetChild(0)->GetChild(0); | |
311 | |
312 // Root --> foo.com --> Cookies. | |
313 CocoaCookieTreeNode* cocoa_node = | |
314 [[[[[controller_ cocoaTreeModel] children] objectAtIndex:0] | |
315 children] objectAtIndex:0]; | |
316 | |
317 EXPECT_NSEQ(l10n_util::GetNSString(IDS_COOKIES_COOKIES), | |
318 [cocoa_node title]); | |
319 | |
320 // Fake update the cookie folder's title. This would never happen in reality, | |
321 // but it tests the code path that ultimately calls CocoaNodeFromTreeNode, | |
322 // which is tested elsewhere. | |
323 node->SetTitle(ASCIIToUTF16("Silly Change")); | |
324 [controller_ modelObserver]->TreeNodeChanged(model, node); | |
325 | |
326 EXPECT_NSEQ(@"Silly Change", [cocoa_node title]); | |
327 } | |
328 | |
329 TEST_F(CookiesWindowControllerTest, DeleteCookie) { | |
330 const GURL url = GURL("http://foo.com"); | |
331 TestingProfile* profile = browser_helper_.profile(); | |
332 net::CookieMonster* cm = profile->GetCookieMonster(); | |
333 cm->SetCookie(url, "A=B"); | |
334 cm->SetCookie(url, "C=D"); | |
335 cm->SetCookie(GURL("http://google.com"), "E=F"); | |
336 | |
337 // This will clean itself up when we call |-closeSheet:|. If we reset the | |
338 // scoper, we'd get a double-free. | |
339 CookiesWindowController* controller = | |
340 [[CookiesWindowController alloc] initWithProfile:profile | |
341 databaseHelper:database_helper_ | |
342 storageHelper:local_storage_helper_ | |
343 appcacheHelper:appcache_helper_ | |
344 indexedDBHelper:indexed_db_helper_]; | |
345 [controller attachSheetTo:test_window()]; | |
346 NSTreeController* treeController = [controller treeController]; | |
347 | |
348 // Select cookie A. | |
349 NSUInteger pathA[3] = {0, 0, 0}; | |
350 NSIndexPath* indexPath = [NSIndexPath indexPathWithIndexes:pathA length:3]; | |
351 [treeController setSelectionIndexPath:indexPath]; | |
352 | |
353 // Press the "Delete" button. | |
354 [controller deleteCookie:nil]; | |
355 | |
356 // Root --> foo.com --> Cookies. | |
357 NSArray* cookies = [[[[[[controller cocoaTreeModel] children] | |
358 objectAtIndex:0] children] objectAtIndex:0] children]; | |
359 EXPECT_EQ(1U, [cookies count]); | |
360 EXPECT_NSEQ(@"C", [[cookies lastObject] title]); | |
361 EXPECT_NSEQ(indexPath, [treeController selectionIndexPath]); | |
362 | |
363 // Select cookie E. | |
364 NSUInteger pathE[3] = {1, 0, 0}; | |
365 indexPath = [NSIndexPath indexPathWithIndexes:pathE length:3]; | |
366 [treeController setSelectionIndexPath:indexPath]; | |
367 | |
368 // Perform delete. | |
369 [controller deleteCookie:nil]; | |
370 | |
371 // Make sure that both the domain level node and the Cookies folder node got | |
372 // deleted because there was only one leaf node. | |
373 EXPECT_EQ(1U, [[[controller cocoaTreeModel] children] count]); | |
374 | |
375 // Select cookie C. | |
376 NSUInteger pathC[3] = {0, 0, 0}; | |
377 indexPath = [NSIndexPath indexPathWithIndexes:pathC length:3]; | |
378 [treeController setSelectionIndexPath:indexPath]; | |
379 | |
380 // Perform delete. | |
381 [controller deleteCookie:nil]; | |
382 | |
383 // Make sure the world didn't explode and that there's nothing in the tree. | |
384 EXPECT_EQ(0U, [[[controller cocoaTreeModel] children] count]); | |
385 | |
386 [controller closeSheet:nil]; | |
387 } | |
388 | |
389 TEST_F(CookiesWindowControllerTest, DidExpandItem) { | |
390 const GURL url = GURL("http://foo.com"); | |
391 TestingProfile* profile = browser_helper_.profile(); | |
392 net::CookieMonster* cm = profile->GetCookieMonster(); | |
393 cm->SetCookie(url, "A=B"); | |
394 cm->SetCookie(url, "C=D"); | |
395 | |
396 controller_.reset( | |
397 [[CookiesWindowController alloc] initWithProfile:profile | |
398 databaseHelper:database_helper_ | |
399 storageHelper:local_storage_helper_ | |
400 appcacheHelper:appcache_helper_ | |
401 indexedDBHelper:indexed_db_helper_]); | |
402 | |
403 // Root --> foo.com. | |
404 CocoaCookieTreeNode* foo = | |
405 [[[controller_ cocoaTreeModel] children] objectAtIndex:0]; | |
406 | |
407 // Create the objects we are going to be testing with. | |
408 id outlineView = [OCMockObject mockForClass:[NSOutlineView class]]; | |
409 id treeNode = [OCMockObject mockForClass:[NSTreeNode class]]; | |
410 NSTreeNode* childTreeNode = | |
411 [NSTreeNode treeNodeWithRepresentedObject:[[foo children] lastObject]]; | |
412 NSArray* fakeChildren = [NSArray arrayWithObject:childTreeNode]; | |
413 | |
414 // Set up the mock object. | |
415 [[[treeNode stub] andReturn:foo] representedObject]; | |
416 [[[treeNode stub] andReturn:fakeChildren] childNodes]; | |
417 | |
418 // Create a fake "ItemDidExpand" notification. | |
419 NSDictionary* userInfo = [NSDictionary dictionaryWithObject:treeNode | |
420 forKey:@"NSObject"]; | |
421 NSNotification* notif = | |
422 [NSNotification notificationWithName:@"ItemDidExpandNotification" | |
423 object:outlineView | |
424 userInfo:userInfo]; | |
425 | |
426 // Make sure we work correctly. | |
427 [[outlineView expect] expandItem:childTreeNode]; | |
428 [controller_ outlineViewItemDidExpand:notif]; | |
429 [outlineView verify]; | |
430 } | |
431 | |
432 TEST_F(CookiesWindowControllerTest, ClearBrowsingData) { | |
433 const GURL url = GURL("http://foo.com"); | |
434 TestingProfile* profile = browser_helper_.profile(); | |
435 net::CookieMonster* cm = profile->GetCookieMonster(); | |
436 cm->SetCookie(url, "A=B"); | |
437 cm->SetCookie(url, "C=D"); | |
438 cm->SetCookie(url, "E=F"); | |
439 | |
440 id mock = [OCMockObject partialMockForObject:controller_.get()]; | |
441 [[mock expect] loadTreeModelFromProfile]; | |
442 | |
443 NSNumber* mask = | |
444 [NSNumber numberWithInt:BrowsingDataRemover::REMOVE_COOKIES]; | |
445 NSDictionary* userInfo = | |
446 [NSDictionary dictionaryWithObject:mask | |
447 forKey:kClearBrowsingDataControllerRemoveMask]; | |
448 NSNotification* notif = | |
449 [NSNotification notificationWithName:kClearBrowsingDataControllerDidDelete | |
450 object:nil | |
451 userInfo:userInfo]; | |
452 [controller_ clearBrowsingDataNotification:notif]; | |
453 | |
454 [mock verify]; | |
455 } | |
456 | |
457 // This test has been flaky under Valgrind and turns the bot red since r38504. | |
458 // Under Mac Tests 10.5, it occasionally reports: | |
459 // malloc: *** error for object 0x31e0468: Non-aligned pointer being freed | |
460 // *** set a breakpoint in malloc_error_break to debug | |
461 // Attempts to reproduce locally were not successful. This code is likely | |
462 // changing in the future, so it's marked flaky for now. http://crbug.com/35327 | |
463 TEST_F(CookiesWindowControllerTest, FLAKY_RemoveButtonEnabled) { | |
464 const GURL url = GURL("http://foo.com"); | |
465 TestingProfile* profile = browser_helper_.profile(); | |
466 net::CookieMonster* cm = profile->GetCookieMonster(); | |
467 cm->SetCookie(url, "A=B"); | |
468 cm->SetCookie(url, "C=D"); | |
469 | |
470 // This will clean itself up when we call |-closeSheet:|. If we reset the | |
471 // scoper, we'd get a double-free. | |
472 database_helper_ = new MockBrowsingDataDatabaseHelper(profile); | |
473 local_storage_helper_ = new MockBrowsingDataLocalStorageHelper(profile); | |
474 local_storage_helper_->AddLocalStorageSamples(); | |
475 CookiesWindowController* controller = | |
476 [[CookiesWindowController alloc] initWithProfile:profile | |
477 databaseHelper:database_helper_ | |
478 storageHelper:local_storage_helper_ | |
479 appcacheHelper:appcache_helper_ | |
480 indexedDBHelper:indexed_db_helper_]; | |
481 local_storage_helper_->Notify(); | |
482 [controller attachSheetTo:test_window()]; | |
483 | |
484 // Nothing should be selected right now. | |
485 EXPECT_FALSE([controller removeButtonEnabled]); | |
486 | |
487 { | |
488 // Pretend to select cookie A. | |
489 NSUInteger path[3] = {0, 0, 0}; | |
490 NSIndexPath* indexPath = [NSIndexPath indexPathWithIndexes:path length:3]; | |
491 [[controller treeController] setSelectionIndexPath:indexPath]; | |
492 [controller outlineViewSelectionDidChange:nil]; | |
493 EXPECT_TRUE([controller removeButtonEnabled]); | |
494 } | |
495 | |
496 { | |
497 // Pretend to select cookie C. | |
498 NSUInteger path[3] = {0, 0, 1}; | |
499 NSIndexPath* indexPath = [NSIndexPath indexPathWithIndexes:path length:3]; | |
500 [[controller treeController] setSelectionIndexPath:indexPath]; | |
501 [controller outlineViewSelectionDidChange:nil]; | |
502 EXPECT_TRUE([controller removeButtonEnabled]); | |
503 } | |
504 | |
505 { | |
506 // Select a local storage node. | |
507 NSUInteger path[3] = {2, 0, 0}; | |
508 NSIndexPath* indexPath = [NSIndexPath indexPathWithIndexes:path length:3]; | |
509 [[controller treeController] setSelectionIndexPath:indexPath]; | |
510 [controller outlineViewSelectionDidChange:nil]; | |
511 EXPECT_TRUE([controller removeButtonEnabled]); | |
512 } | |
513 | |
514 { | |
515 // Pretend to select something that isn't there! | |
516 NSUInteger path[3] = {0, 0, 2}; | |
517 NSIndexPath* indexPath = [NSIndexPath indexPathWithIndexes:path length:3]; | |
518 [[controller treeController] setSelectionIndexPath:indexPath]; | |
519 [controller outlineViewSelectionDidChange:nil]; | |
520 EXPECT_FALSE([controller removeButtonEnabled]); | |
521 } | |
522 | |
523 { | |
524 // Try selecting something that doesn't exist again. | |
525 NSUInteger path[3] = {7, 1, 4}; | |
526 NSIndexPath* indexPath = [NSIndexPath indexPathWithIndexes:path length:3]; | |
527 [[controller treeController] setSelectionIndexPath:indexPath]; | |
528 [controller outlineViewSelectionDidChange:nil]; | |
529 EXPECT_FALSE([controller removeButtonEnabled]); | |
530 } | |
531 | |
532 [controller closeSheet:nil]; | |
533 } | |
534 | |
535 TEST_F(CookiesWindowControllerTest, UpdateFilter) { | |
536 const GURL url = GURL("http://foo.com"); | |
537 TestingProfile* profile = browser_helper_.profile(); | |
538 net::CookieMonster* cm = profile->GetCookieMonster(); | |
539 cm->SetCookie(GURL("http://a.com"), "A=B"); | |
540 cm->SetCookie(GURL("http://aa.com"), "C=D"); | |
541 cm->SetCookie(GURL("http://b.com"), "E=F"); | |
542 cm->SetCookie(GURL("http://d.com"), "G=H"); | |
543 cm->SetCookie(GURL("http://dd.com"), "I=J"); | |
544 | |
545 controller_.reset( | |
546 [[CookiesWindowController alloc] initWithProfile:profile | |
547 databaseHelper:database_helper_ | |
548 storageHelper:local_storage_helper_ | |
549 appcacheHelper:appcache_helper_ | |
550 indexedDBHelper:indexed_db_helper_]); | |
551 | |
552 // Make sure we registered all five cookies. | |
553 EXPECT_EQ(5U, [[[controller_ cocoaTreeModel] children] count]); | |
554 | |
555 NSSearchField* field = | |
556 [[NSSearchField alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)]; | |
557 | |
558 // Make sure we still have five cookies. | |
559 [field setStringValue:@""]; | |
560 [controller_ updateFilter:field]; | |
561 EXPECT_EQ(5U, [[[controller_ cocoaTreeModel] children] count]); | |
562 | |
563 // Search for "a". | |
564 [field setStringValue:@"a"]; | |
565 [controller_ updateFilter:field]; | |
566 EXPECT_EQ(2U, [[[controller_ cocoaTreeModel] children] count]); | |
567 | |
568 // Search for "b". | |
569 [field setStringValue:@"b"]; | |
570 [controller_ updateFilter:field]; | |
571 EXPECT_EQ(1U, [[[controller_ cocoaTreeModel] children] count]); | |
572 | |
573 // Search for "d". | |
574 [field setStringValue:@"d"]; | |
575 [controller_ updateFilter:field]; | |
576 EXPECT_EQ(2U, [[[controller_ cocoaTreeModel] children] count]); | |
577 | |
578 // Search for "e". | |
579 [field setStringValue:@"e"]; | |
580 [controller_ updateFilter:field]; | |
581 EXPECT_EQ(0U, [[[controller_ cocoaTreeModel] children] count]); | |
582 | |
583 // Search for "aa". | |
584 [field setStringValue:@"aa"]; | |
585 [controller_ updateFilter:field]; | |
586 EXPECT_EQ(1U, [[[controller_ cocoaTreeModel] children] count]); | |
587 } | |
588 | |
589 TEST_F(CookiesWindowControllerTest, CreateDatabaseStorageNodes) { | |
590 TestingProfile* profile = browser_helper_.profile(); | |
591 database_helper_ = new MockBrowsingDataDatabaseHelper(profile); | |
592 local_storage_helper_ = new MockBrowsingDataLocalStorageHelper(profile); | |
593 database_helper_->AddDatabaseSamples(); | |
594 controller_.reset( | |
595 [[CookiesWindowController alloc] initWithProfile:profile | |
596 databaseHelper:database_helper_ | |
597 storageHelper:local_storage_helper_ | |
598 appcacheHelper:appcache_helper_ | |
599 indexedDBHelper:indexed_db_helper_]); | |
600 database_helper_->Notify(); | |
601 | |
602 ASSERT_EQ(2U, [[[controller_ cocoaTreeModel] children] count]); | |
603 | |
604 // Root --> gdbhost1. | |
605 CocoaCookieTreeNode* node = | |
606 [[[controller_ cocoaTreeModel] children] objectAtIndex:0]; | |
607 EXPECT_NSEQ(@"gdbhost1", [node title]); | |
608 EXPECT_EQ(kCocoaCookieDetailsTypeFolder, [node nodeType]); | |
609 EXPECT_EQ(1U, [[node children] count]); | |
610 | |
611 // host1 --> Web Databases. | |
612 node = [[node children] lastObject]; | |
613 EXPECT_NSEQ(l10n_util::GetNSString(IDS_COOKIES_WEB_DATABASES), [node title]); | |
614 EXPECT_EQ(kCocoaCookieDetailsTypeFolder, [node nodeType]); | |
615 EXPECT_EQ(1U, [[node children] count]); | |
616 | |
617 // Database Storage --> db1. | |
618 node = [[node children] lastObject]; | |
619 EXPECT_NSEQ(@"db1", [node title]); | |
620 EXPECT_EQ(kCocoaCookieDetailsTypeTreeDatabase, [node nodeType]); | |
621 CocoaCookieDetails* details = [node details]; | |
622 EXPECT_NSEQ(@"description 1", [details databaseDescription]); | |
623 EXPECT_TRUE([details lastModified]); | |
624 EXPECT_TRUE([details fileSize]); | |
625 | |
626 // Root --> gdbhost2. | |
627 node = | |
628 [[[controller_ cocoaTreeModel] children] objectAtIndex:1]; | |
629 EXPECT_NSEQ(@"gdbhost2", [node title]); | |
630 EXPECT_EQ(kCocoaCookieDetailsTypeFolder, [node nodeType]); | |
631 EXPECT_EQ(1U, [[node children] count]); | |
632 | |
633 // host1 --> Web Databases. | |
634 node = [[node children] lastObject]; | |
635 EXPECT_NSEQ(l10n_util::GetNSString(IDS_COOKIES_WEB_DATABASES), [node title]); | |
636 EXPECT_EQ(kCocoaCookieDetailsTypeFolder, [node nodeType]); | |
637 EXPECT_EQ(1U, [[node children] count]); | |
638 | |
639 // Database Storage --> db2. | |
640 node = [[node children] lastObject]; | |
641 EXPECT_NSEQ(@"db2", [node title]); | |
642 EXPECT_EQ(kCocoaCookieDetailsTypeTreeDatabase, [node nodeType]); | |
643 details = [node details]; | |
644 EXPECT_NSEQ(@"description 2", [details databaseDescription]); | |
645 EXPECT_TRUE([details lastModified]); | |
646 EXPECT_TRUE([details fileSize]); | |
647 } | |
648 | |
649 TEST_F(CookiesWindowControllerTest, CreateLocalStorageNodes) { | |
650 TestingProfile* profile = browser_helper_.profile(); | |
651 net::CookieMonster* cm = profile->GetCookieMonster(); | |
652 cm->SetCookie(GURL("http://google.com"), "A=B"); | |
653 cm->SetCookie(GURL("http://dev.chromium.org"), "C=D"); | |
654 database_helper_ = new MockBrowsingDataDatabaseHelper(profile); | |
655 local_storage_helper_ = new MockBrowsingDataLocalStorageHelper(profile); | |
656 local_storage_helper_->AddLocalStorageSamples(); | |
657 controller_.reset( | |
658 [[CookiesWindowController alloc] initWithProfile:profile | |
659 databaseHelper:database_helper_ | |
660 storageHelper:local_storage_helper_ | |
661 appcacheHelper:appcache_helper_ | |
662 indexedDBHelper:indexed_db_helper_]); | |
663 local_storage_helper_->Notify(); | |
664 | |
665 ASSERT_EQ(4U, [[[controller_ cocoaTreeModel] children] count]); | |
666 | |
667 // Root --> host1. | |
668 CocoaCookieTreeNode* node = | |
669 [[[controller_ cocoaTreeModel] children] objectAtIndex:2]; | |
670 EXPECT_NSEQ(@"host1", [node title]); | |
671 EXPECT_EQ(kCocoaCookieDetailsTypeFolder, [node nodeType]); | |
672 EXPECT_EQ(1U, [[node children] count]); | |
673 | |
674 // host1 --> Local Storage. | |
675 node = [[node children] lastObject]; | |
676 EXPECT_NSEQ(l10n_util::GetNSString(IDS_COOKIES_LOCAL_STORAGE), [node title]); | |
677 EXPECT_EQ(kCocoaCookieDetailsTypeFolder, [node nodeType]); | |
678 EXPECT_EQ(1U, [[node children] count]); | |
679 | |
680 // Local Storage --> http://host1:1/. | |
681 node = [[node children] lastObject]; | |
682 EXPECT_NSEQ(@"http://host1:1/", [node title]); | |
683 EXPECT_EQ(kCocoaCookieDetailsTypeTreeLocalStorage, [node nodeType]); | |
684 EXPECT_NSEQ(@"http://host1:1/", [[node details] domain]); | |
685 EXPECT_TRUE([[node details] lastModified]); | |
686 EXPECT_TRUE([[node details] fileSize]); | |
687 | |
688 // Root --> host2. | |
689 node = | |
690 [[[controller_ cocoaTreeModel] children] objectAtIndex:3]; | |
691 EXPECT_NSEQ(@"host2", [node title]); | |
692 EXPECT_EQ(kCocoaCookieDetailsTypeFolder, [node nodeType]); | |
693 EXPECT_EQ(1U, [[node children] count]); | |
694 | |
695 // host2 --> Local Storage. | |
696 node = [[node children] lastObject]; | |
697 EXPECT_NSEQ(l10n_util::GetNSString(IDS_COOKIES_LOCAL_STORAGE), [node title]); | |
698 EXPECT_EQ(kCocoaCookieDetailsTypeFolder, [node nodeType]); | |
699 EXPECT_EQ(1U, [[node children] count]); | |
700 | |
701 // Local Storage --> http://host2:2/. | |
702 node = [[node children] lastObject]; | |
703 EXPECT_NSEQ(@"http://host2:2/", [node title]); | |
704 EXPECT_EQ(kCocoaCookieDetailsTypeTreeLocalStorage, [node nodeType]); | |
705 EXPECT_NSEQ(@"http://host2:2/", [[node details] domain]); | |
706 EXPECT_TRUE([[node details] lastModified]); | |
707 EXPECT_TRUE([[node details] fileSize]); | |
708 } | |
709 | |
710 } // namespace | |
OLD | NEW |