Skip to content

Commit

Permalink
Merge pull request web-platform-tests#132 from w3c/jgraham/message_ev…
Browse files Browse the repository at this point in the history
…ents

Add ability to subscribe to only certain events via postMessage.
  • Loading branch information
jgraham committed May 30, 2015
2 parents 6a4af94 + 2ae2785 commit d0145b8
Showing 1 changed file with 84 additions and 16 deletions.
100 changes: 84 additions & 16 deletions testharness.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ policies and contribution forms [3].
"normal":10000,
"long":60000
},
test_timeout:null
test_timeout:null,
message_events: ["start", "test_state", "result", "completion"]
};

var xhtml_ns = "http://www.w3.org/1999/xhtml";
Expand Down Expand Up @@ -64,6 +65,40 @@ policies and contribution forms [3].
this.output_handler = null;
this.all_loaded = false;
var this_obj = this;
this.message_events = [];

this.message_functions = {
start: [add_start_callback, remove_start_callback,
function (properties) {
this_obj._dispatch("start_callback", [properties],
{type: "start", properties: properties});
}],

test_state: [add_test_state_callback, remove_test_state_callback,
function(test) {
this_obj._dispatch("test_state_callback", [test],
{type: "test_state",
test: test.structured_clone()});
}],
result: [add_result_callback, remove_result_callback,
function (test) {
this_obj.output_handler.show_status();
this_obj._dispatch("result_callback", [test],
{type: "result",
test: test.structured_clone()});
}],
completion: [add_completion_callback, remove_completion_callback,
function (tests, harness_status) {
var cloned_tests = map(tests, function(test) {
return test.structured_clone();
});
this_obj._dispatch("completion_callback", [tests, harness_status],
{type: "complete",
tests: cloned_tests,
status: harness_status.structured_clone()});
}]
}

on_event(window, 'load', function() {
this_obj.all_loaded = true;
});
Expand Down Expand Up @@ -150,30 +185,39 @@ policies and contribution forms [3].
this.output_handler = output;

var this_obj = this;

add_start_callback(function (properties) {
this_obj.output_handler.init(properties);
this_obj._dispatch("start_callback", [properties],
{ type: "start", properties: properties });
});

add_test_state_callback(function(test) {
this_obj.output_handler.show_status();
this_obj._dispatch("test_state_callback", [test],
{ type: "test_state", test: test.structured_clone() });
});

add_result_callback(function (test) {
this_obj.output_handler.show_status();
this_obj._dispatch("result_callback", [test],
{ type: "result", test: test.structured_clone() });
});

add_completion_callback(function (tests, harness_status) {
this_obj.output_handler.show_results(tests, harness_status);
var cloned_tests = map(tests, function(test) { return test.structured_clone(); });
this_obj._dispatch("completion_callback", [tests, harness_status],
{ type: "complete", tests: cloned_tests,
status: harness_status.structured_clone() });
});
this.setup_messages(settings.message_events);
};

WindowTestEnvironment.prototype.setup_messages = function(new_events) {
var this_obj = this;
forEach(settings.message_events, function(x) {
var current_dispatch = this_obj.message_events.indexOf(x) !== -1;
var new_dispatch = new_events.indexOf(x) !== -1;
if (!current_dispatch && new_dispatch) {
this_obj.message_functions[x][0](this_obj.message_functions[x][2]);
} else if (current_dispatch && !new_dispatch) {
this_obj.message_functions[x][1](this_obj.message_functions[x][2]);
}
});
this.message_events = new_events;
}

WindowTestEnvironment.prototype.next_default_test_name = function() {
//Don't use document.title to work around an Opera bug in XHTML documents
var title = document.getElementsByTagName("title")[0];
Expand All @@ -185,6 +229,9 @@ policies and contribution forms [3].

WindowTestEnvironment.prototype.on_new_harness_properties = function(properties) {
this.output_handler.setup(properties);
if (properties.hasOwnProperty("message_events")) {
this.setup_messages(properties.message_events);
}
};

WindowTestEnvironment.prototype.add_on_loaded_callback = function(callback) {
Expand Down Expand Up @@ -1858,21 +1905,42 @@ policies and contribution forms [3].
tests.test_state_callbacks.push(callback);
}

function add_result_callback(callback)
{
function add_result_callback(callback) {
tests.test_done_callbacks.push(callback);
}

function add_completion_callback(callback)
{
tests.all_done_callbacks.push(callback);
function add_completion_callback(callback) {
tests.all_done_callbacks.push(callback);
}

expose(add_start_callback, 'add_start_callback');
expose(add_test_state_callback, 'add_test_state_callback');
expose(add_result_callback, 'add_result_callback');
expose(add_completion_callback, 'add_completion_callback');

function remove(array, item) {
var index = array.indexOf(item);
if (index > -1) {
array.splice(index, 1);
}
}

function remove_start_callback(callback) {
remove(tests.start_callbacks, callback);
}

function remove_test_state_callback(callback) {
remove(tests.test_state_callbacks, callback);
}

function remove_result_callback(callback) {
remove(tests.test_done_callbacks, callback);
}

function remove_completion_callback(callback) {
remove(tests.all_done_callbacks, callback);
}

/*
* Output listener
*/
Expand Down

0 comments on commit d0145b8

Please sign in to comment.