From 1d43d57f196e358bbc6225db7b584c0e9ec25a35 Mon Sep 17 00:00:00 2001 From: Philipp von Weitershausen Date: Mon, 18 Apr 2011 13:31:20 -0700 Subject: [PATCH] Bug 648367 - Add run_next_test to head.js. r=ted --- netwerk/test/unit/test_NetUtil.js | 25 +-------- netwerk/test/unit/test_data_protocol.js | 4 -- netwerk/test/unit/test_file_protocol.js | 10 +--- netwerk/test/unit/test_reopen.js | 10 +--- .../test/unit/test_connection_executeAsync.js | 29 +---------- .../test/unit/test_sqlite_secure_delete.js | 17 +------ storage/test/unit/test_storage_connection.js | 31 +---------- testing/xpcshell/head.js | 51 +++++++++++++++++++ .../tests/expiration/test_analyze_runs.js | 4 +- .../components/places/tests/head_common.js | 34 ------------- .../tests/migration/test_v11_from_v10.js | 4 +- .../test_v11_from_v10_migrated_from_v11.js | 4 +- .../places/tests/queries/test_tags.js | 7 ++- .../unit/test_486978_sort_by_date_queries.js | 8 ++- .../test_PlacesUtils_asyncGetBookmarkIds.js | 4 +- .../tests/unit/test_async_history_api.js | 4 +- .../tests/unit/test_sql_guid_functions.js | 4 +- 17 files changed, 79 insertions(+), 171 deletions(-) diff --git a/netwerk/test/unit/test_NetUtil.js b/netwerk/test/unit/test_NetUtil.js index de25ad9e7988..05a37da2269a 100644 --- a/netwerk/test/unit/test_NetUtil.js +++ b/netwerk/test/unit/test_NetUtil.js @@ -539,7 +539,7 @@ function test_readInputStreamToString_too_many_bytes() //////////////////////////////////////////////////////////////////////////////// //// Test Runner -let tests = [ +[ test_async_write_file, test_async_write_file_deferred, test_async_write_file_safe, @@ -565,32 +565,11 @@ let tests = [ test_readInputStreamToString_no_bytes_arg, test_readInputStreamToString_blocking_stream, test_readInputStreamToString_too_many_bytes, -]; +].forEach(add_test); let index = 0; -function run_next_test() -{ - if (index < tests.length) { - do_test_pending(); - - // Asynchronous test exceptions do not kill the test... - do_execute_soon(function() { - try { - print("Running the next test: " + tests[index].name); - tests[index++](); - } - catch (e) { - do_throw(e); - } - }); - } - - do_test_finished(); -} - function run_test() { - do_test_pending(); run_next_test(); } diff --git a/netwerk/test/unit/test_data_protocol.js b/netwerk/test/unit/test_data_protocol.js index 334fe8b57d68..30cfd528015d 100644 --- a/netwerk/test/unit/test_data_protocol.js +++ b/netwerk/test/unit/test_data_protocol.js @@ -19,10 +19,6 @@ var urls = [ ["DATA:TEXT/PLAIN;BASE64,Zm9 vI%20GJ%0Dhc%0Ag==", "text/plain", "foo bar"] ]; -function run_next_test() { - test_array[test_index++](); -} - function run_test() { dump("*** run_test\n"); diff --git a/netwerk/test/unit/test_file_protocol.js b/netwerk/test/unit/test_file_protocol.js index 208d85ca3da3..f454e8784f6d 100644 --- a/netwerk/test/unit/test_file_protocol.js +++ b/netwerk/test/unit/test_file_protocol.js @@ -7,18 +7,13 @@ const PR_RDONLY = 0x1; // see prio.h const special_type = "application/x-our-special-type"; -var test_index = 0; -var test_array = [ +[ test_read_file, test_read_dir_1, test_read_dir_2, test_upload_file, do_test_finished -]; - -function run_next_test() { - test_array[test_index++](); -} +].forEach(add_test); function getFile(key) { var dirSvc = Components.classes["@mozilla.org/file/directory_service;1"] @@ -228,6 +223,5 @@ function test_upload_file() { } function run_test() { - do_test_pending(); run_next_test(); } diff --git a/netwerk/test/unit/test_reopen.js b/netwerk/test/unit/test_reopen.js index 353018f81e89..ef2c06ffa75b 100644 --- a/netwerk/test/unit/test_reopen.js +++ b/netwerk/test/unit/test_reopen.js @@ -9,15 +9,14 @@ const NS_ERROR_ALREADY_OPENED = 0x804b0049; var chan = null; var httpserv = null; -var test_index = 0; -var test_array = [ +[ test_data_channel, test_http_channel, test_file_channel, // Commented by default as it relies on external ressources //test_ftp_channel, end -]; +].forEach(add_test); // Utility functions @@ -87,10 +86,6 @@ function after_channel_closed() { run_next_test(); } -function run_next_test() { - test_array[test_index++](); -} - function test_channel(createChanClosure) { // First, synchronous reopening test chan = createChanClosure(); @@ -140,6 +135,5 @@ function run_test() { httpserv = new nsHttpServer(); httpserv.start(4444); - do_test_pending(); run_next_test(); } diff --git a/storage/test/unit/test_connection_executeAsync.js b/storage/test/unit/test_connection_executeAsync.js index 0778efceb954..d920fb1632b5 100644 --- a/storage/test/unit/test_connection_executeAsync.js +++ b/storage/test/unit/test_connection_executeAsync.js @@ -307,7 +307,6 @@ function test_double_asyncClose_throws() //////////////////////////////////////////////////////////////////////////////// //// Test Runner -let tests = [ test_create_and_add, test_transaction_created, @@ -315,36 +314,10 @@ let tests = test_asyncClose_does_not_complete_before_statements, test_asyncClose_does_not_throw_no_callback, test_double_asyncClose_throws, -]; -let index = 0; - -function run_next_test() -{ - function _run_next_test() { - if (index < tests.length) { - do_test_pending(); - print("Running the next test: " + tests[index].name); - - // Asynchronous tests means that exceptions don't kill the test. - try { - tests[index++](); - } - catch (e) { - do_throw(e); - } - } - - do_test_finished(); - } - - // For saner stacks, we execute this code RSN. - do_execute_soon(_run_next_test); -} +].forEach(add_test); function run_test() { cleanup(); - - do_test_pending(); run_next_test(); } diff --git a/storage/test/unit/test_sqlite_secure_delete.js b/storage/test/unit/test_sqlite_secure_delete.js index 8a12d3f06fbb..66ddf000a545 100644 --- a/storage/test/unit/test_sqlite_secure_delete.js +++ b/storage/test/unit/test_sqlite_secure_delete.js @@ -112,27 +112,12 @@ function test_delete_removes_data() //////////////////////////////////////////////////////////////////////////////// //// Test Runner -var tests = [ test_delete_removes_data, -]; -let index = 0; - -function run_next_test() -{ - if (index < tests.length) { - do_test_pending(); - print("Running the next test: " + tests[index].name); - tests[index++](); - } - - do_test_finished(); -} + ].forEach(add_test); function run_test() { cleanup(); - - do_test_pending(); run_next_test(); } diff --git a/storage/test/unit/test_storage_connection.js b/storage/test/unit/test_storage_connection.js index eebd4706b6e5..0027148d3758 100644 --- a/storage/test/unit/test_storage_connection.js +++ b/storage/test/unit/test_storage_connection.js @@ -497,7 +497,7 @@ function test_getInterface() //////////////////////////////////////////////////////////////////////////////// //// Test Runner -var tests = [ +[ test_connectionReady_open, test_connectionReady_closed, test_databaseFile, @@ -526,36 +526,9 @@ var tests = [ test_clone_copies_functions, test_clone_copies_overridden_functions, test_getInterface, -]; -let index = 0; - -function run_next_test() -{ - function _run_next_test() { - if (index < tests.length) { - do_test_pending(); - print("Running the next test: " + tests[index].name); - - // Asynchronous tests means that exceptions don't kill the test. - try { - tests[index++](); - } - catch (e) { - do_throw(e); - } - } - - do_test_finished(); - } - - // For saner stacks, we execute this code RSN. - do_execute_soon(_run_next_test); -} +].forEach(add_test); function run_test() { - cleanup(); - - do_test_pending(); run_next_test(); } diff --git a/testing/xpcshell/head.js b/testing/xpcshell/head.js index 3a9c9c403c28..a025db555597 100644 --- a/testing/xpcshell/head.js +++ b/testing/xpcshell/head.js @@ -829,3 +829,54 @@ function run_test_in_child(testFile, optionalCallback) callback); } + +/** + * Add a test function to the list of tests that are to be run asynchronously. + * + * Each test function must call run_next_test() when it's done. Test files + * should call run_next_test() in their run_test function to execute all + * async tests. + * + * @return the test function that was passed in. + */ +let gTests = []; +function add_test(func) { + gTests.push(func); + return func; +} + +/** + * Runs the next test function from the list of async tests. + */ +let gRunningTest = null; +let gTestIndex = 0; // The index of the currently running test. +function run_next_test() +{ + function _run_next_test() + { + if (gTestIndex < gTests.length) { + do_test_pending(); + gRunningTest = gTests[gTestIndex++]; + print("TEST-INFO | " + _TEST_FILE + " | Starting " + + gRunningTest.name); + // Exceptions do not kill asynchronous tests, so they'll time out. + try { + gRunningTest(); + } + catch (e) { + do_throw(e); + } + } + } + + // For sane stacks during failures, we execute this code soon, but not now. + // We do this now, before we call do_test_finished(), to ensure the pending + // counter (_tests_pending) never reaches 0 while we still have tests to run + // (do_execute_soon bumps that counter). + do_execute_soon(_run_next_test); + + if (gRunningTest !== null) { + // Close the previous test do_test_pending call. + do_test_finished(); + } +} diff --git a/toolkit/components/places/tests/expiration/test_analyze_runs.js b/toolkit/components/places/tests/expiration/test_analyze_runs.js index b1948273bfd0..e09c35df62c9 100644 --- a/toolkit/components/places/tests/expiration/test_analyze_runs.js +++ b/toolkit/components/places/tests/expiration/test_analyze_runs.js @@ -135,12 +135,12 @@ function test_shutdown() //////////////////////////////////////////////////////////////////////////////// //// Test Harness -let gTests = [ +[ test_timed, test_debug, test_clear_history, test_shutdown, -]; +].forEach(add_test); function run_test() { diff --git a/toolkit/components/places/tests/head_common.js b/toolkit/components/places/tests/head_common.js index 9f4e78b81913..f20ec05daa3a 100644 --- a/toolkit/components/places/tests/head_common.js +++ b/toolkit/components/places/tests/head_common.js @@ -647,40 +647,6 @@ function do_log_info(aMessage) print("TEST-INFO | " + _TEST_FILE + " | " + aMessage); } -/** - * Runs the next test in the gTests array. gTests should be a array defined in - * each test file. - */ -let gRunningTest = null; -let gTestIndex = 0; // The index of the currently running test. -function run_next_test() -{ - function _run_next_test() - { - if (gTestIndex < gTests.length) { - do_test_pending(); - gRunningTest = gTests[gTestIndex++]; - print("TEST-INFO | " + _TEST_FILE + " | Starting " + - gRunningTest.name); - // Exceptions do not kill asynchronous tests, so they'll time out. - try { - gRunningTest(); - } - catch (e) { - do_throw(e); - } - } - } - - // For sane stacks during failures, we execute this code soon, but not now. - do_execute_soon(_run_next_test); - - if (gRunningTest !== null) { - // Close the previous test do_test_pending call. - do_test_finished(); - } -} - /** * Compares 2 arrays returning whether they contains the same elements. * diff --git a/toolkit/components/places/tests/migration/test_v11_from_v10.js b/toolkit/components/places/tests/migration/test_v11_from_v10.js index 34cc33ed5ffb..a27e03e9eca3 100644 --- a/toolkit/components/places/tests/migration/test_v11_from_v10.js +++ b/toolkit/components/places/tests/migration/test_v11_from_v10.js @@ -305,7 +305,7 @@ function test_final_state() //////////////////////////////////////////////////////////////////////////////// //// Test Runner -let gTests = [ +[ test_initial_state, test_moz_bookmarks_guid_exists, test_bookmark_guids_non_null, @@ -316,7 +316,7 @@ let gTests = [ test_place_guid_annotation_imported, test_place_guid_annotation_removed, test_final_state, -]; +].forEach(add_test); function run_test() { diff --git a/toolkit/components/places/tests/migration/test_v11_from_v10_migrated_from_v11.js b/toolkit/components/places/tests/migration/test_v11_from_v10_migrated_from_v11.js index 96df31cc70f8..c9b281f34c50 100644 --- a/toolkit/components/places/tests/migration/test_v11_from_v10_migrated_from_v11.js +++ b/toolkit/components/places/tests/migration/test_v11_from_v10_migrated_from_v11.js @@ -118,12 +118,12 @@ function test_final_state() //////////////////////////////////////////////////////////////////////////////// //// Test Runner -let gTests = [ +[ test_initial_state, test_bookmark_guids_non_null, test_place_guids_non_null, test_final_state, -]; +].forEach(add_test); function run_test() { diff --git a/toolkit/components/places/tests/queries/test_tags.js b/toolkit/components/places/tests/queries/test_tags.js index 1e93944e6a32..8ba3f4ba0962 100644 --- a/toolkit/components/places/tests/queries/test_tags.js +++ b/toolkit/components/places/tests/queries/test_tags.js @@ -41,9 +41,8 @@ * Tests bookmark and history queries with tags. See bug 399799. */ -// Add your tests here. Each is an object with a summary string |desc| and a -// method run() that's called to run the test. -var gTests = [ +// Add your tests here. Each is a function that's called to run the test. +[ function tags_getter_setter() { @@ -590,7 +589,7 @@ var gTests = [ cleanDatabase(run_next_test); }, -]; +].forEach(add_test); // The tag keys in query URIs, i.e., "place:tag=foo&!tags=1" // --- ----- diff --git a/toolkit/components/places/tests/unit/test_486978_sort_by_date_queries.js b/toolkit/components/places/tests/unit/test_486978_sort_by_date_queries.js index d38a65d59729..cf9c008d92f4 100644 --- a/toolkit/components/places/tests/unit/test_486978_sort_by_date_queries.js +++ b/toolkit/components/places/tests/unit/test_486978_sort_by_date_queries.js @@ -58,12 +58,10 @@ function add_visit(aURI, aTime) { return visitId; } -var gTests = []; - /** * Tests that sorting date query by none will sort by title asc. */ -gTests.push(function() { +add_test(function() { var options = hs.getNewQueryOptions(); options.resultType = options.RESULTS_AS_DATE_QUERY; // This should sort by title asc. @@ -89,7 +87,7 @@ gTests.push(function() { /** * Tests that sorting date query by date will sort accordingly. */ -gTests.push(function() { +add_test(function() { var options = hs.getNewQueryOptions(); options.resultType = options.RESULTS_AS_DATE_QUERY; // This should sort by title asc. @@ -115,7 +113,7 @@ gTests.push(function() { /** * Tests that sorting date site query by date will still sort by title asc. */ -gTests.push(function() { +add_test(function() { var options = hs.getNewQueryOptions(); options.resultType = options.RESULTS_AS_DATE_SITE_QUERY; // This should sort by title asc. diff --git a/toolkit/components/places/tests/unit/test_PlacesUtils_asyncGetBookmarkIds.js b/toolkit/components/places/tests/unit/test_PlacesUtils_asyncGetBookmarkIds.js index 026eb7ed3a9b..f0b8dda44e30 100644 --- a/toolkit/components/places/tests/unit/test_PlacesUtils_asyncGetBookmarkIds.js +++ b/toolkit/components/places/tests/unit/test_PlacesUtils_asyncGetBookmarkIds.js @@ -7,7 +7,7 @@ const TEST_URL = "http://www.example.com/"; -let gTests = [ +[ function test_no_bookmark() { PlacesUtils.asyncGetBookmarkIds(TEST_URL, function (aItemIds, aURI) { @@ -79,7 +79,7 @@ let gTests = [ }, this); }, -]; +].forEach(add_test); function run_test() { run_next_test(); diff --git a/toolkit/components/places/tests/unit/test_async_history_api.js b/toolkit/components/places/tests/unit/test_async_history_api.js index 2575268f886d..796fbb86b5a8 100644 --- a/toolkit/components/places/tests/unit/test_async_history_api.js +++ b/toolkit/components/places/tests/unit/test_async_history_api.js @@ -1252,7 +1252,7 @@ function test_referrer_sessionId_persists() //////////////////////////////////////////////////////////////////////////////// //// Test Runner -let gTests = [ +[ test_interface_exists, test_invalid_uri_throws, test_invalid_places_throws, @@ -1283,7 +1283,7 @@ let gTests = [ test_title_change_notifies, test_visit_notifies, test_referrer_sessionId_persists, -]; +].forEach(add_test); function run_test() { diff --git a/toolkit/components/places/tests/unit/test_sql_guid_functions.js b/toolkit/components/places/tests/unit/test_sql_guid_functions.js index ad1cca327896..ff8c971a84dc 100644 --- a/toolkit/components/places/tests/unit/test_sql_guid_functions.js +++ b/toolkit/components/places/tests/unit/test_sql_guid_functions.js @@ -98,10 +98,10 @@ function test_guid_on_background() //////////////////////////////////////////////////////////////////////////////// //// Test Runner -let gTests = [ +[ test_guid_invariants, test_guid_on_background, -]; +].forEach(add_test); function run_test() {