diff --git a/static/js/instance_status.js b/static/js/instance_status.js index b2cf27e9..0eea5e6e 100644 --- a/static/js/instance_status.js +++ b/static/js/instance_status.js @@ -6,8 +6,8 @@ var vm_status = Number(vm_summary.dataset.status); var status_poll = pollWhileVisible(5000, function () { return $.getJSON(vm_summary.dataset.statusUrl); }, function get_status(data) { - // an XML edit not saved yet stays: the reload waits until it is saved or undone - if (data['status'] != vm_status && !backgroundJobRunning && !(window.xmlEditUnsaved && window.xmlEditUnsaved())) { + // the page in use (a dialog open, a field in use, an XML edit not saved): the reload waits (poll.js) + if (data['status'] != vm_status && !backgroundJobRunning && !pageInUse()) { window.location.reload(); } }); diff --git a/webvirtcloud/tests/test_poll_js.py b/webvirtcloud/tests/test_poll_js.py index ca08356c..1f103569 100644 --- a/webvirtcloud/tests/test_poll_js.py +++ b/webvirtcloud/tests/test_poll_js.py @@ -43,27 +43,28 @@ class PollTestCase(unittest.TestCase): self.assertIn("pollWhileVisible(", source) @unittest.skipUnless(shutil.which("node"), "node is not installed") - def test_the_vm_page_reloads_only_when_the_state_changes(self): + def test_the_vm_page_reloads_only_when_the_state_changes_and_it_is_not_in_use(self): # "status" is a property of the browser's window: a page variable of # that name holds what the browser makes of it (a string, or nothing) script = (BASE / "static/js/instance_status.js").read_text() js = """ const vm = require("vm"); const results = {}; - for (const answer of [1, 5]) { + for (const [answer, inUse] of [[1, false], [5, false], [5, true]]) { let reloads = 0; const page = { pollWhileVisible: (interval, request, onData) => { onData({status: answer}); return {stop() {}}; }, + pageInUse: () => inUse, $: () => ({submit() {}}), document: {getElementById: () => ({dataset: {status: "1", statusUrl: "/instances/1/status/"}})}, }; page.window = {location: {reload: () => reloads++}}; Object.defineProperty(page, "status", {get: () => "", set: () => {}}); vm.runInNewContext(SCRIPT, vm.createContext(page)); - results[answer] = reloads; + results[answer + (inUse ? " in use" : "")] = reloads; } console.log(JSON.stringify(results)); """.replace("SCRIPT", json.dumps(script)) out = subprocess.run(["node", "-e", js], capture_output=True, text=True, timeout=30) self.assertEqual(out.returncode, 0, out.stderr) - self.assertEqual(json.loads(out.stdout), {"1": 0, "5": 1}) + self.assertEqual(json.loads(out.stdout), {"1": 0, "5": 1, "5 in use": 0})