summaryrefslogtreecommitdiffstats
path: root/dom/plugins/test/mochitest/plugin-utils.js
blob: 1d35d2b04967aa1975febc0dccac3bf5a3d13b2b (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
function paintCountIs(plugin, expected, msg) {
  var count = plugin.getPaintCount();
  var realExpected = expected;
  ++realExpected; // extra paint at startup for all async-rendering plugins
  ok(
    realExpected == count,
    msg +
      " (expected " +
      expected +
      " independent paints, expected " +
      realExpected +
      " logged paints, got " +
      count +
      " actual paints)"
  );
}

function getTestPlugin(pluginName) {
  var ph = SpecialPowers.Cc["@mozilla.org/plugin/host;1"].getService(
    SpecialPowers.Ci.nsIPluginHost
  );
  var tags = ph.getPluginTags();
  var name = pluginName || "Test Plug-in";
  for (var tag of tags) {
    if (tag.name == name) {
      return tag;
    }
  }

  ok(false, "Could not find plugin tag with plugin name '" + name + "'");
  return null;
}

// call this to set the test plugin(s) initially expected enabled state.
// it will automatically be reset to it's previous value after the test
// ends
function setTestPluginEnabledState(newEnabledState, pluginName) {
  var oldEnabledState = SpecialPowers.setTestPluginEnabledState(
    newEnabledState,
    pluginName
  );
  var plugin = getTestPlugin(pluginName);
  // Run a nested event loop to wait for the preference change to
  // propagate to the child. Yuck!
  SpecialPowers.Services.tm.spinEventLoopUntil(() => {
    return plugin.enabledState == newEnabledState;
  });
  SimpleTest.registerCleanupFunction(async function() {
    return SpecialPowers.setTestPluginEnabledState(
      await oldEnabledState,
      pluginName
    );
  });
}

function crashAndGetCrashServiceRecord(crashMethodName, callback) {
  var crashMan = SpecialPowers.Cu.import("resource://gre/modules/Services.jsm")
    .Services.crashmanager;

  // First, clear the crash record store.
  info("Waiting for pruneOldCrashes");
  var future = new Date(Date.now() + 1000 * 60 * 60 * 24);
  crashMan.pruneOldCrashes(future).then(
    function() {
      var iframe = document.getElementById("iframe1");
      var p = iframe.contentDocument.getElementById("plugin1");

      var crashDateMS = Date.now();
      try {
        p[crashMethodName]();
        ok(false, "p." + crashMethodName + "() should throw an exception");
      } catch (e) {
        ok(true, "p." + crashMethodName + "() should throw an exception");
      }

      // The crash record store is written and read back asyncly, so poll for
      // the new record.
      function tryGetCrash() {
        info("Waiting for getCrashes");
        crashMan.getCrashes().then(
          SpecialPowers.wrapCallback(function(crashes) {
            if (crashes.length) {
              is(crashes.length, 1, "There should be only one record");
              var crash = SpecialPowers.wrap(crashes[0]);
              ok(!!crash.id, "Record should have an ID");
              ok(!!crash.crashDate, "Record should have a crash date");
              var dateMS = crash.crashDate.valueOf();
              var twoMin = 1000 * 60 * 2;
              ok(
                crashDateMS - twoMin <= dateMS &&
                  dateMS <= crashDateMS + twoMin,
                "Record's crash date should be nowish: " +
                  "now=" +
                  crashDateMS +
                  " recordDate=" +
                  dateMS
              );
              callback(crashMan, crash);
            } else {
              setTimeout(tryGetCrash, 1000);
            }
          }),
          function(err) {
            ok(false, "Error getting crashes: " + err);
            SimpleTest.finish();
          }
        );
      }
      setTimeout(tryGetCrash, 1000);
    },
    function() {
      ok(false, "pruneOldCrashes error");
      SimpleTest.finish();
    }
  );
}

/**
 * Returns a promise which resolves on `mozFullScreenChange`.
 */
function promiseFullScreenChange() {
  return new Promise(resolve => {
    document.addEventListener(
      "fullscreenchange",
      function(e) {
        resolve();
      },
      { once: true }
    );
  });
}

/**
 * Crashes target plugin. Returns a promise; resolves on successful crash,
 * rejects otherwise.
 * @param plugin  Target plugin to attempt to crash.
 */
function crashPlugin(plugin) {
  return new Promise((resolve, reject) => {
    try {
      plugin.crash();
      reject();
    } catch (e) {
      resolve();
    }
  });
}