Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / app_banner / app-banner-event-prompt.html
blobf328d6af1343dd8cefe3f6cc938c2cb1eabd973c
1 <!DOCTYPE html>
2 <script src="../resources/testharness.js"></script>
3 <script src="../resources/testharnessreport.js"></script>
4 <script>
6 var prompt_test_cases = [
8 name: 'prompt-accept',
9 cancel: true,
10 late: false,
11 platform: 'foo',
12 outcome: 'accepted'
13 }, {
14 name: 'prompt-dismiss',
15 cancel: true,
16 late: false,
17 platform: '',
18 outcome: 'dismissed'
19 }, {
20 name: 'prompt-before-preventDefault',
21 cancel: false,
22 late: false,
23 platform: 'foo',
24 outcome: 'accepted'
25 }, {
26 name: 'prompt-late-accept',
27 cancel: true,
28 late: true,
29 platform: 'foo',
30 outcome: 'accepted'
31 }, {
32 name: 'prompt-late-dismiss',
33 cancel: true,
34 late: true,
35 platform: '',
36 outcome: 'dismissed'
37 }, {
38 name: 'prompt-late-without-preventDefault',
39 cancel: false,
40 late: true,
41 platform: '',
42 outcome: 'dismissed'
46 function verify_prompt_resolve(e, t) {
47 e.prompt().then(function() { }, t.unreached_func("prompt() promise should resolve."));
50 function verify_prompt_reject(e, t) {
51 e.prompt().then(t.unreached_func("prompt() promise should reject."),
52 t.step_func(function(error) {
53 assert_true(
54 error instanceof DOMException,
55 "Rejected promise should throw a DOMException."
57 assert_equals(
58 error.message,
59 "The prompt() method may only be called once, following preventDefault().",
60 "Rejected promise does not provide expected message."
66 function verify_userChoice(e, t, test_case, index) {
67 e.userChoice.then(t.step_func(function(result) {
68 assert_equals(result.platform, test_case.platform, 'Resolved platform');
69 assert_equals(result.outcome, test_case.outcome, 'Outcome');
70 })).then(t.step_func(function() {
71 verify_prompt_reject(e, t);
72 })).then(t.step_func(function() {
73 prompt_test(index + 1);
74 t.done();
75 }), t.unreached_func("userChoice promise should resolve."));
78 function prompt_test(index) {
79 if (index >= prompt_test_cases.length)
80 return;
82 var test_case = prompt_test_cases[index];
83 async_test(function(t) {
84 var event = null;
85 var event_handler = t.step_func(function(e) {
86 // Remove the event handler to prevent it being used in subsequent
87 // invocations of prompt_test(). Save event object for call outside handler.
88 window.removeEventListener('beforeinstallprompt', event_handler);
89 event = e;
91 assert_equals(e.platforms.length, 2, 'Number of platforms');
92 assert_equals(e.platforms[0], 'foo', 'First platform');
93 assert_equals(e.platforms[1], 'bar', 'Second platform');
95 if (test_case.cancel) {
96 e.preventDefault();
99 if (test_case.late) {
100 return;
103 if (test_case.cancel) {
104 // Call prompt() to restart the pipeline.
105 verify_prompt_resolve(e, t);
106 } else {
107 // A call to prompt() before preventDefault should reject.
108 verify_prompt_reject(e, t);
111 // prompt() has been fired or the event has not been canceled, so check
112 // the userChoice promise and call the next test.
113 verify_userChoice(e, t, test_case, index);
115 window.addEventListener('beforeinstallprompt', event_handler);
117 testRunner.dispatchBeforeInstallPromptEvent(index, ['foo', 'bar'], t.step_func(function(result) {
118 testRunner.resolveBeforeInstallPromptPromise(index, test_case.platform);
119 }));
121 // Test that firing prompt() outside of the handler resolves or rejects correctly.
122 if (test_case.late) {
123 if (test_case.cancel) {
124 verify_prompt_resolve(event, t);
125 } else {
126 verify_prompt_reject(event, t);
128 // Check userChoice and call the next test.
129 verify_userChoice(event, t, test_case, index);
131 }, test_case.name);
134 prompt_test(0);
135 </script>