summaryrefslogtreecommitdiffstats
path: root/mobile/android/tests/browser/chrome/web_channel.html
blob: 866f3efd23fb3ff1e2bd73f513af125a8e7074e4 (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
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>web_channel_test</title>
</head>
<body>
<script>
  window.onload = function() {
    var testName = window.location.search.replace(/^\?/, "");

    switch(testName) {
      case "generic":
        test_generic();
        break;
      case "twoway":
        test_twoWay();
        break;
      case "multichannel":
        test_multichannel();
        break;
    }
  };

  function test_generic() {
    var event = new window.CustomEvent("WebChannelMessageToChrome", {
      detail: JSON.stringify({
        id: "generic",
        message: {
          something: {
            nested: "hello",
          },
        }
      })
    });

    window.dispatchEvent(event);
  }

  function test_twoWay() {
    var firstMessage = new window.CustomEvent("WebChannelMessageToChrome", {
      detail: JSON.stringify({
        id: "twoway",
        message: {
          command: "one",
        },
      })
    });

    window.addEventListener("WebChannelMessageToContent", function(e) {
      var secondMessage = new window.CustomEvent("WebChannelMessageToChrome", {
        detail: JSON.stringify({
          id: "twoway",
          message: {
            command: "two",
            detail: e.detail.message,
          },
        }),
      });

      if (!e.detail.message.error) {
        window.dispatchEvent(secondMessage);
      }
    }, true);

    window.dispatchEvent(firstMessage);
  }

  function test_multichannel() {
    var event1 = new window.CustomEvent("WebChannelMessageToChrome", {
      detail: JSON.stringify({
        id: "wrongchannel",
        message: {},
      })
    });

    var event2 = new window.CustomEvent("WebChannelMessageToChrome", {
      detail: JSON.stringify({
        id: "multichannel",
        message: {},
      })
    });

    window.dispatchEvent(event1);
    window.dispatchEvent(event2);
  }
</script>
</body>
</html>