diff options
author | mbelshe@google.com <mbelshe@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-27 02:37:29 +0000 |
---|---|---|
committer | mbelshe@google.com <mbelshe@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-27 02:37:29 +0000 |
commit | c5a14d3864b3d648ebde099cd959cbba9203627f (patch) | |
tree | 602a4d54017c3f3a7559a58ede838f126466002a | |
parent | 4a4f066c6eea2638f5ff24bfee54b9e68e7ba174 (diff) | |
download | chromium_src-c5a14d3864b3d648ebde099cd959cbba9203627f.zip chromium_src-c5a14d3864b3d648ebde099cd959cbba9203627f.tar.gz chromium_src-c5a14d3864b3d648ebde099cd959cbba9203627f.tar.bz2 |
Changes to the interval timer:
- move into the window.chromium namespace.
- hide the native HiResTime() function.
- fixup the stop() mechanics.
- Added a layout test.
Review URL: http://codereview.chromium.org/28201
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10572 0039d316-1c4b-4281-b951-d872f2087c98
3 files changed, 86 insertions, 5 deletions
diff --git a/webkit/data/layout_tests/chrome/fast/dom/extensions/interval-expected.txt b/webkit/data/layout_tests/chrome/fast/dom/extensions/interval-expected.txt new file mode 100644 index 0000000..f69fc7f --- /dev/null +++ b/webkit/data/layout_tests/chrome/fast/dom/extensions/interval-expected.txt @@ -0,0 +1,9 @@ +This tests that the chromium.Interval functionality works correctly. + +PASS - initial zero +PASS - start +PASS - restart +PASS - initial stop +PASS - stop +PASS - stopped +PASS - re-stopped diff --git a/webkit/data/layout_tests/chrome/fast/dom/extensions/interval.html b/webkit/data/layout_tests/chrome/fast/dom/extensions/interval.html new file mode 100644 index 0000000..1ff02bc --- /dev/null +++ b/webkit/data/layout_tests/chrome/fast/dom/extensions/interval.html @@ -0,0 +1,67 @@ +<html> +<body onload="test()"> +<div id="result"> +<p> +This tests that the chromium.Interval functionality works correctly. +</p> +</div> +<script> +if (window.layoutTestController) { + layoutTestController.dumpAsText(); +} + +var resultDiv = document.getElementById("result"); + +function check(name, passed) { + if (passed) { + resultDiv.innerHTML += "PASS - " + name + "<br>"; + } else { + resultDiv.innerHTML += "FAIL - " + name + "<br>"; + } +} + +// Spin loop for a short time +function pause(millisecs) { + var start = new Date(); + while ((new Date() - start) < millisecs); +} + + +function test() { + var interval = new chromium.Interval(); + + // Verify initialization. + check("initial zero", interval.microseconds() == 0); + + // Verify that starting the timer works. + interval.start(); + pause(500); + check("start", interval.microseconds() >= 500000); + + // Verify that restarting the interval should reset the beginning time + interval.start(); + pause(1); + check("restart", interval.microseconds() > 0 && interval.microseconds() < 500000); + + // Verify that calling stop() before start() has no effect. + var interval = new chromium.Interval(); + interval.stop(); + check("initial stop", interval.microseconds() == 0); + + // Verify a start/stop sequence. + interval.start(); + pause(50); + interval.stop(); + var ms = interval.microseconds(); + check("stop", ms > 0 && ms < 1000000); + + // Verify that the timer is really stopped. + check("stopped", ms == interval.microseconds()); + + // Verify that re-stopping the timer works. + interval.stop(); + check("re-stopped", interval.microseconds() > ms); +} +</script> +</body> +</html> diff --git a/webkit/port/bindings/v8/extensions/Interval.cpp b/webkit/port/bindings/v8/extensions/Interval.cpp index 6da7f6d..4a5c13d 100644 --- a/webkit/port/bindings/v8/extensions/Interval.cpp +++ b/webkit/port/bindings/v8/extensions/Interval.cpp @@ -14,11 +14,15 @@ class IntervalExtensionWrapper : public v8::Extension { public: IntervalExtensionWrapper() : v8::Extension(kIntervalExtensionName, - "native function HiResTime();" - "function Interval() {" + "var chromium;" + "if (!chromium)" + " chromium = {};" + "chromium.Interval = function() {" " var start_ = 0;" " var stop_ = 0;" + " native function HiResTime();" " this.start = function() {" + " stop_ = 0;" " start_ = HiResTime();" " };" " this.stop = function() {" @@ -27,9 +31,10 @@ public: " stop_ = 0;" " };" " this.microseconds = function() {" - " if (stop_ == 0)" - " stop();" - " return Math.ceil((stop_ - start_) * 1000000);" + " var stop = stop_;" + " if (stop == 0 && start_ != 0)" + " stop = HiResTime();" + " return Math.ceil((stop - start_) * 1000000);" " };" "}") {}; |