| <!DOCTYPE html> |
| <link rel="help" href="https://github.com/samuelgoto/sms-receiver"> |
| <title>Tests the SMS Receiver API</title> |
| <script src="/resources/testharness.js"></script> |
| <script src="/resources/testharnessreport.js"></script> |
| <!-- |
| sms_provider.js is a testing framework that enables engines to test the sms |
| receiver API by intercepting the connection between the browser and the |
| underlying operating system and mock its behavior. |
| |
| Usage: |
| |
| 1) Include <script src="./resources/helper.js"></script> in your test. |
| 2) Set expectations |
| await expect(receive).andReturn(() => { |
| // mock behavior |
| }) |
| 3) Call navigator.sms.receive() |
| 4) Verify results |
| |
| The mocking API is browser agnostic and is designed such that other engines |
| could implement it too. |
| |
| Here are the symbols that are exposed to tests that need to be implemented |
| per engine: |
| |
| - function receive(): the main/only function that can be mocked. |
| - function expect(): the main/only function that enables us to mock it. |
| - enum State {kSuccess, kTimeout}: allows you to mock success/failures. |
| |
| --> |
| <script src="./resources/helper.js"></script> |
| <script> |
| 'use strict'; |
| |
| promise_test(async t => { |
| await expect(receive).andReturn(() => { |
| return Promise.resolve({ |
| status: Status.kSuccess, |
| message: "hello", |
| }); |
| }); |
| |
| let sms = await navigator.sms.receive(); |
| |
| assert_equals(sms.content, "hello"); |
| }, 'Basic usage'); |
| |
| promise_test(async t => { |
| await expect(receive).andReturn(() => { |
| return Promise.resolve({ |
| status: Status.kSuccess, |
| message: "hello1", |
| }); |
| }); |
| await expect(receive).andReturn(() => { |
| return Promise.resolve({ |
| status: Status.kSuccess, |
| message: "hello2", |
| }); |
| }); |
| |
| let sms1 = navigator.sms.receive(); |
| let sms2 = navigator.sms.receive(); |
| |
| let msg2 = await sms2; |
| let msg1 = await sms1; |
| |
| assert_equals(msg1.content, "hello1"); |
| assert_equals(msg2.content, "hello2"); |
| }, 'Handle multiple requests in different order.'); |
| |
| promise_test(async t => { |
| await expect(receive).andReturn(() => { |
| return Promise.resolve({ |
| status: Status.kCancelled, |
| }); |
| }); |
| await expect(receive).andReturn(() => { |
| return Promise.resolve({ |
| status: Status.kSuccess, |
| message: "success", |
| }); |
| }); |
| |
| let cancelled_sms = navigator.sms.receive(); |
| let successful_sms = navigator.sms.receive(); |
| |
| let successful_msg = await successful_sms; |
| assert_equals(successful_msg.content, "success"); |
| |
| try { |
| await cancelled_sms; |
| assert_unreached('Expected AbortError to be thrown.'); |
| } catch (error) { |
| assert_equals(error.name, "AbortError"); |
| assert_equals(error.message, "SMSReceiver was aborted."); |
| } |
| }, 'Handle multiple requests with success and error.'); |
| |
| promise_test(async t => { |
| await expect(receive).andReturn(() => { |
| return Promise.resolve({ |
| status: Status.kCancelled, |
| }); |
| }); |
| |
| try { |
| await navigator.sms.receive(); |
| assert_unreached('Expected CancelledError to be thrown.'); |
| } catch (error) { |
| assert_equals(error.name, "AbortError"); |
| assert_equals(error.message, "SMSReceiver was aborted."); |
| } |
| }, 'Deal with cancelled requests'); |
| |
| promise_test(async t => { |
| const controller = new AbortController(); |
| const signal = controller.signal; |
| |
| controller.abort(); |
| await promise_rejects(t, 'AbortError', navigator.sms.receive({signal})); |
| }, 'Should abort request'); |
| |
| promise_test(async t => { |
| const controller = new AbortController(); |
| const signal = controller.signal; |
| |
| let error = navigator.sms.receive({signal}); |
| controller.abort(); |
| await promise_rejects(t, 'AbortError', error); |
| }, 'Should abort request even while request is in progress.'); |
| |
| </script> |