Skip to content

Instantly share code, notes, and snippets.

@vivekgalatage
Created August 22, 2012 08:55
Show Gist options
  • Save vivekgalatage/3423871 to your computer and use it in GitHub Desktop.
Save vivekgalatage/3423871 to your computer and use it in GitHub Desktop.
harness.js
==========
var initialize_InspectorTest = function() {
counter = 0;
InspectorTest.sendCommand = function(method, params, callback)
{
var parameters = [];
for (var name in params)
parameters.push("\"" + name + "\":" + "\"" + params[name] +"\"");
var message = "{\"method\": \"" + method + "\",\"params\":{" + parameters.join() + "},\"id\":"+ (counter++) +"}";
InspectorFrontendHost.sendMessageToBackend(message);
}
InspectorTest.log = function(message)
{
InspectorTest.sendCommand("Runtime.evaluate", { "expression": "log(\'" + message + "\');" });
}
InspectorTest.setEventHandler = function(handler)
{
this._genericListener = handler;
}
InspectorTest.removeEventHandler = function()
{
this._genericListener = null;
}
InspectorTest.addEventListener = function(method, handler)
{
if (!this._listeners)
this._listeners = {};
this._listeners[method] = handler;
}
InspectorTest.removeEventListener = function(method)
{
this._listeners[method] = null;
}
InspectorTest.completeTest = function()
{
InspectorTest.sendCommand("Runtime.evaluate", { "expression": "closeTest();" });
}
WebInspector.dispatchMessageFromBackend = function(message)
{
}
}
function log(message)
{
console.log(message);
}
function closeTest()
{
console.log("closing");
testRunner.notifyDone();
}
function runTest()
{
testRunner.dumpAsText();
testRunner.waitUntilDone();
var script = "InspectorTest = {}; WebInspector = {};\n (" + initialize_InspectorTest.toString() + ")();\n" + "(" + test.toString() + ")();";
window.internals.openDummyInspectorFrontend(script);
}
Test.html
=========
<html>
<head>
<script type="text/javascript" src="protocol-test.js"></script>
<script>
function test()
{
InspectorTest.sendCommand("DOMStorage.getDOMStorageEntries", {"storageId":"1"}, null);
InspectorTest.log("Completing test");
InspectorTest.completeTest();
}
</script>
</head>
<body onLoad="runTest();">
</body>
</html>
Internals.cpp
=============
class InspectorFrontendClientDummy : public InspectorFrontendClientLocal {
public:
InspectorFrontendClientDummy(InspectorController*, Page*, PassOwnPtr<InspectorFrontendClientLocal::Settings>);
~InspectorFrontendClientDummy() { }
void attachWindow() { }
void detachWindow() { }
String localizedStringsURL() { return String(); }
String hiddenPanels() { return String(); }
void bringToFront() { }
void closeWindow() { }
void inspectedURLChanged(const String&) { }
protected:
virtual void setAttachedWindowHeight(unsigned) { }
};
InspectorFrontendClientDummy::InspectorFrontendClientDummy(InspectorController* controller, Page* page, PassOwnPtr<InspectorFrontendClientLocal::Settings> settings)
: InspectorFrontendClientLocal(controller, page, settings)
{
}
class InspectorFrontendChannelDummy : public InspectorFrontendChannel {
public:
explicit InspectorFrontendChannelDummy(Page*);
~InspectorFrontendChannelDummy() { }
bool sendMessageToFrontend(const String& message);
private:
Page* m_frontendPage;
};
InspectorFrontendChannelDummy::InspectorFrontendChannelDummy(Page* page)
: m_frontendPage(page)
{
}
bool InspectorFrontendChannelDummy::sendMessageToFrontend(const String& message)
{
return InspectorClient::doDispatchMessageOnFrontendPage(m_frontendPage, message);
}
void Internals::openDummyInspectorFrontend(const String& script)
{
Page* page = contextDocument()->frame()->page();
if (!page)
return;
Page::PageClients pc;
fillWithEmptyClients(pc);
m_frontendPage = adoptPtr(new Page(pc));
RefPtr<Frame> mainFrame = Frame::create(m_frontendPage.get(), 0, new EmptyFrameLoaderClient());
PassRefPtr<FrameView> frameView = FrameView::create(mainFrame.get());
mainFrame->setView(frameView);
mainFrame->init();
OwnPtr<InspectorFrontendClientDummy> frontendClient = adoptPtr(new InspectorFrontendClientDummy(page->inspectorController(), m_frontendPage.get(), adoptPtr(new InspectorFrontendClientLocal::Settings())));
page->inspectorController()->setInspectorFrontendClient(frontendClient.release());
InspectorFrontendChannelDummy* channel = new InspectorFrontendChannelDummy(m_frontendPage.get());
page->inspectorController()->connectFrontend(channel);
mainFrame->script()->evaluate(ScriptSourceCode(script));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment