summaryrefslogtreecommitdiffstats
path: root/layout/base/tests/test_zoom_restore_bfcache.html
blob: 798ce1c6e43103138a2cda8490f77131de490291 (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
<!doctype html>
<meta charset="utf-8">
<title>Test for zoom restoration when coming from the bfcache</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/paint_listener.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
<script>
SimpleTest.waitForExplicitFinish();

/**
 * - main page (this one) opens file_zoom_restore_bfcache.html
 * - file_zoom_restore_bfcache.html sends "handlePageShow" to main page
 * - main page sends file_zoom_restore_bfcache.html "case1sendData"
 * - file_zoom_restore_bfcache.html sends "case1data" to main page
 * - main page sends "case1click" to file_zoom_restore_bfcache.html
 * - file_zoom_restore_bfcache.html clicks on <a> element, navigating to uri
 *   file_zoom_restore_bfcache.html?2, and gets bfcached
 * - file_zoom_restore_bfcache.html?2 sends "handlePageShow" to main page
 * - main page sends "case2sendData" to file_zoom_restore_bfcache.html?2
 * - file_zoom_restore_bfcache.html?2 sends "case2data" to main page
 * - main page sends "case2action" to file_zoom_restore_bfcache.html?2
 * - file_zoom_restore_bfcache.html?2 sends "case2dataAnimationFrame" to main page
 * - main page sends "case2back" to file_zoom_restore_bfcache.html?2
 * - file_zoom_restore_bfcache.html?2 navigates back to file_zoom_restore_bfcache.html
 * - file_zoom_restore_bfcache.html sends "handlePageShow" to main page
 * - main page sends "case3sendData to file_zoom_restore_bfcache.html
 * - file_zoom_restore_bfcache.html sends "case3data" to main page
 * - main page sends "close to file_zoom_restore_bfcache.html
 * - file_zoom_restore_bfcache.html closes bc and window and sends back "closed"
 **/

const originalDPR = window.devicePixelRatio;
let loadCount = 0;
var bc = new BroadcastChannel("zoomRestoreBfcache");
var bcPage2 = new BroadcastChannel("zoomRestoreBfcache?2");
bc.onmessage = (msgEvent) => {
  var msg = msgEvent.data;
  var command = msg.command;
  info(`Main page, received command from normal bc=${command}`);
  switch (command) {
    case "handlePageShow": {
      handlePageShow(msgEvent.data.eventPersisted);
      break;
    }
    case "case1data": {
      is(loadCount, 1, "Case 1");
      is(msg.devicePixelRatio, originalDPR, "No zoom");
      bc.postMessage({command: "case1click"});
      // The end of case 1
      break;
    }
    case "case3data": {
      is(loadCount, 2, "Case 3");
      is(msg.devicePixelRatio, originalDPR * 2, "Should preserve zoom when restored");
      SpecialPowers.spawnChrome([], () => {
        // We use FullZoom in file_zoom_restore_bfcache.html to set the zoom level in the
        // parent, but if FullZoom is not available then that will fail. So check it here
        // and mark the test as todo if it's not available.
        return "FullZoom" in this.browsingContext.top.embedderElement.ownerGlobal;
      }).then((hasFullZoom) => {
        if (SpecialPowers.Services.appinfo.sessionHistoryInParent && hasFullZoom) {
          // When bfcache lives in the parent process, we get a proper zoom level
          // update on the browsing context.
          is(msg.frameDevicePixelRatio, originalDPR * 2, "Should preserve zoom on frames too");
        } else {
          todo_is(msg.frameDevicePixelRatio, originalDPR * 2, "Should preserve zoom on frames too");
        }
        bc.postMessage({command: "close"});
        // Now we wait for "closed"
      });
      break;
    }
    case "closed": {
      is(loadCount, 2, "Case 3");
      bc.close();
      SimpleTest.finish();
      break;
    }
    default:
      ok(false, "should not receive extra messages via BroadcastChannel");
  }
}
bcPage2.onmessage = (msgEvent) => {
  var msg = msgEvent.data;
  var command = msg.command;
  info(`Main page, received command from bc?2=${command}`);
  switch (command) {
    case "handlePageShow": {
      handlePageShow(msgEvent.data.eventPersisted);
      break;
    }
    case "case2data": {
      is(loadCount, 2, "Case 2");
      is(msg.devicePixelRatio, originalDPR, "No zoom (yet)")
      is(msg.frameDevicePixelRatio, originalDPR, "No zoom on frame either");
      bcPage2.postMessage({command: "case2action"});
      // Now we wait for "case2dataAnimationFrame"
      break;
    }
    case "case2dataAnimationFrame": {
      is(loadCount, 2, "Case 2");
      is(msg.devicePixelRatio, originalDPR * 2, "Zoomed");
      is(msg.frameDevicePixelRatio, originalDPR * 2, "Zoomed iframe too");
      bcPage2.postMessage({command: "case2back"});
      bcPage2.close();
      // The end of case 2
      break;
    }
    default:
      ok(false, "should not receive extra messages via BroadcastChannel");
  }
}
function handlePageShow(persisted) {
  ok(typeof persisted == "boolean", "Should get the persisted state from the pageshow event");
  if (loadCount == 2) {
    ok(persisted,  "Should've gone into the bfcache after the back navigation");
  } else {
    ok(!persisted,  "Should NOT be retrieved from bfcache");
  }

  if (loadCount == 0) {
    loadCount++;
    bc.postMessage({command: "case1sendData"});
    // Now we wait for the "case1data" message
  } else if (loadCount == 1) {
    loadCount++;
    bcPage2.postMessage({command: "case2sendData"});
    // Now we wait for the "case2data" message
  } else {
    bc.postMessage({command: "case3sendData"});
    // Now we wait for the "case3data" message
  }
}

// If Fission is disabled, the pref is no-op.
SpecialPowers.pushPrefEnv({set: [["fission.bfcacheInParent", true]]}, () => {
  window.open('file_zoom_restore_bfcache.html', '_blank', 'noopener');
});
</script>