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

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

Issue 525106: Move code duplicated in two tests up into the TestingProfile. (Closed)
Patch Set: 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
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009-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 #import <Cocoa/Cocoa.h> 5 #import <Cocoa/Cocoa.h>
6 6
7 #include "app/tree_model.h" 7 #include "app/tree_model.h"
8 #import "base/scoped_nsobject.h" 8 #import "base/scoped_nsobject.h"
9 #include "base/scoped_ptr.h" 9 #include "base/scoped_ptr.h"
10 #include "chrome/browser/cocoa/browser_test_helper.h"
10 #import "chrome/browser/cocoa/cookies_window_controller.h" 11 #import "chrome/browser/cocoa/cookies_window_controller.h"
11 #include "chrome/browser/cocoa/cocoa_test_helper.h" 12 #include "chrome/browser/cocoa/cocoa_test_helper.h"
12 #include "chrome/browser/net/url_request_context_getter.h" 13 #include "chrome/browser/net/url_request_context_getter.h"
13 #include "chrome/browser/cookies_tree_model.h" 14 #include "chrome/browser/cookies_tree_model.h"
14 #include "chrome/test/testing_profile.h" 15 #include "chrome/test/testing_profile.h"
15 #include "googleurl/src/gurl.h" 16 #include "googleurl/src/gurl.h"
16 #include "net/url_request/url_request_context.h" 17 #include "net/url_request/url_request_context.h"
17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
18 #include "testing/platform_test.h" 19 #include "testing/platform_test.h"
19 20
(...skipping 26 matching lines...) Expand all
46 // create this hacky secondary getter. 47 // create this hacky secondary getter.
47 @interface CocoaCookieTreeNode (UglyHacks) 48 @interface CocoaCookieTreeNode (UglyHacks)
48 - (NSMutableArray*)childs; 49 - (NSMutableArray*)childs;
49 @end 50 @end
50 @implementation CocoaCookieTreeNode (UglyHacks) 51 @implementation CocoaCookieTreeNode (UglyHacks)
51 - (NSMutableArray*)childs { 52 - (NSMutableArray*)childs {
52 return children_.get(); 53 return children_.get();
53 } 54 }
54 @end 55 @end
55 56
56 // Copied from src/chrome/cookies_tree_model_unittest.cc.
57 namespace { 57 namespace {
58 58
59 class TestURLRequestContext : public URLRequestContext {
60 public:
61 TestURLRequestContext() {
62 cookie_store_ = new net::CookieMonster();
63 }
64 };
65
66 class TestURLRequestContextGetter : public URLRequestContextGetter {
67 public:
68 virtual URLRequestContext* GetURLRequestContext() {
69 if (!context_)
70 context_ = new TestURLRequestContext();
71 return context_.get();
72 }
73 private:
74 scoped_refptr<URLRequestContext> context_;
75 };
76
77 class CookieTestingProfile : public TestingProfile {
78 public:
79 virtual URLRequestContextGetter* GetRequestContext() {
80 if (!url_request_context_getter_.get())
81 url_request_context_getter_ = new TestURLRequestContextGetter();
82 return url_request_context_getter_.get();
83 }
84 virtual ~CookieTestingProfile() {}
85
86 net::CookieMonster* GetCookieMonster() {
87 return GetRequestContext()->GetCookieStore()->GetCookieMonster();
88 }
89
90 private:
91 scoped_refptr<URLRequestContextGetter> url_request_context_getter_;
92 };
93
94 class CookiesWindowControllerTest : public CocoaTest { 59 class CookiesWindowControllerTest : public CocoaTest {
95 public: 60 public:
96 virtual void SetUp() { 61 virtual void SetUp() {
97 CocoaTest::SetUp(); 62 CocoaTest::SetUp();
63 TestingProfile* profile = browser_helper_.profile();
64 profile->CreateRequestContext();
98 controller_.reset( 65 controller_.reset(
99 [[CookiesWindowController alloc] initWithProfile:&profile_]); 66 [[CookiesWindowController alloc] initWithProfile:profile]);
100 } 67 }
101 68
102 virtual void TearDown() { 69 virtual void TearDown() {
103 CocoaTest::TearDown(); 70 CocoaTest::TearDown();
104 } 71 }
105 72
106 CocoaCookieTreeNode* CocoaNodeFromTreeNode(TreeModelNode* node, 73 CocoaCookieTreeNode* CocoaNodeFromTreeNode(TreeModelNode* node,
107 bool recurse) { 74 bool recurse) {
108 return [controller_ modelObserver]->CocoaNodeFromTreeNode(node, recurse); 75 return [controller_ modelObserver]->CocoaNodeFromTreeNode(node, recurse);
109 } 76 }
110 77
111 CocoaCookieTreeNode* FindCocoaNode(TreeModelNode* node, 78 CocoaCookieTreeNode* FindCocoaNode(TreeModelNode* node,
112 CocoaCookieTreeNode* start) { 79 CocoaCookieTreeNode* start) {
113 return [controller_ modelObserver]->FindCocoaNode(node, start); 80 return [controller_ modelObserver]->FindCocoaNode(node, start);
114 } 81 }
115 82
116 CookieTestingProfile profile_; 83 protected:
84 BrowserTestHelper browser_helper_;
117 scoped_nsobject<CookiesWindowController> controller_; 85 scoped_nsobject<CookiesWindowController> controller_;
118 }; 86 };
119 87
120 TEST_F(CookiesWindowControllerTest, Construction) { 88 TEST_F(CookiesWindowControllerTest, Construction) {
121 std::vector<SkBitmap> skia_icons; 89 std::vector<SkBitmap> skia_icons;
122 [controller_ treeModel]->GetIcons(&skia_icons); 90 [controller_ treeModel]->GetIcons(&skia_icons);
123 91
124 EXPECT_EQ([[controller_ icons] count], skia_icons.size() + 1U); 92 EXPECT_EQ([[controller_ icons] count], skia_icons.size() + 1U);
125 } 93 }
126 94
(...skipping 30 matching lines...) Expand all
157 [[FakeCocoaCookieTreeNode alloc] initWithTreeNode:child1.get()]); 125 [[FakeCocoaCookieTreeNode alloc] initWithTreeNode:child1.get()]);
158 scoped_nsobject<FakeCocoaCookieTreeNode> cocoaChild2( 126 scoped_nsobject<FakeCocoaCookieTreeNode> cocoaChild2(
159 [[FakeCocoaCookieTreeNode alloc] initWithTreeNode:child2.get()]); 127 [[FakeCocoaCookieTreeNode alloc] initWithTreeNode:child2.get()]);
160 [[cocoaParent childs] addObject:cocoaChild1.get()]; 128 [[cocoaParent childs] addObject:cocoaChild1.get()];
161 [[cocoaChild1 childs] addObject:cocoaChild2.get()]; 129 [[cocoaChild1 childs] addObject:cocoaChild2.get()];
162 130
163 EXPECT_EQ(cocoaChild2.get(), FindCocoaNode(child2.get(), cocoaParent.get())); 131 EXPECT_EQ(cocoaChild2.get(), FindCocoaNode(child2.get(), cocoaParent.get()));
164 } 132 }
165 133
166 TEST_F(CookiesWindowControllerTest, CocoaNodeFromTreeNodeCookie) { 134 TEST_F(CookiesWindowControllerTest, CocoaNodeFromTreeNodeCookie) {
167 net::CookieMonster* cm = profile_.GetCookieMonster(); 135 net::CookieMonster* cm = browser_helper_.profile()->GetCookieMonster();
168 cm->SetCookie(GURL("http://foo.com"), "A=B"); 136 cm->SetCookie(GURL("http://foo.com"), "A=B");
169 CookiesTreeModel model(&profile_); 137 CookiesTreeModel model(browser_helper_.profile());
170 138
171 // Root --> foo.com --> Cookies --> A. Create node for 'A'. 139 // Root --> foo.com --> Cookies --> A. Create node for 'A'.
172 TreeModelNode* node = model.GetRoot()->GetChild(0)->GetChild(0)->GetChild(0); 140 TreeModelNode* node = model.GetRoot()->GetChild(0)->GetChild(0)->GetChild(0);
173 CocoaCookieTreeNode* cookie = CocoaNodeFromTreeNode(node, false); 141 CocoaCookieTreeNode* cookie = CocoaNodeFromTreeNode(node, false);
174 142
175 EXPECT_TRUE([@"B" isEqualToString:[cookie content]]); 143 EXPECT_TRUE([@"B" isEqualToString:[cookie content]]);
176 EXPECT_TRUE([@"End of the session" isEqualToString:[cookie expires]]); 144 EXPECT_TRUE([@"End of the session" isEqualToString:[cookie expires]]);
177 EXPECT_TRUE([@"Any kind of connection" isEqualToString:[cookie sendFor]]); 145 EXPECT_TRUE([@"Any kind of connection" isEqualToString:[cookie sendFor]]);
178 EXPECT_TRUE([@"A" isEqualToString:[cookie title]]); 146 EXPECT_TRUE([@"A" isEqualToString:[cookie title]]);
179 EXPECT_TRUE([@"A" isEqualToString:[cookie name]]); 147 EXPECT_TRUE([@"A" isEqualToString:[cookie name]]);
180 EXPECT_TRUE([@"/" isEqualToString:[cookie path]]); 148 EXPECT_TRUE([@"/" isEqualToString:[cookie path]]);
181 EXPECT_EQ(0U, [[cookie childs] count]); 149 EXPECT_EQ(0U, [[cookie childs] count]);
182 EXPECT_TRUE([cookie created]); 150 EXPECT_TRUE([cookie created]);
183 EXPECT_TRUE([cookie isLeaf]); 151 EXPECT_TRUE([cookie isLeaf]);
184 EXPECT_EQ(node, [cookie treeNode]); 152 EXPECT_EQ(node, [cookie treeNode]);
185 } 153 }
186 154
187 TEST_F(CookiesWindowControllerTest, CocoaNodeFromTreeNodeRecursive) { 155 TEST_F(CookiesWindowControllerTest, CocoaNodeFromTreeNodeRecursive) {
188 net::CookieMonster* cm = profile_.GetCookieMonster(); 156 net::CookieMonster* cm = browser_helper_.profile()->GetCookieMonster();
189 cm->SetCookie(GURL("http://foo.com"), "A=B"); 157 cm->SetCookie(GURL("http://foo.com"), "A=B");
190 CookiesTreeModel model(&profile_); 158 CookiesTreeModel model(browser_helper_.profile());
191 159
192 // Root --> foo.com --> Cookies --> A. Create node for 'foo.com'. 160 // Root --> foo.com --> Cookies --> A. Create node for 'foo.com'.
193 CookieTreeNode* node = model.GetRoot()->GetChild(0); 161 CookieTreeNode* node = model.GetRoot()->GetChild(0);
194 CocoaCookieTreeNode* domain = CocoaNodeFromTreeNode(node, true); 162 CocoaCookieTreeNode* domain = CocoaNodeFromTreeNode(node, true);
195 CocoaCookieTreeNode* cookies = [[domain childs] objectAtIndex:0]; 163 CocoaCookieTreeNode* cookies = [[domain childs] objectAtIndex:0];
196 CocoaCookieTreeNode* cookie = [[cookies childs] objectAtIndex:0]; 164 CocoaCookieTreeNode* cookie = [[cookies childs] objectAtIndex:0];
197 165
198 // Test domain-level node. 166 // Test domain-level node.
199 EXPECT_TRUE([@"foo.com" isEqualToString:[domain title]]); 167 EXPECT_TRUE([@"foo.com" isEqualToString:[domain title]]);
200 168
(...skipping 16 matching lines...) Expand all
217 EXPECT_TRUE([@"/" isEqualToString:[cookie path]]); 185 EXPECT_TRUE([@"/" isEqualToString:[cookie path]]);
218 EXPECT_TRUE([@"foo.com" isEqualToString:[cookie domain]]); 186 EXPECT_TRUE([@"foo.com" isEqualToString:[cookie domain]]);
219 EXPECT_EQ(0U, [[cookie childs] count]); 187 EXPECT_EQ(0U, [[cookie childs] count]);
220 EXPECT_TRUE([cookie created]); 188 EXPECT_TRUE([cookie created]);
221 EXPECT_TRUE([cookie isLeaf]); 189 EXPECT_TRUE([cookie isLeaf]);
222 EXPECT_EQ(node->GetChild(0)->GetChild(0), [cookie treeNode]); 190 EXPECT_EQ(node->GetChild(0)->GetChild(0), [cookie treeNode]);
223 } 191 }
224 192
225 TEST_F(CookiesWindowControllerTest, TreeNodesAdded) { 193 TEST_F(CookiesWindowControllerTest, TreeNodesAdded) {
226 const GURL url = GURL("http://foo.com"); 194 const GURL url = GURL("http://foo.com");
227 net::CookieMonster* cm = profile_.GetCookieMonster(); 195 TestingProfile* profile = browser_helper_.profile();
196 net::CookieMonster* cm = profile->GetCookieMonster();
228 cm->SetCookie(url, "A=B"); 197 cm->SetCookie(url, "A=B");
229 198
230 controller_.reset( 199 controller_.reset(
231 [[CookiesWindowController alloc] initWithProfile:&profile_]); 200 [[CookiesWindowController alloc] initWithProfile:profile]);
232 201
233 // Root --> foo.com --> Cookies. 202 // Root --> foo.com --> Cookies.
234 NSMutableArray* cocoa_children = 203 NSMutableArray* cocoa_children =
235 [[[[[[controller_ cocoaTreeModel] childs] objectAtIndex:0] 204 [[[[[[controller_ cocoaTreeModel] childs] objectAtIndex:0]
236 childs] objectAtIndex:0] childs]; 205 childs] objectAtIndex:0] childs];
237 EXPECT_EQ(1U, [cocoa_children count]); 206 EXPECT_EQ(1U, [cocoa_children count]);
238 207
239 // Create some cookies. 208 // Create some cookies.
240 cm->SetCookie(url, "C=D"); 209 cm->SetCookie(url, "C=D");
241 cm->SetCookie(url, "E=F"); 210 cm->SetCookie(url, "E=F");
(...skipping 13 matching lines...) Expand all
255 224
256 // Manually notify the observer. 225 // Manually notify the observer.
257 [controller_ modelObserver]->TreeNodesAdded(model, parent, 1, 2); 226 [controller_ modelObserver]->TreeNodesAdded(model, parent, 1, 2);
258 227
259 // Check that we have created 2 more Cocoa nodes. 228 // Check that we have created 2 more Cocoa nodes.
260 EXPECT_EQ(3U, [cocoa_children count]); 229 EXPECT_EQ(3U, [cocoa_children count]);
261 } 230 }
262 231
263 TEST_F(CookiesWindowControllerTest, TreeNodesRemoved) { 232 TEST_F(CookiesWindowControllerTest, TreeNodesRemoved) {
264 const GURL url = GURL("http://foo.com"); 233 const GURL url = GURL("http://foo.com");
265 net::CookieMonster* cm = profile_.GetCookieMonster(); 234 TestingProfile* profile = browser_helper_.profile();
235 net::CookieMonster* cm = profile->GetCookieMonster();
266 cm->SetCookie(url, "A=B"); 236 cm->SetCookie(url, "A=B");
267 cm->SetCookie(url, "C=D"); 237 cm->SetCookie(url, "C=D");
268 cm->SetCookie(url, "E=F"); 238 cm->SetCookie(url, "E=F");
269 239
270 controller_.reset( 240 controller_.reset(
271 [[CookiesWindowController alloc] initWithProfile:&profile_]); 241 [[CookiesWindowController alloc] initWithProfile:profile]);
272 242
273 // Root --> foo.com --> Cookies. 243 // Root --> foo.com --> Cookies.
274 NSMutableArray* cocoa_children = 244 NSMutableArray* cocoa_children =
275 [[[[[[controller_ cocoaTreeModel] childs] objectAtIndex:0] 245 [[[[[[controller_ cocoaTreeModel] childs] objectAtIndex:0]
276 childs] objectAtIndex:0] childs]; 246 childs] objectAtIndex:0] childs];
277 EXPECT_EQ(3U, [cocoa_children count]); 247 EXPECT_EQ(3U, [cocoa_children count]);
278 248
279 CookiesTreeModel* model = [controller_ treeModel]; 249 CookiesTreeModel* model = [controller_ treeModel];
280 // Root --> foo.com --> Cookies. 250 // Root --> foo.com --> Cookies.
281 CookieTreeNode* parent = model->GetRoot()->GetChild(0)->GetChild(0); 251 CookieTreeNode* parent = model->GetRoot()->GetChild(0)->GetChild(0);
282 252
283 // Pretend to remove the nodes. 253 // Pretend to remove the nodes.
284 [controller_ modelObserver]->TreeNodesRemoved(model, parent, 1, 2); 254 [controller_ modelObserver]->TreeNodesRemoved(model, parent, 1, 2);
285 255
286 EXPECT_EQ(1U, [cocoa_children count]); 256 EXPECT_EQ(1U, [cocoa_children count]);
287 257
288 NSString* title = [[cocoa_children objectAtIndex:0] name]; 258 NSString* title = [[cocoa_children objectAtIndex:0] name];
289 EXPECT_TRUE([@"A" isEqualToString:title]); 259 EXPECT_TRUE([@"A" isEqualToString:title]);
290 } 260 }
291 261
292 TEST_F(CookiesWindowControllerTest, TreeNodeChildrenReordered) { 262 TEST_F(CookiesWindowControllerTest, TreeNodeChildrenReordered) {
293 const GURL url = GURL("http://foo.com"); 263 const GURL url = GURL("http://foo.com");
294 net::CookieMonster* cm = profile_.GetCookieMonster(); 264 TestingProfile* profile = browser_helper_.profile();
265 net::CookieMonster* cm = profile->GetCookieMonster();
295 cm->SetCookie(url, "A=B"); 266 cm->SetCookie(url, "A=B");
296 cm->SetCookie(url, "C=D"); 267 cm->SetCookie(url, "C=D");
297 cm->SetCookie(url, "E=F"); 268 cm->SetCookie(url, "E=F");
298 269
299 controller_.reset( 270 controller_.reset(
300 [[CookiesWindowController alloc] initWithProfile:&profile_]); 271 [[CookiesWindowController alloc] initWithProfile:profile]);
301 272
302 // Root --> foo.com --> Cookies. 273 // Root --> foo.com --> Cookies.
303 NSMutableArray* cocoa_children = 274 NSMutableArray* cocoa_children =
304 [[[[[[controller_ cocoaTreeModel] childs] objectAtIndex:0] 275 [[[[[[controller_ cocoaTreeModel] childs] objectAtIndex:0]
305 childs] objectAtIndex:0] childs]; 276 childs] objectAtIndex:0] childs];
306 EXPECT_EQ(3U, [cocoa_children count]); 277 EXPECT_EQ(3U, [cocoa_children count]);
307 278
308 // Check default ordering. 279 // Check default ordering.
309 CocoaCookieTreeNode* node = [cocoa_children objectAtIndex:0]; 280 CocoaCookieTreeNode* node = [cocoa_children objectAtIndex:0];
310 EXPECT_TRUE([@"A" isEqualToString:[node name]]); 281 EXPECT_TRUE([@"A" isEqualToString:[node name]]);
(...skipping 19 matching lines...) Expand all
330 node = [cocoa_children objectAtIndex:0]; 301 node = [cocoa_children objectAtIndex:0];
331 EXPECT_TRUE([@"E" isEqualToString:[node name]]); 302 EXPECT_TRUE([@"E" isEqualToString:[node name]]);
332 node = [cocoa_children objectAtIndex:1]; 303 node = [cocoa_children objectAtIndex:1];
333 EXPECT_TRUE([@"A" isEqualToString:[node name]]); 304 EXPECT_TRUE([@"A" isEqualToString:[node name]]);
334 node = [cocoa_children objectAtIndex:2]; 305 node = [cocoa_children objectAtIndex:2];
335 EXPECT_TRUE([@"C" isEqualToString:[node name]]); 306 EXPECT_TRUE([@"C" isEqualToString:[node name]]);
336 } 307 }
337 308
338 TEST_F(CookiesWindowControllerTest, TreeNodeChanged) { 309 TEST_F(CookiesWindowControllerTest, TreeNodeChanged) {
339 const GURL url = GURL("http://foo.com"); 310 const GURL url = GURL("http://foo.com");
340 net::CookieMonster* cm = profile_.GetCookieMonster(); 311 TestingProfile* profile = browser_helper_.profile();
312 net::CookieMonster* cm = profile->GetCookieMonster();
341 cm->SetCookie(url, "A=B"); 313 cm->SetCookie(url, "A=B");
342 314
343 controller_.reset( 315 controller_.reset(
344 [[CookiesWindowController alloc] initWithProfile:&profile_]); 316 [[CookiesWindowController alloc] initWithProfile:profile]);
345 317
346 CookiesTreeModel* model = [controller_ treeModel]; 318 CookiesTreeModel* model = [controller_ treeModel];
347 // Root --> foo.com --> Cookies. 319 // Root --> foo.com --> Cookies.
348 CookieTreeNode* node = model->GetRoot()->GetChild(0)->GetChild(0); 320 CookieTreeNode* node = model->GetRoot()->GetChild(0)->GetChild(0);
349 321
350 // Root --> foo.com --> Cookies. 322 // Root --> foo.com --> Cookies.
351 CocoaCookieTreeNode* cocoa_node = 323 CocoaCookieTreeNode* cocoa_node =
352 [[[[[controller_ cocoaTreeModel] childs] objectAtIndex:0] 324 [[[[[controller_ cocoaTreeModel] childs] objectAtIndex:0]
353 childs] objectAtIndex:0]; 325 childs] objectAtIndex:0];
354 326
355 EXPECT_TRUE([@"Cookies" isEqualToString:[cocoa_node title]]); 327 EXPECT_TRUE([@"Cookies" isEqualToString:[cocoa_node title]]);
356 328
357 // Fake update the cookie folder's title. This would never happen in reality, 329 // Fake update the cookie folder's title. This would never happen in reality,
358 // but it tests the code path that ultimately calls CocoaNodeFromTreeNode, 330 // but it tests the code path that ultimately calls CocoaNodeFromTreeNode,
359 // which is tested elsewhere. 331 // which is tested elsewhere.
360 node->SetTitle(L"Silly Change"); 332 node->SetTitle(L"Silly Change");
361 [controller_ modelObserver]->TreeNodeChanged(model, node); 333 [controller_ modelObserver]->TreeNodeChanged(model, node);
362 334
363 EXPECT_TRUE([@"Silly Change" isEqualToString:[cocoa_node title]]); 335 EXPECT_TRUE([@"Silly Change" isEqualToString:[cocoa_node title]]);
364 } 336 }
365 337
366 } // namespace 338 } // namespace
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/cookies_tree_model_unittest.cc » ('j') | chrome/test/testing_profile.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698