Index: native_client_sdk/src/examples/api/messaging/test.js |
diff --git a/native_client_sdk/src/examples/api/messaging/test.js b/native_client_sdk/src/examples/api/messaging/test.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..dbcecfd867370161738c912bd9fd0a47aa36a465 |
--- /dev/null |
+++ b/native_client_sdk/src/examples/api/messaging/test.js |
@@ -0,0 +1,65 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+function addTests() { |
+ var currentTest = null; |
+ |
+ function dispatchClick(id) { |
+ currentTest.log('Clicking button "' + id + '".'); |
+ $(id).dispatchEvent(new MouseEvent('click')); |
+ } |
+ |
+ function setInputValue(id, value) { |
+ currentTest.log('Setting input box "' + id + '" to "' + value + '".'); |
+ $(id).value = value; |
+ } |
+ |
+ function clearResult() { |
+ $('result').textContent = ''; |
+ } |
+ |
+ function getResult() { |
+ return $('result').textContent; |
+ } |
+ |
+ function waitForResult(expected) { |
+ var intervalId = window.setInterval(function() { |
+ var actual = parseInt(getResult()); |
+ |
+ if (result === '') { |
+ currentTest.log('No result yet, waiting.'); |
+ return; |
+ } |
+ |
+ // Got a result. |
+ window.clearInterval(intervalId); |
+ |
+ if (actual === expected) { |
+ currentTest.log('Got expected value (' + expected + ').'); |
+ currentTest.pass(); |
+ } else { |
+ currentTest.fail('Unexpected value ' + actual + ', expected ' + |
+ expected); |
+ } |
+ }, 100); |
+ } |
+ |
+ common.tester.addAsyncTest('async_message', function(test) { |
+ currentTest = test; |
+ clearResult(); |
+ setInputValue('addend1', 1234); |
+ setInputValue('addend2', 2345); |
+ dispatchClick('addAsync'); |
+ waitForResult(3579); |
+ }); |
+ |
+ common.tester.addAsyncTest('sync_message', function(test) { |
+ currentTest = test; |
+ clearResult(); |
+ setInputValue('addend1', 42); |
+ setInputValue('addend2', 314); |
+ dispatchClick('addSync'); |
+ waitForResult(356); |
+ }); |
+} |