From c7f528dab8e0a35efe217defddc9b92f4bda199d Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Fri, 21 Aug 2015 08:53:31 -0700 Subject: [PATCH] Migrate some tests to sky/unit/test --- .../unit/test/engine/append_child_test.dart | 60 +++++++++++ .../engine/attribute_collection_test.dart | 61 ++++++++++++ packages/unit/test/engine/canvas_test.dart | 25 +++++ packages/unit/test/engine/color_test.dart | 40 ++++++++ .../engine/document_child_mutations_test.dart | 99 +++++++++++++++++++ packages/unit/test/engine/dom_utils.dart | 15 +++ .../test/engine/get_child_elements_test.dart | 23 +++++ .../unit/test/engine/inline_style_test.dart | 38 +++++++ .../unit/test/engine/insert_before_test.dart | 21 ++++ .../unit/test/engine/owner_scope_test.dart | 17 ++++ .../unit/test/engine/replace_child_test.dart | 54 ++++++++++ packages/unit/test/engine/text_test.dart | 10 ++ 12 files changed, 463 insertions(+) create mode 100644 packages/unit/test/engine/append_child_test.dart create mode 100644 packages/unit/test/engine/attribute_collection_test.dart create mode 100644 packages/unit/test/engine/canvas_test.dart create mode 100644 packages/unit/test/engine/color_test.dart create mode 100644 packages/unit/test/engine/document_child_mutations_test.dart create mode 100644 packages/unit/test/engine/dom_utils.dart create mode 100644 packages/unit/test/engine/get_child_elements_test.dart create mode 100644 packages/unit/test/engine/inline_style_test.dart create mode 100644 packages/unit/test/engine/insert_before_test.dart create mode 100644 packages/unit/test/engine/owner_scope_test.dart create mode 100644 packages/unit/test/engine/replace_child_test.dart create mode 100644 packages/unit/test/engine/text_test.dart diff --git a/packages/unit/test/engine/append_child_test.dart b/packages/unit/test/engine/append_child_test.dart new file mode 100644 index 0000000000..2fc1e41f4b --- /dev/null +++ b/packages/unit/test/engine/append_child_test.dart @@ -0,0 +1,60 @@ +import 'dart:sky'; + +import 'package:test/test.dart'; + +import 'dom_utils.dart'; + +void main() { + Document document = new Document(); + + test("should throw with invalid arguments", () { + var parent = document.createElement("div"); + expect(() { + parent.appendChild(); + }, throws); + expect(() { + parent.appendChild(null); + }, throws); + expect(() { + parent.appendChild({"tagName": "div"}); + }, throws); + }); + + test("should insert children", () { + var parent = document.createElement("div"); + var child1 = parent.appendChild(document.createElement("div")); + var child2 = parent.appendChild(document.createText(" text ")); + var child3 = parent.appendChild(document.createText(" ")); + var child4 = parent.appendChild(document.createElement("div")); + expect(child1.parentNode, equals(parent)); + expect(child2.parentNode, equals(parent)); + expect(child3.parentNode, equals(parent)); + expect(child4.parentNode, equals(parent)); + expect(childNodeCount(parent), equals(4)); + expect(childElementCount(parent), equals(2)); + }); + + test("should insert children with a fragment", () { + var fragment = document.createDocumentFragment(); + var child1 = fragment.appendChild(document.createElement("div")); + var child2 = fragment.appendChild(document.createText(" text ")); + var child3 = fragment.appendChild(document.createText(" ")); + var child4 = fragment.appendChild(document.createElement("div")); + var parent = document.createElement("div"); + parent.appendChild(fragment); + expect(child1.parentNode, equals(parent)); + expect(child2.parentNode, equals(parent)); + expect(child3.parentNode, equals(parent)); + expect(child4.parentNode, equals(parent)); + expect(childNodeCount(parent), equals(4)); + expect(childElementCount(parent), equals(2)); + }); + + // TODO(dart): These might be real bugs too. + // test("should throw when appending to a text", () { + // var parent = new Text(); + // expect(() { + // parent.appendChild(document.createElement("div")); + // }, throws); + // }); +} diff --git a/packages/unit/test/engine/attribute_collection_test.dart b/packages/unit/test/engine/attribute_collection_test.dart new file mode 100644 index 0000000000..2619f6afd6 --- /dev/null +++ b/packages/unit/test/engine/attribute_collection_test.dart @@ -0,0 +1,61 @@ +import 'dart:sky'; + +import 'package:test/test.dart'; + +void main() { + var div; + setUp(() { + var document = new Document(); + div = document.createElement("div"); + }); + + test("should get by index", () { + div.setAttribute("attr0", "value0"); + div.setAttribute("attr1", "value1"); + var attrs = div.getAttributes(); + expect(attrs.length, equals(2)); + expect(attrs[0].name, equals("attr0")); + expect(attrs[0].value, equals("value0")); + expect(attrs[1].name, equals("attr1")); + expect(attrs[1].value, equals("value1")); + }); + test("should set by name", () { + div.setAttribute("attrName", "value0"); + expect(div.getAttribute("attrName"), equals("value0")); + expect(div.getAttributes()[0].name, equals("attrName")); + expect(div.getAttributes()[0].value, equals("value0")); + div.setAttribute("attrName", "new value"); + expect(div.getAttribute("attrName"), equals("new value")); + expect(div.getAttributes()[0].name, equals("attrName")); + expect(div.getAttributes()[0].value, equals("new value")); + }); + test("should be case sensitive", () { + div.setAttribute("attrName", "value0"); + expect(div.getAttribute("attrname"), isNull); + expect(div.getAttribute("attrName"), equals("value0")); + }); + test("should not live update", () { + div.setAttribute("attr0", "0"); + div.setAttribute("attr1", "1"); + div.setAttribute("attr2", "2"); + var oldAttributes = div.getAttributes(); + expect(oldAttributes.length, equals(3)); + div.removeAttribute("attr1"); + expect(oldAttributes.length, equals(3)); + div.setAttribute("attr0", "value0"); + div.setAttribute("attr2", "value2"); + var newAttributes = div.getAttributes(); + expect(newAttributes.length, equals(2)); + expect(newAttributes[0].name, equals("attr0")); + expect(newAttributes[0].value, equals("value0")); + expect(newAttributes[1].name, equals("attr2")); + expect(newAttributes[1].value, equals("value2")); + expect(newAttributes, isNot(equals(oldAttributes))); + expect(oldAttributes[0].name, equals("attr0")); + expect(oldAttributes[0].value, equals("0")); + expect(oldAttributes[1].name, equals("attr1")); + expect(oldAttributes[1].value, equals("1")); + expect(oldAttributes[2].name, equals("attr2")); + expect(oldAttributes[2].value, equals("2")); + }); +} diff --git a/packages/unit/test/engine/canvas_test.dart b/packages/unit/test/engine/canvas_test.dart new file mode 100644 index 0000000000..25df2bb299 --- /dev/null +++ b/packages/unit/test/engine/canvas_test.dart @@ -0,0 +1,25 @@ +import 'dart:sky' as sky; +import 'dart:sky' show Rect, Color, Paint; + +import 'package:test/test.dart'; +import 'package:vector_math/vector_math.dart'; + +void main() { + + sky.PictureRecorder recorder = new sky.PictureRecorder(); + sky.Canvas canvas = new sky.Canvas(recorder, new Rect.fromLTRB(0.0, 0.0, 100.0, 100.0)); + + test("matrix access should work", () { + // Matrix equality doesn't work! + // https://github.com/google/vector_math.dart/issues/147 + expect(canvas.getTotalMatrix(), equals(new Matrix4.identity().storage)); + Matrix4 matrix = new Matrix4.identity(); + // Round-tripping through getTotalMatrix will lose the z value + // So only scale to 1x in the z direction. + matrix.scale(2.0, 2.0, 1.0); + canvas.setMatrix(matrix.storage); + canvas.drawPaint(new Paint()..color = const Color(0xFF00FF00)); + expect(canvas.getTotalMatrix(), equals(matrix.storage)); + }); + +} diff --git a/packages/unit/test/engine/color_test.dart b/packages/unit/test/engine/color_test.dart new file mode 100644 index 0000000000..53a74072a0 --- /dev/null +++ b/packages/unit/test/engine/color_test.dart @@ -0,0 +1,40 @@ +import 'dart:sky'; + +import 'package:test/test.dart'; + +void main() { + test("color accessors should work", () { + Color foo = new Color(0x12345678); + expect(foo.alpha, equals(0x12)); + expect(foo.red, equals(0x34)); + expect(foo.green, equals(0x56)); + expect(foo.blue, equals(0x78)); + }); + + test("paint set to black", () { + Color c = new Color(0x00000000); + Paint p = new Paint(); + p.color = c; + expect(c.toString(), equals('Color(0x00000000)')); + }); + + test("color created with out of bounds value", () { + try { + Color c = new Color(0x100 << 24); + Paint p = new Paint(); + p.color = c; + } catch (e) { + expect(e != null, equals(true)); + } + }); + + test("color created with wildly out of bounds value", () { + try { + Color c = new Color(1 << 1000000); + Paint p = new Paint(); + p.color = c; + } catch (e) { + expect(e != null, equals(true)); + } + }); +} diff --git a/packages/unit/test/engine/document_child_mutations_test.dart b/packages/unit/test/engine/document_child_mutations_test.dart new file mode 100644 index 0000000000..d36937b0df --- /dev/null +++ b/packages/unit/test/engine/document_child_mutations_test.dart @@ -0,0 +1,99 @@ +import 'dart:sky'; + +import 'package:test/test.dart'; + +import 'dom_utils.dart'; + +void main() { + var doc; + + setUp(() { + doc = new Document(); + }); + + test("should allow replacing the document element", () { + var oldChild = doc.appendChild(doc.createElement("div")); + expect(childElementCount(doc), equals(1)); + var newChild = doc.createElement("div"); + oldChild.replaceWith([newChild]); + expect(childElementCount(doc), equals(1)); + expect(newChild.parentNode, equals(doc)); + expect(oldChild.parentNode, isNull); + }); + + test("should allow replacing a text child with an element", () { + var oldChild = doc.appendChild(doc.createText("text here")); + expect(childElementCount(doc), equals(0)); + expect(childNodeCount(doc), equals(1)); + var newChild = doc.createElement("div"); + oldChild.replaceWith([newChild]); + expect(childElementCount(doc), equals(1)); + expect(childNodeCount(doc), equals(1)); + expect(newChild.parentNode, equals(doc)); + expect(oldChild.parentNode, isNull); + }); + + test("should allow replacing the document element with text", () { + var oldChild = doc.appendChild(doc.createElement("div")); + expect(childElementCount(doc), equals(1)); + var newChild = doc.createText(" text "); + oldChild.replaceWith([newChild]); + expect(childElementCount(doc), equals(0)); + expect(childNodeCount(doc), equals(1)); + expect(newChild.parentNode, equals(doc)); + expect(oldChild.parentNode, isNull); + }); + + test("should allow inserting text with a fragment", () { + var fragment = doc.createDocumentFragment(); + fragment.appendChild(doc.createText(" text ")); + fragment.appendChild(doc.createText(" text ")); + expect(childNodeCount(doc), equals(0)); + doc.appendChild(fragment); + expect(childElementCount(doc), equals(0)); + expect(childNodeCount(doc), equals(2)); + }); + + test("should allow replacing the document element with a fragment", () { + var oldChild = doc.appendChild(doc.createElement("div")); + expect(childElementCount(doc), equals(1)); + var fragment = doc.createDocumentFragment(); + fragment.appendChild(doc.createText(" text ")); + var newChild = fragment.appendChild(doc.createElement("div")); + fragment.appendChild(doc.createText(" ")); + oldChild.replaceWith([fragment]); + expect(childElementCount(doc), equals(1)); + expect(childNodeCount(doc), equals(3)); + expect(newChild.parentNode, equals(doc)); + expect(oldChild.parentNode, isNull); + }); + + test("should throw when inserting multiple elements", () { + doc.appendChild(doc.createElement("div")); + var oldChild = doc.appendChild(doc.createText(" text ")); + expect(childElementCount(doc), equals(1)); + var newChild = doc.createElement("div"); + // expect(() { + // doc.replaceChild(newChild, 0); + // }, throws); + // expect(() { + // doc.insertBefore(newChild, oldChild); + // }, throws); + }); + + test("should throw when inserting multiple elements with a fragment", () { + var oldChild = doc.appendChild(doc.createElement("div")); + expect(childElementCount(doc), equals(1)); + var fragment = doc.createDocumentFragment(); + fragment.appendChild(doc.createText(" text ")); + fragment.appendChild(doc.createElement("div")); + fragment.appendChild(doc.createElement("div")); + fragment.appendChild(doc.createText(" ")); + // expect(() { + // doc.replaceChild(fragment, 0); + // }, throws); + // expect(() { + // doc.insertBefore(fragment, oldChild); + // }, throws); + }); +} diff --git a/packages/unit/test/engine/dom_utils.dart b/packages/unit/test/engine/dom_utils.dart new file mode 100644 index 0000000000..ceb96907db --- /dev/null +++ b/packages/unit/test/engine/dom_utils.dart @@ -0,0 +1,15 @@ +import 'dart:sky'; + +int childNodeCount(parent) { + int count = 0; + for (Node node = parent.firstChild; node != null; node = node.nextSibling) + ++count; + return count; +} + +int childElementCount(parent) { + int count = 0; + for (Element element = parent.firstElementChild; element != null; element = element.nextElementSibling) + ++count; + return count; +} diff --git a/packages/unit/test/engine/get_child_elements_test.dart b/packages/unit/test/engine/get_child_elements_test.dart new file mode 100644 index 0000000000..0161c56980 --- /dev/null +++ b/packages/unit/test/engine/get_child_elements_test.dart @@ -0,0 +1,23 @@ +import 'dart:sky'; + +import 'package:test/test.dart'; + +void main() { + test("getChildElements should only include immediate children", () { + var doc = new Document(); + var parent = doc.createElement('parent'); + var child1 = doc.createElement('child1'); + var child2 = doc.createElement('child1'); + var grandchild = doc.createElement('grandchild'); + + doc.appendChild(parent); + parent.appendChild(child1); + parent.appendChild(child2); + child1.appendChild(grandchild); + + var children = parent.getChildElements(); + expect(children.length, equals(2)); + expect(children[0], equals(child1)); + expect(children[1], equals(child2)); + }); +} diff --git a/packages/unit/test/engine/inline_style_test.dart b/packages/unit/test/engine/inline_style_test.dart new file mode 100644 index 0000000000..148ffca8db --- /dev/null +++ b/packages/unit/test/engine/inline_style_test.dart @@ -0,0 +1,38 @@ +import 'dart:sky' as sky; + +import 'package:test/test.dart'; + +void main() { + test('should be settable using "style" attribute', () { + sky.LayoutRoot layoutRoot = new sky.LayoutRoot(); + var document = new sky.Document(); + var foo = document.createElement('foo'); + layoutRoot.rootElement = foo; + + foo.setAttribute('style', 'color: red'); + + expect(foo.getAttribute('style'), equals('color: red')); + expect(foo.style["color"], equals('rgb(255, 0, 0)')); + }); + + test('should not crash when setting style to null', () { + sky.LayoutRoot layoutRoot = new sky.LayoutRoot(); + var document = new sky.Document(); + var foo = document.createElement('foo'); + layoutRoot.rootElement = foo; + + expect(foo.style['color'], isNull); + foo.style["color"] = null; // This used to crash. + expect(foo.style['color'], isNull); + foo.style["color"] = "blue"; + expect(foo.style['color'], equals("rgb(0, 0, 255)")); + foo.style["color"] = null; + expect(foo.style['color'], isNull); + foo.style["color"] = "blue"; + expect(foo.style['color'], equals("rgb(0, 0, 255)")); + foo.style.removeProperty("color"); + expect(foo.style['color'], isNull); + + layoutRoot.layout(); + }); +} diff --git a/packages/unit/test/engine/insert_before_test.dart b/packages/unit/test/engine/insert_before_test.dart new file mode 100644 index 0000000000..2e6a5fa1e2 --- /dev/null +++ b/packages/unit/test/engine/insert_before_test.dart @@ -0,0 +1,21 @@ +import 'dart:sky'; + +import 'package:test/test.dart'; + +void main() { + Document document = new Document(); + + test("should throw with invalid arguments", () { + var parent = document.createElement("div"); + var child = document.createElement("div"); + parent.appendChild(child); + // TODO(eseidel): This should throw! + // expect(() { + // parent.insertBefore([parent]); + // }, throws); + expect(() { + child.insertBefore([parent]); + }, throws); + }); + +} diff --git a/packages/unit/test/engine/owner_scope_test.dart b/packages/unit/test/engine/owner_scope_test.dart new file mode 100644 index 0000000000..6f3d69e650 --- /dev/null +++ b/packages/unit/test/engine/owner_scope_test.dart @@ -0,0 +1,17 @@ +import 'dart:sky'; + +import 'package:test/test.dart'; + +void main() { + test("should return null for elements not a child of a scope", () { + var doc = new Document(); + var element = doc.createElement("div"); + expect(element.owner, isNull); + }); + test("should return the document for elements in the document scope", () { + var doc = new Document(); + var element = doc.createElement("div"); + doc.appendChild(element); + expect(element.owner, equals(doc)); + }); +} diff --git a/packages/unit/test/engine/replace_child_test.dart b/packages/unit/test/engine/replace_child_test.dart new file mode 100644 index 0000000000..d275693c3b --- /dev/null +++ b/packages/unit/test/engine/replace_child_test.dart @@ -0,0 +1,54 @@ +import 'dart:sky'; + +import 'package:test/test.dart'; + +import 'dom_utils.dart'; + +void main() { + var document = new Document(); + + test("should replace elements", () { + var parent = document.createElement("div"); + var oldChild = parent.appendChild(document.createElement("div")); + var newChild = document.createElement("div"); + oldChild.replaceWith([newChild]); + expect(oldChild.parentNode, isNull); + expect(newChild.parentNode, equals(parent)); + }); + + test("should replace text", () { + var parent = document.createElement("div"); + var oldChild = parent.appendChild(document.createText(" it's a text ")); + var newChild = document.createElement("div"); + oldChild.replaceWith([newChild]); + expect(oldChild.parentNode, isNull); + expect(newChild.parentNode, equals(parent)); + }); + + test("should replace children with a fragment", () { + var fragment = document.createDocumentFragment(); + var child1 = fragment.appendChild(document.createElement("div")); + var child2 = fragment.appendChild(document.createText(" text ")); + var child3 = fragment.appendChild(document.createText(" ")); + var child4 = fragment.appendChild(document.createElement("div")); + var parent = document.createElement("div"); + var oldChild = parent.appendChild(document.createElement("div")); + var lastChild = parent.appendChild(document.createElement("div")); + oldChild.replaceWith([fragment]); + expect(child1.parentNode, equals(parent)); + expect(child2.parentNode, equals(parent)); + expect(child3.parentNode, equals(parent)); + expect(child4.parentNode, equals(parent)); + expect(oldChild.parentNode, isNull); + expect(childNodeCount(parent), equals(5)); + expect(childElementCount(parent), equals(3)); + expect(parent.lastChild, equals(lastChild)); + }); + + // test("should throw when appending to a text", () { + // var parent = new Text(); + // expect(() { + // parent.replaceChild(document.createElement("div"), null); + // }, throws); + // }); +} diff --git a/packages/unit/test/engine/text_test.dart b/packages/unit/test/engine/text_test.dart new file mode 100644 index 0000000000..acec02a66c --- /dev/null +++ b/packages/unit/test/engine/text_test.dart @@ -0,0 +1,10 @@ +import 'dart:sky' as sky; + +import 'package:test/test.dart'; + +void main() { + test("createText(null) shouldn't crash", () { + var doc = new sky.Document(); + doc.createText(null); + }); +}