| 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 | |
| 6 #library('dartcombat_tests'); | |
| 7 | |
| 8 #import('dart:html'); | |
| 9 #import('dart:isolate'); | |
| 10 #import('../../../../../lib/unittest/unittest.dart'); | |
| 11 #import('../../../../../lib/unittest/html_config.dart'); | |
| 12 #import('../../../../dartcombat/dartcombatlib.dart'); | |
| 13 | |
| 14 ReceivePort testPort; | |
| 15 | |
| 16 main() { | |
| 17 useHtmlConfiguration(); | |
| 18 testPort = new ReceivePort(); | |
| 19 | |
| 20 setupUI(); | |
| 21 // add relative URL to stylesheet to the correct location. Note: The CSS is | |
| 22 // needed because the app uses a flexbox layout, which is then used to | |
| 23 // interpret UI events (mousedown/up/click). | |
| 24 var link = new Element.tag('link'); | |
| 25 link.rel = 'stylesheet'; | |
| 26 link.type = 'text/css'; | |
| 27 link.href = '../../../client/samples/dartcombat/dartcombat.css'; | |
| 28 document.head.nodes.add(link); | |
| 29 | |
| 30 asyncTest('wait until setup', 1, () { | |
| 31 int playersReady = 0; | |
| 32 testPort.receive((message, SendPort reply) { | |
| 33 if (message[0] == '_TEST:handleSetup') { | |
| 34 playersReady++; | |
| 35 if (playersReady == 2) { | |
| 36 callbackDone(); | |
| 37 } | |
| 38 } | |
| 39 }); | |
| 40 createPlayersForTest(testPort.toSendPort()); | |
| 41 }); | |
| 42 | |
| 43 | |
| 44 asyncTest('parallel shoot', 1, () { | |
| 45 // player 2 is configured to make parallel super shots towards player 1 | |
| 46 var p1OwnBoard = document.query('#p1own'); | |
| 47 | |
| 48 // add a boat of length 4 using mousedown/mousemove/mouseup | |
| 49 var startCell = p1OwnBoard.nodes[0].nodes[4].nodes[2]; | |
| 50 var endCell = p1OwnBoard.nodes[0].nodes[4].nodes[5]; | |
| 51 _serialInvokeAsync([ | |
| 52 () => doMouseEvent("mousedown", startCell), | |
| 53 () => doMouseEvent("mousemove", endCell), | |
| 54 () => doMouseEvent("mouseup", endCell), | |
| 55 () { | |
| 56 // check that the boat was added: | |
| 57 var boat = p1OwnBoard.nodes[1]; | |
| 58 expect(boat.classes).equalsSet(['icons', 'boat4', 'boatdir-left']); | |
| 59 | |
| 60 // check that the boat shows as sunk in player 1's board: | |
| 61 // Note that the shoot order is respected: center, left, right, up, down, | |
| 62 // left, right again, as if they progress in parallel. | |
| 63 List<int> expectedShots = const [ | |
| 64 Constants.HIT, 3, 4, // initial shot (center) | |
| 65 Constants.HIT, 2, 4, // left | |
| 66 Constants.HIT, 4, 4, // right | |
| 67 Constants.MISS, 3, 3, // up | |
| 68 Constants.MISS, 3, 5, // down | |
| 69 Constants.MISS, 1, 4, // left | |
| 70 Constants.SUNK, 5, 4]; // right | |
| 71 _expectShotSequence(expectedShots, p1OwnBoard, 1); | |
| 72 | |
| 73 // hit the boat from the enemy side. | |
| 74 var p2EnemyBoard = document.query("#p2enemy"); | |
| 75 var hitCell = p2EnemyBoard.nodes[0].nodes[4].nodes[3]; | |
| 76 doMouseEvent("click", hitCell); | |
| 77 }]); | |
| 78 }); | |
| 79 | |
| 80 asyncTest('serial shoot', 1, () { | |
| 81 // player 1 is configured to make serial super shots towards player 2 | |
| 82 var p2OwnBoard = document.query('#p2own'); | |
| 83 | |
| 84 // add a boat of length 4 using mousedown/mousemove/mouseup | |
| 85 var startCell = p2OwnBoard.nodes[0].nodes[3].nodes[8]; | |
| 86 var endCell = p2OwnBoard.nodes[0].nodes[7].nodes[8]; | |
| 87 _serialInvokeAsync([ | |
| 88 () => doMouseEvent("mousedown", startCell), | |
| 89 () => doMouseEvent("mousemove", endCell), | |
| 90 () => doMouseEvent("mouseup", endCell), | |
| 91 () { | |
| 92 // check that the boat was added: | |
| 93 var boat = p2OwnBoard.nodes[1]; | |
| 94 expect(boat.classes).equalsSet(['icons', 'boat5', 'boatdir-down']); | |
| 95 | |
| 96 // check that the boat shows as sunk in player 2's board: | |
| 97 // Note that the shoot order is respected: center, left, right, up, down | |
| 98 // sequentially. | |
| 99 List<int> expectedShots = const [ | |
| 100 Constants.HIT, 8, 4, // initial shot (center) | |
| 101 Constants.MISS, 7, 4, // left (miss - stop this direction) | |
| 102 Constants.MISS, 9, 4, // right (miss - stop this direction) | |
| 103 Constants.HIT, 8, 3, // up | |
| 104 Constants.MISS, 8, 2, // up (miss - stop this direction) | |
| 105 Constants.HIT, 8, 5, // down | |
| 106 Constants.HIT, 8, 6, // down | |
| 107 Constants.SUNK, 8, 7]; // down (sunk - done) | |
| 108 _expectShotSequence(expectedShots, p2OwnBoard, 2); | |
| 109 | |
| 110 // hit the boat from the enemy side. | |
| 111 var p1EnemyBoard = document.query("#p1enemy"); | |
| 112 var hitCell = p1EnemyBoard.nodes[0].nodes[4].nodes[8]; | |
| 113 doMouseEvent("click", hitCell); | |
| 114 }]); | |
| 115 }); | |
| 116 } | |
| 117 | |
| 118 _expectShotSequence(List<int> expectedShots, Element playerBoard, int id) { | |
| 119 int shots = 0; | |
| 120 testPort.receive((message, SendPort reply) { | |
| 121 if (message[0] == '_TEST:handleShot') { | |
| 122 expect(message[1]).equals(id); | |
| 123 expect(message[2]).equals(expectedShots[(shots * 3)]); | |
| 124 expect(message[3]).equals(expectedShots[(shots * 3) + 1]); | |
| 125 expect(message[4]).equals(expectedShots[(shots * 3) + 2]); | |
| 126 _checkNodeInfo(playerBoard.nodes[shots + 2], | |
| 127 'icons ' + (expectedShots[shots * 3] == Constants.MISS | |
| 128 ? 'miss' : 'hit-onboat'), | |
| 129 expectedShots[(shots * 3) + 1] * 50, | |
| 130 expectedShots[(shots * 3) + 2] * 50); | |
| 131 shots++; | |
| 132 if (shots * 3 == expectedShots.length) { | |
| 133 callbackDone(); | |
| 134 } | |
| 135 } | |
| 136 }); | |
| 137 } | |
| 138 | |
| 139 _checkNodeInfo(node, className, x, y) { | |
| 140 expect(node.classes).equalsSet(className.split(' ')); | |
| 141 expect(node.style.getPropertyValue('left')).equals('${x}px'); | |
| 142 expect(node.style.getPropertyValue('top')).equals('${y}px'); | |
| 143 } | |
| 144 | |
| 145 doMouseEvent(String type, var targetCell) { | |
| 146 final point = window.webkitConvertPointFromNodeToPage(targetCell, | |
| 147 new Point(5, 5)); | |
| 148 | |
| 149 MouseEvent e = new MouseEvent(type, window, 0, 0, 0, point.x, point.y, 0, | |
| 150 relatedTarget: targetCell); | |
| 151 targetCell.on[type].dispatch(e); | |
| 152 } | |
| 153 | |
| 154 void _serialInvokeAsync(List closures) { | |
| 155 final length = closures.length; | |
| 156 if (length > 0) { | |
| 157 int i = 0; | |
| 158 void invokeNext() { | |
| 159 closures[i](); | |
| 160 i++; | |
| 161 if (i < length) { | |
| 162 window.setTimeout(invokeNext, 0); | |
| 163 } | |
| 164 } | |
| 165 window.setTimeout(invokeNext, 0); | |
| 166 } | |
| 167 } | |
| OLD | NEW |