Refactor microphone/camera permission requests
[chromium-blink-merge.git] / gin / modules / timer_unittest.cc
blob612275e4729becc079cc71fa2ca27b84020e8c02
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"
18 namespace gin {
20 namespace {
22 class Result : public Wrappable<Result> {
23 public:
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; }
32 void Quit() {
33 base::MessageLoop::current()->QuitNow();
36 private:
37 Result() : count_(0) {
40 ~Result() override {}
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);
49 int count_;
52 WrapperInfo Result::kWrapperInfo = { gin::kEmbedderNativeGin };
54 struct TestHelper {
55 TestHelper(v8::Isolate* isolate)
56 : runner(new ShellRunner(&delegate, isolate)),
57 scope(runner.get()),
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;
75 Runner::Scope scope;
76 Handle<TimerModule> timer_module;
77 Handle<Result> result;
80 } // namespace
82 typedef V8Test TimerUnittest;
84 TEST_F(TimerUnittest, OneShot) {
85 TestHelper helper(instance_->isolate());
86 std::string source =
87 "timer.createOneShot(0, function() {"
88 " result.count++;"
89 "});";
91 helper.runner->Run(source, "script");
92 EXPECT_EQ(0, helper.result->count());
94 helper.QuitSoon(&message_loop_);
95 message_loop_.Run();
96 EXPECT_EQ(1, helper.result->count());
99 TEST_F(TimerUnittest, OneShotCancel) {
100 TestHelper helper(instance_->isolate());
101 std::string source =
102 "var t = timer.createOneShot(0, function() {"
103 " result.count++;"
104 "});"
105 "t.cancel()";
107 helper.runner->Run(source, "script");
108 EXPECT_EQ(0, helper.result->count());
110 helper.QuitSoon(&message_loop_);
111 message_loop_.Run();
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.
120 std::string source =
121 "timer.createRepeating(0, function() {"
122 " result.count++;"
123 " if (result.count == 3) {"
124 " result.quit();"
125 " }"
126 "});";
128 helper.runner->Run(source, "script");
129 EXPECT_EQ(0, helper.result->count());
131 message_loop_.Run();
132 EXPECT_EQ(3, helper.result->count());
135 TEST_F(TimerUnittest, TimerCallbackToDestroyedRunner) {
136 TestHelper helper(instance_->isolate());
137 std::string source =
138 "timer.createOneShot(0, function() {"
139 " result.count++;"
140 "});";
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);
148 message_loop_.Run();
150 // Timer should not have run because it was deleted.
151 EXPECT_EQ(0, helper.result->count());
154 } // namespace gin