2 <script src=
"../resources/testharness.js"></script>
3 <script src=
"../resources/testharnessreport.js"></script>
6 var prompt_test_cases
= [
14 name
: 'prompt-dismiss',
20 name
: 'prompt-before-preventDefault',
26 name
: 'prompt-late-accept',
32 name
: 'prompt-late-dismiss',
38 name
: 'prompt-late-without-preventDefault',
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
) {
54 error
instanceof DOMException
,
55 "Rejected promise should throw a DOMException."
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);
75 }), t
.unreached_func("userChoice promise should resolve."));
78 function prompt_test(index
) {
79 if (index
>= prompt_test_cases
.length
)
82 var test_case
= prompt_test_cases
[index
];
83 async_test(function(t
) {
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
);
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
) {
103 if (test_case
.cancel
) {
104 // Call prompt() to restart the pipeline.
105 verify_prompt_resolve(e
, t
);
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
);
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
);
126 verify_prompt_reject(event
, t
);
128 // Check userChoice and call the next test.
129 verify_userChoice(event
, t
, test_case
, index
);