-
Notifications
You must be signed in to change notification settings - Fork 54
/
MockColorPicker.jsm
115 lines (104 loc) · 3.16 KB
/
MockColorPicker.jsm
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var EXPORTED_SYMBOLS = ["MockColorPicker"];
const Cm = Components.manager;
const CONTRACT_ID = "@mozilla.org/colorpicker;1";
// Allow stuff from this scope to be accessed from non-privileged scopes. This
// would crash if used outside of automation.
Cu.forcePermissiveCOWs();
var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
var oldClassID = "";
var newClassID = Cc["@mozilla.org/uuid-generator;1"]
.getService(Ci.nsIUUIDGenerator)
.generateUUID();
var newFactory = function(window) {
return {
createInstance(aOuter, aIID) {
if (aOuter) {
throw Cr.NS_ERROR_NO_AGGREGATION;
}
return new MockColorPickerInstance(window).QueryInterface(aIID);
},
lockFactory(aLock) {
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
},
QueryInterface: ChromeUtils.generateQI([Ci.nsIFactory]),
};
};
var MockColorPicker = {
init(window) {
this.reset();
this.factory = newFactory(window);
if (!registrar.isCIDRegistered(newClassID)) {
try {
oldClassID = registrar.contractIDToCID(CONTRACT_ID);
} catch (ex) {
oldClassID = "";
dump(
"TEST-INFO | can't get colorpicker registered component, " +
"assuming there is none"
);
}
registrar.registerFactory(newClassID, "", CONTRACT_ID, this.factory);
}
},
reset() {
this.returnColor = "";
this.showCallback = null;
this.shown = false;
this.showing = false;
},
cleanup() {
var previousFactory = this.factory;
this.reset();
this.factory = null;
registrar.unregisterFactory(newClassID, previousFactory);
if (oldClassID != "") {
registrar.registerFactory(oldClassID, "", CONTRACT_ID, null);
}
},
};
function MockColorPickerInstance(window) {
this.window = window;
}
MockColorPickerInstance.prototype = {
QueryInterface: ChromeUtils.generateQI([Ci.nsIColorPicker]),
init(aParent, aTitle, aInitialColor) {
this.parent = aParent;
this.initialColor = aInitialColor;
},
initialColor: "",
parent: null,
open(aColorPickerShownCallback) {
MockColorPicker.showing = true;
MockColorPicker.shown = true;
this.window.setTimeout(() => {
let result = "";
try {
if (typeof MockColorPicker.showCallback == "function") {
var updateCb = function(color) {
result = color;
aColorPickerShownCallback.update(color);
};
let returnColor = MockColorPicker.showCallback(this, updateCb);
if (typeof returnColor === "string") {
result = returnColor;
}
} else if (typeof MockColorPicker.returnColor === "string") {
result = MockColorPicker.returnColor;
}
} catch (ex) {
dump(
"TEST-UNEXPECTED-FAIL | Exception in MockColorPicker.jsm open() " +
"method: " +
ex +
"\n"
);
}
if (aColorPickerShownCallback) {
aColorPickerShownCallback.done(result);
}
}, 0);
},
};