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