summaryrefslogtreecommitdiffstats
path: root/dom/tests/browser/browser_localStorage_snapshotting.js
blob: bc53591f848e4786095917be82cf9be7919a647f (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
const HELPER_PAGE_URL =
  "https://example.com/browser/dom/tests/browser/page_localstorage_snapshotting.html";
const HELPER_PAGE_ORIGIN = "https://example.com/";

/* import-globals-from helper_localStorage.js */

let testDir = gTestPath.substr(0, gTestPath.lastIndexOf("/"));
Services.scriptloader.loadSubScript(testDir + "/helper_localStorage.js", this);

function clearOrigin() {
  let principal =
    Services.scriptSecurityManager.createContentPrincipalFromOrigin(
      HELPER_PAGE_ORIGIN
    );
  let request = Services.qms.clearStoragesForPrincipal(
    principal,
    "default",
    "ls"
  );
  let promise = new Promise(resolve => {
    request.callback = () => {
      resolve();
    };
  });
  return promise;
}

async function applyMutations(knownTab, mutations) {
  await SpecialPowers.spawn(
    knownTab.tab.linkedBrowser,
    [mutations],
    function (mutations) {
      return content.wrappedJSObject.applyMutations(
        Cu.cloneInto(mutations, content)
      );
    }
  );
}

async function verifyState(knownTab, expectedState) {
  let actualState = await SpecialPowers.spawn(
    knownTab.tab.linkedBrowser,
    [],
    function () {
      return content.wrappedJSObject.getState();
    }
  );

  for (let [expectedKey, expectedValue] of Object.entries(expectedState)) {
    ok(actualState.hasOwnProperty(expectedKey), "key present: " + expectedKey);
    is(actualState[expectedKey], expectedValue, "value correct");
  }
  for (let actualKey of Object.keys(actualState)) {
    if (!expectedState.hasOwnProperty(actualKey)) {
      ok(false, "actual state has key it shouldn't have: " + actualKey);
    }
  }
}

async function getKeys(knownTab) {
  let keys = await SpecialPowers.spawn(
    knownTab.tab.linkedBrowser,
    [],
    function () {
      return content.wrappedJSObject.getKeys();
    }
  );
  return keys;
}

async function beginExplicitSnapshot(knownTab) {
  await SpecialPowers.spawn(knownTab.tab.linkedBrowser, [], function () {
    return content.wrappedJSObject.beginExplicitSnapshot();
  });
}

async function checkpointExplicitSnapshot(knownTab) {
  await SpecialPowers.spawn(knownTab.tab.linkedBrowser, [], function () {
    return content.wrappedJSObject.checkpointExplicitSnapshot();
  });
}

async function endExplicitSnapshot(knownTab) {
  await SpecialPowers.spawn(knownTab.tab.linkedBrowser, [], function () {
    return content.wrappedJSObject.endExplicitSnapshot();
  });
}

async function verifyHasSnapshot(knownTab, expectedHasSnapshot) {
  let hasSnapshot = await SpecialPowers.spawn(
    knownTab.tab.linkedBrowser,
    [],
    function () {
      return content.wrappedJSObject.getHasSnapshot();
    }
  );
  is(hasSnapshot, expectedHasSnapshot, "Correct has snapshot");
}

async function verifySnapshotUsage(knownTab, expectedSnapshotUsage) {
  let snapshotUsage = await SpecialPowers.spawn(
    knownTab.tab.linkedBrowser,
    [],
    function () {
      return content.wrappedJSObject.getSnapshotUsage();
    }
  );
  is(snapshotUsage, expectedSnapshotUsage, "Correct snapshot usage");
}

async function verifyParentState(expectedState) {
  let principal =
    Services.scriptSecurityManager.createContentPrincipalFromOrigin(
      HELPER_PAGE_ORIGIN
    );

  let actualState = await Services.domStorageManager.getState(principal);

  for (let [expectedKey, expectedValue] of Object.entries(expectedState)) {
    ok(actualState.hasOwnProperty(expectedKey), "key present: " + expectedKey);
    is(actualState[expectedKey], expectedValue, "value correct");
  }
  for (let actualKey of Object.keys(actualState)) {
    if (!expectedState.hasOwnProperty(actualKey)) {
      ok(false, "actual state has key it shouldn't have: " + actualKey);
    }
  }
}

// We spin up a ton of child processes.
requestLongerTimeout(4);

/**
 * Verify snapshotting of our localStorage implementation in multi-e10s setup.
 */
add_task(async function () {
  if (!Services.domStorageManager.nextGenLocalStorageEnabled) {
    ok(true, "Test ignored when the next gen local storage is not enabled.");
    return;
  }

  await SpecialPowers.pushPrefEnv({
    set: [
      // Enable LocalStorage's testing API so we can explicitly create
      // snapshots when needed.
      ["dom.storage.testing", true],
      // Force multiple web and webIsolated content processes so that the
      // multi-e10s logic works correctly.
      ["dom.ipc.processCount", 8],
      ["dom.ipc.processCount.webIsolated", 4],
    ],
  });

  // Ensure that there is no localstorage data by forcing the origin to be
  // cleared prior to the start of our test..
  await clearOrigin();

  // - Open tabs.  Don't configure any of them yet.
  const knownTabs = new KnownTabs();
  const writerTab1 = await openTestTab(
    HELPER_PAGE_URL,
    "writer1",
    knownTabs,
    true
  );
  const writerTab2 = await openTestTab(
    HELPER_PAGE_URL,
    "writer2",
    knownTabs,
    true
  );
  const readerTab1 = await openTestTab(
    HELPER_PAGE_URL,
    "reader1",
    knownTabs,
    true
  );
  const readerTab2 = await openTestTab(
    HELPER_PAGE_URL,
    "reader2",
    knownTabs,
    true
  );

  const initialMutations = [
    [null, null],
    ["key1", "initial1"],
    ["key2", "initial2"],
    ["key3", "initial3"],
    ["key5", "initial5"],
    ["key6", "initial6"],
    ["key7", "initial7"],
    ["key8", "initial8"],
  ];

  const initialState = {
    key1: "initial1",
    key2: "initial2",
    key3: "initial3",
    key5: "initial5",
    key6: "initial6",
    key7: "initial7",
    key8: "initial8",
  };

  let sizeOfOneKey;
  let sizeOfOneValue;
  let sizeOfOneItem;
  let sizeOfKeys = 0;
  let sizeOfItems = 0;

  let entries = Object.entries(initialState);
  for (let i = 0; i < entries.length; i++) {
    let entry = entries[i];
    let sizeOfKey = entry[0].length;
    let sizeOfValue = entry[1].length;
    let sizeOfItem = sizeOfKey + sizeOfValue;
    if (i == 0) {
      sizeOfOneKey = sizeOfKey;
      sizeOfOneValue = sizeOfValue;
      sizeOfOneItem = sizeOfItem;
    }
    sizeOfKeys += sizeOfKey;
    sizeOfItems += sizeOfItem;
  }

  info("Size of one key is " + sizeOfOneKey);
  info("Size of one value is " + sizeOfOneValue);
  info("Size of one item is " + sizeOfOneItem);
  info("Size of keys is " + sizeOfKeys);
  info("Size of items is " + sizeOfItems);

  const prefillValues = [
    // Zero prefill (prefill disabled)
    0,
    // Less than one key length prefill
    sizeOfOneKey - 1,
    // Greater than one key length and less than one item length prefill
    sizeOfOneKey + 1,
    // Precisely one item length prefill
    sizeOfOneItem,
    // Precisely two times one item length prefill
    2 * sizeOfOneItem,
    // Precisely size of keys prefill
    sizeOfKeys,
    // Less than size of keys plus one value length prefill
    sizeOfKeys + sizeOfOneValue - 1,
    // Precisely size of keys plus one value length prefill
    sizeOfKeys + sizeOfOneValue,
    // Greater than size of keys plus one value length and less than size of
    // keys plus two times one value length prefill
    sizeOfKeys + sizeOfOneValue + 1,
    // Precisely size of keys plus two times one value length prefill
    sizeOfKeys + 2 * sizeOfOneValue,
    // Precisely size of keys plus three times one value length prefill
    sizeOfKeys + 3 * sizeOfOneValue,
    // Precisely size of keys plus four times one value length prefill
    sizeOfKeys + 4 * sizeOfOneValue,
    // Precisely size of keys plus five times one value length prefill
    sizeOfKeys + 5 * sizeOfOneValue,
    // Precisely size of keys plus six times one value length prefill
    sizeOfKeys + 6 * sizeOfOneValue,
    // Precisely size of items prefill
    sizeOfItems,
    // Unlimited prefill
    -1,
  ];

  for (let prefillValue of prefillValues) {
    info("Setting prefill value to " + prefillValue);

    await SpecialPowers.pushPrefEnv({
      set: [["dom.storage.snapshot_prefill", prefillValue]],
    });

    const gradualPrefillValues = [
      // Zero gradual prefill
      0,
      // Less than one key length gradual prefill
      sizeOfOneKey - 1,
      // Greater than one key length and less than one item length gradual
      // prefill
      sizeOfOneKey + 1,
      // Precisely one item length gradual prefill
      sizeOfOneItem,
      // Precisely two times one item length gradual prefill
      2 * sizeOfOneItem,
      // Precisely three times one item length gradual prefill
      3 * sizeOfOneItem,
      // Precisely four times one item length gradual prefill
      4 * sizeOfOneItem,
      // Precisely five times one item length gradual prefill
      5 * sizeOfOneItem,
      // Precisely six times one item length gradual prefill
      6 * sizeOfOneItem,
      // Precisely size of items prefill
      sizeOfItems,
      // Unlimited gradual prefill
      -1,
    ];

    for (let gradualPrefillValue of gradualPrefillValues) {
      info("Setting gradual prefill value to " + gradualPrefillValue);

      await SpecialPowers.pushPrefEnv({
        set: [["dom.storage.snapshot_gradual_prefill", gradualPrefillValue]],
      });

      info("Stage 1");

      const setRemoveMutations1 = [
        ["key0", "setRemove10"],
        ["key1", "setRemove11"],
        ["key2", null],
        ["key3", "setRemove13"],
        ["key4", "setRemove14"],
        ["key5", "setRemove15"],
        ["key6", "setRemove16"],
        ["key7", "setRemove17"],
        ["key8", null],
        ["key9", "setRemove19"],
      ];

      const setRemoveState1 = {
        key0: "setRemove10",
        key1: "setRemove11",
        key3: "setRemove13",
        key4: "setRemove14",
        key5: "setRemove15",
        key6: "setRemove16",
        key7: "setRemove17",
        key9: "setRemove19",
      };

      const setRemoveMutations2 = [
        ["key0", "setRemove20"],
        ["key1", null],
        ["key2", "setRemove22"],
        ["key3", "setRemove23"],
        ["key4", "setRemove24"],
        ["key5", "setRemove25"],
        ["key6", "setRemove26"],
        ["key7", null],
        ["key8", "setRemove28"],
        ["key9", "setRemove29"],
      ];

      const setRemoveState2 = {
        key0: "setRemove20",
        key2: "setRemove22",
        key3: "setRemove23",
        key4: "setRemove24",
        key5: "setRemove25",
        key6: "setRemove26",
        key8: "setRemove28",
        key9: "setRemove29",
      };

      // Apply initial mutations using an explicit snapshot. The explicit
      // snapshot here ensures that the parent process have received the
      // changes.
      await beginExplicitSnapshot(writerTab1);
      await applyMutations(writerTab1, initialMutations);
      await endExplicitSnapshot(writerTab1);

      // Begin explicit snapshots in all tabs except readerTab2. All these tabs
      // should see the initial state regardless what other tabs are doing.
      await beginExplicitSnapshot(writerTab1);
      await beginExplicitSnapshot(writerTab2);
      await beginExplicitSnapshot(readerTab1);

      // Apply first array of set/remove mutations in writerTab1 and end the
      // explicit snapshot. This will trigger saving of values in other active
      // snapshots.
      await applyMutations(writerTab1, setRemoveMutations1);
      await endExplicitSnapshot(writerTab1);

      // Begin an explicit snapshot in readerTab2. writerTab1 already ended its
      // explicit snapshot, so readerTab2 should see mutations done by
      // writerTab1.
      await beginExplicitSnapshot(readerTab2);

      // Apply second array of set/remove mutations in writerTab2 and end the
      // explicit snapshot. This will trigger saving of values in other active
      // snapshots, but only if they haven't been saved already.
      await applyMutations(writerTab2, setRemoveMutations2);
      await endExplicitSnapshot(writerTab2);

      // Verify state in readerTab1, it should match the initial state.
      await verifyState(readerTab1, initialState);
      await endExplicitSnapshot(readerTab1);

      // Verify state in readerTab2, it should match the state after the first
      // array of set/remove mutatations have been applied and "commited".
      await verifyState(readerTab2, setRemoveState1);
      await endExplicitSnapshot(readerTab2);

      // Verify final state, it should match the state after the second array of
      // set/remove mutation have been applied and "commited". An explicit
      // snapshot is used.
      await beginExplicitSnapshot(readerTab1);
      await verifyState(readerTab1, setRemoveState2);
      await endExplicitSnapshot(readerTab1);

      info("Stage 2");

      const setRemoveClearMutations1 = [
        ["key0", "setRemoveClear10"],
        ["key1", null],
        [null, null],
      ];

      const setRemoveClearState1 = {};

      const setRemoveClearMutations2 = [
        ["key8", null],
        ["key9", "setRemoveClear29"],
        [null, null],
      ];

      const setRemoveClearState2 = {};

      // This is very similar to previous stage except that in addition to
      // set/remove, the clear operation is involved too.
      await beginExplicitSnapshot(writerTab1);
      await applyMutations(writerTab1, initialMutations);
      await endExplicitSnapshot(writerTab1);

      await beginExplicitSnapshot(writerTab1);
      await beginExplicitSnapshot(writerTab2);
      await beginExplicitSnapshot(readerTab1);

      await applyMutations(writerTab1, setRemoveClearMutations1);
      await endExplicitSnapshot(writerTab1);

      await beginExplicitSnapshot(readerTab2);

      await applyMutations(writerTab2, setRemoveClearMutations2);
      await endExplicitSnapshot(writerTab2);

      await verifyState(readerTab1, initialState);
      await endExplicitSnapshot(readerTab1);

      await verifyState(readerTab2, setRemoveClearState1);
      await endExplicitSnapshot(readerTab2);

      await beginExplicitSnapshot(readerTab1);
      await verifyState(readerTab1, setRemoveClearState2);
      await endExplicitSnapshot(readerTab1);

      info("Stage 3");

      const changeOrderMutations = [
        ["key1", null],
        ["key2", null],
        ["key3", null],
        ["key5", null],
        ["key6", null],
        ["key7", null],
        ["key8", null],
        ["key8", "initial8"],
        ["key7", "initial7"],
        ["key6", "initial6"],
        ["key5", "initial5"],
        ["key3", "initial3"],
        ["key2", "initial2"],
        ["key1", "initial1"],
      ];

      // Apply initial mutations using an explicit snapshot. The explicit
      // snapshot here ensures that the parent process have received the
      // changes.
      await beginExplicitSnapshot(writerTab1);
      await applyMutations(writerTab1, initialMutations);
      await endExplicitSnapshot(writerTab1);

      // Begin explicit snapshots in all tabs except writerTab2 which is not
      // used in this stage. All these tabs should see the initial order
      // regardless what other tabs are doing.
      await beginExplicitSnapshot(readerTab1);
      await beginExplicitSnapshot(writerTab1);
      await beginExplicitSnapshot(readerTab2);

      // Get all keys in readerTab1 and end the explicit snapshot. No mutations
      // have been applied yet.
      let tab1Keys = await getKeys(readerTab1);
      await endExplicitSnapshot(readerTab1);

      // Apply mutations that change the order of keys and end the explicit
      // snapshot. The state is unchanged. This will trigger saving of key order
      // in other active snapshots, but only if the order hasn't been saved
      // already.
      await applyMutations(writerTab1, changeOrderMutations);
      await endExplicitSnapshot(writerTab1);

      // Get all keys in readerTab2 and end the explicit snapshot. Change order
      // mutations have been applied, but the order should stay unchanged.
      let tab2Keys = await getKeys(readerTab2);
      await endExplicitSnapshot(readerTab2);

      // Verify the key order is the same.
      is(tab2Keys.length, tab1Keys.length, "Correct keys length");
      for (let i = 0; i < tab2Keys.length; i++) {
        is(tab2Keys[i], tab1Keys[i], "Correct key");
      }

      // Verify final state, it should match the initial state since applied
      // mutations only changed the key order. An explicit snapshot is used.
      await beginExplicitSnapshot(readerTab1);
      await verifyState(readerTab1, initialState);
      await endExplicitSnapshot(readerTab1);
    }
  }

  // - Clean up.
  await cleanupTabs(knownTabs);

  clearOrigin();
});

/**
 * Verify that snapshots are able to work with negative usage. This can happen
 * when there's an item stored in localStorage with given size and then two
 * snaphots (created at the same time) mutate the item. The first one replases
 * it with something bigger and the other one removes it.
 */
add_task(async function () {
  if (!Services.domStorageManager.nextGenLocalStorageEnabled) {
    ok(true, "Test ignored when the next gen local storage is not enabled.");
    return;
  }

  await SpecialPowers.pushPrefEnv({
    set: [
      // Force multiple web and webIsolated content processes so that the
      // multi-e10s logic works correctly.
      ["dom.ipc.processCount", 4],
      ["dom.ipc.processCount.webIsolated", 2],
      // Disable snapshot peak usage pre-incrementation to make the testing
      // easier.
      ["dom.storage.snapshot_peak_usage.initial_preincrement", 0],
      ["dom.storage.snapshot_peak_usage.reduced_initial_preincrement", 0],
      ["dom.storage.snapshot_peak_usage.gradual_preincrement", 0],
      ["dom.storage.snapshot_peak_usage.reuced_gradual_preincrement", 0],
      // Enable LocalStorage's testing API so we can explicitly create
      // snapshots when needed.
      ["dom.storage.testing", true],
    ],
  });

  // Ensure that there is no localstorage data by forcing the origin to be
  // cleared prior to the start of our test.
  await clearOriginStorageEnsuringNoPreload(HELPER_PAGE_ORIGIN);

  // Open tabs.  Don't configure any of them yet.
  const knownTabs = new KnownTabs();
  const writerTab1 = await openTestTab(
    HELPER_PAGE_URL,
    "writer1",
    knownTabs,
    true
  );
  const writerTab2 = await openTestTab(
    HELPER_PAGE_URL,
    "writer2",
    knownTabs,
    true
  );

  // Apply the initial mutation using an explicit snapshot. The explicit
  // snapshot here ensures that the parent process have received the changes.
  await beginExplicitSnapshot(writerTab1);
  await applyMutations(writerTab1, [["key", "something"]]);
  await endExplicitSnapshot(writerTab1);

  // Begin explicit snapshots in both tabs. Both tabs should see the initial
  // state.
  await beginExplicitSnapshot(writerTab1);
  await beginExplicitSnapshot(writerTab2);

  // Apply the first mutation in writerTab1 and end the explicit snapshot.
  await applyMutations(writerTab1, [["key", "somethingBigger"]]);
  await endExplicitSnapshot(writerTab1);

  // Apply the second mutation in writerTab2 and end the explicit snapshot.
  await applyMutations(writerTab2, [["key", null]]);
  await endExplicitSnapshot(writerTab2);

  // Verify the final state, it should match the state after the second
  // mutation has been applied and "commited". An explicit snapshot is used.
  await beginExplicitSnapshot(writerTab1);
  await verifyState(writerTab1, {});
  await endExplicitSnapshot(writerTab1);

  // Clean up.
  await cleanupTabs(knownTabs);

  clearOriginStorageEnsuringNoPreload(HELPER_PAGE_ORIGIN);
});

/**
 * Verify that snapshot usage is correctly updated after each operation.
 */
add_task(async function () {
  if (!Services.domStorageManager.nextGenLocalStorageEnabled) {
    ok(true, "Test ignored when the next gen local storage is not enabled.");
    return;
  }

  await SpecialPowers.pushPrefEnv({
    set: [
      // Force multiple web and webIsolated content processes so that the
      // multi-e10s logic works correctly.
      ["dom.ipc.processCount", 4],
      ["dom.ipc.processCount.webIsolated", 2],
      // Disable snapshot peak usage pre-incrementation to make the testing
      // easier.
      ["dom.storage.snapshot_peak_usage.initial_preincrement", 0],
      ["dom.storage.snapshot_peak_usage.reduced_initial_preincrement", 0],
      ["dom.storage.snapshot_peak_usage.gradual_preincrement", 0],
      ["dom.storage.snapshot_peak_usage.reuced_gradual_preincrement", 0],
      // Enable LocalStorage's testing API so we can explicitly create
      // snapshots when needed.
      ["dom.storage.testing", true],
    ],
  });

  // Ensure that there is no localstorage data by forcing the origin to be
  // cleared prior to the start of our test.
  await clearOriginStorageEnsuringNoPreload(HELPER_PAGE_ORIGIN);

  // Open tabs.  Don't configure any of them yet.
  const knownTabs = new KnownTabs();
  const writerTab1 = await openTestTab(
    HELPER_PAGE_URL,
    "writer1",
    knownTabs,
    true
  );
  const writerTab2 = await openTestTab(
    HELPER_PAGE_URL,
    "writer2",
    knownTabs,
    true
  );

  // Apply the initial mutation using an explicit snapshot. The explicit
  // snapshot here ensures that the parent process have received the changes.
  await beginExplicitSnapshot(writerTab1);
  await verifySnapshotUsage(writerTab1, 0);
  await applyMutations(writerTab1, [["key", "something"]]);
  await verifySnapshotUsage(writerTab1, 12);
  await endExplicitSnapshot(writerTab1);
  await verifyHasSnapshot(writerTab1, false);

  // Begin an explicit snapshot in writerTab1 and apply the first mutatation
  // in it.
  await beginExplicitSnapshot(writerTab1);
  await verifySnapshotUsage(writerTab1, 12);
  await applyMutations(writerTab1, [["key", "somethingBigger"]]);
  await verifySnapshotUsage(writerTab1, 18);

  // Begin an explicit snapshot in writerTab2 and apply the second mutatation
  // in it.
  await beginExplicitSnapshot(writerTab2);
  await verifySnapshotUsage(writerTab2, 18);
  await applyMutations(writerTab2, [[null, null]]);
  await verifySnapshotUsage(writerTab2, 6);

  // End explicit snapshots in both tabs.
  await endExplicitSnapshot(writerTab1);
  await verifyHasSnapshot(writerTab1, false);
  await endExplicitSnapshot(writerTab2);
  await verifyHasSnapshot(writerTab2, false);

  // Verify the final state, it should match the state after the second
  // mutation has been applied and "commited". An explicit snapshot is used.
  await beginExplicitSnapshot(writerTab1);
  await verifySnapshotUsage(writerTab1, 0);
  await verifyState(writerTab1, {});
  await endExplicitSnapshot(writerTab1);
  await verifyHasSnapshot(writerTab1, false);

  // Clean up.
  await cleanupTabs(knownTabs);

  clearOriginStorageEnsuringNoPreload(HELPER_PAGE_ORIGIN);
});

/**
 * Verify that datastore in the parent is correctly updated after a checkpoint.
 */
add_task(async function () {
  if (!Services.domStorageManager.nextGenLocalStorageEnabled) {
    ok(true, "Test ignored when the next gen local storage is not enabled.");
    return;
  }

  await SpecialPowers.pushPrefEnv({
    set: [
      // Force multiple web and webIsolated content processes so that the
      // multi-e10s logic works correctly.
      ["dom.ipc.processCount", 4],
      ["dom.ipc.processCount.webIsolated", 2],
      // Enable LocalStorage's testing API so we can explicitly create
      // snapshots when needed.
      ["dom.storage.testing", true],
    ],
  });

  // Ensure that there is no localstorage data by forcing the origin to be
  // cleared prior to the start of our test.
  await clearOriginStorageEnsuringNoPreload(HELPER_PAGE_ORIGIN);

  // Open tabs.  Don't configure any of them yet.
  const knownTabs = new KnownTabs();
  const writerTab1 = await openTestTab(
    HELPER_PAGE_URL,
    "writer1",
    knownTabs,
    true
  );

  await verifyParentState({});

  // Apply the initial mutation using an explicit snapshot. The explicit
  // snapshot here ensures that the parent process have received the changes.
  await beginExplicitSnapshot(writerTab1);
  await verifyParentState({});
  await applyMutations(writerTab1, [["key", "something"]]);
  await verifyParentState({});
  await endExplicitSnapshot(writerTab1);

  await verifyParentState({ key: "something" });

  // Begin an explicit snapshot in writerTab1, apply the first mutation in
  // writerTab1 and checkpoint the explicit snapshot.
  await beginExplicitSnapshot(writerTab1);
  await verifyParentState({ key: "something" });
  await applyMutations(writerTab1, [["key", "somethingBigger"]]);
  await verifyParentState({ key: "something" });
  await checkpointExplicitSnapshot(writerTab1);

  await verifyParentState({ key: "somethingBigger" });

  // Apply the second mutation in writerTab1 and checkpoint the explicit
  // snapshot.
  await applyMutations(writerTab1, [["key", null]]);
  await verifyParentState({ key: "somethingBigger" });
  await checkpointExplicitSnapshot(writerTab1);

  await verifyParentState({});

  // Apply the third mutation in writerTab1 and end the explicit snapshot.
  await applyMutations(writerTab1, [["otherKey", "something"]]);
  await verifyParentState({});
  await endExplicitSnapshot(writerTab1);

  await verifyParentState({ otherKey: "something" });

  // Verify the final state, it should match the state after the third mutation
  // has been applied and "commited". An explicit snapshot is used.
  await beginExplicitSnapshot(writerTab1);
  await verifyParentState({ otherKey: "something" });
  await verifyState(writerTab1, { otherKey: "something" });
  await endExplicitSnapshot(writerTab1);

  await verifyParentState({ otherKey: "something" });

  // Clean up.
  await cleanupTabs(knownTabs);

  clearOriginStorageEnsuringNoPreload(HELPER_PAGE_ORIGIN);
});