1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/sys_string_conversions.h"
#include "chrome/browser/cocoa/cocoa_test_helper.h"
#include "chrome/browser/cocoa/cookie_prompt_window_controller.h"
#include "chrome/browser/cookie_modal_dialog.h"
// A mock class which implements just enough functionality to
// act as a radio with a pre-specified selected button.
@interface MockRadioButtonMatrix : NSObject {
@private
NSInteger selectedRow_;
}
- (NSInteger)selectedRow;
@end
@implementation MockRadioButtonMatrix
- (id)initWithSelectedRow:(NSInteger)selectedRow {
if ((self = [super init])) {
selectedRow_ = selectedRow;
}
return self;
}
- (NSInteger)selectedRow {
return selectedRow_;
}
@end
namespace {
// A subclass of the |CookiePromptModalDialog| that allows tests of
// some of the prompt window controller's functionality without having
// a full environment by overriding select methods and intercepting
// calls that would otherwise rely on behavior only present in a full
// environment.
class CookiePromptModalDialogMock : public CookiePromptModalDialog {
public:
CookiePromptModalDialogMock(const GURL& origin,
const std::string& cookie_line);
virtual void AllowSiteData(bool remember, bool session_expire);
virtual void BlockSiteData(bool remember);
bool allow() const { return allow_; }
bool remember() const { return remember_; }
private:
// The result of the block/unblock decision.
bool allow_;
// Whether the block/accept decision should be remembered.
bool remember_;
};
CookiePromptModalDialogMock::CookiePromptModalDialogMock(
const GURL& origin,
const std::string& cookie_line)
: CookiePromptModalDialog(NULL, NULL, origin, cookie_line, NULL),
allow_(false),
remember_(false) {
}
void CookiePromptModalDialogMock::AllowSiteData(bool remember,
bool session_expire) {
remember_ = remember;
allow_ = true;
}
void CookiePromptModalDialogMock::BlockSiteData(bool remember) {
remember_ = remember;
allow_ = false;
}
class CookiePromptWindowControllerTest : public CocoaTest {
};
TEST_F(CookiePromptWindowControllerTest, CreateForCookie) {
GURL url("http://chromium.org");
std::string cookieLine(
"PHPSESSID=0123456789abcdef0123456789abcdef; path=/");
scoped_ptr<CookiePromptModalDialog> dialog(
new CookiePromptModalDialog(NULL, NULL, url, cookieLine, NULL));
scoped_nsobject<CookiePromptWindowController> controller(
[[CookiePromptWindowController alloc] initWithDialog:dialog.get()]);
EXPECT_TRUE(controller.get());
EXPECT_TRUE([controller.get() window]);
}
TEST_F(CookiePromptWindowControllerTest, CreateForDatabase) {
GURL url("http://google.com");
string16 databaseName(base::SysNSStringToUTF16(@"some database"));
scoped_ptr<CookiePromptModalDialog> dialog(
new CookiePromptModalDialog(NULL, NULL, url, databaseName, NULL));
scoped_nsobject<CookiePromptWindowController> controller(
[[CookiePromptWindowController alloc] initWithDialog:dialog.get()]);
EXPECT_TRUE(controller.get());
EXPECT_TRUE([controller.get() window]);
}
TEST_F(CookiePromptWindowControllerTest, CreateForLocalStorage) {
GURL url("http://chromium.org");
string16 key(base::SysNSStringToUTF16(@"key"));
string16 value(base::SysNSStringToUTF16(@"value"));
scoped_ptr<CookiePromptModalDialog> dialog(
new CookiePromptModalDialog(NULL, NULL, url, key, value, NULL));
scoped_nsobject<CookiePromptWindowController> controller(
[[CookiePromptWindowController alloc] initWithDialog:dialog.get()]);
EXPECT_TRUE(controller.get());
EXPECT_TRUE([controller.get() window]);
}
TEST_F(CookiePromptWindowControllerTest, RememberMyChoiceAllow) {
GURL url("http://chromium.org");
std::string cookieLine(
"PHPSESSID=0123456789abcdef0123456789abcdef; path=/");
scoped_ptr<CookiePromptModalDialogMock> dialog(
new CookiePromptModalDialogMock(url, cookieLine));
scoped_nsobject<CookiePromptWindowController> controller(
[[CookiePromptWindowController alloc] initWithDialog:dialog.get()]);
scoped_nsobject<MockRadioButtonMatrix> checkbox([[MockRadioButtonMatrix alloc]
initWithSelectedRow:0]);
[controller.get() setValue:checkbox.get() forKey:@"radioGroupMatrix_"];
[controller.get() processModalDialogResult:dialog.get()
returnCode:NSAlertFirstButtonReturn];
// Need to make sure that the retainCount for the mock radio button
// goes back down to 1--the controller won't do it for us. And
// even calling setValue:forKey: again with a nil doesn't
// decrement it. Ugly, but otherwise valgrind complains.
[checkbox.get() release];
EXPECT_TRUE(dialog->remember());
EXPECT_TRUE(dialog->allow());
}
TEST_F(CookiePromptWindowControllerTest, RememberMyChoiceBlock) {
GURL url("http://codereview.chromium.org");
std::string cookieLine(
"PHPSESSID=0123456789abcdef0123456789abcdef; path=/");
scoped_ptr<CookiePromptModalDialogMock> dialog(
new CookiePromptModalDialogMock(url, cookieLine));
scoped_nsobject<CookiePromptWindowController> controller(
[[CookiePromptWindowController alloc] initWithDialog:dialog.get()]);
scoped_nsobject<MockRadioButtonMatrix> checkbox([[MockRadioButtonMatrix alloc]
initWithSelectedRow:0]);
[controller.get() setValue:checkbox.get() forKey:@"radioGroupMatrix_"];
[controller.get() processModalDialogResult:dialog.get()
returnCode:NSAlertSecondButtonReturn];
// Need to make sure that the retainCount for the mock radio button
// goes back down to 1--the controller won't do it for us. And
// even calling setValue:forKey: again with nil doesn't
// decrement it. Ugly, but otherwise valgrind complains.
[checkbox.get() release];
EXPECT_TRUE(dialog->remember());
EXPECT_FALSE(dialog->allow());
}
TEST_F(CookiePromptWindowControllerTest, DontRememberMyChoiceAllow) {
GURL url("http://chromium.org");
std::string cookieLine(
"PHPSESSID=0123456789abcdef0123456789abcdef; path=/");
scoped_ptr<CookiePromptModalDialogMock> dialog(
new CookiePromptModalDialogMock(url, cookieLine));
scoped_nsobject<CookiePromptWindowController> controller(
[[CookiePromptWindowController alloc] initWithDialog:dialog.get()]);
scoped_nsobject<MockRadioButtonMatrix> checkbox([[MockRadioButtonMatrix alloc]
initWithSelectedRow:1]);
[controller.get() setValue:checkbox.get() forKey:@"radioGroupMatrix_"];
[controller.get() processModalDialogResult:dialog.get()
returnCode:NSAlertFirstButtonReturn];
// Need to make sure that the retainCount for the mock radio button
// goes back down to 1--the controller won't do it for us. And
// even calling setValue:forKey: again with a nil doesn't
// decrement it. Ugly, but otherwise valgrind complains.
[checkbox.get() release];
EXPECT_FALSE(dialog->remember());
EXPECT_TRUE(dialog->allow());
}
TEST_F(CookiePromptWindowControllerTest, DontRememberMyChoiceBlock) {
GURL url("http://codereview.chromium.org");
std::string cookieLine(
"PHPSESSID=0123456789abcdef0123456789abcdef; path=/");
scoped_ptr<CookiePromptModalDialogMock> dialog(
new CookiePromptModalDialogMock(url, cookieLine));
scoped_nsobject<CookiePromptWindowController> controller(
[[CookiePromptWindowController alloc] initWithDialog:dialog.get()]);
scoped_nsobject<MockRadioButtonMatrix> checkbox([[MockRadioButtonMatrix alloc]
initWithSelectedRow:1]);
[controller.get() setValue:checkbox.get() forKey:@"radioGroupMatrix_"];
[controller.get() processModalDialogResult:dialog.get()
returnCode:NSAlertSecondButtonReturn];
// Need to make sure that the retainCount for the mock radio button
// goes back down to 1--the controller won't do it for us. And
// even calling setValue:forKey: again with a nil doesn't
// decrement it. Ugly, but otherwise valgrind complains.
[checkbox.get() release];
EXPECT_FALSE(dialog->remember());
EXPECT_FALSE(dialog->allow());
}
} // namespace
|