diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-10 03:05:46 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-10 03:05:46 +0000 |
commit | 89adefdf3ebe499bee700670b6501ad3652adbe8 (patch) | |
tree | b1a8291fc3a00ec7dd33ba7273a9d28dfc5935ed /mojo/apps | |
parent | 2549d89cde93cc3d5d26a35d3da446b94e433a4c (diff) | |
download | chromium_src-89adefdf3ebe499bee700670b6501ad3652adbe8.zip chromium_src-89adefdf3ebe499bee700670b6501ad3652adbe8.tar.gz chromium_src-89adefdf3ebe499bee700670b6501ad3652adbe8.tar.bz2 |
Introduce a monotonic_clock module for Mojo.js.
Am having trouble coming up with a satisfactory way to test.
BUG=
Review URL: https://codereview.chromium.org/129633005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@244062 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/apps')
-rw-r--r-- | mojo/apps/js/bindings/monotonic_clock.cc | 42 | ||||
-rw-r--r-- | mojo/apps/js/bindings/monotonic_clock.h | 22 | ||||
-rw-r--r-- | mojo/apps/js/bindings/monotonic_clock_unittests.js | 20 | ||||
-rw-r--r-- | mojo/apps/js/main.js | 14 | ||||
-rw-r--r-- | mojo/apps/js/mojo_runner_delegate.cc | 2 | ||||
-rw-r--r-- | mojo/apps/js/test/run_js_tests.cc | 23 |
6 files changed, 112 insertions, 11 deletions
diff --git a/mojo/apps/js/bindings/monotonic_clock.cc b/mojo/apps/js/bindings/monotonic_clock.cc new file mode 100644 index 0000000..3ca87af --- /dev/null +++ b/mojo/apps/js/bindings/monotonic_clock.cc @@ -0,0 +1,42 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "mojo/apps/js/bindings/monotonic_clock.h" + +#include "gin/object_template_builder.h" +#include "gin/per_isolate_data.h" +#include "gin/public/wrapper_info.h" +#include "mojo/public/system/core_cpp.h" + +namespace mojo { +namespace apps { + +namespace { + +gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin }; + +double GetMonotonicSeconds() { + const double kMicrosecondsPerSecond = 1000000; + return static_cast<double>(mojo::GetTimeTicksNow()) / kMicrosecondsPerSecond; +} + +} // namespace + +const char MonotonicClock::kModuleName[] = "monotonic_clock"; + +v8::Local<v8::Value> MonotonicClock::GetModule(v8::Isolate* isolate) { + gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); + v8::Local<v8::ObjectTemplate> templ = + data->GetObjectTemplate(&g_wrapper_info); + if (templ.IsEmpty()) { + templ = gin::ObjectTemplateBuilder(isolate) + .SetMethod("seconds", GetMonotonicSeconds) + .Build(); + data->SetObjectTemplate(&g_wrapper_info, templ); + } + return templ->NewInstance(); +} + +} // namespace apps +} // namespace mojo diff --git a/mojo/apps/js/bindings/monotonic_clock.h b/mojo/apps/js/bindings/monotonic_clock.h new file mode 100644 index 0000000..6278821 --- /dev/null +++ b/mojo/apps/js/bindings/monotonic_clock.h @@ -0,0 +1,22 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MOJO_APPS_JS_BINDING_MONOTONIC_CLOCK_H_ +#define MOJO_APPS_JS_BINDING_MONOTONIC_CLOCK_H_ + +#include "v8/include/v8.h" + +namespace mojo { +namespace apps { + +class MonotonicClock { + public: + static const char kModuleName[]; + static v8::Local<v8::Value> GetModule(v8::Isolate* isolate); +}; + +} // namespace apps +} // namespace mojo + +#endif // MOJO_APPS_JS_BINDING_MONOTONIC_CLOCK_H_ diff --git a/mojo/apps/js/bindings/monotonic_clock_unittests.js b/mojo/apps/js/bindings/monotonic_clock_unittests.js new file mode 100644 index 0000000..a1a253e --- /dev/null +++ b/mojo/apps/js/bindings/monotonic_clock_unittests.js @@ -0,0 +1,20 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +define([ + "console", + "gin/test/expect", + "monotonic_clock", + "timer", + "mojo/apps/js/bindings/threading" +], function(console, expect, monotonicClock, timer, threading) { + var global = this; + var then = monotonicClock.seconds(); + var t = timer.createOneShot(100, function() { + var now = monotonicClock.seconds(); + expect(now).toBeGreaterThan(then); + global.result = "PASS"; + threading.quit(); + }); +}); diff --git a/mojo/apps/js/main.js b/mojo/apps/js/main.js index d5483f7..4f821a7 100644 --- a/mojo/apps/js/main.js +++ b/mojo/apps/js/main.js @@ -4,6 +4,7 @@ define([ 'console', + 'monotonic_clock', 'timer', 'mojo/apps/js/bindings/connector', 'mojo/apps/js/bindings/core', @@ -13,6 +14,7 @@ define([ 'mojom/gles2', 'mojom/shell', ], function(console, + monotonicClock, timer, connector, core, @@ -313,7 +315,7 @@ define([ function GLES2ClientImpl(remote) { this.remote_ = remote; - this.lastTime_ = Date.now(); + this.lastTime_ = monotonicClock.seconds(); this.angle_ = 45; } GLES2ClientImpl.prototype = @@ -354,11 +356,11 @@ define([ }; GLES2ClientImpl.prototype.handleTimer = function() { - var now = Date.now(); - var timeDelta = Date.now() - this.lastTime_; + var now = monotonicClock.seconds(); + var secondsDelta = now - this.lastTime_; this.lastTime_ = now; - this.angle_ += this.getRotationForTimeDelgate(timeDelta); + this.angle_ += this.getRotationForTimeDelta(secondsDelta); this.angle_ = this.angle_ % 360; var aspect = this.width_ / this.height_; @@ -377,8 +379,8 @@ define([ this.drawCube(); }; - GLES2ClientImpl.prototype.getRotationForTimeDelgate = function(timeDelta) { - return timeDelta * .04; + GLES2ClientImpl.prototype.getRotationForTimeDelta = function(secondsDelta) { + return secondsDelta * 40; }; GLES2ClientImpl.prototype.contextLost = function() { diff --git a/mojo/apps/js/mojo_runner_delegate.cc b/mojo/apps/js/mojo_runner_delegate.cc index ba9de40..c160f16 100644 --- a/mojo/apps/js/mojo_runner_delegate.cc +++ b/mojo/apps/js/mojo_runner_delegate.cc @@ -13,6 +13,7 @@ #include "gin/try_catch.h" #include "mojo/apps/js/bindings/core.h" #include "mojo/apps/js/bindings/gl/module.h" +#include "mojo/apps/js/bindings/monotonic_clock.h" #include "mojo/apps/js/bindings/support.h" #include "mojo/apps/js/bindings/threading.h" @@ -51,6 +52,7 @@ MojoRunnerDelegate::MojoRunnerDelegate() AddBuiltinModule(js::Core::kModuleName, js::Core::GetModule); AddBuiltinModule(js::Support::kModuleName, js::Support::GetModule); AddBuiltinModule(mojo::js::gl::kModuleName, mojo::js::gl::GetModule); + AddBuiltinModule(MonotonicClock::kModuleName, MonotonicClock::GetModule); AddBuiltinModule(Threading::kModuleName, Threading::GetModule); } diff --git a/mojo/apps/js/test/run_js_tests.cc b/mojo/apps/js/test/run_js_tests.cc index e794eb6..653a7e7 100644 --- a/mojo/apps/js/test/run_js_tests.cc +++ b/mojo/apps/js/test/run_js_tests.cc @@ -4,10 +4,14 @@ #include "base/file_util.h" #include "base/path_service.h" +#include "gin/modules/console.h" #include "gin/modules/module_registry.h" +#include "gin/modules/timer.h" #include "gin/test/file_runner.h" #include "gin/test/gtest.h" #include "mojo/apps/js/bindings/core.h" +#include "mojo/apps/js/bindings/monotonic_clock.h" +#include "mojo/apps/js/bindings/threading.h" #include "testing/gtest/include/gtest/gtest.h" namespace mojo { @@ -17,14 +21,19 @@ namespace { class TestRunnerDelegate : public gin::FileRunnerDelegate { public: TestRunnerDelegate() { + AddBuiltinModule(gin::Console::kModuleName, gin::Console::GetModule); AddBuiltinModule(Core::kModuleName, Core::GetModule); + AddBuiltinModule(gin::TimerModule::kName, gin::TimerModule::GetModule); + AddBuiltinModule(apps::MonotonicClock::kModuleName, + apps::MonotonicClock::GetModule); + AddBuiltinModule(apps::Threading::kModuleName, apps::Threading::GetModule); } private: DISALLOW_COPY_AND_ASSIGN(TestRunnerDelegate); }; -void RunTest(std::string test) { +void RunTest(std::string test, bool run_until_idle) { base::FilePath path; PathService::Get(base::DIR_SOURCE_ROOT, &path); path = path.AppendASCII("mojo") @@ -33,16 +42,16 @@ void RunTest(std::string test) { .AppendASCII("bindings") .AppendASCII(test); TestRunnerDelegate delegate; - gin::RunTestFromFile(path, &delegate); + gin::RunTestFromFile(path, &delegate, run_until_idle); } // TODO(abarth): Should we autogenerate these stubs from GYP? TEST(JSTest, core) { - RunTest("core_unittests.js"); + RunTest("core_unittests.js", true); } TEST(JSTest, codec) { - RunTest("codec_unittests.js"); + RunTest("codec_unittests.js", true); } TEST(JSTest, sample_test) { @@ -58,7 +67,11 @@ TEST(JSTest, sample_test) { } TEST(JSTest, connector) { - RunTest("connector_unittests.js"); + RunTest("connector_unittests.js", true); +} + +TEST(JSTest, monotonic_clock) { + RunTest("monotonic_clock_unittests.js", false); } } // namespace |