Commit 89adefdf authored by aa@chromium.org's avatar aa@chromium.org

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
parent 2549d89c
......@@ -44,7 +44,8 @@ void FileRunnerDelegate::UnhandledException(Runner* runner,
FAIL() << try_catch.GetStackTrace();
}
void RunTestFromFile(const base::FilePath& path, FileRunnerDelegate* delegate) {
void RunTestFromFile(const base::FilePath& path, FileRunnerDelegate* delegate,
bool run_until_idle) {
ASSERT_TRUE(base::PathExists(path)) << path.LossyDisplayName();
std::string source;
ASSERT_TRUE(ReadFileToString(path, &source));
......@@ -58,7 +59,11 @@ void RunTestFromFile(const base::FilePath& path, FileRunnerDelegate* delegate) {
v8::V8::SetCaptureStackTraceForUncaughtExceptions(true);
runner.Run(source, path.AsUTF8Unsafe());
message_loop.RunUntilIdle();
if (run_until_idle) {
message_loop.RunUntilIdle();
} else {
message_loop.Run();
}
v8::Handle<v8::Value> result = runner.context()->Global()->Get(
StringToSymbol(runner.isolate(), "result"));
......
......@@ -29,7 +29,8 @@ class FileRunnerDelegate : public ModuleRunnerDelegate {
DISALLOW_COPY_AND_ASSIGN(FileRunnerDelegate);
};
void RunTestFromFile(const base::FilePath& path, FileRunnerDelegate* delegate);
void RunTestFromFile(const base::FilePath& path, FileRunnerDelegate* delegate,
bool run_until_idle = true);
} // namespace gin
......
// 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
// 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_
// 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();
});
});
......@@ -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() {
......
......@@ -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);
}
......
......@@ -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
......
......@@ -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',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment