2011-02-15 Pavel Feldman <pfeldman@chromium.org>
[WebKit-https.git] / LayoutTests / http / tests / inspector / elements-test.js
1 var initialize_ElementTest = function() {
2
3
4 InspectorTest.nodeWithId = function(idValue, callback)
5 {
6     var result = null;
7     var topLevelChildrenRequested = false;
8     var pendingRequests = 0;
9     function processChildren(topLevel, children)
10     {
11         pendingRequests--;
12         if (result)
13             return;
14
15         for (var i = 0; children && i < children.length; ++i) {
16             var childNode = children[i];
17             if (childNode.getAttribute("id") === idValue) {
18                 result = childNode;
19                 if (callback)
20                     callback(result);
21                 return;
22             }
23             pendingRequests++;
24             WebInspector.domAgent.getChildNodesAsync(childNode, processChildren.bind(null, false));
25         }
26
27         if (topLevel)
28             topLevelChildrenRequested = true;
29         if (topLevelChildrenRequested && !result && !pendingRequests)
30             callback(null);    
31     }
32     pendingRequests++;
33     WebInspector.domAgent.getChildNodesAsync(WebInspector.domAgent.document, processChildren.bind(this, true));
34 };
35
36 InspectorTest.expandedNodeWithId = function(idValue)
37 {
38     var result;
39     function callback(node)
40     {
41         result = node;
42     }
43     InspectorTest.nodeWithId(idValue, callback);
44     return result;
45 }
46
47 InspectorTest.selectNodeWithId = function(idValue, callback)
48 {
49     function mycallback(node)
50     {
51         if (node)
52             WebInspector.updateFocusedNode(node.id);
53         if (callback)
54             InspectorTest.runAfterPendingDispatches(callback.bind(null, node));
55     }
56     InspectorTest.nodeWithId(idValue, mycallback);
57 };
58
59 InspectorTest.dumpSelectedElementStyles = function(excludeComputed, excludeMatched, omitLonghands)
60 {
61     var styleSections = WebInspector.panels.elements.sidebarPanes.styles.sections;
62     for (var pseudoId in styleSections) {
63         var pseudoName = WebInspector.StylesSidebarPane.PseudoIdNames[pseudoId];
64         var sections = styleSections[pseudoId];
65         for (var i = 0; i < sections.length; ++i) {
66             var section = sections[i];
67             if (section.computedStyle && excludeComputed)
68                 continue;
69             if (section.rule && excludeMatched)
70                 continue;
71             if (section.element.previousSibling && section.element.previousSibling.className === "styles-sidebar-separator")
72                 InspectorTest.addResult("======== " + section.element.previousSibling.textContent + " ========");
73             InspectorTest.addResult((section.expanded ? "[expanded] " : "[collapsed] ") + section.titleElement.textContent + " (" + section.subtitleAsTextForTest + ")");
74             section.expand();
75             InspectorTest.dumpStyleTreeOutline(section.propertiesTreeOutline, omitLonghands ? 1 : 2);
76             InspectorTest.addResult("");
77         }
78         InspectorTest.addResult("");
79     }
80 };
81
82 // FIXME: this returns the first tree item found (may fail for same-named properties in a style).
83 InspectorTest.getElementStylePropertyTreeItem = function(propertyName)
84 {
85     var styleSections = WebInspector.panels.elements.sidebarPanes.styles.sections[0];
86     var elementStyleSection = styleSections[1];
87     var outline = elementStyleSection.propertiesTreeOutline;
88     for (var i = 0; i < outline.children.length; ++i) {
89         var treeItem = outline.children[i];
90         if (treeItem.name === propertyName)
91             return treeItem;
92     }
93     return null;
94 };
95
96 InspectorTest.dumpStyleTreeOutline = function(treeItem, depth)
97 {
98     var children = treeItem.children;
99     for (var i = 0; i < children.length; ++i)
100         InspectorTest.dumpStyleTreeItem(children[i], "", depth || 2);
101 };
102
103 InspectorTest.dumpStyleTreeItem = function(treeItem, prefix, depth)
104 {
105     // Filter out width and height properties in order to minimize
106     // potential diffs.
107     if (!treeItem.listItemElement.textContent.indexOf("width") ||
108         !treeItem.listItemElement.textContent.indexOf("height"))
109         return;
110
111     if (treeItem.listItemElement.hasStyleClass("inherited"))
112         return;
113     var typePrefix = "";
114     if (treeItem.listItemElement.hasStyleClass("overloaded"))
115         typePrefix += "/-- overloaded --/ ";
116     if (treeItem.listItemElement.hasStyleClass("disabled"))
117         typePrefix += "/-- disabled --/ ";
118     var textContent = treeItem.listItemElement.textContent;
119
120     // Add non-selectable url text.
121     var textData = treeItem.listItemElement.querySelector("[data-uncopyable]");
122     if (textData)
123         textContent += textData.getAttribute("data-uncopyable");
124     InspectorTest.addResult(prefix + typePrefix + textContent);
125     if (--depth) {
126         treeItem.expand();
127         var children = treeItem.children;
128         for (var i = 0; children && i < children.length; ++i)
129             InspectorTest.dumpStyleTreeItem(children[i], prefix + "    ", depth);
130     }
131 };
132
133 InspectorTest.dumpElementsTree = function(rootNode)
134 {
135     function beautify(element)
136     {
137         return element.textContent.replace(/\u200b/g, "").replace(/\n/g, "").trim();
138     }
139
140     function print(treeItem, prefix)
141     {
142         if (treeItem.listItemElement) {
143             var expander;
144             if (treeItem.hasChildren) {
145                 if (treeItem.expanded)
146                     expander = "- ";
147                 else
148                     expander = "+ ";
149             } else
150                 expander = "  ";
151
152             InspectorTest.addResult(prefix + expander + beautify(treeItem.listItemElement));
153         }
154         
155
156         if (!treeItem.expanded)
157             return;
158
159         var children = treeItem.children;
160         for (var i = 0; children && i < children.length - 1; ++i)
161             print(children[i], prefix + "    ");
162
163         // Closing tag.
164         if (children && children.length)
165             print(children[children.length - 1], prefix);
166     }
167
168     WebInspector.panels.elements.updateModifiedNodes();
169     var treeOutline = WebInspector.panels.elements.treeOutline;
170     print(rootNode ? treeOutline.findTreeElement(rootNode) : treeOutline, "");
171 };
172
173 InspectorTest.expandElementsTree = function(callback)
174 {
175     function expand(treeItem)
176     {
177         var children = treeItem.children;
178         for (var i = 0; children && i < children.length; ++i) {
179             children[i].expand();
180             expand(children[i]);
181         }
182     }
183
184     function mycallback()
185     {
186         WebInspector.panels.elements.updateModifiedNodes();
187         expand(WebInspector.panels.elements.treeOutline);
188         if (callback)
189             callback();
190     }
191     InspectorTest.nodeWithId(/nonstring/, mycallback);
192 };
193
194 InspectorTest.dumpDOMAgentTree = function()
195 {
196     function dump(node, prefix)
197     {
198         InspectorTest.addResult(prefix + node.nodeName + "[" + node.id + "]");
199         var children = node.children;
200         for (var i = 0; children && i < children.length; ++i)
201             dump(children[i], prefix + "    ");
202     }
203     dump(WebInspector.domAgent.document, "");
204 };
205
206 };