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