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