aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test-pageMod.js
blob: 3f7e698cc7bfe509ab02122bb6ba24c301b2f646 (plain) (blame)
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
/*jslint white: false, eqeqeq: false, plusplus: false, onevar: false, newcap: false */
/*global exports: false, require: false, console: false, log: false */
"use strict";
var Cc = require("chrome").Cc;
var Ci = require("chrome").Ci;
var main = require("main");
var utilMod = require("util");
var testPageMod = require("pagemod-test-helpers").testPageMod;
var self = require("self");

var theURL = main.theURL;
var testURL = self.data.url('tests/change-more-bugs01.html');
var JSONifiedMessage = '{"cmd":"testMessage","data":{"a":"first","b":"second"}}';

exports.ensureMessagesWork = function(test) {
  var msg = new utilMod.Message("testMessage", {
    a : "first",
    b : "second"
  });
  test.assertEqual(msg.cmd, "testMessage",
      "msg.cmd comes over well");
  test.assertEqual(msg.data.a, "first",
      "msg.data.a comes over well");
  test.assertEqual(msg.data.b, "second",
      "msg.data.b comes over well");
  test.assertEqual(JSON.stringify(msg), JSONifiedMessage,
      "JSONification of Message works as well");
};

/*
 *
 * var theURL = main.theURL; var testURL =
 * self.data.url('tests/change-more-bugs01.html');
 *
 * var contentScriptLibraries = { "bugzilla.redhat.com": [
 * self.data.url("util.js"), self.data.url("color.js"),
 * self.data.url("rhbzpage.js"), self.data.url("bzpage.js") ] };
 *
 * libbz.initialize(libbz.config, function () { pageMod.PageMod({ include: [
 * "https://bugzilla.redhat.com/show_bug.cgi?id=*" ], contentScriptWhen:
 * 'ready', contentScriptFile: contentScriptLibraries["bugzilla.redhat.com"],
 * onAttach: function onAttach(worker, msg) { console.log("worker: " + worker);
 * worker.on('message', function (msg) { messageHandler(worker, msg); }); } });
 * });
 *
 * pageMod.PageMod({ include: [ "https://bugzilla.redhat.com/process_bug.cgi" ],
 * contentScriptWhen: 'ready', contentScriptFile: self.data.url("skip-bug.js")
 * });
 */

var ensureSimplePageLoad = function(test) {
  console.log("testURL = " + testURL);
  testPageMod(test, testURL, [
    {
      include : [
        "*"
      ],
      contentScriptWhen : 'ready',
      contentScriptFile : [
          self.data.url("libPW.js"),
          self.data.url("simplePageWorker.js")
      ],
      onAttach : function onAttach(worker) {
        worker.on('message', function(msg) {
          switch (msg.cmd) {
          case "CallBack":
            worker
                .postMessage(new utilMod.Message("Main", null));
            break;
          default:
            console.error(msg);
          }
        });
      }
    }
  ], function(win, done) {
    test.assertNotEqual(win.document
        .getElementsByTagName("form")[0], null,
        "test of loading the page");
    done();
  });
};

var ensurePageLoadsWell = function(test) {
  var wm = Cc['@mozilla.org/appshell/window-mediator;1']
      .getService(Ci.nsIWindowMediator);
  var browserWindow = wm
      .getMostRecentWindow("navigator:browser");
  if (!browserWindow) {
    test
        .fail("page-mod tests: could not find the browser window, so "
            + "will not run. Use -a firefox to run the pagemod tests.");
    return null;
  }

  var loader = test.makeSandboxedLoader();
  var pageMod = loader.require("page-mod");
  var testDoc = {}, b = {}, tabBrowser = {}, newTab = {};

  pageMod.PageMod({
    include : [
      "*"
    ],
    contentScriptWhen : 'ready',
    contentScriptFile : [
        self.data.url("libPW.js"),
        self.data.url("pageWorker.js")
    ],
    onAttach : function onAttach(worker) {
      worker.on('message', function(msg) {
        switch (msg.cmd) {
        case "testReady":
          testDoc = b.contentWindow.wrappedJSObject.document;
          test.assertNotEqual(testDoc
              .getElementById("dupeid_action"), null,
              "test of DOM modifications of the page");
          pageMod.destroy();
          tabBrowser.removeTab(newTab);
          test.done();
          // the test itself
          break;
        default:
          main.messageHandler(worker, msg);
        }
      });
    }
  });

  tabBrowser = browserWindow.gBrowser;
  newTab = tabBrowser.addTab(testURL);
  tabBrowser.selectedTab = newTab;
  b = tabBrowser.getBrowserForTab(newTab);
};