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/shell_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) {
42 ObjectTemplateBuilder
GetObjectTemplateBuilder(
43 v8::Isolate
* isolate
) override
{
44 return Wrappable
<Result
>::GetObjectTemplateBuilder(isolate
)
45 .SetProperty("count", &Result::count
, &Result::set_count
)
46 .SetMethod("quit", &Result::Quit
);
52 WrapperInfo
Result::kWrapperInfo
= { gin::kEmbedderNativeGin
};
55 TestHelper(v8::Isolate
* isolate
)
56 : runner(new ShellRunner(&delegate
, isolate
)),
58 timer_module(TimerModule::Create(isolate
)),
59 result(Result::Create(isolate
)) {
60 EXPECT_FALSE(runner
->global().IsEmpty());
61 runner
->global()->Set(StringToV8(isolate
, "timer"),
62 timer_module
->GetWrapper(isolate
));
63 runner
->global()->Set(StringToV8(isolate
, "result"),
64 result
->GetWrapper(isolate
));
67 void QuitSoon(base::MessageLoop
* message_loop
) {
68 message_loop
->PostDelayedTask(FROM_HERE
,
69 base::MessageLoop::QuitWhenIdleClosure(),
70 base::TimeDelta::FromMilliseconds(0));
73 ShellRunnerDelegate delegate
;
74 scoped_ptr
<ShellRunner
> runner
;
76 Handle
<TimerModule
> timer_module
;
77 Handle
<Result
> result
;
82 typedef V8Test TimerUnittest
;
84 TEST_F(TimerUnittest
, OneShot
) {
85 TestHelper
helper(instance_
->isolate());
87 "timer.createOneShot(0, function() {"
91 helper
.runner
->Run(source
, "script");
92 EXPECT_EQ(0, helper
.result
->count());
94 helper
.QuitSoon(&message_loop_
);
96 EXPECT_EQ(1, helper
.result
->count());
99 TEST_F(TimerUnittest
, OneShotCancel
) {
100 TestHelper
helper(instance_
->isolate());
102 "var t = timer.createOneShot(0, function() {"
107 helper
.runner
->Run(source
, "script");
108 EXPECT_EQ(0, helper
.result
->count());
110 helper
.QuitSoon(&message_loop_
);
112 EXPECT_EQ(0, helper
.result
->count());
115 TEST_F(TimerUnittest
, Repeating
) {
116 TestHelper
helper(instance_
->isolate());
118 // TODO(aa): Cannot do: if (++result.count == 3) because of v8 bug. Create
119 // test case and report.
121 "timer.createRepeating(0, function() {"
123 " if (result.count == 3) {"
128 helper
.runner
->Run(source
, "script");
129 EXPECT_EQ(0, helper
.result
->count());
132 EXPECT_EQ(3, helper
.result
->count());
135 TEST_F(TimerUnittest
, TimerCallbackToDestroyedRunner
) {
136 TestHelper
helper(instance_
->isolate());
138 "timer.createOneShot(0, function() {"
142 helper
.runner
->Run(source
, "script");
143 EXPECT_EQ(0, helper
.result
->count());
145 // Destroy runner, which should destroy the timer object we created.
146 helper
.QuitSoon(&message_loop_
);
147 helper
.runner
.reset(NULL
);
150 // Timer should not have run because it was deleted.
151 EXPECT_EQ(0, helper
.result
->count());