1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!-- TODO(arv): Check in Closue unit tests and make this run as part of the 5 tests --> 6 <script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.js"></script> 7 <script src="../cr.js"></script> 8 <script src="linkcontroller.js"></script> 9 <script> 10 11 goog.require('goog.testing.MockControl'); 12 goog.require('goog.testing.PropertyReplacer'); 13 goog.require('goog.testing.jsunit'); 14 goog.require('goog.testing.mockmatchers'); 15 16 </script> 17 </head> 18 <body> 19 <script> 20 21 var mockControl, propertyReplacer, mockWindow; 22 var chrome = chrome || {}; 23 chrome.tabs = chrome.tabs || {}; 24 chrome.windows = chrome.windows || {}; 25 26 var ObjectEqualsMatcher = goog.testing.mockmatchers.ObjectEquals; 27 var SaveArgumentMatcher = goog.testing.mockmatchers.SaveArgument; 28 29 var MSG = 'MSG'; 30 var localStrings = { 31 getStringF: function(msg, number) { 32 assertEquals('should_open_all', msg); 33 return MSG + number; 34 } 35 }; 36 37 var URL1 = 'http://chromium.org/'; 38 var URL2 = '#hash'; 39 var WINDOW_ID = 'WINDOW_ID'; 40 41 function setUp() { 42 mockControl = new goog.testing.MockControl; 43 chrome.tabs.create = mockControl.createFunctionMock(); 44 chrome.windows.create = mockControl.createFunctionMock(); 45 46 propertyReplacer = new goog.testing.PropertyReplacer; 47 48 mockWindow = { 49 confirm: mockControl.createFunctionMock(), 50 location: { 51 get href() { 52 return 'http://www.google.com/'; 53 }, 54 set href(url) { 55 assertEquals(URL1, url); 56 } 57 } 58 }; 59 } 60 61 function tearDown() { 62 mockControl.$tearDown(); 63 propertyReplacer.reset(); 64 } 65 66 function testGetWarningMessage() { 67 var lc = new cr.LinkController(localStrings); 68 var msg = lc.getWarningMessage(10); 69 assertEquals(MSG + 10, msg); 70 } 71 72 function openUrlFromEventHelper(event, isMac, expectedKind) { 73 var lc = new cr.LinkController(localStrings); 74 75 lc.openUrls = mockControl.createFunctionMock(); 76 lc.openUrls([URL1], expectedKind); 77 78 propertyReplacer.set(cr, 'isMac', isMac); 79 80 mockControl.$replayAll(); 81 82 lc.openUrlFromEvent(URL1, event); 83 84 mockControl.$verifyAll(); 85 } 86 87 /////////////////////////////////////////////////////////////////////////////// 88 89 function testOpenUrlFromEventForegroundTab() { 90 var e = { 91 type: 'click', 92 button: 0, 93 shiftKey: true, 94 ctrlKey: true 95 }; 96 openUrlFromEventHelper(e, false, cr.LinkKind.FOREGROUND_TAB); 97 } 98 99 function testOpenUrlFromEventForegroundTabMac() { 100 var e = { 101 type: 'click', 102 button: 0, 103 shiftKey: true, 104 metaKey: true 105 }; 106 openUrlFromEventHelper(e, true, cr.LinkKind.FOREGROUND_TAB); 107 } 108 109 function testOpenUrlFromEventForegroundTabEnter() { 110 var e = { 111 type: 'keydown', 112 keyIdentifier: 'Enter', 113 shiftKey: true, 114 ctrlKey: true 115 }; 116 openUrlFromEventHelper(e, false, cr.LinkKind.FOREGROUND_TAB); 117 } 118 119 function testOpenUrlFromEventForegroundTabEnterMac() { 120 var e = { 121 type: 'keydown', 122 keyIdentifier: 'Enter', 123 shiftKey: true, 124 metaKey: true 125 }; 126 openUrlFromEventHelper(e, true, cr.LinkKind.FOREGROUND_TAB); 127 } 128 129 function testOpenUrlFromEventForegroundTabMiddleClick() { 130 var e = { 131 type: 'click', 132 button: 1, 133 shiftKey: true 134 }; 135 openUrlFromEventHelper(e, false, cr.LinkKind.FOREGROUND_TAB); 136 } 137 138 /////////////////////////////////////////////////////////////////////////////// 139 140 function testOpenUrlFromEventBackgroundTab() { 141 var e = { 142 type: 'click', 143 button: 0, 144 ctrlKey: true 145 }; 146 openUrlFromEventHelper(e, false, cr.LinkKind.BACKGROUND_TAB); 147 } 148 149 function testOpenUrlFromEventBackgroundTabMac() { 150 var e = { 151 type: 'click', 152 button: 0, 153 metaKey: true 154 }; 155 openUrlFromEventHelper(e, true, cr.LinkKind.BACKGROUND_TAB); 156 } 157 158 function testOpenUrlFromEventBackgroundTabEnter() { 159 var e = { 160 type: 'keydown', 161 keyIdentifier: 'Enter', 162 ctrlKey: true 163 }; 164 openUrlFromEventHelper(e, false, cr.LinkKind.BACKGROUND_TAB); 165 } 166 167 function testOpenUrlFromEventBackgroundTabEnterMac() { 168 var e = { 169 type: 'keydown', 170 keyIdentifier: 'Enter', 171 metaKey: true 172 }; 173 openUrlFromEventHelper(e, true, cr.LinkKind.BACKGROUND_TAB); 174 } 175 176 function testOpenUrlFromEventBackgroundTabMiddleClick() { 177 var e = { 178 type: 'click', 179 button: 1 180 }; 181 openUrlFromEventHelper(e, false, cr.LinkKind.BACKGROUND_TAB); 182 } 183 184 /////////////////////////////////////////////////////////////////////////////// 185 186 function testOpenUrlFromEventWindow() { 187 var e = { 188 type: 'click', 189 button: 0, 190 shiftKey: true 191 }; 192 openUrlFromEventHelper(e, false, cr.LinkKind.WINDOW); 193 } 194 195 function testOpenUrlFromEventWindowEnter() { 196 var e = { 197 type: 'keydown', 198 keyIdentifier: 'Enter', 199 shiftKey: true 200 }; 201 openUrlFromEventHelper(e, false, cr.LinkKind.WINDOW); 202 } 203 204 /////////////////////////////////////////////////////////////////////////////// 205 206 function testOpenUrlFromEventSelf() { 207 var e = { 208 type: 'click', 209 button: 0 210 }; 211 openUrlFromEventHelper(e, false, cr.LinkKind.SELF); 212 } 213 214 function testOpenUrlFromEventSelfEnter() { 215 var e = { 216 type: 'keydown', 217 keyIdentifier: 'Enter' 218 }; 219 openUrlFromEventHelper(e, false, cr.LinkKind.SELF); 220 } 221 222 /////////////////////////////////////////////////////////////////////////////// 223 224 function testOpenUrl() { 225 var lc = new cr.LinkController(localStrings); 226 lc.openUrls = mockControl.createFunctionMock(); 227 228 lc.openUrls(new ObjectEqualsMatcher([URL1]), cr.LinkKind.SELF); 229 mockControl.$replayAll(); 230 231 lc.openUrl(URL1, cr.LinkKind.SELF); 232 233 mockControl.$verifyAll(); 234 } 235 236 /////////////////////////////// OpenUrls ////////////////////////////////////// 237 238 function testOpenUrlsTooFew() { 239 var lc = new cr.LinkController(localStrings); 240 241 mockControl.$replayAll(); 242 243 lc.openUrls([], cr.LinkKind.SELF); 244 245 mockControl.$verifyAll(); 246 } 247 248 249 function testOpenUrlsTooMany() { 250 var lc = new cr.LinkController(localStrings); 251 lc.warningLimit = 9; 252 253 var urls = new Array(lc.warningLimit + 1); 254 lc.window = mockWindow; 255 256 lc.window.confirm('MSG10').$returns(false); 257 mockControl.$replayAll(); 258 259 lc.openUrls(urls, cr.LinkKind.SELF); 260 261 mockControl.$verifyAll(); 262 } 263 264 function testOpenUrlsSelf() { 265 var lc = new cr.LinkController(localStrings); 266 lc.window = mockWindow; 267 268 mockControl.$replayAll(); 269 270 lc.openUrls([URL1], cr.LinkKind.SELF); 271 272 mockControl.$verifyAll(); 273 } 274 275 function testOpenUrlsForegroundTab() { 276 var lc = new cr.LinkController(localStrings); 277 lc.window = mockWindow; 278 279 chrome.tabs.create(new ObjectEqualsMatcher({url: URL1, selected: true})); 280 chrome.tabs.create(new ObjectEqualsMatcher({ 281 url: 'http://www.google.com/#hash', 282 selected: false 283 })); 284 285 mockControl.$replayAll(); 286 287 lc.openUrls([URL1, URL2], cr.LinkKind.FOREGROUND_TAB); 288 289 mockControl.$verifyAll(); 290 } 291 292 function testOpenUrlsBackgroundTab() { 293 var lc = new cr.LinkController(localStrings); 294 lc.window = mockWindow; 295 296 chrome.tabs.create(new ObjectEqualsMatcher({url: URL1, selected: false})); 297 chrome.tabs.create(new ObjectEqualsMatcher({ 298 url: 'http://www.google.com/#hash', 299 selected: false 300 })); 301 302 mockControl.$replayAll(); 303 304 lc.openUrls([URL1, URL2], cr.LinkKind.BACKGROUND_TAB); 305 306 mockControl.$verifyAll(); 307 } 308 309 function testOpenUrlsWindow() { 310 var lc = new cr.LinkController(localStrings); 311 lc.window = mockWindow; 312 313 var callbackMatcher = new SaveArgumentMatcher; 314 chrome.windows.create(new ObjectEqualsMatcher({url: URL1, incognito: false}), 315 callbackMatcher); 316 chrome.tabs.create(new ObjectEqualsMatcher({ 317 url: 'http://www.google.com/#hash', 318 windowId: WINDOW_ID, 319 selected: false 320 })); 321 322 mockControl.$replayAll(); 323 324 lc.openUrls([URL1, URL2], cr.LinkKind.WINDOW); 325 callbackMatcher.arg({id: WINDOW_ID}); 326 327 mockControl.$verifyAll(); 328 } 329 330 function testOpenUrlsIncognito() { 331 var lc = new cr.LinkController(localStrings); 332 lc.window = mockWindow; 333 334 var callbackMatcher = new SaveArgumentMatcher; 335 chrome.windows.create(new ObjectEqualsMatcher({url: URL1, incognito: true}), 336 callbackMatcher); 337 chrome.tabs.create(new ObjectEqualsMatcher({ 338 url: 'http://www.google.com/#hash', 339 windowId: WINDOW_ID, 340 selected: false 341 })); 342 343 mockControl.$replayAll(); 344 345 lc.openUrls([URL1, URL2], cr.LinkKind.INCOGNITO); 346 callbackMatcher.arg({id: WINDOW_ID}); 347 348 mockControl.$verifyAll(); 349 } 350 351 </script> 352 </body> 353 </html> 354