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