1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "gin/modules/timer.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h"
9 #include "gin/handle.h"
10 #include "gin/object_template_builder.h"
11 #include "gin/public/isolate_holder.h"
12 #include "gin/runner.h"
13 #include "gin/test/v8_test.h"
14 #include "gin/try_catch.h"
15 #include "gin/wrappable.h"
16 #include "v8/include/v8.h"
22 class Result
: public Wrappable
<Result
> {
24 static WrapperInfo kWrapperInfo
;
25 static Handle
<Result
> Create(v8::Isolate
* isolate
) {
26 return CreateHandle(isolate
, new Result());
29 int count() const { return count_
; }
30 void set_count(int count
) { count_
= count
; }
33 base::MessageLoop::current()->QuitNow();
37 Result() : count_(0) {
43 virtual ObjectTemplateBuilder
GetObjectTemplateBuilder(
44 v8::Isolate
* isolate
) OVERRIDE
{
45 return Wrappable
<Result
>::GetObjectTemplateBuilder(isolate
)
46 .SetProperty("count", &Result::count
, &Result::set_count
)
47 .SetMethod("quit", &Result::Quit
);
53 WrapperInfo
Result::kWrapperInfo
= { gin::kEmbedderNativeGin
};
56 TestHelper(v8::Isolate
* isolate
)
57 : runner(new Runner(&delegate
, isolate
)),
59 timer_module(TimerModule::Create(isolate
)),
60 result(Result::Create(isolate
)) {
61 EXPECT_FALSE(runner
->global().IsEmpty());
62 runner
->global()->Set(StringToV8(isolate
, "timer"),
63 timer_module
->GetWrapper(isolate
));
64 runner
->global()->Set(StringToV8(isolate
, "result"),
65 result
->GetWrapper(isolate
));
69 loop
.PostDelayedTask(FROM_HERE
, base::MessageLoop::QuitWhenIdleClosure(),
70 base::TimeDelta::FromMilliseconds(0));
73 RunnerDelegate delegate
;
74 scoped_ptr
<Runner
> runner
;
76 Handle
<TimerModule
> timer_module
;
77 Handle
<Result
> result
;
78 base::MessageLoop loop
;
83 typedef V8Test TimerUnittest
;
85 TEST_F(TimerUnittest
, OneShot
) {
86 TestHelper
helper(instance_
->isolate());
88 "timer.createOneShot(0, function() {"
92 helper
.runner
->Run(source
, "script");
93 EXPECT_EQ(0, helper
.result
->count());
97 EXPECT_EQ(1, helper
.result
->count());
100 TEST_F(TimerUnittest
, OneShotCancel
) {
101 TestHelper
helper(instance_
->isolate());
103 "var t = timer.createOneShot(0, function() {"
108 helper
.runner
->Run(source
, "script");
109 EXPECT_EQ(0, helper
.result
->count());
113 EXPECT_EQ(0, helper
.result
->count());
116 TEST_F(TimerUnittest
, Repeating
) {
117 TestHelper
helper(instance_
->isolate());
119 // TODO(aa): Cannot do: if (++result.count == 3) because of v8 bug. Create
120 // test case and report.
122 "timer.createRepeating(0, function() {"
124 " if (result.count == 3) {"
129 helper
.runner
->Run(source
, "script");
130 EXPECT_EQ(0, helper
.result
->count());
133 EXPECT_EQ(3, helper
.result
->count());
136 TEST_F(TimerUnittest
, TimerCallbackToDestroyedRunner
) {
137 TestHelper
helper(instance_
->isolate());
139 "timer.createOneShot(0, function() {"
143 helper
.runner
->Run(source
, "script");
144 EXPECT_EQ(0, helper
.result
->count());
146 // Destroy runner, which should destroy the timer object we created.
148 helper
.runner
.reset(NULL
);
151 // Timer should not have run because it was deleted.
152 EXPECT_EQ(0, helper
.result
->count());