summaryrefslogtreecommitdiffstats
path: root/mobile/android/components/extensions/test/mochitest/test_ext_tabs_goBack_goForward.html
blob: 0d143e2ac6ff59500bc54b0c4258a585aceb9a27 (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
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Tabs goBack and goForward Test</title>
    <script
      type="text/javascript"
      src="/tests/SimpleTest/SimpleTest.js"
    ></script>
    <script
      type="text/javascript"
      src="/tests/SimpleTest/ExtensionTestUtils.js"
    ></script>
    <script type="text/javascript" src="head.js"></script>
    <link rel="stylesheet" href="/tests/SimpleTest/test.css" />
  </head>
  <body>
    <script type="text/javascript">
      "use strict";

      add_task(async function test_tabs_goBack_goForward() {
        const extension = ExtensionTestUtils.loadExtension({
          manifest: {
            permissions: ["tabs"],
          },

          files: {
            "tab1.html": `<head>
              <meta charset=UTF-8">
              <title>tab1</title>
            </head>`,
            "tab2.html": `<head>
              <meta charset=UTF-8">
              <title>tab2</title>
            </head>`,
          },

          async background() {
            let tabUpdatedCount = 0;
            let tab = {};

            browser.tabs.onUpdated.addListener(
              async (tabId, changeInfo, tabInfo) => {
                if (
                  changeInfo.status !== "complete" ||
                  tabId !== tab.id ||
                  tabInfo.url === "about:blank"
                ) {
                  return;
                }

                tabUpdatedCount++;
                switch (tabUpdatedCount) {
                  case 1:
                    browser.test.assertEq(
                      "tab1",
                      tabInfo.title,
                      "tab1 is found as expected"
                    );
                    browser.tabs.update(tabId, { url: "tab2.html" });
                    break;

                  case 2:
                    browser.test.assertEq(
                      "tab2",
                      tabInfo.title,
                      "tab2 is found as expected"
                    );
                    browser.tabs.update(tabId, { url: "tab1.html" });
                    break;

                  case 3:
                    browser.test.assertEq(
                      "tab1",
                      tabInfo.title,
                      "tab1 is found as expected"
                    );
                    browser.tabs.goBack();
                    break;

                  case 4:
                    browser.test.assertEq(
                      "tab2",
                      tabInfo.title,
                      "tab2 is found after navigating backward with empty parameter"
                    );
                    browser.tabs.goBack(tabId);
                    break;

                  case 5:
                    browser.test.assertEq(
                      "tab1",
                      tabInfo.title,
                      "tab1 is found after navigating backward with tabId as parameter"
                    );
                    browser.tabs.goForward();
                    break;

                  case 6:
                    browser.test.assertEq(
                      "tab2",
                      tabInfo.title,
                      "tab2 is found after navigating forward with empty parameter"
                    );
                    browser.tabs.goForward(tabId);
                    break;

                  case 7:
                    browser.test.assertEq(
                      "tab1",
                      tabInfo.title,
                      "tab1 is found after navigating forward with tabId as parameter"
                    );
                    await browser.tabs.remove(tabId);
                    browser.test.notifyPass("tabs.goBack.goForward");
                    break;

                  default:
                    break;
                }
              }
            );

            tab = await browser.tabs.create({ url: "tab1.html", active: true });
          },
        });

        await extension.startup();
        await extension.awaitFinish("tabs.goBack.goForward");
        await extension.unload();
      });
    </script>
  </body>
</html>