summaryrefslogtreecommitdiffstats
path: root/mojo
diff options
context:
space:
mode:
Diffstat (limited to 'mojo')
-rw-r--r--mojo/apps/js/bindings/monotonic_clock.cc42
-rw-r--r--mojo/apps/js/bindings/monotonic_clock.h22
-rw-r--r--mojo/apps/js/bindings/monotonic_clock_unittests.js20
-rw-r--r--mojo/apps/js/main.js14
-rw-r--r--mojo/apps/js/mojo_runner_delegate.cc2
-rw-r--r--mojo/apps/js/test/run_js_tests.cc23
-rw-r--r--mojo/mojo_apps.gypi2
7 files changed, 114 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
diff --git a/mojo/mojo_apps.gypi b/mojo/mojo_apps.gypi
index 6f8aa4e..f53960c 100644
--- a/mojo/mojo_apps.gypi
+++ b/mojo/mojo_apps.gypi
@@ -37,6 +37,8 @@
'apps/js/bindings/gl/module.h',
'apps/js/bindings/handle.cc',
'apps/js/bindings/handle.h',
+ 'apps/js/bindings/monotonic_clock.cc',
+ 'apps/js/bindings/monotonic_clock.h',
'apps/js/bindings/support.cc',
'apps/js/bindings/support.h',
'apps/js/bindings/waiting_callback.cc',