Adding mapbox-gl branch

This commit is contained in:
Andreas Hocevar
2015-03-16 18:50:27 +01:00
parent 7985f030fa
commit 57ee7f52fd
3109 changed files with 943365 additions and 0 deletions
@@ -0,0 +1,293 @@
// Copyright 2009 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Testing utilities for editor specific DOM related tests.
*
*/
goog.provide('goog.testing.editor.dom');
goog.require('goog.dom.NodeType');
goog.require('goog.dom.TagIterator');
goog.require('goog.dom.TagWalkType');
goog.require('goog.iter');
goog.require('goog.string');
goog.require('goog.testing.asserts');
/**
* Returns the previous (in document order) node from the given node that is a
* non-empty text node, or null if none is found or opt_stopAt is not an
* ancestor of node. Note that if the given node has children, the search will
* start from the end tag of the node, meaning all its descendants will be
* included in the search, unless opt_skipDescendants is true.
* @param {Node} node Node to start searching from.
* @param {Node=} opt_stopAt Node to stop searching at (search will be
* restricted to this node's subtree), defaults to the body of the document
* containing node.
* @param {boolean=} opt_skipDescendants Whether to skip searching the given
* node's descentants.
* @return {Text} The previous (in document order) node from the given node
* that is a non-empty text node, or null if none is found.
*/
goog.testing.editor.dom.getPreviousNonEmptyTextNode = function(
node, opt_stopAt, opt_skipDescendants) {
return goog.testing.editor.dom.getPreviousNextNonEmptyTextNodeHelper_(
node, opt_stopAt, opt_skipDescendants, true);
};
/**
* Returns the next (in document order) node from the given node that is a
* non-empty text node, or null if none is found or opt_stopAt is not an
* ancestor of node. Note that if the given node has children, the search will
* start from the start tag of the node, meaning all its descendants will be
* included in the search, unless opt_skipDescendants is true.
* @param {Node} node Node to start searching from.
* @param {Node=} opt_stopAt Node to stop searching at (search will be
* restricted to this node's subtree), defaults to the body of the document
* containing node.
* @param {boolean=} opt_skipDescendants Whether to skip searching the given
* node's descentants.
* @return {Text} The next (in document order) node from the given node that
* is a non-empty text node, or null if none is found or opt_stopAt is not
* an ancestor of node.
*/
goog.testing.editor.dom.getNextNonEmptyTextNode = function(
node, opt_stopAt, opt_skipDescendants) {
return goog.testing.editor.dom.getPreviousNextNonEmptyTextNodeHelper_(
node, opt_stopAt, opt_skipDescendants, false);
};
/**
* Helper that returns the previous or next (in document order) node from the
* given node that is a non-empty text node, or null if none is found or
* opt_stopAt is not an ancestor of node. Note that if the given node has
* children, the search will start from the end or start tag of the node
* (depending on whether it's searching for the previous or next node), meaning
* all its descendants will be included in the search, unless
* opt_skipDescendants is true.
* @param {Node} node Node to start searching from.
* @param {Node=} opt_stopAt Node to stop searching at (search will be
* restricted to this node's subtree), defaults to the body of the document
* containing node.
* @param {boolean=} opt_skipDescendants Whether to skip searching the given
* node's descentants.
* @param {boolean=} opt_isPrevious Whether to search for the previous non-empty
* text node instead of the next one.
* @return {Text} The next (in document order) node from the given node that
* is a non-empty text node, or null if none is found or opt_stopAt is not
* an ancestor of node.
* @private
*/
goog.testing.editor.dom.getPreviousNextNonEmptyTextNodeHelper_ = function(
node, opt_stopAt, opt_skipDescendants, opt_isPrevious) {
opt_stopAt = opt_stopAt || node.ownerDocument.body;
// Initializing the iterator to iterate over the children of opt_stopAt
// makes it stop only when it finishes iterating through all of that
// node's children, even though we will start at a different node and exit
// that starting node's subtree in the process.
var iter = new goog.dom.TagIterator(opt_stopAt, opt_isPrevious);
// TODO(user): Move this logic to a new method in TagIterator such as
// skipToNode().
// Then we set the iterator to start at the given start node, not opt_stopAt.
var walkType; // Let TagIterator set the initial walk type by default.
var depth = goog.testing.editor.dom.getRelativeDepth_(node, opt_stopAt);
if (depth == -1) {
return null; // Fail because opt_stopAt is not an ancestor of node.
}
if (node.nodeType == goog.dom.NodeType.ELEMENT) {
if (opt_skipDescendants) {
// Specifically set the initial walk type so that we skip the descendant
// subtree by starting at the start if going backwards or at the end if
// going forwards.
walkType = opt_isPrevious ? goog.dom.TagWalkType.START_TAG :
goog.dom.TagWalkType.END_TAG;
} else {
// We're starting "inside" an element node so the depth needs to be one
// deeper than the node's actual depth. That's how TagIterator works!
depth++;
}
}
iter.setPosition(node, walkType, depth);
// Advance the iterator so it skips the start node.
try {
iter.next();
} catch (e) {
return null; // It could have been a leaf node.
}
// Now just get the first non-empty text node the iterator finds.
var filter = goog.iter.filter(iter,
goog.testing.editor.dom.isNonEmptyTextNode_);
try {
return /** @type {Text} */ (filter.next());
} catch (e) { // No next item is available so return null.
return null;
}
};
/**
* Returns whether the given node is a non-empty text node.
* @param {Node} node Node to be checked.
* @return {boolean} Whether the given node is a non-empty text node.
* @private
*/
goog.testing.editor.dom.isNonEmptyTextNode_ = function(node) {
return !!node && node.nodeType == goog.dom.NodeType.TEXT && node.length > 0;
};
/**
* Returns the depth of the given node relative to the given parent node, or -1
* if the given node is not a descendant of the given parent node. E.g. if
* node == parentNode returns 0, if node.parentNode == parentNode returns 1,
* etc.
* @param {Node} node Node whose depth to get.
* @param {Node} parentNode Node relative to which the depth should be
* calculated.
* @return {number} The depth of the given node relative to the given parent
* node, or -1 if the given node is not a descendant of the given parent
* node.
* @private
*/
goog.testing.editor.dom.getRelativeDepth_ = function(node, parentNode) {
var depth = 0;
while (node) {
if (node == parentNode) {
return depth;
}
node = node.parentNode;
depth++;
}
return -1;
};
/**
* Assert that the range is surrounded by the given strings. This is useful
* because different browsers can place the range endpoints inside different
* nodes even when visually the range looks the same. Also, there may be empty
* text nodes in the way (again depending on the browser) making it difficult to
* use assertRangeEquals.
* @param {string} before String that should occur immediately before the start
* point of the range. If this is the empty string, assert will only succeed
* if there is no text before the start point of the range.
* @param {string} after String that should occur immediately after the end
* point of the range. If this is the empty string, assert will only succeed
* if there is no text after the end point of the range.
* @param {goog.dom.AbstractRange} range The range to be tested.
* @param {Node=} opt_stopAt Node to stop searching at (search will be
* restricted to this node's subtree).
*/
goog.testing.editor.dom.assertRangeBetweenText = function(before,
after,
range,
opt_stopAt) {
var previousText =
goog.testing.editor.dom.getTextFollowingRange_(range, true, opt_stopAt);
if (before == '') {
assertNull('Expected nothing before range but found <' + previousText + '>',
previousText);
} else {
assertNotNull('Expected <' + before + '> before range but found nothing',
previousText);
assertTrue('Expected <' + before + '> before range but found <' +
previousText + '>',
goog.string.endsWith(
/** @type {string} */ (previousText), before));
}
var nextText =
goog.testing.editor.dom.getTextFollowingRange_(range, false, opt_stopAt);
if (after == '') {
assertNull('Expected nothing after range but found <' + nextText + '>',
nextText);
} else {
assertNotNull('Expected <' + after + '> after range but found nothing',
nextText);
assertTrue('Expected <' + after + '> after range but found <' +
nextText + '>',
goog.string.startsWith(
/** @type {string} */ (nextText), after));
}
};
/**
* Returns the text that follows the given range, where the term "follows" means
* "comes immediately before the start of the range" if isBefore is true, and
* "comes immediately after the end of the range" if isBefore is false, or null
* if no non-empty text node is found.
* @param {goog.dom.AbstractRange} range The range to search from.
* @param {boolean} isBefore Whether to search before the range instead of
* after it.
* @param {Node=} opt_stopAt Node to stop searching at (search will be
* restricted to this node's subtree).
* @return {?string} The text that follows the given range, or null if no
* non-empty text node is found.
* @private
*/
goog.testing.editor.dom.getTextFollowingRange_ = function(range,
isBefore,
opt_stopAt) {
var followingTextNode;
var endpointNode = isBefore ? range.getStartNode() : range.getEndNode();
var endpointOffset = isBefore ? range.getStartOffset() : range.getEndOffset();
var getFollowingTextNode =
isBefore ? goog.testing.editor.dom.getPreviousNonEmptyTextNode :
goog.testing.editor.dom.getNextNonEmptyTextNode;
if (endpointNode.nodeType == goog.dom.NodeType.TEXT) {
// Range endpoint is in a text node.
var endText = endpointNode.nodeValue;
if (isBefore ? endpointOffset > 0 : endpointOffset < endText.length) {
// There is text in this node following the endpoint so return the portion
// that follows the endpoint.
return isBefore ? endText.substr(0, endpointOffset) :
endText.substr(endpointOffset);
} else {
// There is no text following the endpoint so look for the follwing text
// node.
followingTextNode = getFollowingTextNode(endpointNode, opt_stopAt);
return followingTextNode && followingTextNode.nodeValue;
}
} else {
// Range endpoint is in an element node.
var numChildren = endpointNode.childNodes.length;
if (isBefore ? endpointOffset > 0 : endpointOffset < numChildren) {
// There is at least one child following the endpoint.
var followingChild =
endpointNode.childNodes[isBefore ? endpointOffset - 1 :
endpointOffset];
if (goog.testing.editor.dom.isNonEmptyTextNode_(followingChild)) {
// The following child has text so return that.
return followingChild.nodeValue;
} else {
// The following child has no text so look for the following text node.
followingTextNode = getFollowingTextNode(followingChild, opt_stopAt);
return followingTextNode && followingTextNode.nodeValue;
}
} else {
// There is no child following the endpoint, so search from the endpoint
// node, but don't search its children because they are not following the
// endpoint!
followingTextNode = getFollowingTextNode(endpointNode, opt_stopAt, true);
return followingTextNode && followingTextNode.nodeValue;
}
}
};
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<!--
@author marcosalmeida@google.com (Marcos Almeida)
-->
<title>
Closure Unit Tests - goog.testing.editor.dom
</title>
<script src="../../base.js">
</script>
<script>
goog.require('goog.testing.editor.domTest');
</script>
</head>
<body>
<div id="root">
</div>
</body>
</html>
<!--
Copyright 2009 The Closure Library Authors. All Rights Reserved.
Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
@@ -0,0 +1,290 @@
// Copyright 2009 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
goog.provide('goog.testing.editor.domTest');
goog.setTestOnly('goog.testing.editor.domTest');
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.functions');
goog.require('goog.testing.editor.dom');
goog.require('goog.testing.jsunit');
var root;
var parentNode, childNode1, childNode2, childNode3;
var first, middle, last;
function setUpPage() {
root = goog.dom.getElement('root');
}
function tearDown() {
root.innerHTML = '';
}
function setUpNonEmptyTests() {
childNode1 = goog.dom.createElement(goog.dom.TagName.DIV);
childNode2 = goog.dom.createElement(goog.dom.TagName.DIV);
childNode3 = goog.dom.createElement(goog.dom.TagName.DIV);
parentNode = goog.dom.createDom(goog.dom.TagName.DIV,
null,
childNode1,
childNode2,
childNode3);
goog.dom.appendChild(root, parentNode);
childNode1.appendChild(goog.dom.createTextNode('One'));
childNode1.appendChild(goog.dom.createTextNode(''));
childNode2.appendChild(goog.dom.createElement(goog.dom.TagName.BR));
childNode2.appendChild(goog.dom.createTextNode('TwoA'));
childNode2.appendChild(goog.dom.createTextNode('TwoB'));
childNode2.appendChild(goog.dom.createElement(goog.dom.TagName.BR));
childNode3.appendChild(goog.dom.createTextNode(''));
childNode3.appendChild(goog.dom.createTextNode('Three'));
}
function testGetNextNonEmptyTextNode() {
setUpNonEmptyTests();
var nodeOne =
goog.testing.editor.dom.getNextNonEmptyTextNode(parentNode);
assertEquals('Should have found the next non-empty text node',
'One',
nodeOne.nodeValue);
var nodeTwoA =
goog.testing.editor.dom.getNextNonEmptyTextNode(nodeOne);
assertEquals('Should have found the next non-empty text node',
'TwoA',
nodeTwoA.nodeValue);
var nodeTwoB =
goog.testing.editor.dom.getNextNonEmptyTextNode(nodeTwoA);
assertEquals('Should have found the next non-empty text node',
'TwoB',
nodeTwoB.nodeValue);
var nodeThree =
goog.testing.editor.dom.getNextNonEmptyTextNode(nodeTwoB);
assertEquals('Should have found the next non-empty text node',
'Three',
nodeThree.nodeValue);
var nodeNull =
goog.testing.editor.dom.getNextNonEmptyTextNode(nodeThree, parentNode);
assertNull('Should not have found any non-empty text node', nodeNull);
var nodeStop =
goog.testing.editor.dom.getNextNonEmptyTextNode(nodeOne, childNode1);
assertNull('Should have stopped before finding a node', nodeStop);
var nodeBeforeStop =
goog.testing.editor.dom.getNextNonEmptyTextNode(nodeTwoA, childNode2);
assertEquals('Should have found the next non-empty text node',
'TwoB',
nodeBeforeStop.nodeValue);
}
function testGetPreviousNonEmptyTextNode() {
setUpNonEmptyTests();
var nodeThree =
goog.testing.editor.dom.getPreviousNonEmptyTextNode(parentNode);
assertEquals('Should have found the previous non-empty text node',
'Three',
nodeThree.nodeValue);
var nodeTwoB =
goog.testing.editor.dom.getPreviousNonEmptyTextNode(nodeThree);
assertEquals('Should have found the previous non-empty text node',
'TwoB',
nodeTwoB.nodeValue);
var nodeTwoA =
goog.testing.editor.dom.getPreviousNonEmptyTextNode(nodeTwoB);
assertEquals('Should have found the previous non-empty text node',
'TwoA',
nodeTwoA.nodeValue);
var nodeOne =
goog.testing.editor.dom.getPreviousNonEmptyTextNode(nodeTwoA);
assertEquals('Should have found the previous non-empty text node',
'One',
nodeOne.nodeValue);
var nodeNull =
goog.testing.editor.dom.getPreviousNonEmptyTextNode(nodeOne,
parentNode);
assertNull('Should not have found any non-empty text node', nodeNull);
var nodeStop =
goog.testing.editor.dom.getPreviousNonEmptyTextNode(nodeThree,
childNode3);
assertNull('Should have stopped before finding a node', nodeStop);
var nodeBeforeStop =
goog.testing.editor.dom.getPreviousNonEmptyTextNode(nodeTwoB,
childNode2);
assertEquals('Should have found the previous non-empty text node',
'TwoA',
nodeBeforeStop.nodeValue);
}
function setUpAssertRangeBetweenText() {
// Create the following structure: <[01]><[]><[23]>
// Where <> delimits spans, [] delimits text nodes, 01 and 23 are text.
// We will test all 10 positions in between 0 and 2. All should pass.
first = goog.dom.createDom(goog.dom.TagName.SPAN, null, '01');
middle = goog.dom.createElement(goog.dom.TagName.SPAN);
var emptyTextNode = goog.dom.createTextNode('');
goog.dom.appendChild(middle, emptyTextNode);
last = goog.dom.createDom(goog.dom.TagName.SPAN, null, '23');
goog.dom.appendChild(root, first);
goog.dom.appendChild(root, middle);
goog.dom.appendChild(root, last);
}
function createFakeRange(startNode, startOffset, opt_endNode, opt_endOffset) {
opt_endNode = opt_endNode || startNode;
opt_endOffset = opt_endOffset || startOffset;
return {
getStartNode: goog.functions.constant(startNode),
getStartOffset: goog.functions.constant(startOffset),
getEndNode: goog.functions.constant(opt_endNode),
getEndOffset: goog.functions.constant(opt_endOffset)
};
}
function testAssertRangeBetweenText0() {
setUpAssertRangeBetweenText();
goog.testing.editor.dom.assertRangeBetweenText('0', '1',
createFakeRange(first.firstChild, 1));
}
function testAssertRangeBetweenText1() {
setUpAssertRangeBetweenText();
goog.testing.editor.dom.assertRangeBetweenText('1', '2',
createFakeRange(first.firstChild, 2));
}
function testAssertRangeBetweenText2() {
setUpAssertRangeBetweenText();
goog.testing.editor.dom.assertRangeBetweenText('1', '2',
createFakeRange(first, 1));
}
function testAssertRangeBetweenText3() {
setUpAssertRangeBetweenText();
goog.testing.editor.dom.assertRangeBetweenText('1', '2',
createFakeRange(root, 1));
}
function testAssertRangeBetweenText4() {
setUpAssertRangeBetweenText();
goog.testing.editor.dom.assertRangeBetweenText('1', '2',
createFakeRange(middle, 0));
}
function testAssertRangeBetweenText5() {
setUpAssertRangeBetweenText();
goog.testing.editor.dom.assertRangeBetweenText('1', '2',
createFakeRange(middle.firstChild, 0));
}
function testAssertRangeBetweenText6() {
setUpAssertRangeBetweenText();
goog.testing.editor.dom.assertRangeBetweenText('1', '2',
createFakeRange(middle, 1));
}
function testAssertRangeBetweenText7() {
setUpAssertRangeBetweenText();
goog.testing.editor.dom.assertRangeBetweenText('1', '2',
createFakeRange(root, 2));
}
function testAssertRangeBetweenText8() {
setUpAssertRangeBetweenText();
goog.testing.editor.dom.assertRangeBetweenText('1', '2',
createFakeRange(last, 0));
}
function testAssertRangeBetweenText9() {
setUpAssertRangeBetweenText();
goog.testing.editor.dom.assertRangeBetweenText('1', '2',
createFakeRange(last.firstChild, 0));
}
function testAssertRangeBetweenTextBefore() {
setUpAssertRangeBetweenText();
// Test that it works when the cursor is at the beginning of all text.
goog.testing.editor.dom.assertRangeBetweenText('', '0',
createFakeRange(first.firstChild, 0),
root); // Restrict to root div so it won't find /n's and script.
}
function testAssertRangeBetweenTextAfter() {
setUpAssertRangeBetweenText();
// Test that it works when the cursor is at the end of all text.
goog.testing.editor.dom.assertRangeBetweenText('3', '',
createFakeRange(last.firstChild, 2),
root); // Restrict to root div so it won't find /n's and script.
}
function testAssertRangeBetweenTextFail1() {
setUpAssertRangeBetweenText();
var e = assertThrows('assertRangeBetweenText should have failed',
function() {
goog.testing.editor.dom.assertRangeBetweenText('1', '3',
createFakeRange(first.firstChild, 2));
});
assertContains('Assert reason incorrect',
'Expected <3> after range but found <23>', e.message);
}
function testAssertRangeBetweenTextFail2() {
setUpAssertRangeBetweenText();
var e = assertThrows('assertRangeBetweenText should have failed',
function() {
goog.testing.editor.dom.assertRangeBetweenText('1', '2',
createFakeRange(first.firstChild, 2, last.firstChild, 1));
});
assertContains('Assert reason incorrect',
'Expected <2> after range but found <3>', e.message);
}
function testAssertRangeBetweenTextBeforeFail() {
setUpAssertRangeBetweenText();
// Test that it gives the right message when the cursor is at the beginning
// of all text but you're expecting something before it.
var e = assertThrows('assertRangeBetweenText should have failed',
function() {
goog.testing.editor.dom.assertRangeBetweenText('-1', '0',
createFakeRange(first.firstChild, 0),
root); // Restrict to root div so it won't find /n's and script.
});
assertContains('Assert reason incorrect',
'Expected <-1> before range but found nothing', e.message);
}
function testAssertRangeBetweenTextAfterFail() {
setUpAssertRangeBetweenText();
// Test that it gives the right message when the cursor is at the end
// of all text but you're expecting something after it.
var e = assertThrows('assertRangeBetweenText should have failed',
function() {
goog.testing.editor.dom.assertRangeBetweenText('3', '4',
createFakeRange(last.firstChild, 2),
root); // Restrict to root div so it won't find /n's and script.
});
assertContains('Assert reason incorrect',
'Expected <4> after range but found nothing', e.message);
}
@@ -0,0 +1,116 @@
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Mock of goog.editor.field.
*
* @author robbyw@google.com (Robby Walker)
*/
goog.provide('goog.testing.editor.FieldMock');
goog.require('goog.dom');
goog.require('goog.dom.Range');
goog.require('goog.editor.Field');
goog.require('goog.testing.LooseMock');
goog.require('goog.testing.mockmatchers');
/**
* Mock of goog.editor.Field.
* @param {Window=} opt_window Window the field would edit. Defaults to
* {@code window}.
* @param {Window=} opt_appWindow "AppWindow" of the field, which can be
* different from {@code opt_window} when mocking a field that uses an
* iframe. Defaults to {@code opt_window}.
* @param {goog.dom.AbstractRange=} opt_range An object (mock or real) to be
* returned by getRange(). If ommitted, a new goog.dom.Range is created
* from the window every time getRange() is called.
* @constructor
* @extends {goog.testing.LooseMock}
* @suppress {missingProperties} Mocks do not fit in the type system well.
* @final
*/
goog.testing.editor.FieldMock =
function(opt_window, opt_appWindow, opt_range) {
goog.testing.LooseMock.call(this, goog.editor.Field);
opt_window = opt_window || window;
opt_appWindow = opt_appWindow || opt_window;
this.getAppWindow();
this.$anyTimes();
this.$returns(opt_appWindow);
this.getRange();
this.$anyTimes();
this.$does(function() {
return opt_range || goog.dom.Range.createFromWindow(opt_window);
});
this.getEditableDomHelper();
this.$anyTimes();
this.$returns(goog.dom.getDomHelper(opt_window.document));
this.usesIframe();
this.$anyTimes();
this.getBaseZindex();
this.$anyTimes();
this.$returns(0);
this.restoreSavedRange(goog.testing.mockmatchers.ignoreArgument);
this.$anyTimes();
this.$does(function(range) {
if (range) {
range.restore();
}
this.focus();
});
// These methods cannot be set on the prototype, because the prototype
// gets stepped on by the mock framework.
var inModalMode = false;
/**
* @return {boolean} Whether we're in modal interaction mode.
*/
this.inModalMode = function() {
return inModalMode;
};
/**
* @param {boolean} mode Sets whether we're in modal interaction mode.
*/
this.setModalMode = function(mode) {
inModalMode = mode;
};
var uneditable = false;
/**
* @return {boolean} Whether the field is uneditable.
*/
this.isUneditable = function() {
return uneditable;
};
/**
* @param {boolean} isUneditable Whether the field is uneditable.
*/
this.setUneditable = function(isUneditable) {
uneditable = isUneditable;
};
};
goog.inherits(goog.testing.editor.FieldMock, goog.testing.LooseMock);
@@ -0,0 +1,180 @@
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Class that allows for simple text editing tests.
*
* @author robbyw@google.com (Robby Walker)
*/
goog.provide('goog.testing.editor.TestHelper');
goog.require('goog.Disposable');
goog.require('goog.dom');
goog.require('goog.dom.Range');
goog.require('goog.editor.BrowserFeature');
goog.require('goog.editor.node');
goog.require('goog.editor.plugins.AbstractBubblePlugin');
goog.require('goog.testing.dom');
/**
* Create a new test controller.
* @param {Element} root The root editable element.
* @constructor
* @extends {goog.Disposable}
* @final
*/
goog.testing.editor.TestHelper = function(root) {
if (!root) {
throw Error('Null root');
}
goog.Disposable.call(this);
/**
* Convenience variable for root DOM element.
* @type {!Element}
* @private
*/
this.root_ = root;
/**
* The starting HTML of the editable element.
* @type {string}
* @private
*/
this.savedHtml_ = '';
};
goog.inherits(goog.testing.editor.TestHelper, goog.Disposable);
/**
* Selects a new root element.
* @param {Element} root The root editable element.
*/
goog.testing.editor.TestHelper.prototype.setRoot = function(root) {
if (!root) {
throw Error('Null root');
}
this.root_ = root;
};
/**
* Make the root element editable. Alse saves its HTML to be restored
* in tearDown.
*/
goog.testing.editor.TestHelper.prototype.setUpEditableElement = function() {
this.savedHtml_ = this.root_.innerHTML;
if (goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE) {
this.root_.contentEditable = true;
} else {
this.root_.ownerDocument.designMode = 'on';
}
this.root_.setAttribute('g_editable', 'true');
};
/**
* Reset the element previously initialized, restoring its HTML and making it
* non editable.
* @suppress {accessControls} Private state of
* {@link goog.editor.plugins.AbstractBubblePlugin} is accessed for test
* purposes.
*/
goog.testing.editor.TestHelper.prototype.tearDownEditableElement = function() {
if (goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE) {
this.root_.contentEditable = false;
} else {
this.root_.ownerDocument.designMode = 'off';
}
goog.dom.removeChildren(this.root_);
this.root_.innerHTML = this.savedHtml_;
this.root_.removeAttribute('g_editable');
if (goog.editor.plugins && goog.editor.plugins.AbstractBubblePlugin) {
// Remove old bubbles.
for (var key in goog.editor.plugins.AbstractBubblePlugin.bubbleMap_) {
goog.editor.plugins.AbstractBubblePlugin.bubbleMap_[key].dispose();
}
// Ensure we get a new bubble for each test.
goog.editor.plugins.AbstractBubblePlugin.bubbleMap_ = {};
}
};
/**
* Assert that the html in 'root' is substantially similar to htmlPattern.
* This method tests for the same set of styles, and for the same order of
* nodes. Breaking whitespace nodes are ignored. Elements can be annotated
* with classnames corresponding to keys in goog.userAgent and will be
* expected to show up in that user agent and expected not to show up in
* others.
* @param {string} htmlPattern The pattern to match.
*/
goog.testing.editor.TestHelper.prototype.assertHtmlMatches = function(
htmlPattern) {
goog.testing.dom.assertHtmlContentsMatch(htmlPattern, this.root_);
};
/**
* Finds the first text node descendant of root with the given content.
* @param {string|RegExp} textOrRegexp The text to find, or a regular
* expression to find a match of.
* @return {Node} The first text node that matches, or null if none is found.
*/
goog.testing.editor.TestHelper.prototype.findTextNode = function(textOrRegexp) {
return goog.testing.dom.findTextNode(textOrRegexp, this.root_);
};
/**
* Select from the given from offset in the given from node to the given
* to offset in the optionally given to node. If nodes are passed in, uses them,
* otherwise uses findTextNode to find the nodes to select. Selects a caret
* if opt_to and opt_toOffset are not given.
* @param {Node|string} from Node or text of the node to start the selection at.
* @param {number} fromOffset Offset within the above node to start the
* selection at.
* @param {Node|string=} opt_to Node or text of the node to end the selection
* at.
* @param {number=} opt_toOffset Offset within the above node to end the
* selection at.
*/
goog.testing.editor.TestHelper.prototype.select = function(from, fromOffset,
opt_to, opt_toOffset) {
var end;
var start = end = goog.isString(from) ? this.findTextNode(from) : from;
var endOffset;
var startOffset = endOffset = fromOffset;
if (opt_to && goog.isNumber(opt_toOffset)) {
end = goog.isString(opt_to) ? this.findTextNode(opt_to) : opt_to;
endOffset = opt_toOffset;
}
goog.dom.Range.createFromNodes(start, startOffset, end, endOffset).select();
};
/** @override */
goog.testing.editor.TestHelper.prototype.disposeInternal = function() {
if (goog.editor.node.isEditableContainer(this.root_)) {
this.tearDownEditableElement();
}
delete this.root_;
goog.testing.editor.TestHelper.base(this, 'disposeInternal');
};
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.
Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>
Closure Unit Tests - goog.testing.editor.TestHelper
</title>
<script src="../../base.js">
</script>
<script>
goog.require('goog.testing.editor.TestHelperTest');
</script>
</head>
<body>
<div id="root">
</div>
<div id="root2">Root 2</div>
</body>
</html>
@@ -0,0 +1,175 @@
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
goog.provide('goog.testing.editor.TestHelperTest');
goog.setTestOnly('goog.testing.editor.TestHelperTest');
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.editor.node');
goog.require('goog.testing.editor.TestHelper');
goog.require('goog.testing.jsunit');
goog.require('goog.userAgent');
var root;
var helper;
function setUp() {
root = goog.dom.getElement('root');
goog.dom.removeChildren(root);
helper = new goog.testing.editor.TestHelper(root);
}
function tearDown() {
helper.dispose();
}
function testSetRoot() {
helper.setRoot(goog.dom.getElement('root2'));
helper.assertHtmlMatches('Root 2');
}
function testSetupEditableElement() {
helper.setUpEditableElement();
assertTrue(goog.editor.node.isEditableContainer(root));
}
function testTearDownEditableElement() {
helper.setUpEditableElement();
assertTrue(goog.editor.node.isEditableContainer(root));
helper.tearDownEditableElement();
assertFalse(goog.editor.node.isEditableContainer(root));
}
function testFindNode() {
// Test the easiest case.
root.innerHTML = 'a<br>b';
assertEquals(helper.findTextNode('a'), root.firstChild);
assertEquals(helper.findTextNode('b'), root.lastChild);
assertNull(helper.findTextNode('c'));
}
function testFindNodeDuplicate() {
// Test duplicate.
root.innerHTML = 'c<br>c';
assertEquals('Should return first duplicate', helper.findTextNode('c'),
root.firstChild);
}
function findNodeWithHierarchy() {
// Test a more complicated hierarchy.
root.innerHTML = '<div>a<p>b<span>c</span>d</p>e</div>';
assertEquals(goog.dom.TagName.DIV,
helper.findTextNode('a').parentNode.tagName);
assertEquals(goog.dom.TagName.P,
helper.findTextNode('b').parentNode.tagName);
assertEquals(goog.dom.TagName.SPAN,
helper.findTextNode('c').parentNode.tagName);
assertEquals(goog.dom.TagName.P,
helper.findTextNode('d').parentNode.tagName);
assertEquals(goog.dom.TagName.DIV,
helper.findTextNode('e').parentNode.tagName);
}
function setUpAssertHtmlMatches() {
var tag1, tag2;
if (goog.userAgent.IE) {
tag1 = goog.dom.TagName.DIV;
} else if (goog.userAgent.WEBKIT) {
tag1 = goog.dom.TagName.P;
tag2 = goog.dom.TagName.BR;
} else if (goog.userAgent.GECKO) {
tag1 = goog.dom.TagName.SPAN;
tag2 = goog.dom.TagName.BR;
}
var parent = goog.dom.createDom(goog.dom.TagName.DIV);
root.appendChild(parent);
parent.style.fontSize = '2em';
parent.style.display = 'none';
if (goog.userAgent.IE || goog.userAgent.GECKO) {
parent.appendChild(goog.dom.createTextNode('NonWebKitText'));
}
if (tag1) {
var e1 = goog.dom.createDom(tag1);
parent.appendChild(e1);
parent = e1;
}
if (tag2) {
parent.appendChild(goog.dom.createDom(tag2));
}
parent.appendChild(goog.dom.createTextNode('Text'));
if (goog.userAgent.WEBKIT) {
root.firstChild.appendChild(goog.dom.createTextNode('WebKitText'));
}
}
function testAssertHtmlMatches() {
setUpAssertHtmlMatches();
helper.assertHtmlMatches('<div style="display: none; font-size: 2em">' +
'[[IE GECKO]]NonWebKitText<div class="IE"><p class="WEBKIT">' +
'<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
'</div>[[WEBKIT]]WebKitText');
}
function testAssertHtmlMismatchText() {
setUpAssertHtmlMatches();
var e = assertThrows('Should fail due to mismatched text', function() {
helper.assertHtmlMatches('<div style="display: none; font-size: 2em">' +
'[[IE GECKO]]NonWebKitText<div class="IE"><p class="WEBKIT">' +
'<span class="GECKO"><br class="GECKO WEBKIT">Bad</span></p></div>' +
'</div>[[WEBKIT]]Extra');
});
assertContains('Text should match', e.message);
}
function testAssertHtmlMismatchTag() {
setUpAssertHtmlMatches();
var e = assertThrows('Should fail due to mismatched tag', function() {
helper.assertHtmlMatches('<span style="display: none; font-size: 2em">' +
'[[IE GECKO]]NonWebKitText<div class="IE"><p class="WEBKIT">' +
'<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
'</span>[[WEBKIT]]Extra');
});
assertContains('Tag names should match', e.message);
}
function testAssertHtmlMismatchStyle() {
setUpAssertHtmlMatches();
var e = assertThrows('Should fail due to mismatched style', function() {
helper.assertHtmlMatches('<div style="display: none; font-size: 3em">' +
'[[IE GECKO]]NonWebKitText<div class="IE"><p class="WEBKIT">' +
'<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
'</div>[[WEBKIT]]Extra');
});
assertContains('Should have same styles', e.message);
}
function testAssertHtmlMismatchOptionalText() {
setUpAssertHtmlMatches();
var e = assertThrows('Should fail due to mismatched style', function() {
helper.assertHtmlMatches('<div style="display: none; font-size: 2em">' +
'[[IE GECKO]]Bad<div class="IE"><p class="WEBKIT">' +
'<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
'</div>[[WEBKIT]]Bad');
});
assertContains('Text should match', e.message);
}