Skip to content

Commit

Permalink
Introduce a monotonic_clock module for Mojo.js.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
aa@chromium.org committed Jan 10, 2014
1 parent 2549d89 commit 89adefd
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 14 deletions.
9 changes: 7 additions & 2 deletions gin/test/file_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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"));
Expand Down
3 changes: 2 additions & 1 deletion gin/test/file_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
42 changes: 42 additions & 0 deletions mojo/apps/js/bindings/monotonic_clock.cc
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions mojo/apps/js/bindings/monotonic_clock.h
Original file line number Diff line number Diff line change
@@ -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_
20 changes: 20 additions & 0 deletions mojo/apps/js/bindings/monotonic_clock_unittests.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
14 changes: 8 additions & 6 deletions mojo/apps/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

define([
'console',
'monotonic_clock',
'timer',
'mojo/apps/js/bindings/connector',
'mojo/apps/js/bindings/core',
Expand All @@ -13,6 +14,7 @@ define([
'mojom/gles2',
'mojom/shell',
], function(console,
monotonicClock,
timer,
connector,
core,
Expand Down Expand Up @@ -313,7 +315,7 @@ define([

function GLES2ClientImpl(remote) {
this.remote_ = remote;
this.lastTime_ = Date.now();
this.lastTime_ = monotonicClock.seconds();
this.angle_ = 45;
}
GLES2ClientImpl.prototype =
Expand Down Expand Up @@ -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_;
Expand All @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions mojo/apps/js/mojo_runner_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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);
}

Expand Down
23 changes: 18 additions & 5 deletions mojo/apps/js/test/run_js_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand All @@ -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) {
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions mojo/mojo_apps.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 89adefd

Please sign in to comment.