| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 | 5 |
| 6 #library('dartcombat_tests'); | 6 #library('dartcombat_tests'); |
| 7 | 7 |
| 8 #import('dart:html'); | 8 #import('dart:html'); |
| 9 #import('../../../../testing/unittest/unittest.dart'); | 9 #import('../../../../testing/unittest/unittest.dart'); |
| 10 #import('../../../../samples/dartcombat/dartcombatlib.dart'); | 10 #import('../../../../samples/dartcombat/dartcombatlib.dart'); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 createPlayersForTest(testPort.toSendPort()); | 49 createPlayersForTest(testPort.toSendPort()); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void testParallelShoot() { | 52 void testParallelShoot() { |
| 53 // player 2 is configured to make parallel super shots towards player 1 | 53 // player 2 is configured to make parallel super shots towards player 1 |
| 54 var p1OwnBoard = document.query("#p1own"); | 54 var p1OwnBoard = document.query("#p1own"); |
| 55 | 55 |
| 56 // add a boat of length 4 using mousedown/mousemove/mouseup | 56 // add a boat of length 4 using mousedown/mousemove/mouseup |
| 57 var startCell = p1OwnBoard.nodes[0].nodes[4].nodes[2]; | 57 var startCell = p1OwnBoard.nodes[0].nodes[4].nodes[2]; |
| 58 var endCell = p1OwnBoard.nodes[0].nodes[4].nodes[5]; | 58 var endCell = p1OwnBoard.nodes[0].nodes[4].nodes[5]; |
| 59 doMouseEvent("mousedown", startCell); | 59 serialInvokeAsync([ |
| 60 doMouseEvent("mousemove", endCell); | 60 () => doMouseEvent("mousedown", startCell), |
| 61 doMouseEvent("mouseup", endCell); | 61 () => doMouseEvent("mousemove", endCell), |
| 62 () => doMouseEvent("mouseup", endCell), |
| 63 () { |
| 64 // check that the boat was added: |
| 65 var boat = p1OwnBoard.nodes[1]; |
| 66 Expect.setEquals(["icons", "boat4", "boatdir-left"], boat.classes); |
| 62 | 67 |
| 63 // check that the boat was added: | 68 // check that the boat shows as sunk in player 1's board: |
| 64 var boat = p1OwnBoard.nodes[1]; | 69 // Note that the shoot order is respected: center, left, right, up, down, |
| 65 Expect.setEquals(["icons", "boat4", "boatdir-left"], boat.classes); | 70 // left, right again, as if they progress in parallel. |
| 71 List<int> expectedShots = const [ |
| 72 Constants.HIT, 3, 4, // initial shot (center) |
| 73 Constants.HIT, 2, 4, // left |
| 74 Constants.HIT, 4, 4, // right |
| 75 Constants.MISS, 3, 3, // up |
| 76 Constants.MISS, 3, 5, // down |
| 77 Constants.MISS, 1, 4, // left |
| 78 Constants.SUNK, 5, 4]; // right |
| 79 _expectShotSequence(expectedShots, p1OwnBoard, 1); |
| 66 | 80 |
| 67 // check that the boat shows as sunk in player 1's board: | 81 // hit the boat from the enemy side. |
| 68 // Note that the shoot order is respected: center, left, right, up, down, | 82 var p2EnemyBoard = document.query("#p2enemy"); |
| 69 // left, right again, as if they progress in parallel. | 83 var hitCell = p2EnemyBoard.nodes[0].nodes[4].nodes[3]; |
| 70 List<int> expectedShots = const [ | 84 doMouseEvent("click", hitCell); |
| 71 Constants.HIT, 3, 4, // initial shot (center) | 85 }]); |
| 72 Constants.HIT, 2, 4, // left | |
| 73 Constants.HIT, 4, 4, // right | |
| 74 Constants.MISS, 3, 3, // up | |
| 75 Constants.MISS, 3, 5, // down | |
| 76 Constants.MISS, 1, 4, // left | |
| 77 Constants.SUNK, 5, 4]; // right | |
| 78 _expectShotSequence(expectedShots, p1OwnBoard, 1); | |
| 79 | |
| 80 // hit the boat from the enemy side. | |
| 81 var p2EnemyBoard = document.query("#p2enemy"); | |
| 82 var hitCell = p2EnemyBoard.nodes[0].nodes[4].nodes[3]; | |
| 83 doMouseEvent("click", hitCell); | |
| 84 } | 86 } |
| 85 | 87 |
| 86 void testSerialShoot() { | 88 void testSerialShoot() { |
| 87 // player 1 is configured to make serial super shots towards player 2 | 89 // player 1 is configured to make serial super shots towards player 2 |
| 88 var p2OwnBoard = document.query("#p2own"); | 90 var p2OwnBoard = document.query("#p2own"); |
| 89 | 91 |
| 90 // add a boat of length 4 using mousedown/mousemove/mouseup | 92 // add a boat of length 4 using mousedown/mousemove/mouseup |
| 91 var startCell = p2OwnBoard.nodes[0].nodes[3].nodes[8]; | 93 var startCell = p2OwnBoard.nodes[0].nodes[3].nodes[8]; |
| 92 var endCell = p2OwnBoard.nodes[0].nodes[7].nodes[8]; | 94 var endCell = p2OwnBoard.nodes[0].nodes[7].nodes[8]; |
| 93 doMouseEvent("mousedown", startCell); | 95 serialInvokeAsync([ |
| 94 doMouseEvent("mousemove", endCell); | 96 () => doMouseEvent("mousedown", startCell), |
| 95 doMouseEvent("mouseup", endCell); | 97 () => doMouseEvent("mousemove", endCell), |
| 98 () => doMouseEvent("mouseup", endCell), |
| 99 () { |
| 100 // check that the boat was added: |
| 101 var boat = p2OwnBoard.nodes[1]; |
| 102 Expect.setEquals(["icons", "boat5", "boatdir-down"], boat.classes); |
| 96 | 103 |
| 97 // check that the boat was added: | 104 // check that the boat shows as sunk in player 2's board: |
| 98 var boat = p2OwnBoard.nodes[1]; | 105 // Note that the shoot order is respected: center, left, right, up, down |
| 99 Expect.setEquals(["icons", "boat5", "boatdir-down"], boat.classes); | 106 // sequentially. |
| 107 List<int> expectedShots = const [ |
| 108 Constants.HIT, 8, 4, // initial shot (center) |
| 109 Constants.MISS, 7, 4, // left (miss - stop this direction) |
| 110 Constants.MISS, 9, 4, // right (miss - stop this direction) |
| 111 Constants.HIT, 8, 3, // up |
| 112 Constants.MISS, 8, 2, // up (miss - stop this direction) |
| 113 Constants.HIT, 8, 5, // down |
| 114 Constants.HIT, 8, 6, // down |
| 115 Constants.SUNK, 8, 7]; // down (sunk - done) |
| 116 _expectShotSequence(expectedShots, p2OwnBoard, 2); |
| 100 | 117 |
| 101 // check that the boat shows as sunk in player 2's board: | 118 // hit the boat from the enemy side. |
| 102 // Note that the shoot order is respected: center, left, right, up, down | 119 var p1EnemyBoard = document.query("#p1enemy"); |
| 103 // sequentially. | 120 var hitCell = p1EnemyBoard.nodes[0].nodes[4].nodes[8]; |
| 104 List<int> expectedShots = const [ | 121 doMouseEvent("click", hitCell); |
| 105 Constants.HIT, 8, 4, // initial shot (center) | 122 }]); |
| 106 Constants.MISS, 7, 4, // left (miss - stop this direction) | |
| 107 Constants.MISS, 9, 4, // right (miss - stop this direction) | |
| 108 Constants.HIT, 8, 3, // up | |
| 109 Constants.MISS, 8, 2, // up (miss - stop this direction) | |
| 110 Constants.HIT, 8, 5, // down | |
| 111 Constants.HIT, 8, 6, // down | |
| 112 Constants.SUNK, 8, 7]; // down (sunk - done) | |
| 113 _expectShotSequence(expectedShots, p2OwnBoard, 2); | |
| 114 | |
| 115 // hit the boat from the enemy side. | |
| 116 var p1EnemyBoard = document.query("#p1enemy"); | |
| 117 var hitCell = p1EnemyBoard.nodes[0].nodes[4].nodes[8]; | |
| 118 doMouseEvent("click", hitCell); | |
| 119 } | 123 } |
| 120 | 124 |
| 121 void _expectShotSequence( | 125 void _expectShotSequence( |
| 122 List<int> expectedShots, Element playerBoard, int id) { | 126 List<int> expectedShots, Element playerBoard, int id) { |
| 123 int shots = 0; | 127 int shots = 0; |
| 124 testPort.receive((message, SendPort reply) { | 128 testPort.receive((message, SendPort reply) { |
| 125 if (message[0] == '_TEST:handleShot') { | 129 if (message[0] == '_TEST:handleShot') { |
| 126 Expect.equals(id, message[1]); | 130 Expect.equals(id, message[1]); |
| 127 Expect.equals(expectedShots[(shots * 3)], message[2]); | 131 Expect.equals(expectedShots[(shots * 3)], message[2]); |
| 128 Expect.equals(expectedShots[(shots * 3) + 1], message[3]); | 132 Expect.equals(expectedShots[(shots * 3) + 1], message[3]); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 150 void doMouseEvent(String type, var targetCell) { | 154 void doMouseEvent(String type, var targetCell) { |
| 151 final point = window.webkitConvertPointFromNodeToPage(targetCell, | 155 final point = window.webkitConvertPointFromNodeToPage(targetCell, |
| 152 new Point(5, 5)); | 156 new Point(5, 5)); |
| 153 | 157 |
| 154 MouseEvent e = document.createEvent('MouseEvents'); | 158 MouseEvent e = document.createEvent('MouseEvents'); |
| 155 e.initMouseEvent(type, true, true, window, 0, 0, 0, point.x, point.y, | 159 e.initMouseEvent(type, true, true, window, 0, 0, 0, point.x, point.y, |
| 156 false, false, false, false, 0, targetCell); | 160 false, false, false, false, 0, targetCell); |
| 157 targetCell.on[type].dispatch(e); | 161 targetCell.on[type].dispatch(e); |
| 158 } | 162 } |
| 159 } | 163 } |
| OLD | NEW |