add spec
This commit is contained in:
parent
df5bad3f89
commit
3a60ab386c
4 changed files with 168 additions and 11 deletions
|
@ -51,17 +51,26 @@ void Debugger::DispatchProtocolMessage(DevToolsAgentHost* agent_host,
|
||||||
std::string method;
|
std::string method;
|
||||||
if (!dict->GetString("method", &method))
|
if (!dict->GetString("method", &method))
|
||||||
return;
|
return;
|
||||||
base::DictionaryValue* params = nullptr;
|
base::DictionaryValue* params_value = nullptr;
|
||||||
dict->GetDictionary("params", ¶ms);
|
base::DictionaryValue params;
|
||||||
Emit("message", method, *params);
|
if (dict->GetDictionary("params", ¶ms_value))
|
||||||
|
params.Swap(params_value);
|
||||||
|
Emit("message", method, params);
|
||||||
} else {
|
} else {
|
||||||
auto send_command_callback = pending_requests_[id];
|
auto send_command_callback = pending_requests_[id];
|
||||||
pending_requests_.erase(id);
|
pending_requests_.erase(id);
|
||||||
if (send_command_callback.is_null())
|
if (send_command_callback.is_null())
|
||||||
return;
|
return;
|
||||||
base::DictionaryValue* result = nullptr;
|
base::DictionaryValue* error_body = nullptr;
|
||||||
dict->GetDictionary("result", &result);
|
base::DictionaryValue error;
|
||||||
send_command_callback.Run(*result);
|
if (dict->GetDictionary("error", &error_body))
|
||||||
|
error.Swap(error_body);
|
||||||
|
|
||||||
|
base::DictionaryValue* result_body = nullptr;
|
||||||
|
base::DictionaryValue result;
|
||||||
|
if (dict->GetDictionary("result", &result_body))
|
||||||
|
result.Swap(result_body);
|
||||||
|
send_command_callback.Run(error, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,6 +96,10 @@ void Debugger::Attach(mate::Arguments* args) {
|
||||||
agent_host_->AttachClient(this);
|
agent_host_->AttachClient(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Debugger::IsAttached() {
|
||||||
|
return agent_host_.get() ? agent_host_->IsAttached() : false;
|
||||||
|
}
|
||||||
|
|
||||||
void Debugger::Detach() {
|
void Debugger::Detach() {
|
||||||
if (!agent_host_.get())
|
if (!agent_host_.get())
|
||||||
return;
|
return;
|
||||||
|
@ -97,7 +110,7 @@ void Debugger::Detach() {
|
||||||
|
|
||||||
void Debugger::SendCommand(mate::Arguments* args) {
|
void Debugger::SendCommand(mate::Arguments* args) {
|
||||||
if (!agent_host_.get())
|
if (!agent_host_.get())
|
||||||
args->ThrowError("Debugger is not attached to a target");
|
return;
|
||||||
|
|
||||||
std::string method;
|
std::string method;
|
||||||
if (!args->GetNext(&method)) {
|
if (!args->GetNext(&method)) {
|
||||||
|
@ -134,6 +147,7 @@ void Debugger::BuildPrototype(v8::Isolate* isolate,
|
||||||
v8::Local<v8::ObjectTemplate> prototype) {
|
v8::Local<v8::ObjectTemplate> prototype) {
|
||||||
mate::ObjectTemplateBuilder(isolate, prototype)
|
mate::ObjectTemplateBuilder(isolate, prototype)
|
||||||
.SetMethod("attach", &Debugger::Attach)
|
.SetMethod("attach", &Debugger::Attach)
|
||||||
|
.SetMethod("isAttached", &Debugger::IsAttached)
|
||||||
.SetMethod("detach", &Debugger::Detach)
|
.SetMethod("detach", &Debugger::Detach)
|
||||||
.SetMethod("sendCommand", &Debugger::SendCommand);
|
.SetMethod("sendCommand", &Debugger::SendCommand);
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,8 @@ class Debugger: public mate::TrackableObject<Debugger>,
|
||||||
public content::DevToolsAgentHostClient {
|
public content::DevToolsAgentHostClient {
|
||||||
public:
|
public:
|
||||||
using SendCommandCallback =
|
using SendCommandCallback =
|
||||||
base::Callback<void(const base::DictionaryValue&)>;
|
base::Callback<void(const base::DictionaryValue&,
|
||||||
|
const base::DictionaryValue&)>;
|
||||||
|
|
||||||
static mate::Handle<Debugger> Create(
|
static mate::Handle<Debugger> Create(
|
||||||
v8::Isolate* isolate, content::WebContents* web_contents);
|
v8::Isolate* isolate, content::WebContents* web_contents);
|
||||||
|
@ -54,6 +55,7 @@ class Debugger: public mate::TrackableObject<Debugger>,
|
||||||
using PendingRequestMap = std::map<int, SendCommandCallback>;
|
using PendingRequestMap = std::map<int, SendCommandCallback>;
|
||||||
|
|
||||||
void Attach(mate::Arguments* args);
|
void Attach(mate::Arguments* args);
|
||||||
|
bool IsAttached();
|
||||||
void Detach();
|
void Detach();
|
||||||
void SendCommand(mate::Arguments* args);
|
void SendCommand(mate::Arguments* args);
|
||||||
|
|
||||||
|
|
|
@ -861,10 +861,14 @@ win.webContents.debugger.sendCommand("Network.enable");
|
||||||
|
|
||||||
#### `webContents.debugger.attach([protocolVersion])`
|
#### `webContents.debugger.attach([protocolVersion])`
|
||||||
|
|
||||||
* `protocolVersion` String - Requested debugging protocol version.
|
* `protocolVersion` String (optional) - Requested debugging protocol version.
|
||||||
|
|
||||||
Attaches the debugger to the `webContents`.
|
Attaches the debugger to the `webContents`.
|
||||||
|
|
||||||
|
#### `webContents.debugger.isAttached()`
|
||||||
|
|
||||||
|
Returns a boolean indicating whether a debugger is attached to the `webContents`.
|
||||||
|
|
||||||
#### `webContents.debugger.detach()`
|
#### `webContents.debugger.detach()`
|
||||||
|
|
||||||
Detaches the debugger from the `webContents`.
|
Detaches the debugger from the `webContents`.
|
||||||
|
@ -873,8 +877,9 @@ Detaches the debugger from the `webContents`.
|
||||||
|
|
||||||
* `method` String - Method name, should be one of the methods defined by the
|
* `method` String - Method name, should be one of the methods defined by the
|
||||||
remote debugging protocol.
|
remote debugging protocol.
|
||||||
* `commandParams` Object - JSON object with request parameters.
|
* `commandParams` Object (optional) - JSON object with request parameters.
|
||||||
* `callback` Function - Response
|
* `callback` Function (optional) - Response
|
||||||
|
* `error` Object - Error message indicating the failure of the command.
|
||||||
* `result` Object - Response defined by the 'returns' attribute of
|
* `result` Object - Response defined by the 'returns' attribute of
|
||||||
the command description in the remote debugging protocol.
|
the command description in the remote debugging protocol.
|
||||||
|
|
||||||
|
|
136
spec/api-debugger-spec.js
Normal file
136
spec/api-debugger-spec.js
Normal file
|
@ -0,0 +1,136 @@
|
||||||
|
var assert, path, remote, BrowserWindow;
|
||||||
|
|
||||||
|
assert = require('assert');
|
||||||
|
|
||||||
|
path = require('path');
|
||||||
|
|
||||||
|
remote = require('electron').remote;
|
||||||
|
|
||||||
|
BrowserWindow = remote.BrowserWindow;
|
||||||
|
|
||||||
|
describe('debugger module', function() {
|
||||||
|
var fixtures, w;
|
||||||
|
fixtures = path.resolve(__dirname, 'fixtures');
|
||||||
|
w = null;
|
||||||
|
beforeEach(function() {
|
||||||
|
if (w != null) {
|
||||||
|
w.destroy();
|
||||||
|
}
|
||||||
|
w = new BrowserWindow({
|
||||||
|
show: false,
|
||||||
|
width: 400,
|
||||||
|
height: 400
|
||||||
|
});
|
||||||
|
});
|
||||||
|
afterEach(function() {
|
||||||
|
if (w != null) {
|
||||||
|
w.destroy();
|
||||||
|
}
|
||||||
|
w = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('debugger.attach', function() {
|
||||||
|
it('fails when devtools is already open', function(done) {
|
||||||
|
w.webContents.on('did-finish-load', function() {
|
||||||
|
w.webContents.openDevTools();
|
||||||
|
try {
|
||||||
|
w.webContents.debugger.attach();
|
||||||
|
} catch(err) {
|
||||||
|
assert(w.webContents.debugger.isAttached());
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
w.webContents.loadURL('file://' + path.join(fixtures, 'pages', 'a.html'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fails when protocol version is not supported', function(done) {
|
||||||
|
try {
|
||||||
|
w.webContents.debugger.attach("2.0");
|
||||||
|
} catch(err) {
|
||||||
|
assert(!w.webContents.debugger.isAttached());
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('attaches when no protocol version is specified', function(done) {
|
||||||
|
try {
|
||||||
|
w.webContents.debugger.attach();
|
||||||
|
} catch(err) {
|
||||||
|
done('unexpected error : ' + err);
|
||||||
|
}
|
||||||
|
assert(w.webContents.debugger.isAttached());
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('debugger.detach', function() {
|
||||||
|
it('fires detach event', function(done) {
|
||||||
|
w.webContents.debugger.on('detach', function(e, reason) {
|
||||||
|
assert.equal(reason, 'target closed');
|
||||||
|
assert(!w.webContents.debugger.isAttached());
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
w.webContents.debugger.attach();
|
||||||
|
} catch(err) {
|
||||||
|
done('unexpected error : ' + err);
|
||||||
|
}
|
||||||
|
w.webContents.debugger.detach();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('debugger.sendCommand', function() {
|
||||||
|
it('retuns response', function(done) {
|
||||||
|
w.webContents.loadURL('about:blank');
|
||||||
|
try {
|
||||||
|
w.webContents.debugger.attach();
|
||||||
|
} catch(err) {
|
||||||
|
done('unexpected error : ' + err);
|
||||||
|
}
|
||||||
|
var callback = function(err, res) {
|
||||||
|
assert(!res.wasThrown);
|
||||||
|
assert.equal(res.result.value, 6);
|
||||||
|
w.webContents.debugger.detach();
|
||||||
|
done();
|
||||||
|
};
|
||||||
|
const params = {
|
||||||
|
"expression": "4+2",
|
||||||
|
};
|
||||||
|
w.webContents.debugger.sendCommand("Runtime.evaluate", params, callback);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fires message event', function(done) {
|
||||||
|
var url = 'file://' + path.join(fixtures, 'pages', 'a.html');
|
||||||
|
w.webContents.loadURL(url);
|
||||||
|
try {
|
||||||
|
w.webContents.debugger.attach();
|
||||||
|
} catch(err) {
|
||||||
|
done('unexpected error : ' + err);
|
||||||
|
}
|
||||||
|
w.webContents.debugger.on('message', function(e, method, params) {
|
||||||
|
if(method == "Console.messageAdded") {
|
||||||
|
assert.equal(params.message.type, 'log');
|
||||||
|
assert.equal(params.message.url, url);
|
||||||
|
assert.equal(params.message.text, 'a');
|
||||||
|
w.webContents.debugger.detach();
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
w.webContents.debugger.sendCommand("Console.enable");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns error message when command fails', function(done) {
|
||||||
|
w.webContents.loadURL('about:blank');
|
||||||
|
try {
|
||||||
|
w.webContents.debugger.attach();
|
||||||
|
} catch(err) {
|
||||||
|
done('unexpected error : ' + err);
|
||||||
|
}
|
||||||
|
w.webContents.debugger.sendCommand("Test", function(err) {
|
||||||
|
assert.equal(err.message, '\'Test\' wasn\'t found');
|
||||||
|
w.webContents.debugger.detach();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue