OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "gin/modules/timer.h" | 5 #include "gin/modules/timer.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "gin/handle.h" | 9 #include "gin/handle.h" |
10 #include "gin/object_template_builder.h" | 10 #include "gin/object_template_builder.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 scope(runner.get()), | 57 scope(runner.get()), |
58 timer_module(TimerModule::Create(isolate)), | 58 timer_module(TimerModule::Create(isolate)), |
59 result(Result::Create(isolate)) { | 59 result(Result::Create(isolate)) { |
60 EXPECT_FALSE(runner->global().IsEmpty()); | 60 EXPECT_FALSE(runner->global().IsEmpty()); |
61 runner->global()->Set(StringToV8(isolate, "timer"), | 61 runner->global()->Set(StringToV8(isolate, "timer"), |
62 timer_module->GetWrapper(isolate)); | 62 timer_module->GetWrapper(isolate)); |
63 runner->global()->Set(StringToV8(isolate, "result"), | 63 runner->global()->Set(StringToV8(isolate, "result"), |
64 result->GetWrapper(isolate)); | 64 result->GetWrapper(isolate)); |
65 } | 65 } |
66 | 66 |
67 void QuitSoon() { | 67 void QuitSoon(base::MessageLoop* message_loop) { |
68 loop.PostDelayedTask(FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), | 68 message_loop->PostDelayedTask(FROM_HERE, |
69 base::TimeDelta::FromMilliseconds(0)); | 69 base::MessageLoop::QuitWhenIdleClosure(), |
| 70 base::TimeDelta::FromMilliseconds(0)); |
70 } | 71 } |
71 | 72 |
72 ShellRunnerDelegate delegate; | 73 ShellRunnerDelegate delegate; |
73 scoped_ptr<ShellRunner> runner; | 74 scoped_ptr<ShellRunner> runner; |
74 Runner::Scope scope; | 75 Runner::Scope scope; |
75 Handle<TimerModule> timer_module; | 76 Handle<TimerModule> timer_module; |
76 Handle<Result> result; | 77 Handle<Result> result; |
77 base::MessageLoop loop; | |
78 }; | 78 }; |
79 | 79 |
80 } // namespace | 80 } // namespace |
81 | 81 |
82 typedef V8Test TimerUnittest; | 82 typedef V8Test TimerUnittest; |
83 | 83 |
84 TEST_F(TimerUnittest, OneShot) { | 84 TEST_F(TimerUnittest, OneShot) { |
85 TestHelper helper(instance_->isolate()); | 85 TestHelper helper(instance_->isolate()); |
86 std::string source = | 86 std::string source = |
87 "timer.createOneShot(0, function() {" | 87 "timer.createOneShot(0, function() {" |
88 " result.count++;" | 88 " result.count++;" |
89 "});"; | 89 "});"; |
90 | 90 |
91 helper.runner->Run(source, "script"); | 91 helper.runner->Run(source, "script"); |
92 EXPECT_EQ(0, helper.result->count()); | 92 EXPECT_EQ(0, helper.result->count()); |
93 | 93 |
94 helper.QuitSoon(); | 94 helper.QuitSoon(&message_loop_); |
95 helper.loop.Run(); | 95 message_loop_.Run(); |
96 EXPECT_EQ(1, helper.result->count()); | 96 EXPECT_EQ(1, helper.result->count()); |
97 } | 97 } |
98 | 98 |
99 TEST_F(TimerUnittest, OneShotCancel) { | 99 TEST_F(TimerUnittest, OneShotCancel) { |
100 TestHelper helper(instance_->isolate()); | 100 TestHelper helper(instance_->isolate()); |
101 std::string source = | 101 std::string source = |
102 "var t = timer.createOneShot(0, function() {" | 102 "var t = timer.createOneShot(0, function() {" |
103 " result.count++;" | 103 " result.count++;" |
104 "});" | 104 "});" |
105 "t.cancel()"; | 105 "t.cancel()"; |
106 | 106 |
107 helper.runner->Run(source, "script"); | 107 helper.runner->Run(source, "script"); |
108 EXPECT_EQ(0, helper.result->count()); | 108 EXPECT_EQ(0, helper.result->count()); |
109 | 109 |
110 helper.QuitSoon(); | 110 helper.QuitSoon(&message_loop_); |
111 helper.loop.Run(); | 111 message_loop_.Run(); |
112 EXPECT_EQ(0, helper.result->count()); | 112 EXPECT_EQ(0, helper.result->count()); |
113 } | 113 } |
114 | 114 |
115 TEST_F(TimerUnittest, Repeating) { | 115 TEST_F(TimerUnittest, Repeating) { |
116 TestHelper helper(instance_->isolate()); | 116 TestHelper helper(instance_->isolate()); |
117 | 117 |
118 // TODO(aa): Cannot do: if (++result.count == 3) because of v8 bug. Create | 118 // TODO(aa): Cannot do: if (++result.count == 3) because of v8 bug. Create |
119 // test case and report. | 119 // test case and report. |
120 std::string source = | 120 std::string source = |
121 "timer.createRepeating(0, function() {" | 121 "timer.createRepeating(0, function() {" |
122 " result.count++;" | 122 " result.count++;" |
123 " if (result.count == 3) {" | 123 " if (result.count == 3) {" |
124 " result.quit();" | 124 " result.quit();" |
125 " }" | 125 " }" |
126 "});"; | 126 "});"; |
127 | 127 |
128 helper.runner->Run(source, "script"); | 128 helper.runner->Run(source, "script"); |
129 EXPECT_EQ(0, helper.result->count()); | 129 EXPECT_EQ(0, helper.result->count()); |
130 | 130 |
131 helper.loop.Run(); | 131 message_loop_.Run(); |
132 EXPECT_EQ(3, helper.result->count()); | 132 EXPECT_EQ(3, helper.result->count()); |
133 } | 133 } |
134 | 134 |
135 TEST_F(TimerUnittest, TimerCallbackToDestroyedRunner) { | 135 TEST_F(TimerUnittest, TimerCallbackToDestroyedRunner) { |
136 TestHelper helper(instance_->isolate()); | 136 TestHelper helper(instance_->isolate()); |
137 std::string source = | 137 std::string source = |
138 "timer.createOneShot(0, function() {" | 138 "timer.createOneShot(0, function() {" |
139 " result.count++;" | 139 " result.count++;" |
140 "});"; | 140 "});"; |
141 | 141 |
142 helper.runner->Run(source, "script"); | 142 helper.runner->Run(source, "script"); |
143 EXPECT_EQ(0, helper.result->count()); | 143 EXPECT_EQ(0, helper.result->count()); |
144 | 144 |
145 // Destroy runner, which should destroy the timer object we created. | 145 // Destroy runner, which should destroy the timer object we created. |
146 helper.QuitSoon(); | 146 helper.QuitSoon(&message_loop_); |
147 helper.runner.reset(NULL); | 147 helper.runner.reset(NULL); |
148 helper.loop.Run(); | 148 message_loop_.Run(); |
149 | 149 |
150 // Timer should not have run because it was deleted. | 150 // Timer should not have run because it was deleted. |
151 EXPECT_EQ(0, helper.result->count()); | 151 EXPECT_EQ(0, helper.result->count()); |
152 } | 152 } |
153 | 153 |
154 } // namespace gin | 154 } // namespace gin |
OLD | NEW |