summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/plugins/browser_CTP_favorfallback.js
blob: ba3af78f57ec3fd2f456183871bf86d81c12d3b7 (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
var rootDir = getRootDirectory(gTestPath);
const gTestRoot = rootDir.replace(
  "chrome://mochitests/content/",
  "http://127.0.0.1:8888/"
);
var gPluginHost = Cc["@mozilla.org/plugin/host;1"].getService(Ci.nsIPluginHost);

add_task(async function() {
  registerCleanupFunction(function() {
    clearAllPluginPermissions();
    Services.prefs.clearUserPref("plugins.favorfallback.mode");
    Services.prefs.clearUserPref("plugins.favorfallback.rules");
  });
});

add_task(async function() {
  Services.prefs.setCharPref("plugins.favorfallback.mode", "follow-ctp");
});

/* The expected behavior of each testcase is documented with its markup
 * in plugin_favorfallback.html.
 *
 *  - "name" is the name of the testcase in the test file.
 *  - "rule" is how the plugins.favorfallback.rules must be configured
 *           for this testcase.
 */
const testcases = [
  {
    name: "video",
    rule: "video",
  },

  {
    name: "nosrc",
    rule: "nosrc",
  },

  {
    name: "embed",
    rule: "embed,true",
  },

  {
    name: "adobelink",
    rule: "adobelink,true",
  },

  {
    name: "installinstructions",
    rule: "installinstructions,true",
  },
];

add_task(async function() {
  for (let testcase of Object.values(testcases)) {
    info(`Running testcase ${testcase.name}`);

    Services.prefs.setCharPref("plugins.favorfallback.rules", testcase.rule);

    let tab = await BrowserTestUtils.openNewForegroundTab(
      gBrowser,
      `${gTestRoot}plugin_favorfallback.html?testcase=${testcase.name}`
    );

    await SpecialPowers.spawn(
      tab.linkedBrowser,
      [testcase.name],
      async function testPlugins(name) {
        let testcaseDiv = content.document.getElementById(`testcase_${name}`);
        let ctpPlugins = testcaseDiv.querySelectorAll(".expected_ctp");

        for (let ctpPlugin of ctpPlugins) {
          ok(
            ctpPlugin instanceof Ci.nsIObjectLoadingContent,
            "This is a plugin object"
          );
          is(
            ctpPlugin.pluginFallbackType,
            Ci.nsIObjectLoadingContent.PLUGIN_ALTERNATE,
            "Plugins always use alternate content"
          );
        }

        let fallbackPlugins = testcaseDiv.querySelectorAll(
          ".expected_fallback"
        );

        for (let fallbackPlugin of fallbackPlugins) {
          ok(
            fallbackPlugin instanceof Ci.nsIObjectLoadingContent,
            "This is a plugin object"
          );
          is(
            fallbackPlugin.pluginFallbackType,
            Ci.nsIObjectLoadingContent.PLUGIN_ALTERNATE,
            "Plugin fallback content was used"
          );
        }
      }
    );

    BrowserTestUtils.removeTab(tab);
  }
});