UNPKG

sku-pg

Version:

Skulpt is a Javascript implementation of Python 2.x. Python that runs in your browser!

443 lines (385 loc) 14.1 kB
<!DOCTYPE html> <html> <!-- Copyright 2010 The Closure Library Authors. All Rights Reserved. Use of this source code is governed by an Apache 2.0 License. See the COPYING file for details. --> <!-- Author: marcosalmeida@google.com (Marcos Almeida) --> <head> <title>goog.ui.editor.LinkDialog Tests</title> <script src="../../base.js"></script> <script> goog.require('goog.dom'); goog.require('goog.dom.DomHelper'); goog.require('goog.editor.BrowserFeature'); goog.require('goog.editor.Link'); goog.require('goog.events.Event'); goog.require('goog.events.EventHandler'); goog.require('goog.math.Size'); goog.require('goog.testing.MockControl'); goog.require('goog.testing.PropertyReplacer'); goog.require('goog.testing.dom'); goog.require('goog.testing.events'); goog.require('goog.testing.jsunit'); goog.require('goog.testing.mockmatchers'); goog.require('goog.testing.mockmatchers.ArgumentMatcher'); goog.require('goog.ui.editor.AbstractDialog.EventType'); goog.require('goog.ui.editor.LinkDialog'); goog.require('goog.ui.editor.LinkDialog.OkEvent'); goog.require('goog.ui.editor.messages'); </script> <link rel="stylesheet" href="../../css/dialog.css"/> <link rel="stylesheet" href="../../css/linkbutton.css"/> <link rel="stylesheet" href="../../css/editor/dialog.css"/> <link rel="stylesheet" href="../../css/editor/linkdialog.css"/> </head> <body> <iframe id="appWindowIframe" src="javascript:''"></iframe> <script> var dialog; var mockCtrl; var mockLink; var mockOkHandler; var mockGetViewportSize; var mockWindowOpen; var isNew; var anchorElem; var stubs = new goog.testing.PropertyReplacer(); var ANCHOR_TEXT = 'anchor text'; var ANCHOR_URL = 'http://www.google.com/'; var ANCHOR_EMAIL = 'm@r.cos'; var ANCHOR_MAILTO = 'mailto:' + ANCHOR_EMAIL; function setUp() { anchorElem = goog.dom.createElement(goog.dom.TagName.A); goog.dom.appendChild(goog.dom.getDocument().body, anchorElem); mockCtrl = new goog.testing.MockControl(); mockLink = mockCtrl.createLooseMock(goog.editor.Link); mockOkHandler = mockCtrl.createLooseMock(goog.events.EventHandler); isNew = false; mockLink.isNew(); mockLink.$anyTimes(); mockLink.$does(function() { return isNew; }); mockLink.getCurrentText(); mockLink.$anyTimes(); mockLink.$does(function() { return anchorElem.innerHTML; }); mockLink.setTextAndUrl(goog.testing.mockmatchers.isString, goog.testing.mockmatchers.isString); mockLink.$anyTimes(); mockLink.$does(function(text, url) { anchorElem.innerHTML = text; anchorElem.href = url; }); mockLink.$registerArgumentListVerifier('placeCursorRightOf', function() { return true; }); mockLink.placeCursorRightOf(goog.testing.mockmatchers.iBoolean); mockLink.$anyTimes(); mockLink.getAnchor(); mockLink.$anyTimes(); mockLink.$returns(anchorElem); mockWindowOpen = mockCtrl.createFunctionMock('open'); stubs.set(window, 'open', mockWindowOpen); } function tearDown() { dialog.dispose(); goog.dom.removeNode(anchorElem); stubs.reset(); } function setUpAnchor(href, text, opt_isNew) { anchorElem.href = href; anchorElem.innerHTML = text; isNew = !!opt_isNew; } /** * Creates and shows the dialog to be tested. * @param {Document} opt_document Document to render the dialog into. Defaults * to the main window's document. */ function createAndShow(opt_document) { dialog = new goog.ui.editor.LinkDialog(new goog.dom.DomHelper(opt_document), mockLink); dialog.addEventListener(goog.ui.editor.AbstractDialog.EventType.OK, mockOkHandler); dialog.show(); } /** * Sets up the mock event handler to expect an OK event with the given text * and url. */ function expectOk(linkText, linkUrl) { mockOkHandler.handleEvent(new goog.testing.mockmatchers.ArgumentMatcher( function(arg) { return arg.type == goog.ui.editor.AbstractDialog.EventType.OK && arg.linkText == linkText && arg.linkUrl == linkUrl; })); } /** * Tests that when you show the dialog for a new link, the input fields are * empty, the web tab is selected and focus is in the url input field. * @param {Document} opt_document Document to render the dialog into. Defaults * to the main window's document. */ function testShowForNewLink(opt_document) { mockCtrl.$replayAll(); setUpAnchor('', '', true); // Must be done before creating the dialog. createAndShow(opt_document); assertEquals('Display text input field should be empty', '', getDisplayInputText()); assertEquals('Url input field should be empty', '', getUrlInputText()); assertEquals('On the web tab should be selected', goog.ui.editor.LinkDialog.Id_.ON_WEB, dialog.curTabId_); if (goog.editor.BrowserFeature.HAS_ACTIVE_ELEMENT) { assertEquals('Focus should be on url input', getUrlInput(), dialog.dom.getDocument().activeElement); } mockCtrl.$verifyAll(); } /** * Fakes that the mock field is using an iframe and does the same test as * testShowForNewLink(). */ function testShowForNewLinkWithDiffAppWindow() { testShowForNewLink(goog.dom.getElement('appWindowIframe').contentDocument); } /** * Tests that when you show the dialog for a url link, the input fields are * filled in, the web tab is selected and focus is in the url input field. */ function testShowForUrlLink() { mockCtrl.$replayAll(); setUpAnchor(ANCHOR_URL, ANCHOR_TEXT); createAndShow(); assertEquals('Display text input field should be filled in', ANCHOR_TEXT, getDisplayInputText()); assertEquals('Url input field should be filled in', ANCHOR_URL, getUrlInputText()); assertEquals('On the web tab should be selected', goog.ui.editor.LinkDialog.Id_.ON_WEB, dialog.curTabId_); if (goog.editor.BrowserFeature.HAS_ACTIVE_ELEMENT) { assertEquals('Focus should be on url input', getUrlInput(), dialog.dom.getDocument().activeElement); } mockCtrl.$verifyAll(); } /** * Tests that when you show the dialog for a mailto link, the input fields are * filled in, the email tab is selected and focus is in the email input field. */ function testShowForMailtoLink() { mockCtrl.$replayAll(); setUpAnchor(ANCHOR_MAILTO, ANCHOR_TEXT); createAndShow(); assertEquals('Display text input field should be filled in', ANCHOR_TEXT, getDisplayInputText()); assertEquals('Email input field should be filled in', ANCHOR_EMAIL, // The 'mailto:' is not in the input! getEmailInputText()); assertEquals('Email tab should be selected', goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS, dialog.curTabId_); if (goog.editor.BrowserFeature.HAS_ACTIVE_ELEMENT) { assertEquals('Focus should be on email input', getEmailInput(), dialog.dom.getDocument().activeElement); } mockCtrl.$verifyAll(); } /** * Tests that the display text is autogenerated from the url input in the * right situations (and not generated when appropriate too). */ function testAutogeneration() { mockCtrl.$replayAll(); setUpAnchor('', '', true); createAndShow(); // Simulate typing a url when everything is empty, should autogen. setUrlInputText(ANCHOR_URL); assertEquals('Display text should have been autogenerated', ANCHOR_URL, getDisplayInputText()); // Simulate typing text when url is set, afterwards should not autogen. setDisplayInputText(ANCHOR_TEXT); setUrlInputText(ANCHOR_MAILTO); assertNotEquals('Display text should not have been autogenerated', ANCHOR_MAILTO, getDisplayInputText()); assertEquals('Display text should have remained the same', ANCHOR_TEXT, getDisplayInputText()); // Simulate typing text equal to existing url, afterwards should autogen. setDisplayInputText(ANCHOR_MAILTO); setUrlInputText(ANCHOR_URL); assertEquals('Display text should have been autogenerated', ANCHOR_URL, getDisplayInputText()); mockCtrl.$verifyAll(); } /** * Tests that clicking OK with the url tab selected dispatches an event with * the proper link data. */ function testOkForUrl() { expectOk(ANCHOR_TEXT, ANCHOR_URL); mockCtrl.$replayAll(); setUpAnchor('', '', true); createAndShow(); dialog.tabPane_.setSelectedTabId(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB); setDisplayInputText(ANCHOR_TEXT); setUrlInputText(ANCHOR_URL); goog.testing.events.fireClickSequence(dialog.getOkButtonElement()); mockCtrl.$verifyAll(); } /** * Tests that clicking OK with the url tab selected but with an email address * in the url field dispatches an event with the proper link data. */ function testOkForUrlWithEmail() { expectOk(ANCHOR_TEXT, ANCHOR_MAILTO); mockCtrl.$replayAll(); setUpAnchor('', '', true); createAndShow(); dialog.tabPane_.setSelectedTabId(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB); setDisplayInputText(ANCHOR_TEXT); setUrlInputText(ANCHOR_EMAIL); goog.testing.events.fireClickSequence(dialog.getOkButtonElement()); mockCtrl.$verifyAll(); } /** * Tests that clicking OK with the email tab selected dispatches an event with * the proper link data. */ function testOkForEmail() { expectOk(ANCHOR_TEXT, ANCHOR_MAILTO); mockCtrl.$replayAll(); setUpAnchor('', '', true); createAndShow(); dialog.tabPane_.setSelectedTabId( goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS_TAB); setDisplayInputText(ANCHOR_TEXT); setEmailInputText(ANCHOR_EMAIL); goog.testing.events.fireClickSequence(dialog.getOkButtonElement()); mockCtrl.$verifyAll(); } /** * Test that clicking on the test button opens a new window with the correct * options. */ function testWebTestButton() { if (goog.userAgent.GECKO) { // TODO(robbyw): Figure out why this is flaky and fix it. return; } var size = goog.dom.getViewportSize(window); var width = Math.max(size.width - 50, 50); var height = Math.max(size.height - 50, 50); mockWindowOpen(ANCHOR_URL, '_blank', 'width=' + width + ',height=' + height + ',toolbar=1,scrollbars=1,location=1,statusbar=0,' + 'menubar=1,resizable=1'); mockCtrl.$replayAll(); setUpAnchor(ANCHOR_URL, ANCHOR_TEXT); createAndShow(); var testLink = goog.testing.dom.findTextNode( goog.ui.editor.messages.MSG_TEST_THIS_LINK, dialog.dialogInternal_.getElement()); goog.testing.events.fireClickSequence(testLink.parentNode); mockCtrl.$verifyAll(); } /** * Test that clicking on the test button does not open a new window when * the event is canceled. */ function testWebTestButtonPreventDefault() { mockCtrl.$replayAll(); setUpAnchor(ANCHOR_URL, ANCHOR_TEXT); createAndShow(); goog.events.listen(dialog, goog.ui.editor.LinkDialog.EventType.BEFORE_TEST_LINK, function(e) { assertEquals(e.url, ANCHOR_URL); e.preventDefault(); }); var testLink = goog.testing.dom.findTextNode( goog.ui.editor.messages.MSG_TEST_THIS_LINK, dialog.dialogInternal_.getElement()); goog.testing.events.fireClickSequence(testLink.parentNode); mockCtrl.$verifyAll(); } /** * Test that the setTextToDisplayVisible() correctly works. * options. */ function testSetTextToDisplayVisible() { mockCtrl.$replayAll(); setUpAnchor('', '', true); createAndShow(); assertNotEquals('none', goog.style.getStyle(dialog.textToDisplayDiv_, 'display')); dialog.setTextToDisplayVisible(false); assertEquals('none', goog.style.getStyle(dialog.textToDisplayDiv_, 'display')); dialog.setTextToDisplayVisible(true); assertNotEquals('none', goog.style.getStyle(dialog.textToDisplayDiv_, 'display')); mockCtrl.$verifyAll(); } function getDisplayInput() { return dialog.dom.getElement(goog.ui.editor.LinkDialog.Id_.TEXT_TO_DISPLAY); } function getDisplayInputText() { return getDisplayInput().value; } function setDisplayInputText(text) { var textInput = getDisplayInput(); textInput.value = text; // Fire event so that dialog behaves like when user types. goog.testing.events.fireBrowserEvent(new goog.events.Event('keyup', textInput)); } function getUrlInput() { return dialog.dom.getElement(goog.ui.editor.LinkDialog.Id_.ON_WEB_INPUT); } function getUrlInputText() { return getUrlInput().value; } function setUrlInputText(text) { var urlInput = getUrlInput(); urlInput.value = text; // Fire event so that dialog behaves like when user types. dialog.urlInputHandler_.dispatchEvent( goog.events.InputHandler.EventType.INPUT); } function getEmailInput() { return dialog.dom.getElement( goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS_INPUT); } function getEmailInputText() { return getEmailInput().value; } function setEmailInputText(text) { var emailInput = getEmailInput(); emailInput.value = text; // Fire event so that dialog behaves like when user types. dialog.emailInputHandler_.dispatchEvent( goog.events.InputHandler.EventType.INPUT); } </script> </body> </html>