| 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 <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "base/single_thread_task_runner.h" |
| 10 #include "gin/handle.h" | 12 #include "gin/handle.h" |
| 11 #include "gin/object_template_builder.h" | 13 #include "gin/object_template_builder.h" |
| 12 #include "gin/public/isolate_holder.h" | 14 #include "gin/public/isolate_holder.h" |
| 13 #include "gin/shell_runner.h" | 15 #include "gin/shell_runner.h" |
| 14 #include "gin/test/v8_test.h" | 16 #include "gin/test/v8_test.h" |
| 15 #include "gin/try_catch.h" | 17 #include "gin/try_catch.h" |
| 16 #include "gin/wrappable.h" | 18 #include "gin/wrappable.h" |
| 17 #include "v8/include/v8.h" | 19 #include "v8/include/v8.h" |
| 18 | 20 |
| 19 namespace gin { | 21 namespace gin { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 timer_module(TimerModule::Create(isolate)), | 61 timer_module(TimerModule::Create(isolate)), |
| 60 result(Result::Create(isolate)) { | 62 result(Result::Create(isolate)) { |
| 61 EXPECT_FALSE(runner->global().IsEmpty()); | 63 EXPECT_FALSE(runner->global().IsEmpty()); |
| 62 runner->global()->Set(StringToV8(isolate, "timer"), | 64 runner->global()->Set(StringToV8(isolate, "timer"), |
| 63 timer_module->GetWrapper(isolate)); | 65 timer_module->GetWrapper(isolate)); |
| 64 runner->global()->Set(StringToV8(isolate, "result"), | 66 runner->global()->Set(StringToV8(isolate, "result"), |
| 65 result->GetWrapper(isolate)); | 67 result->GetWrapper(isolate)); |
| 66 } | 68 } |
| 67 | 69 |
| 68 void QuitSoon(base::MessageLoop* message_loop) { | 70 void QuitSoon(base::MessageLoop* message_loop) { |
| 69 message_loop->PostDelayedTask(FROM_HERE, | 71 message_loop->task_runner()->PostDelayedTask( |
| 70 base::MessageLoop::QuitWhenIdleClosure(), | 72 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), |
| 71 base::TimeDelta::FromMilliseconds(0)); | 73 base::TimeDelta::FromMilliseconds(0)); |
| 72 } | 74 } |
| 73 | 75 |
| 74 ShellRunnerDelegate delegate; | 76 ShellRunnerDelegate delegate; |
| 75 std::unique_ptr<ShellRunner> runner; | 77 std::unique_ptr<ShellRunner> runner; |
| 76 Runner::Scope scope; | 78 Runner::Scope scope; |
| 77 Handle<TimerModule> timer_module; | 79 Handle<TimerModule> timer_module; |
| 78 Handle<Result> result; | 80 Handle<Result> result; |
| 79 }; | 81 }; |
| 80 | 82 |
| 81 } // namespace | 83 } // namespace |
| 82 | 84 |
| 83 typedef V8Test TimerUnittest; | 85 typedef V8Test TimerUnittest; |
| 84 | 86 |
| 85 TEST_F(TimerUnittest, OneShot) { | 87 TEST_F(TimerUnittest, OneShot) { |
| 86 TestHelper helper(instance_->isolate()); | 88 TestHelper helper(instance_->isolate()); |
| 87 std::string source = | 89 std::string source = |
| 88 "timer.createOneShot(0, function() {" | 90 "timer.createOneShot(0, function() {" |
| 89 " result.count++;" | 91 " result.count++;" |
| 90 "});"; | 92 "});"; |
| 91 | 93 |
| 92 helper.runner->Run(source, "script"); | 94 helper.runner->Run(source, "script"); |
| 93 EXPECT_EQ(0, helper.result->count()); | 95 EXPECT_EQ(0, helper.result->count()); |
| 94 | 96 |
| 95 helper.QuitSoon(&message_loop_); | 97 helper.QuitSoon(&message_loop_); |
| 96 message_loop_.Run(); | 98 base::RunLoop().Run(); |
| 97 EXPECT_EQ(1, helper.result->count()); | 99 EXPECT_EQ(1, helper.result->count()); |
| 98 } | 100 } |
| 99 | 101 |
| 100 TEST_F(TimerUnittest, OneShotCancel) { | 102 TEST_F(TimerUnittest, OneShotCancel) { |
| 101 TestHelper helper(instance_->isolate()); | 103 TestHelper helper(instance_->isolate()); |
| 102 std::string source = | 104 std::string source = |
| 103 "var t = timer.createOneShot(0, function() {" | 105 "var t = timer.createOneShot(0, function() {" |
| 104 " result.count++;" | 106 " result.count++;" |
| 105 "});" | 107 "});" |
| 106 "t.cancel()"; | 108 "t.cancel()"; |
| 107 | 109 |
| 108 helper.runner->Run(source, "script"); | 110 helper.runner->Run(source, "script"); |
| 109 EXPECT_EQ(0, helper.result->count()); | 111 EXPECT_EQ(0, helper.result->count()); |
| 110 | 112 |
| 111 helper.QuitSoon(&message_loop_); | 113 helper.QuitSoon(&message_loop_); |
| 112 message_loop_.Run(); | 114 base::RunLoop().Run(); |
| 113 EXPECT_EQ(0, helper.result->count()); | 115 EXPECT_EQ(0, helper.result->count()); |
| 114 } | 116 } |
| 115 | 117 |
| 116 TEST_F(TimerUnittest, Repeating) { | 118 TEST_F(TimerUnittest, Repeating) { |
| 117 TestHelper helper(instance_->isolate()); | 119 TestHelper helper(instance_->isolate()); |
| 118 | 120 |
| 119 // TODO(aa): Cannot do: if (++result.count == 3) because of v8 bug. Create | 121 // TODO(aa): Cannot do: if (++result.count == 3) because of v8 bug. Create |
| 120 // test case and report. | 122 // test case and report. |
| 121 std::string source = | 123 std::string source = |
| 122 "timer.createRepeating(0, function() {" | 124 "timer.createRepeating(0, function() {" |
| 123 " result.count++;" | 125 " result.count++;" |
| 124 " if (result.count == 3) {" | 126 " if (result.count == 3) {" |
| 125 " result.quit();" | 127 " result.quit();" |
| 126 " }" | 128 " }" |
| 127 "});"; | 129 "});"; |
| 128 | 130 |
| 129 helper.runner->Run(source, "script"); | 131 helper.runner->Run(source, "script"); |
| 130 EXPECT_EQ(0, helper.result->count()); | 132 EXPECT_EQ(0, helper.result->count()); |
| 131 | 133 |
| 132 message_loop_.Run(); | 134 base::RunLoop().Run(); |
| 133 EXPECT_EQ(3, helper.result->count()); | 135 EXPECT_EQ(3, helper.result->count()); |
| 134 } | 136 } |
| 135 | 137 |
| 136 TEST_F(TimerUnittest, TimerCallbackToDestroyedRunner) { | 138 TEST_F(TimerUnittest, TimerCallbackToDestroyedRunner) { |
| 137 TestHelper helper(instance_->isolate()); | 139 TestHelper helper(instance_->isolate()); |
| 138 std::string source = | 140 std::string source = |
| 139 "timer.createOneShot(0, function() {" | 141 "timer.createOneShot(0, function() {" |
| 140 " result.count++;" | 142 " result.count++;" |
| 141 "});"; | 143 "});"; |
| 142 | 144 |
| 143 helper.runner->Run(source, "script"); | 145 helper.runner->Run(source, "script"); |
| 144 EXPECT_EQ(0, helper.result->count()); | 146 EXPECT_EQ(0, helper.result->count()); |
| 145 | 147 |
| 146 // Destroy runner, which should destroy the timer object we created. | 148 // Destroy runner, which should destroy the timer object we created. |
| 147 helper.QuitSoon(&message_loop_); | 149 helper.QuitSoon(&message_loop_); |
| 148 helper.runner.reset(NULL); | 150 helper.runner.reset(NULL); |
| 149 message_loop_.Run(); | 151 base::RunLoop().Run(); |
| 150 | 152 |
| 151 // Timer should not have run because it was deleted. | 153 // Timer should not have run because it was deleted. |
| 152 EXPECT_EQ(0, helper.result->count()); | 154 EXPECT_EQ(0, helper.result->count()); |
| 153 } | 155 } |
| 154 | 156 |
| 155 } // namespace gin | 157 } // namespace gin |
| OLD | NEW |