OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #library('view_tests'); |
| 6 |
| 7 #import('dart:html'); |
| 8 #import('../../../../../ui_lib/base/base.dart'); |
| 9 #import('../../../../../ui_lib/view/view.dart'); |
| 10 #import('../../../../../../client/testing/unittest/unittest.dart'); |
| 11 |
| 12 void main() { |
| 13 test('does not render immediately', () { |
| 14 final view = new TestView(); |
| 15 expect(view.isRendered).isFalse(); |
| 16 |
| 17 view.addToDocument(document.body); |
| 18 expect(view.isRendered).isTrue(); |
| 19 }); |
| 20 |
| 21 group('addToDocument()', () { |
| 22 test('causes view to render', () { |
| 23 final view = new TestView(); |
| 24 view.addToDocument(document.body); |
| 25 expect(view.isRendered).isTrue(); |
| 26 }); |
| 27 |
| 28 test('calls afterRender()', () { |
| 29 var result = ''; |
| 30 final view = new TestView(); |
| 31 view.renderFn = () { |
| 32 result += 'render'; |
| 33 return new Element.html('<div class="test"></div>'); |
| 34 }; |
| 35 |
| 36 view.afterRenderFn = (node) { result += 'after'; }; |
| 37 |
| 38 |
| 39 view.addToDocument(document.body); |
| 40 expect(result).equals('renderafter'); |
| 41 }); |
| 42 |
| 43 test('calls enterDocument()', () { |
| 44 final view = new TestView(); |
| 45 bool entered = false; |
| 46 view.enterDocumentFn = () { entered = true; }; |
| 47 |
| 48 view.addToDocument(document.body); |
| 49 expect(entered).isTrue(); |
| 50 }); |
| 51 }); |
| 52 |
| 53 group('afterRender()', () { |
| 54 test('passes rendered node', () { |
| 55 final rendered = new Element.html('<div class="node"></div>'); |
| 56 final view = new TestView(); |
| 57 view.renderFn = () => rendered; |
| 58 view.afterRenderFn = (node) { expect(node).equals(rendered); }; |
| 59 |
| 60 view.addToDocument(document.body); |
| 61 }); |
| 62 }); |
| 63 |
| 64 group('childViewAdded()', () { |
| 65 test('calls enterDocument() if parent is in document', () { |
| 66 final parent = new TestView(); |
| 67 parent.addToDocument(document.body); |
| 68 |
| 69 bool entered = false; |
| 70 final child = new TestView(); |
| 71 child.enterDocumentFn = () { entered = true; }; |
| 72 |
| 73 // Add the child. |
| 74 parent.childViews = [child]; |
| 75 parent.childViewAdded(child); |
| 76 |
| 77 expect(entered).isTrue(); |
| 78 }); |
| 79 |
| 80 test('does not call enterDocument() if parent is not in document', () { |
| 81 final parent = new TestView(); |
| 82 |
| 83 bool entered = false; |
| 84 final child = new TestView(); |
| 85 child.enterDocumentFn = () { entered = true; }; |
| 86 |
| 87 // Add the child. |
| 88 parent.childViews = [child]; |
| 89 parent.childViewAdded(child); |
| 90 |
| 91 expect(entered).isFalse(); |
| 92 }); |
| 93 |
| 94 test('calls enterDocument() each time added', () { |
| 95 final parent = new TestView(); |
| 96 parent.addToDocument(document.body); |
| 97 |
| 98 var entered = 0; |
| 99 final child = new TestView(); |
| 100 child.enterDocumentFn = () { entered++; }; |
| 101 |
| 102 // Add the child. |
| 103 parent.childViews = [child]; |
| 104 parent.childViewAdded(child); |
| 105 parent.childViewRemoved(child); |
| 106 parent.childViewAdded(child); |
| 107 parent.childViewRemoved(child); |
| 108 parent.childViewAdded(child); |
| 109 parent.childViewRemoved(child); |
| 110 |
| 111 expect(entered).equals(3); |
| 112 }); |
| 113 }); |
| 114 |
| 115 group('childViewRemoved()', () { |
| 116 test('calls exitDocument() if parent is in document', () { |
| 117 final parent = new TestView(); |
| 118 parent.addToDocument(document.body); |
| 119 |
| 120 bool exited = false; |
| 121 final child = new TestView(); |
| 122 child.exitDocumentFn = () { exited = true; }; |
| 123 |
| 124 // Remove the child. |
| 125 parent.childViews = []; |
| 126 parent.childViewRemoved(child); |
| 127 |
| 128 expect(exited).isTrue(); |
| 129 }); |
| 130 |
| 131 test('does not call exitDocument() if parent is not in document', () { |
| 132 final parent = new TestView(); |
| 133 |
| 134 bool exited = false; |
| 135 final child = new TestView(); |
| 136 child.exitDocumentFn = () { exited = true; }; |
| 137 |
| 138 // Remove the child. |
| 139 parent.childViews = []; |
| 140 parent.childViewRemoved(child); |
| 141 |
| 142 expect(exited).isFalse(); |
| 143 }); |
| 144 |
| 145 test('calls exitDocument() each time removed', () { |
| 146 final parent = new TestView(); |
| 147 parent.addToDocument(document.body); |
| 148 |
| 149 var exited = 0; |
| 150 final child = new TestView(); |
| 151 child.exitDocumentFn = () { exited++; }; |
| 152 |
| 153 // Add the child. |
| 154 parent.childViews = [child]; |
| 155 parent.childViewAdded(child); |
| 156 parent.childViewRemoved(child); |
| 157 parent.childViewAdded(child); |
| 158 parent.childViewRemoved(child); |
| 159 parent.childViewAdded(child); |
| 160 parent.childViewRemoved(child); |
| 161 |
| 162 expect(exited).equals(3); |
| 163 }); |
| 164 }); |
| 165 |
| 166 group('enterDocument()', () { |
| 167 test('children are called before parents', () { |
| 168 var result = ''; |
| 169 |
| 170 final parent = new TestView(); |
| 171 parent.enterDocumentFn = () { result += 'parent'; }; |
| 172 |
| 173 final child = new TestView(); |
| 174 child.enterDocumentFn = () { result += 'child'; }; |
| 175 |
| 176 parent.childViews = [child]; |
| 177 |
| 178 parent.addToDocument(document.body); |
| 179 expect(result).equals('childparent'); |
| 180 }); |
| 181 }); |
| 182 } |
| 183 |
| 184 class TestView extends View { |
| 185 Function renderFn; |
| 186 Function afterRenderFn; |
| 187 Function enterDocumentFn; |
| 188 Function exitDocumentFn; |
| 189 List<View> childViews; |
| 190 |
| 191 TestView() : super(), childViews = [] { |
| 192 // Default behavior. |
| 193 renderFn = () => new Element.html('<div class="test"></div>'); |
| 194 afterRenderFn = (node) {}; |
| 195 enterDocumentFn = () {}; |
| 196 exitDocumentFn = () {}; |
| 197 } |
| 198 |
| 199 Element render() => renderFn(); |
| 200 void afterRender(Element node) { afterRenderFn(node); } |
| 201 void enterDocument() { enterDocumentFn(); } |
| 202 void exitDocument() { exitDocumentFn(); } |
| 203 } |
OLD | NEW |