From bdee9721ecdf8a8553eb40584c059d5fcc32bf7c Mon Sep 17 00:00:00 2001 From: Zev Benjamin Date: Thu, 6 Mar 2014 18:44:15 -0500 Subject: [PATCH] tests: Delay loading old messages and starting get_updates until phantomjs thinks the page has loaded This doesn't affect code when not in testing. It shaves 7 seconds off of casper test time on my machine. (imported from commit 7e27fa781bcf16f36d9c8f058427ba57c41068bd) --- static/js/message_store.js | 2 +- static/js/server_events.js | 2 +- static/js/util.js | 8 ++++++++ zerver/tests/frontend/common.js | 7 +++++++ zerver/tests/frontend/node/server_events.js | 2 +- 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/static/js/message_store.js b/static/js/message_store.js index 62dbd8f3da..e60b168f87 100644 --- a/static/js/message_store.js +++ b/static/js/message_store.js @@ -493,7 +493,7 @@ exports.clear = function clear() { this.stored_messages = {}; }; -$(function () { +util.execute_early(function () { // get the initial message list function load_more(messages) { diff --git a/static/js/server_events.js b/static/js/server_events.js index e1d0fb2a13..db98f20403 100644 --- a/static/js/server_events.js +++ b/static/js/server_events.js @@ -336,7 +336,7 @@ setInterval(function () { watchdog_time = new_time; }, 5000); -$(function () { +util.execute_early(function () { $(document).on('unsuspend', function () { // Immediately poll for new events on unsuspend blueslip.log("Restarting get_events due to unsuspend"); diff --git a/static/js/util.js b/static/js/util.js index 8867d557a9..ed3578fc5c 100644 --- a/static/js/util.js +++ b/static/js/util.js @@ -345,6 +345,14 @@ if (typeof $ !== 'undefined') { }; } +exports.execute_early = function (func) { + if (page_params.test_suite) { + $(document).one('phantom_page_loaded', func); + } else { + $(func); + } +}; + return exports; }()); if (typeof module !== 'undefined') { diff --git a/zerver/tests/frontend/common.js b/zerver/tests/frontend/common.js index e417dbdcc3..f0d14eb62b 100644 --- a/zerver/tests/frontend/common.js +++ b/zerver/tests/frontend/common.js @@ -62,6 +62,13 @@ exports.initialize_casper = function (viewport) { } }); + casper.on('load.finished', function () { + casper.evaluateOrDie(function () { + $(document).trigger($.Event('phantom_page_loaded')); + return true; + }); + }); + casper.evaluate(function () { window.localStorage.clear(); }); diff --git a/zerver/tests/frontend/node/server_events.js b/zerver/tests/frontend/node/server_events.js index cf204c8c08..59eea69fb3 100644 --- a/zerver/tests/frontend/node/server_events.js +++ b/zerver/tests/frontend/node/server_events.js @@ -28,13 +28,13 @@ set_global('channel', {}); set_global('home_msg_list', { selected_id: function () {return 1;} }); +set_global('page_params', {test_suite: false}); var server_events = require('js/server_events.js'); var setup = function (results) { server_events.home_view_loaded(); - set_global('page_params', {}); global.blueslip.error = function (msg, more_info, stack) { results.msg = msg; results.more_info = more_info;