summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_sessions_incognito.js
blob: aecad9e8ec4f662a8e45011f02ef843c187cfcd4 (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
"use strict";

add_task(async function test_sessions_tab_value_private() {
  Services.obs.notifyObservers(null, "browser:purge-session-history");
  is(
    SessionStore.getClosedWindowCount(),
    0,
    "No closed window sessions at start of test"
  );

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      browser_specific_settings: {
        gecko: {
          id: "exampleextension@mozilla.org",
        },
      },
      permissions: ["sessions"],
    },
    background() {
      browser.test.onMessage.addListener(async (msg, pbw) => {
        if (msg == "value") {
          await browser.test.assertRejects(
            browser.sessions.setWindowValue(pbw.windowId, "foo", "bar"),
            /Invalid window ID/,
            "should not be able to set incognito window session data"
          );
          await browser.test.assertRejects(
            browser.sessions.getWindowValue(pbw.windowId, "foo"),
            /Invalid window ID/,
            "should not be able to get incognito window session data"
          );
          await browser.test.assertRejects(
            browser.sessions.removeWindowValue(pbw.windowId, "foo"),
            /Invalid window ID/,
            "should not be able to remove incognito window session data"
          );
          await browser.test.assertRejects(
            browser.sessions.setTabValue(pbw.tabId, "foo", "bar"),
            /Invalid tab ID/,
            "should not be able to set incognito tab session data"
          );
          await browser.test.assertRejects(
            browser.sessions.getTabValue(pbw.tabId, "foo"),
            /Invalid tab ID/,
            "should not be able to get incognito tab session data"
          );
          await browser.test.assertRejects(
            browser.sessions.removeTabValue(pbw.tabId, "foo"),
            /Invalid tab ID/,
            "should not be able to remove incognito tab session data"
          );
        }
        if (msg == "restore") {
          await browser.test.assertRejects(
            browser.sessions.restore(),
            /Could not restore object/,
            "should not be able to restore incognito last window session data"
          );
          if (pbw) {
            await browser.test.assertRejects(
              browser.sessions.restore(pbw.sessionId),
              /Could not restore object/,
              `should not be able to restore incognito session ID ${pbw.sessionId} session data`
            );
          }
        }
        browser.test.sendMessage("done");
      });
    },
  });

  let winData = await getIncognitoWindow("http://mochi.test:8888/");
  await extension.startup();

  // Test value set/get APIs on a private window and tab.
  extension.sendMessage("value", winData.details);
  await extension.awaitMessage("done");

  // Test restoring a private tab.
  let tab = await BrowserTestUtils.openNewForegroundTab(
    winData.win.gBrowser,
    "http://example.com"
  );
  let sessionUpdatePromise = BrowserTestUtils.waitForSessionStoreUpdate(tab);
  BrowserTestUtils.removeTab(tab);
  await sessionUpdatePromise;
  let closedTabData = SessionStore.getClosedTabDataForWindow(winData.win);

  extension.sendMessage("restore", {
    sessionId: String(closedTabData[0].closedId),
  });
  await extension.awaitMessage("done");

  // Test restoring a private window.
  sessionUpdatePromise = BrowserTestUtils.waitForSessionStoreUpdate(
    winData.win.gBrowser.selectedTab
  );
  await BrowserTestUtils.closeWindow(winData.win);
  await sessionUpdatePromise;

  is(
    SessionStore.getClosedWindowCount(),
    0,
    "The closed window was added to Recently Closed Windows"
  );

  // If the window gets restored, test will fail with an unclosed window.
  extension.sendMessage("restore");
  await extension.awaitMessage("done");

  await extension.unload();
});