Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: chrome/browser/cocoa/cookies_window_controller_unittest.mm

Issue 523025: [Mac] Implement the cookie manager (Closed)
Patch Set: Address all comments Created 10 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #import <Cocoa/Cocoa.h>
6
7 #include "app/tree_model.h"
8 #import "base/scoped_nsobject.h"
9 #include "base/scoped_ptr.h"
10 #import "chrome/browser/cocoa/cookies_window_controller.h"
11 #include "chrome/browser/cocoa/cocoa_test_helper.h"
12 #include "chrome/browser/net/url_request_context_getter.h"
13 #include "chrome/browser/cookies_tree_model.h"
14 #include "chrome/test/testing_profile.h"
15 #include "googleurl/src/gurl.h"
16 #include "net/url_request/url_request_context.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "testing/platform_test.h"
19
20 // Copied from src/chrome/cookies_tree_model_unittest.cc.
21 namespace {
22
23 class TestURLRequestContext : public URLRequestContext {
24 public:
25 TestURLRequestContext() {
26 cookie_store_ = new net::CookieMonster();
27 }
28 };
29
30 class TestURLRequestContextGetter : public URLRequestContextGetter {
31 public:
32 virtual URLRequestContext* GetURLRequestContext() {
33 if (!context_)
34 context_ = new TestURLRequestContext();
35 return context_.get();
36 }
37 private:
38 scoped_refptr<URLRequestContext> context_;
39 };
40
41 class CookieTestingProfile : public TestingProfile {
42 public:
43 virtual URLRequestContextGetter* GetRequestContext() {
44 if (!url_request_context_getter_.get())
45 url_request_context_getter_ = new TestURLRequestContextGetter();
46 return url_request_context_getter_.get();
47 }
48 virtual ~CookieTestingProfile() {}
49
50 net::CookieMonster* GetCookieMonster() {
51 return GetRequestContext()->GetCookieStore()->GetCookieMonster();
52 }
53
54 private:
55 scoped_refptr<URLRequestContextGetter> url_request_context_getter_;
56 };
57
58 } // namespace
59
60 // Used to test FindCocoaNode. This only sets the title and node, without
61 // initializing any other members.
62 @interface FakeCocoaCookieTreeNode : CocoaCookieTreeNode {
63 TreeModelNode* testNode_;
64 }
65 - (id)initWithTreeNode:(TreeModelNode*)node;
66 @end
67 @implementation FakeCocoaCookieTreeNode
68 - (id)initWithTreeNode:(TreeModelNode*)node {
69 if ((self = [super init])) {
70 testNode_ = node;
71 children_.reset([[NSMutableArray alloc] init]);
72 }
73 return self;
74 }
75 - (TreeModelNode*)treeNode {
76 return testNode_;
77 }
78 @end
79
80 // For some utterly inexplicable reason, |-children| returns an NSArray rather
81 // than an NSMutableArray, BUT ONLY IN THIS UNIT TEST, and not in the actual
82 // window controller. This leads to errors for |-addObject:| messages. Maybe
83 // it's a compiler bug? Or there's something strange happening via swizzling in
84 // the test environment? Or did somebody forget to sacrifice a lamb on the new
85 // moon? After investigating for long time, the easiest/best solution was to
86 // create this hacky secondary getter.
87 @interface CocoaCookieTreeNode (UglyHacks)
88 - (NSMutableArray*)childs;
89 @end
90 @implementation CocoaCookieTreeNode (UglyHacks)
91 - (NSMutableArray*)childs {
92 return children_.get();
93 }
94 @end
95
96 class CookiesWindowControllerTest : public CocoaTest {
97 public:
98 virtual void SetUp() {
99 CocoaTest::SetUp();
100 controller_.reset(
101 [[CookiesWindowController alloc] initWithProfile:&profile_]);
102 }
103
104 virtual void TearDown() {
105 CocoaTest::TearDown();
106 }
107
108 CocoaCookieTreeNode* CocoaNodeFromTreeNode(TreeModelNode* node,
109 bool recurse) {
110 return [controller_ modelObserver]->CocoaNodeFromTreeNode(node, recurse);
111 }
112
113 CocoaCookieTreeNode* FindCocoaNode(TreeModelNode* node,
114 CocoaCookieTreeNode* start) {
115 return [controller_ modelObserver]->FindCocoaNode(node, start);
116 }
117
118 CookieTestingProfile profile_;
119 scoped_nsobject<CookiesWindowController> controller_;
120 };
121
122 TEST_F(CookiesWindowControllerTest, Construction) {
123 std::vector<SkBitmap> skia_icons;
124 [controller_ treeModel]->GetIcons(&skia_icons);
125
126 EXPECT_EQ([[controller_ icons] count], skia_icons.size() + 1U);
127 }
128
129 TEST_F(CookiesWindowControllerTest, FindCocoaNodeRoot) {
130 scoped_ptr< TreeNodeWithValue<int> > search(new TreeNodeWithValue<int>(42));
131 scoped_nsobject<FakeCocoaCookieTreeNode> node(
132 [[FakeCocoaCookieTreeNode alloc] initWithTreeNode:search.get()]);
133 EXPECT_EQ(node.get(), FindCocoaNode(search.get(), node.get()));
134 }
135
136 TEST_F(CookiesWindowControllerTest, FindCocoaNodeImmediateChild) {
137 scoped_ptr< TreeNodeWithValue<int> > parent(new TreeNodeWithValue<int>(100));
138 scoped_ptr< TreeNodeWithValue<int> > child1(new TreeNodeWithValue<int>(10));
139 scoped_ptr< TreeNodeWithValue<int> > child2(new TreeNodeWithValue<int>(20));
140 scoped_nsobject<FakeCocoaCookieTreeNode> cocoaParent(
141 [[FakeCocoaCookieTreeNode alloc] initWithTreeNode:parent.get()]);
142 scoped_nsobject<FakeCocoaCookieTreeNode> cocoaChild1(
143 [[FakeCocoaCookieTreeNode alloc] initWithTreeNode:child1.get()]);
144 scoped_nsobject<FakeCocoaCookieTreeNode> cocoaChild2(
145 [[FakeCocoaCookieTreeNode alloc] initWithTreeNode:child2.get()]);
146 [[cocoaParent childs] addObject:cocoaChild1.get()];
147 [[cocoaParent childs] addObject:cocoaChild2.get()];
148
149 EXPECT_EQ(cocoaChild2.get(), FindCocoaNode(child2.get(), cocoaParent.get()));
150 }
151
152 TEST_F(CookiesWindowControllerTest, FindCocoaNodeRecursive) {
153 scoped_ptr< TreeNodeWithValue<int> > parent(new TreeNodeWithValue<int>(100));
154 scoped_ptr< TreeNodeWithValue<int> > child1(new TreeNodeWithValue<int>(10));
155 scoped_ptr< TreeNodeWithValue<int> > child2(new TreeNodeWithValue<int>(20));
156 scoped_nsobject<FakeCocoaCookieTreeNode> cocoaParent(
157 [[FakeCocoaCookieTreeNode alloc] initWithTreeNode:parent.get()]);
158 scoped_nsobject<FakeCocoaCookieTreeNode> cocoaChild1(
159 [[FakeCocoaCookieTreeNode alloc] initWithTreeNode:child1.get()]);
160 scoped_nsobject<FakeCocoaCookieTreeNode> cocoaChild2(
161 [[FakeCocoaCookieTreeNode alloc] initWithTreeNode:child2.get()]);
162 [[cocoaParent childs] addObject:cocoaChild1.get()];
163 [[cocoaChild1 childs] addObject:cocoaChild2.get()];
164
165 EXPECT_EQ(cocoaChild2.get(), FindCocoaNode(child2.get(), cocoaParent.get()));
166 }
167
168 TEST_F(CookiesWindowControllerTest, CocoaNodeFromTreeNodeCookie) {
169 net::CookieMonster* cm = profile_.GetCookieMonster();
170 cm->SetCookie(GURL("http://foo.com"), "A=B");
171 CookiesTreeModel model(&profile_);
172
173 // Root --> foo.com --> Cookies --> A. Create node for 'A'.
174 TreeModelNode* node = model.GetRoot()->GetChild(0)->GetChild(0)->GetChild(0);
175 CocoaCookieTreeNode* cookie = CocoaNodeFromTreeNode(node, false);
176
177 EXPECT_TRUE([@"B" isEqualToString:[cookie content]]);
178 EXPECT_TRUE([@"End of the session" isEqualToString:[cookie expires]]);
179 EXPECT_TRUE([@"Any kind of connection" isEqualToString:[cookie sendFor]]);
180 EXPECT_TRUE([@"A" isEqualToString:[cookie title]]);
181 EXPECT_TRUE([@"A" isEqualToString:[cookie name]]);
182 EXPECT_TRUE([@"/" isEqualToString:[cookie path]]);
183 EXPECT_EQ(0U, [[cookie childs] count]);
184 EXPECT_TRUE([cookie created]);
185 EXPECT_TRUE([cookie isLeaf]);
186 EXPECT_EQ(node, [cookie treeNode]);
187 }
188
189 TEST_F(CookiesWindowControllerTest, CocoaNodeFromTreeNodeRecursive) {
190 net::CookieMonster* cm = profile_.GetCookieMonster();
191 cm->SetCookie(GURL("http://foo.com"), "A=B");
192 CookiesTreeModel model(&profile_);
193
194 // Root --> foo.com --> Cookies --> A. Create node for 'foo.com'.
195 CookieTreeNode* node = model.GetRoot()->GetChild(0);
196 CocoaCookieTreeNode* domain = CocoaNodeFromTreeNode(node, true);
197 CocoaCookieTreeNode* cookies = [[domain childs] objectAtIndex:0];
198 CocoaCookieTreeNode* cookie = [[cookies childs] objectAtIndex:0];
199
200 // Test domain-level node.
201 EXPECT_TRUE([@"foo.com" isEqualToString:[domain title]]);
202
203 EXPECT_FALSE([domain isLeaf]);
204 EXPECT_EQ(1U, [[domain childs] count]);
205 EXPECT_EQ(node, [domain treeNode]);
206
207 // Test "Cookies" folder node.
208 EXPECT_TRUE([@"Cookies" isEqualToString:[cookies title]]);
209 EXPECT_FALSE([cookies isLeaf]);
210 EXPECT_EQ(1U, [[cookies childs] count]);
211 EXPECT_EQ(node->GetChild(0), [cookies treeNode]);
212
213 // Test cookie node. This is the same as CocoaNodeFromTreeNodeCookie.
214 EXPECT_TRUE([@"B" isEqualToString:[cookie content]]);
215 EXPECT_TRUE([@"End of the session" isEqualToString:[cookie expires]]);
216 EXPECT_TRUE([@"Any kind of connection" isEqualToString:[cookie sendFor]]);
217 EXPECT_TRUE([@"A" isEqualToString:[cookie title]]);
218 EXPECT_TRUE([@"A" isEqualToString:[cookie name]]);
219 EXPECT_TRUE([@"/" isEqualToString:[cookie path]]);
220 EXPECT_TRUE([@"foo.com" isEqualToString:[cookie domain]]);
221 EXPECT_EQ(0U, [[cookie childs] count]);
222 EXPECT_TRUE([cookie created]);
223 EXPECT_TRUE([cookie isLeaf]);
224 EXPECT_EQ(node->GetChild(0)->GetChild(0), [cookie treeNode]);
225 }
226
227 TEST_F(CookiesWindowControllerTest, TreeNodesAdded) {
228 const GURL url = GURL("http://foo.com");
229 net::CookieMonster* cm = profile_.GetCookieMonster();
230 cm->SetCookie(url, "A=B");
231
232 controller_.reset(
233 [[CookiesWindowController alloc] initWithProfile:&profile_]);
234
235 // Root --> foo.com --> Cookies.
236 NSMutableArray* cocoa_children =
237 [[[[[[controller_ cocoaTreeModel] childs] objectAtIndex:0]
238 childs] objectAtIndex:0] childs];
239 EXPECT_EQ(1U, [cocoa_children count]);
240
241 // Create some cookies.
242 cm->SetCookie(url, "C=D");
243 cm->SetCookie(url, "E=F");
244
245 net::CookieMonster::CookieList list = cm->GetAllCookies();
246 CookiesTreeModel* model = [controller_ treeModel];
247 // Root --> foo.com --> Cookies.
248 CookieTreeNode* parent = model->GetRoot()->GetChild(0)->GetChild(0);
249
250 ASSERT_EQ(3U, list.size());
251
252 // Add the cookie nodes.
253 CookieTreeCookieNode* cnode = new CookieTreeCookieNode(&list[1]);
254 parent->Add(1, cnode); // |parent| takes ownership.
255 cnode = new CookieTreeCookieNode(&list[2]);
256 parent->Add(2, cnode);
257
258 // Manually notify the observer.
259 [controller_ modelObserver]->TreeNodesAdded(model, parent, 1, 2);
260
261 // Check that we have created 2 more Cocoa nodes.
262 EXPECT_EQ(3U, [cocoa_children count]);
263 }
264
265 TEST_F(CookiesWindowControllerTest, TreeNodesRemoved) {
266 const GURL url = GURL("http://foo.com");
267 net::CookieMonster* cm = profile_.GetCookieMonster();
268 cm->SetCookie(url, "A=B");
269 cm->SetCookie(url, "C=D");
270 cm->SetCookie(url, "E=F");
271
272 controller_.reset(
273 [[CookiesWindowController alloc] initWithProfile:&profile_]);
274
275 // Root --> foo.com --> Cookies.
276 NSMutableArray* cocoa_children =
277 [[[[[[controller_ cocoaTreeModel] childs] objectAtIndex:0]
278 childs] objectAtIndex:0] childs];
279 EXPECT_EQ(3U, [cocoa_children count]);
280
281 CookiesTreeModel* model = [controller_ treeModel];
282 // Root --> foo.com --> Cookies.
283 CookieTreeNode* parent = model->GetRoot()->GetChild(0)->GetChild(0);
284
285 // Pretend to remove the nodes.
286 [controller_ modelObserver]->TreeNodesRemoved(model, parent, 1, 2);
287
288 EXPECT_EQ(1U, [cocoa_children count]);
289
290 NSString* title = [[cocoa_children objectAtIndex:0] name];
291 EXPECT_TRUE([@"A" isEqualToString:title]);
292 }
293
294 TEST_F(CookiesWindowControllerTest, TreeNodeChildrenReordered) {
295 const GURL url = GURL("http://foo.com");
296 net::CookieMonster* cm = profile_.GetCookieMonster();
297 cm->SetCookie(url, "A=B");
298 cm->SetCookie(url, "C=D");
299 cm->SetCookie(url, "E=F");
300
301 controller_.reset(
302 [[CookiesWindowController alloc] initWithProfile:&profile_]);
303
304 // Root --> foo.com --> Cookies.
305 NSMutableArray* cocoa_children =
306 [[[[[[controller_ cocoaTreeModel] childs] objectAtIndex:0]
307 childs] objectAtIndex:0] childs];
308 EXPECT_EQ(3U, [cocoa_children count]);
309
310 // Check default ordering.
311 CocoaCookieTreeNode* node = [cocoa_children objectAtIndex:0];
312 EXPECT_TRUE([@"A" isEqualToString:[node name]]);
313 node = [cocoa_children objectAtIndex:1];
314 EXPECT_TRUE([@"C" isEqualToString:[node name]]);
315 node = [cocoa_children objectAtIndex:2];
316 EXPECT_TRUE([@"E" isEqualToString:[node name]]);
317
318 CookiesTreeModel* model = [controller_ treeModel];
319 // Root --> foo.com --> Cookies.
320 CookieTreeNode* parent = model->GetRoot()->GetChild(0)->GetChild(0);
321
322 // Reorder the nodes.
323 CookieTreeNode* node_e = parent->Remove(2);
324 CookieTreeNode* node_c = parent->Remove(1);
325 parent->Add(0, node_e);
326 parent->Add(2, node_c);
327
328 // Notify the observer of the reordering.
329 [controller_ modelObserver]->TreeNodeChildrenReordered(model, parent);
330
331 // Check the new order.
332 node = [cocoa_children objectAtIndex:0];
333 EXPECT_TRUE([@"E" isEqualToString:[node name]]);
334 node = [cocoa_children objectAtIndex:1];
335 EXPECT_TRUE([@"A" isEqualToString:[node name]]);
336 node = [cocoa_children objectAtIndex:2];
337 EXPECT_TRUE([@"C" isEqualToString:[node name]]);
338 }
339
340 TEST_F(CookiesWindowControllerTest, TreeNodeChanged) {
341 const GURL url = GURL("http://foo.com");
342 net::CookieMonster* cm = profile_.GetCookieMonster();
343 cm->SetCookie(url, "A=B");
344
345 controller_.reset(
346 [[CookiesWindowController alloc] initWithProfile:&profile_]);
347
348 CookiesTreeModel* model = [controller_ treeModel];
349 // Root --> foo.com --> Cookies.
350 CookieTreeNode* node = model->GetRoot()->GetChild(0)->GetChild(0);
351
352 // Root --> foo.com --> Cookies.
353 CocoaCookieTreeNode* cocoa_node =
354 [[[[[controller_ cocoaTreeModel] childs] objectAtIndex:0]
355 childs] objectAtIndex:0];
356
357 EXPECT_TRUE([@"Cookies" isEqualToString:[cocoa_node title]]);
358
359 // Fake update the cookie folder's title. This would never happen in reality,
360 // but it tests the code path that ultimately calls CocoaNodeFromTreeNode,
361 // which is tested elsewhere.
362 node->SetTitle(L"Silly Change");
363 [controller_ modelObserver]->TreeNodeChanged(model, node);
364
365 EXPECT_TRUE([@"Silly Change" isEqualToString:[cocoa_node title]]);
366 }
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/cookies_window_controller.mm ('k') | chrome/browser/cocoa/preferences_window_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698