|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 // Tests devtools API |
|
5 |
|
6 const Cu = Components.utils; |
|
7 const toolId = "test-tool"; |
|
8 |
|
9 let tempScope = {}; |
|
10 Cu.import("resource://gre/modules/devtools/event-emitter.js", tempScope); |
|
11 let EventEmitter = tempScope.EventEmitter; |
|
12 |
|
13 function test() { |
|
14 addTab("about:blank", function(aBrowser, aTab) { |
|
15 runTests(aTab); |
|
16 }); |
|
17 } |
|
18 |
|
19 function runTests(aTab) { |
|
20 let toolDefinition = { |
|
21 id: toolId, |
|
22 isTargetSupported: function() true, |
|
23 visibilityswitch: "devtools.test-tool.enabled", |
|
24 url: "about:blank", |
|
25 label: "someLabel", |
|
26 build: function(iframeWindow, toolbox) { |
|
27 let panel = new DevToolPanel(iframeWindow, toolbox); |
|
28 return panel.open(); |
|
29 }, |
|
30 }; |
|
31 |
|
32 ok(gDevTools, "gDevTools exists"); |
|
33 is(gDevTools.getToolDefinitionMap().has(toolId), false, |
|
34 "The tool is not registered"); |
|
35 |
|
36 gDevTools.registerTool(toolDefinition); |
|
37 is(gDevTools.getToolDefinitionMap().has(toolId), true, |
|
38 "The tool is registered"); |
|
39 |
|
40 let target = TargetFactory.forTab(gBrowser.selectedTab); |
|
41 gDevTools.showToolbox(target, toolId).then(function(toolbox) { |
|
42 is(toolbox.target, target, "toolbox target is correct"); |
|
43 is(toolbox._host.hostTab, gBrowser.selectedTab, "toolbox host is correct"); |
|
44 continueTests(toolbox); |
|
45 }).then(null, console.error); |
|
46 } |
|
47 |
|
48 function continueTests(toolbox, panel) { |
|
49 ok(toolbox.getCurrentPanel(), "panel value is correct"); |
|
50 is(toolbox.currentToolId, toolId, "toolbox _currentToolId is correct"); |
|
51 |
|
52 ok(!toolbox.doc.getElementById("toolbox-tab-" + toolId).hasAttribute("icon-invertable"), |
|
53 "The tool tab does not have the invertable attribute"); |
|
54 |
|
55 ok(toolbox.doc.getElementById("toolbox-tab-inspector").hasAttribute("icon-invertable"), |
|
56 "The builtin tool tabs do have the invertable attribute"); |
|
57 |
|
58 let toolDefinitions = gDevTools.getToolDefinitionMap(); |
|
59 is(toolDefinitions.has(toolId), true, "The tool is in gDevTools"); |
|
60 |
|
61 let toolDefinition = toolDefinitions.get(toolId); |
|
62 is(toolDefinition.id, toolId, "toolDefinition id is correct"); |
|
63 |
|
64 gDevTools.unregisterTool(toolId); |
|
65 is(gDevTools.getToolDefinitionMap().has(toolId), false, |
|
66 "The tool is no longer registered"); |
|
67 |
|
68 toolbox.destroy().then(function() { |
|
69 let target = TargetFactory.forTab(gBrowser.selectedTab); |
|
70 ok(gDevTools._toolboxes.get(target) == null, "gDevTools doesn't know about target"); |
|
71 ok(toolbox._target == null, "toolbox doesn't know about target."); |
|
72 |
|
73 finishUp(); |
|
74 }).then(null, console.error); |
|
75 } |
|
76 |
|
77 function finishUp() { |
|
78 tempScope = null; |
|
79 gBrowser.removeCurrentTab(); |
|
80 finish(); |
|
81 } |
|
82 |
|
83 /** |
|
84 * When a Toolbox is started it creates a DevToolPanel for each of the tools |
|
85 * by calling toolDefinition.build(). The returned object should |
|
86 * at least implement these functions. They will be used by the ToolBox. |
|
87 * |
|
88 * There may be no benefit in doing this as an abstract type, but if nothing |
|
89 * else gives us a place to write documentation. |
|
90 */ |
|
91 function DevToolPanel(iframeWindow, toolbox) { |
|
92 EventEmitter.decorate(this); |
|
93 |
|
94 this._toolbox = toolbox; |
|
95 |
|
96 /*let doc = iframeWindow.document |
|
97 let label = doc.createElement("label"); |
|
98 let textNode = doc.createTextNode("Some Tool"); |
|
99 |
|
100 label.appendChild(textNode); |
|
101 doc.body.appendChild(label);*/ |
|
102 } |
|
103 |
|
104 DevToolPanel.prototype = { |
|
105 open: function() { |
|
106 let deferred = promise.defer(); |
|
107 |
|
108 executeSoon(function() { |
|
109 this._isReady = true; |
|
110 this.emit("ready"); |
|
111 deferred.resolve(this); |
|
112 }.bind(this)); |
|
113 |
|
114 return deferred.promise; |
|
115 }, |
|
116 |
|
117 get target() this._toolbox.target, |
|
118 |
|
119 get toolbox() this._toolbox, |
|
120 |
|
121 get isReady() this._isReady, |
|
122 |
|
123 _isReady: false, |
|
124 |
|
125 destroy: function DTI_destroy() { |
|
126 return promise.defer(null); |
|
127 }, |
|
128 }; |