summaryrefslogtreecommitdiffstats
path: root/dom/plugins/test/mochitest/plugin-utils.js
blob: 90d3c285d6dfc7ec63b3c50a664e0b0e61eecbb2 (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
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);
  while (plugin.enabledState != newEnabledState) {
    // Run a nested event loop to wait for the preference change to
    // propagate to the child. Yuck!
    SpecialPowers.Services.tm.currentThread.processNextEvent(true);
  }
  SimpleTest.registerCleanupFunction(function() {
    SpecialPowers.setTestPluginEnabledState(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();
  });
}