summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/ForceRefreshParent.sys.mjs
blob: cb2d4809e9fc0770cdabc471b3d2dc3b641d5acd (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
var maxCacheLoadCount = 3;
var cachedLoadCount = 0;
var baseLoadCount = 0;
var done = false;

export class ForceRefreshParent extends JSWindowActorParent {
  constructor() {
    super();
  }

  receiveMessage(msg) {
    // if done is called, ignore the msg.
    if (done) {
      return;
    }
    if (msg.data.type === "base-load") {
      baseLoadCount += 1;
      if (cachedLoadCount === maxCacheLoadCount) {
        ForceRefreshParent.SimpleTest.is(
          baseLoadCount,
          2,
          "cached load should occur before second base load"
        );
        done = true;
        return ForceRefreshParent.done();
      }
      if (baseLoadCount !== 1) {
        ForceRefreshParent.SimpleTest.ok(
          false,
          "base load without cached load should only occur once"
        );
        done = true;
        return ForceRefreshParent.done();
      }
    } else if (msg.data.type === "base-register") {
      ForceRefreshParent.SimpleTest.ok(
        !cachedLoadCount,
        "cached load should not occur before base register"
      );
      ForceRefreshParent.SimpleTest.is(
        baseLoadCount,
        1,
        "register should occur after first base load"
      );
    } else if (msg.data.type === "base-sw-ready") {
      ForceRefreshParent.SimpleTest.ok(
        !cachedLoadCount,
        "cached load should not occur before base ready"
      );
      ForceRefreshParent.SimpleTest.is(
        baseLoadCount,
        1,
        "ready should occur after first base load"
      );
      ForceRefreshParent.refresh();
    } else if (msg.data.type === "cached-load") {
      ForceRefreshParent.SimpleTest.ok(
        cachedLoadCount < maxCacheLoadCount,
        "cached load should not occur too many times"
      );
      ForceRefreshParent.SimpleTest.is(
        baseLoadCount,
        1,
        "cache load occur after first base load"
      );
      cachedLoadCount += 1;
      if (cachedLoadCount < maxCacheLoadCount) {
        return ForceRefreshParent.refresh();
      }
      ForceRefreshParent.forceRefresh();
    } else if (msg.data.type === "cached-failure") {
      ForceRefreshParent.SimpleTest.ok(false, "failure: " + msg.data.detail);
      done = true;
      ForceRefreshParent.done();
    }
  }
}