summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/presentation-api/controlling-ua/PresentationConnection_onterminate-manual.https.html
blob: 7fdc2dbdcd9df90000c6effa2984b10c4ca43328 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!DOCTYPE html>

<meta charset="utf-8">
<title>Terminating a presentation in a controlling browsing context</title>
<link rel="author" title="Intel" href="http://www.intel.com">
<link rel="author" title="Chunyan Wang" href="mailto:chunyanx.wang@intel.com">
<link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs/">
<link rel="help" href="https://w3c.github.io/presentation-api/#terminating-a-presentation-in-a-controlling-browsing-context">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="common.js"></script>
<style>iframe { display: none; }</style>
<p>
  Click the button below and select the available presentation display, to start the manual test. The test passes if a "PASS" result appears.<br>
  This test asks you to click the button twice, unless the test fails.<br>
</p>
<button id="presentBtn">Start Presentation Test</button>
<iframe id="childFrame" src="support/iframe.html"></iframe>

<script>
  setup({explicit_timeout: true});
  const presentBtn = document.getElementById('presentBtn');
  const childFrame = document.getElementById('childFrame');

  promise_test(t => {
    const clickWatcher = new EventWatcher(t, presentBtn, 'click');
    const messageWatcher = new EventWatcher(t, window, 'message');
    const request = new PresentationRequest(presentationUrls);
    let connection, eventWatcher, timeout;

    t.add_cleanup(() => {
      if (connection) {
        connection.onconnect = () => { connection.terminate(); };
        if (connection.state === 'closed')
          request.reconnect(connection.id);
        else
          connection.terminate();
      }
    });

    const startTimeout = () => {
      timeout = t.step_timeout(() => {
          t.force_timeout();
          t.done();
      }, 10000);
    };

    const checkTerminateEvent = evt => {
      assert_true(evt.isTrusted && !evt.bubbles && !evt.cancelable && evt instanceof Event, 'A simple event is fired.');
      assert_equals(evt.type, 'terminate', 'The event name is "terminate".');
      assert_equals(evt.target, connection, 'event.target is the presentation connection.');
      assert_equals(connection.state, 'terminated', 'State of the presentation connection is "terminated".');
    };

    const watchEvent = (obj, watcher, type) => {
      const watchHandler = new Promise(resolve => {
        obj['on' + type] = evt => { resolve(evt); };
      });
      return Promise.all([ watchHandler, watcher.wait_for(type) ]).then(results => {
        assert_equals(results[0], results[1], 'Both on' + type + ' and addEventListener pass the same event object.');
        return results[0];
      });
    };

    const waitForEvent = (obj, watcher, type) => {
      const watchHandler = new Promise(resolve => {
        obj['on' + type] = evt => { resolve(evt); };
      });
      return Promise.race([ watchHandler, watcher.wait_for(type) ]);
    };

    return Promise.all([
      clickWatcher.wait_for('click'),
      messageWatcher.wait_for('message')
    ]).then(() => {
      presentBtn.disabled = true;

      return request.start();
    }).then(c => {
      startTimeout();

      connection = c;
      eventWatcher = new EventWatcher(t, connection, 'terminate');

      // Step 1: terminate the presentation when the presentation connection is in "connecting" state
      connection.terminate();
      return Promise.race([
        new Promise((_, reject) => {
          t.step_timeout(() => { reject('The presentation is not terminated successfully when the presentation connection in "connecting" state.'); }, 3000);
        }),
        watchEvent(connection, eventWatcher, 'terminate')
      ]);
    }).then(evt => {
      checkTerminateEvent(evt);

      // Step 2: terminate the presentation when the presentation connection is in "closed" state (nothing should happen)
      presentBtn.textContent = 'Continue Presentation Test';
      presentBtn.disabled = false;
      clearTimeout(timeout);
      return clickWatcher.wait_for('click');
    }).then(() => {
      return request.start();
    }).then(c => {
      startTimeout();
      connection = c;
      eventWatcher = new EventWatcher(t, connection, ['connect', 'close', 'terminate']);
      return eventWatcher.wait_for('connect');
    }).then(() => {
      connection.close();
      return eventWatcher.wait_for('close');
    }).then(() => {
      const terminateWatcher = new EventWatcher(t, connection, 'terminate');
      connection.terminate();
      return Promise.race([
        new Promise(resolve => { t.step_timeout(resolve, 1000); }),
        waitForEvent(connection, terminateWatcher, 'terminate').then(() => {
          assert_unreached('Invoking PresentationConnection.terminate() in the "closed" state causes nothing.'); })
      ]);
    }).then(() => {
      // Step 3: terminate the presentation when the presentation connection is in "connected" state;
      // this step also checks an event fired at another presentation connection in a nested browsing context
      return request.reconnect(connection.id);
    }).then(() => {
      return eventWatcher.wait_for('connect');
    }).then(() => {
      childFrame.contentWindow.postMessage('terminate?id=' + connection.id, '*');
      return messageWatcher.wait_for('message')
    }).then(() => {
      connection.terminate();
      return Promise.race([
        new Promise((_, reject) => {
          t.step_timeout(() => { reject('The presentation is not terminated successfully when the presentation connection in "connected" state.'); }, 3000);
        }),
        Promise.all([
          watchEvent(connection, eventWatcher, 'terminate'),
          messageWatcher.wait_for('message')
        ])
      ]);
    }).then(results => {
      checkTerminateEvent(results[0]);
      const evt = results[1].data;
      assert_true(evt.isSimpleEvent, 'A simple event is fired in a nested browsing context.');
      assert_equals(evt.type, 'terminate', 'The event name is "terminate".');
      assert_true(evt.checkConnection, 'event.target is the presentation connection.');
      assert_equals(evt.state, 'terminated', 'State of the presentation connection is "terminated".');

      // Step 4: terminate the presentation when the presentation connection is in "terminated" state (nothing should happen)
      const terminateWatcher = new EventWatcher(t, connection, 'terminate');
      connection.terminate();
      return Promise.race([
        new Promise(resolve => { t.step_timeout(resolve, 1000); }),
        waitForEvent(connection, terminateWatcher, 'terminate').then(() => {
          assert_unreached('Invoking PresentationConnection.terminate() in the "terminated" state causes nothing.'); })
      ]);
    });
  });
</script>