2011-11-21 Pavel Feldman <pfeldman@google.com>
[WebKit-https.git] / LayoutTests / inspector / extensions / extensions-events.html
1 <html>
2 <head>
3 <script src="../../http/tests/inspector/inspector-test.js"></script>
4 <script src="../../http/tests/inspector/extensions-test.js"></script>
5 <script type="text/javascript">
6
7 function extension_testOnSelectionChanged(nextTest)
8 {
9     function onSelectionChanged()
10     {
11         webInspector.panels.elements.onSelectionChanged.removeListener(onSelectionChanged);
12         output("onSelectionChanged fired");
13         nextTest();
14     }
15     webInspector.panels.elements.onSelectionChanged.addListener(onSelectionChanged);
16     webInspector.inspectedWindow.eval("inspect(document.body.children[0])");
17 }
18
19 function extension_testOnRequestFinished(nextTest)
20 {
21     function onRequestFinished()
22     {
23         webInspector.network.onRequestFinished.removeListener(onRequestFinished);
24         output("onRequestFinished fired");
25         nextTest();
26     }
27     webInspector.network.onRequestFinished.addListener(onRequestFinished);
28     webInspector.inspectedWindow.eval("var xhr = new XMLHttpRequest(); xhr.open('GET', '" + location.href + "', false); xhr.send(null);");
29 }
30
31 function extension_testOnNavigated(nextTest)
32 {
33     var urls = [];
34     var loadCount = 0;
35
36     function onLoad()
37     {
38         ++loadCount;
39         processEvent();
40     }
41     function processEvent()
42     {
43         if (loadCount !== urls.length)
44             return;
45         if (loadCount === 1)
46             evaluateOnFrontend("InspectorTest.navigate(WebInspector.mainResource.url.substring(0, WebInspector.mainResource.url.indexOf('?')), reply)", onLoad);
47         else {
48             webInspector.network.onNavigated.removeListener(onNavigated);
49             for (var i = 0; i < urls.length; ++i)
50                 output("Navigated to: " + urls[i]);
51             nextTest();
52         }
53     }
54     function onNavigated(url)
55     {
56         urls.push(url.replace(/^(.*\/)*/, ""));
57         processEvent();
58     }
59     webInspector.network.onNavigated.addListener(onNavigated);
60     evaluateOnFrontend("InspectorTest.navigate(WebInspector.mainResource.url + '?navigated', reply)", onLoad);
61 }
62
63 function extension_testViewShowHide(nextTest)
64 {
65     var listenersToCleanup = [];
66     var sidebar;
67     var beenToExtensionPanel = false;
68
69     function onViewEvent(type, viewName, viewWindow)
70     {
71         output("Got " + type + " event for " + viewName);
72         if (type !== "onShown")
73             return;
74         if (viewName === "panel") {
75             output("Panel shown, location: " + trimURL(viewWindow.location.href));
76             extension_showPanel("elements");
77         } else if (viewName === "sidebar") {
78             output("Sidebar shown, location: " + trimURL(viewWindow.location.href));
79             if (!beenToExtensionPanel) {
80                 extension_showPanel("extension");
81                 beenToExtensionPanel = true;
82             } else {
83                 cleanupListeners();
84                 nextTest();
85             }
86         }
87     }
88     function addListener(view, viewName, type)
89     {
90         var listener = bind(onViewEvent, null, type, viewName);
91         var event = view[type];
92         listenersToCleanup.push({ event: event, listener: listener });
93         event.addListener(listener);
94     }
95     function cleanupListeners()
96     {
97         for (var i = 0; i < listenersToCleanup.length; ++i)
98             listenersToCleanup[i].event.removeListener(listenersToCleanup[i].listener);
99     }
100     function onPanelCreated(panel)
101     {
102         addListener(panel, "panel", "onShown");
103         addListener(panel, "panel", "onHidden");
104         addListener(sidebar, "sidebar", "onHidden");
105         addListener(sidebar, "sidebar", "onShown");
106         sidebar.setPage(basePath + "extension-sidebar.html");
107     }
108     extension_showPanel("elements");
109     var basePath = location.pathname.replace(/\/[^/]*$/, "/");
110     function onSidebarCreated(sidebarPane)
111     {
112         sidebar = sidebarPane;
113         webInspector.panels.create("Test Panel", basePath + "extension-panel.png", basePath + "extension-panel.html", onPanelCreated);
114     }
115     webInspector.panels.elements.createSidebarPane("Test Sidebar", onSidebarCreated);
116 }
117
118 function extension_testTimelineEvents(nextTest)
119 {
120     function onTimelineEvent(record)
121     {
122         if (record.type === "Layout") {
123             output("Got Layout event from timeline");
124             webInspector.timeline.onEventRecorded.removeListener(onTimelineEvent);
125             nextTest();
126         }
127     }
128
129     webInspector.timeline.onEventRecorded.addListener(onTimelineEvent);
130
131     // Trigger a relayout.
132     webInspector.inspectedWindow.eval("document.body.style.float = 'left';");
133 }
134
135 </script>
136 </head>
137 <body onload="runTest()">
138 <p>Tests WebInspector extension API</p>
139 </body>
140 </html>