Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / inspector-protocol / css / css-protocol-test.js
1 function initialize_cssTest()
2 {
3
4 InspectorTest.dumpStyleSheetText = function(styleSheetId, callback)
5 {
6     InspectorTest.sendCommandOrDie("CSS.getStyleSheetText", { styleSheetId: styleSheetId }, onStyleSheetText);
7     function onStyleSheetText(result)
8     {
9         InspectorTest.log("==== Style sheet text ====");
10         InspectorTest.log(result.text);
11         callback();
12     }
13 }
14
15 function updateStyleSheetRange(command, styleSheetId, expectError, options, callback)
16 {
17     options.styleSheetId = styleSheetId;
18     if (expectError)
19         InspectorTest.sendCommand(command, options, onResponse);
20     else
21         InspectorTest.sendCommandOrDie(command, options, onSuccess);
22
23     function onSuccess()
24     {
25         InspectorTest.dumpStyleSheetText(styleSheetId, callback);
26     }
27
28     function onResponse(message)
29     {
30         if (!message.error) {
31             InspectorTest.log("ERROR: protocol method call did not return expected error. Instead, the following message was received: " + JSON.stringify(message));
32             InspectorTest.completeTest();
33             return;
34         }
35         InspectorTest.log("Expected protocol error: " + message.error.message);
36         callback();
37     }
38 }
39
40 InspectorTest.setPropertyText = updateStyleSheetRange.bind(null, "CSS.setPropertyText");
41 InspectorTest.setRuleSelector = updateStyleSheetRange.bind(null, "CSS.setRuleSelector");
42 InspectorTest.addRule = updateStyleSheetRange.bind(null, "CSS.addRule");
43
44 InspectorTest.requestMainFrameId = function(callback)
45 {
46     InspectorTest.sendCommandOrDie("Page.enable", {}, pageEnabled);
47
48     function pageEnabled()
49     {
50         InspectorTest.sendCommandOrDie("Page.getResourceTree", {}, resourceTreeLoaded);
51     }
52
53     function resourceTreeLoaded(payload)
54     {
55         callback(payload.frameTree.frame.id);
56     }
57 };
58
59 InspectorTest.requestDocumentNodeId = function(callback)
60 {
61     InspectorTest.sendCommandOrDie("DOM.getDocument", {}, onGotDocument);
62
63     function onGotDocument(result)
64     {
65         callback(result.root.nodeId);
66     }
67 };
68
69 InspectorTest.requestNodeId = function(documentNodeId, selector, callback)
70 {
71     InspectorTest.sendCommandOrDie("DOM.querySelector", { "nodeId": documentNodeId , "selector": selector }, onGotNode);
72
73     function onGotNode(result)
74     {
75         callback(result.nodeId);
76     }
77 };
78
79 InspectorTest.dumpRuleMatch = function(ruleMatch)
80 {
81     function log(indent, string)
82     {
83         var indentString = Array(indent+1).join(" ");
84         InspectorTest.log(indentString + string);
85     }
86
87     var rule = ruleMatch.rule;
88     var matchingSelectors = ruleMatch.matchingSelectors;
89     var media = rule.media || [];
90     var mediaLine = "";
91     for (var i = 0; i < media.length; ++i)
92         mediaLine += (i > 0 ? " " : "") + media[i].text;
93     var baseIndent = 0;
94     if (mediaLine.length) {
95         log(baseIndent, "@media " + mediaLine);
96         baseIndent += 4;
97     }
98     var selectorLine = "";
99     var selectors = rule.selectorList.selectors;
100     for (var i = 0; i < selectors.length; ++i) {
101         if (i > 0)
102             selectorLine += ", ";
103         var matching = matchingSelectors.indexOf(i) !== -1;
104         if (matching)
105             selectorLine += "*";
106         selectorLine += selectors[i].value;
107         if (matching)
108             selectorLine += "*";
109     }
110     selectorLine += " {";
111     selectorLine += "    " + rule.origin;
112     log(baseIndent, selectorLine);
113     var style = rule.style;
114     var cssProperties = style.cssProperties;
115     for (var i = 0; i < cssProperties.length; ++i) {
116         var cssProperty = cssProperties[i];
117         var propertyLine = cssProperty.name + ": " + cssProperty.value + ";";
118         log(baseIndent + 4, propertyLine);
119     }
120     log(baseIndent, "}");
121 };
122
123 InspectorTest.displayName = function(url)
124 {
125     return url.substr(url.lastIndexOf("/") + 1);
126 };
127
128 InspectorTest.loadAndDumpMatchingRulesForNode = function(nodeId, callback)
129 {
130     InspectorTest.sendCommandOrDie("CSS.getMatchedStylesForNode", { "nodeId": nodeId }, matchingRulesLoaded);
131
132     function matchingRulesLoaded(result)
133     {
134         InspectorTest.log("Dumping matched rules: ");
135         var ruleMatches = result.matchedCSSRules;
136         for (var i = 0; i < ruleMatches.length; ++i) {
137             var ruleMatch = ruleMatches[i];
138             var origin = ruleMatch.rule.origin;
139             if (origin !== "inspector" && origin !== "regular")
140                 continue;
141             InspectorTest.dumpRuleMatch(ruleMatch);
142         }
143         callback();
144     }
145 }
146
147 InspectorTest.loadAndDumpMatchingRules = function(documentNodeId, selector, callback)
148 {
149     InspectorTest.requestNodeId(documentNodeId, selector, nodeIdLoaded);
150
151     function nodeIdLoaded(nodeId)
152     {
153         InspectorTest.loadAndDumpMatchingRulesForNode(nodeId, callback);
154     }
155 }
156
157 }