summaryrefslogtreecommitdiffstats
path: root/dom/push/test/xpcshell/test_notification_ack.js
blob: 87c29f417af60b90173871bea2b16f35ef7e7161 (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
158
159
160
161
162
163
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

var userAgentID = "5ab1d1df-7a3d-4024-a469-b9e1bb399fad";

function run_test() {
  do_get_profile();
  setPrefs({ userAgentID });
  run_next_test();
}

add_task(async function test_notification_ack() {
  let db = PushServiceWebSocket.newPushDB();
  registerCleanupFunction(() => {
    return db.drop().then(_ => db.close());
  });
  let records = [
    {
      channelID: "21668e05-6da8-42c9-b8ab-9cc3f4d5630c",
      pushEndpoint: "https://example.com/update/1",
      scope: "https://example.org/1",
      originAttributes: "",
      version: 1,
      quota: Infinity,
      systemRecord: true,
    },
    {
      channelID: "9a5ff87f-47c9-4215-b2b8-0bdd38b4b305",
      pushEndpoint: "https://example.com/update/2",
      scope: "https://example.org/2",
      originAttributes: "",
      version: 2,
      quota: Infinity,
      systemRecord: true,
    },
    {
      channelID: "5477bfda-22db-45d4-9614-fee369630260",
      pushEndpoint: "https://example.com/update/3",
      scope: "https://example.org/3",
      originAttributes: "",
      version: 3,
      quota: Infinity,
      systemRecord: true,
    },
  ];
  for (let record of records) {
    await db.put(record);
  }

  let notifyCount = 0;
  let notifyPromise = promiseObserverNotification(
    PushServiceComponent.pushTopic,
    () => ++notifyCount == 3
  );

  let acks = 0;
  let ackDone;
  let ackPromise = new Promise(resolve => (ackDone = resolve));
  PushService.init({
    serverURI: "wss://push.example.org/",
    db,
    makeWebSocket(uri) {
      return new MockWebSocket(uri, {
        onHello(request) {
          equal(
            request.uaid,
            userAgentID,
            "Should send matching device IDs in handshake"
          );
          this.serverSendMsg(
            JSON.stringify({
              messageType: "hello",
              uaid: userAgentID,
              status: 200,
            })
          );
          this.serverSendMsg(
            JSON.stringify({
              messageType: "notification",
              updates: [
                {
                  channelID: "21668e05-6da8-42c9-b8ab-9cc3f4d5630c",
                  version: 2,
                },
              ],
            })
          );
        },
        onACK(request) {
          equal(request.messageType, "ack", "Should send acknowledgements");
          let updates = request.updates;
          switch (++acks) {
            case 1:
              deepEqual(
                [
                  {
                    channelID: "21668e05-6da8-42c9-b8ab-9cc3f4d5630c",
                    version: 2,
                    code: 100,
                  },
                ],
                updates,
                "Wrong updates for acknowledgement 1"
              );
              this.serverSendMsg(
                JSON.stringify({
                  messageType: "notification",
                  updates: [
                    {
                      channelID: "9a5ff87f-47c9-4215-b2b8-0bdd38b4b305",
                      version: 4,
                    },
                    {
                      channelID: "5477bfda-22db-45d4-9614-fee369630260",
                      version: 6,
                    },
                  ],
                })
              );
              break;

            case 2:
              deepEqual(
                [
                  {
                    channelID: "9a5ff87f-47c9-4215-b2b8-0bdd38b4b305",
                    version: 4,
                    code: 100,
                  },
                ],
                updates,
                "Wrong updates for acknowledgement 2"
              );
              break;

            case 3:
              deepEqual(
                [
                  {
                    channelID: "5477bfda-22db-45d4-9614-fee369630260",
                    version: 6,
                    code: 100,
                  },
                ],
                updates,
                "Wrong updates for acknowledgement 3"
              );
              ackDone();
              break;

            default:
              ok(false, "Unexpected acknowledgement " + acks);
          }
        },
      });
    },
  });

  await notifyPromise;
  await ackPromise;
});