summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/speculation-rules/prerender/web-database.https.html
blob: 3ef1141e2063e9eda6bbf5b7a4b62760b5c52c03 (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
<!DOCTYPE html>
<title>Same-origin prerendering can access Web Database</title>
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="resources/utils.js"></script>
<body>
<script>

setup(() => assertSpeculationRulesIsSupported());

async function insertQuery() {
  return new Promise(resolve => {
    const db = openDatabase("test", "1.0", "test database", 1024);
    db.transaction(function (tx) {
      tx.executeSql('CREATE TABLE IF NOT EXISTS foo (text)');
      tx.executeSql('INSERT INTO foo (text) VALUES ("bar")');
    }, function(error) {
      resolve(error);
    }, function() {
      resolve("Success");
    });
  });
}

async function selectQuery() {
  return new Promise(resolve => {
    const db = openDatabase("test", "1.0", "test database", 1024);
    db.transaction(function (tx) {
        tx.executeSql('CREATE TABLE IF NOT EXISTS foo (text)');
        tx.executeSql('SELECT * FROM foo', [], function (tx, results) {
          resolve(results.rows.length);
        });
      }, function(tx, error) {
        resolve(error);
      });
  });
}

promise_test(async t => {
  const uid = token();
  const bc = new PrerenderChannel('prerender-channel', uid);

  const gotMessage = new Promise(resolve => {
    bc.addEventListener('message', e => {
      resolve(e.data);
    }, {
      once: true
    });
  });

  const insertResult = await insertQuery();
  assert_equals(insertResult, "Success",
        'primary page should be able to execute statements from Web Database.');

  // Start prerendering a page that attempts to access Web Database.
  startPrerendering(`resources/web-database-access.html?uid=${uid}`);
  const result = await gotMessage;

  assert_equals(
    result, "Success",
    'prerendering page should be able to read from Web Database');

  const selectResult = await selectQuery();
  assert_equals(
    selectResult, 2,
    'prerendering page should be able to write to Web Database');
}, 'prerendering page should be able to access Web Database');

</script>
</body>