3 <script src=
"../../inspector-test.js"></script>
4 <script src=
"../../elements-test.js"></script>
5 <link rel=
"stylesheet" href=
"../styles/resources/stylesheet-tracking.css" />
15 function addStyleSheet()
17 var styleElement
= document
.createElement("style");
18 styleElement
.id
= "style";
19 styleElement
.type
= "text/css";
20 styleElement
.textContent
= "@import url(\"resources/stylesheet-tracking-import.css\");\na { color: green; }"
21 document
.head
.appendChild(styleElement
);
24 function removeImport()
26 document
.getElementById("style").sheet
.deleteRule(0);
29 function removeStyleSheet()
31 document
.head
.removeChild(document
.getElementById("style"));
36 var iframe
= document
.createElement("iframe");
37 iframe
.setAttribute("seamless", "seamless");
38 iframe
.src
= "resources/stylesheet-tracking-iframe.html";
39 document
.body
.appendChild(iframe
);
44 return document
.getElementsByTagName("iframe")[0];
47 function addIframeStyleSheets()
49 iframe().contentWindow
.postMessage("addStyleSheets", "*");
52 function navigateIframe()
54 iframe().src
= iframe().src
;
57 function removeIframeStyleSheets()
59 iframe().contentWindow
.postMessage("removeStyleSheets", "*");
62 function removeIframe()
64 var element
= iframe();
65 element
.parentElement
.removeChild(element
);
72 InspectorTest
.cssModel
.addEventListener(WebInspector
.CSSStyleModel
.Events
.StyleSheetAdded
, styleSheetAdded
, null);
73 InspectorTest
.cssModel
.addEventListener(WebInspector
.CSSStyleModel
.Events
.StyleSheetRemoved
, styleSheetRemoved
, null);
74 var headers
= InspectorTest
.cssModel
.styleSheetHeaders();
75 InspectorTest
.addResult(headers
.length
+ " headers known:");
76 sortAndDumpData(headers
);
78 InspectorTest
.selectNodeAndWaitForStyles("inspected", step0
);
83 InspectorTest
.addResult("=== Adding iframe... ===");
84 InspectorTest
.evaluateInPage("loadIframe()");
85 waitStyleSheetAdded(1, iframeAdded
);
87 function iframeAdded()
89 InspectorTest
.addResult("=== Adding iframe stylesheet... ===");
90 waitStyleSheetAdded(2, navigateIframe
);
91 InspectorTest
.evaluateInPage("addIframeStyleSheets()");
94 function navigateIframe()
96 InspectorTest
.addResult("=== Navigating iframe... ===");
97 waitStyleSheetRemoved(3, frameNavigated
);
98 InspectorTest
.evaluateInPage("navigateIframe()");
100 function frameNavigated()
102 waitStyleSheetAdded(1, styleSheetsAdded
);
105 function styleSheetsAdded()
107 InspectorTest
.addResult("=== Adding iframe stylesheet... ===");
108 waitStyleSheetAdded(2, removeIframeStyleSheets
);
109 InspectorTest
.evaluateInPage("addIframeStyleSheets()");
113 function removeIframeStyleSheets()
115 InspectorTest
.addResult("=== Removing iframe stylesheet... ===");
116 waitStyleSheetRemoved(2, step1
)
117 InspectorTest
.evaluateInPage("removeIframeStyleSheets()");
123 InspectorTest
.addResult("=== Adding stylesheet... ===");
124 waitStyleSheetAdded(4, step2
);
125 InspectorTest
.evaluateInPage("addStyleSheet()");
130 InspectorTest
.addResult("=== Removing @import... ===");
131 waitStyleSheetRemoved(3, step3
);
132 InspectorTest
.evaluateInPage("removeImport()");
137 InspectorTest
.addResult("=== Removing stylesheet... ===");
138 waitStyleSheetRemoved(1, step4
);
139 InspectorTest
.evaluateInPage("removeStyleSheet()");
144 InspectorTest
.addResult("=== Adding rule... ===");
145 waitStyleSheetAdded(1);
146 InspectorTest
.addNewRule("#inspected", successCallback
);
148 function successCallback()
150 InspectorTest
.addResult("Rule added");
157 InspectorTest
.addResult("=== Removing iframe... ===");
158 InspectorTest
.evaluateInPage("removeIframe()");
159 waitStyleSheetRemoved(1, step6
);
164 InspectorTest
.completeTest();
169 var addedSheets
= [];
171 function waitStyleSheetAdded(count
, next
)
173 addedSheetCount
= count
;
174 addedCallback
= next
;
177 function styleSheetAdded(event
)
179 var header
= event
.data
;
180 addedSheets
.push(header
);
182 if (addedSheetCount
> 0)
184 else if (addedSheetCount
< 0)
185 InspectorTest
.addResult("Unexpected styleSheetAdded()");
187 InspectorTest
.addResult("Stylesheets added:");
188 sortAndDumpData(addedSheets
);
191 var callback
= addedCallback
;
192 addedCallback
= null;
198 var removedSheetCount
;
199 var removedSheets
= [];
201 function waitStyleSheetRemoved(count
, next
)
203 removedSheetCount
= count
;
204 removedCallback
= next
;
207 function styleSheetRemoved(event
)
209 var header
= event
.data
;
210 removedSheets
.push(header
);
212 if (removedSheetCount
> 0)
214 else if (removedSheetCount
< 0)
215 InspectorTest
.addResult("Unexpected styleSheetRemoved()");
217 InspectorTest
.addResult("Stylesheets removed:");
218 sortAndDumpData(removedSheets
);
220 if (removedCallback
) {
221 var callback
= removedCallback
;
222 removedCallback
= null;
227 function sortAndDumpData(sheets
)
229 function sorter(a
, b
)
231 return a
.sourceURL
.localeCompare(b
.sourceURL
);
233 sheets
= sheets
.sort(sorter
);
234 for (var i
= 0; i
< sheets
.length
; ++i
) {
235 var header
= sheets
[i
];
236 InspectorTest
.addResult(" URL=" + trimURL(header
.sourceURL
));
237 InspectorTest
.addResult(" origin=" + header
.origin
);
238 InspectorTest
.addResult(" isInline=" + header
.isInline
);
239 InspectorTest
.addResult(" hasSourceURL=" + header
.hasSourceURL
);
243 function trimURL(url
)
247 var lastIndex
= url
.lastIndexOf("inspector/");
250 return ".../" + url
.substr(lastIndex
);
256 <body onload=
"runTest()">
258 Tests that the styleSheetAdded and styleSheetRemoved events are reported into the frontend.
<a href=
"https://bugs.webkit.org/show_bug.cgi?id=105828">Bug
105828</a>.
261 <div id=
"inspected">Text
</div>