Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(452)

Side by Side Diff: gin/modules/timer_unittest.cc

Issue 1130433002: Adding task runner/ message loop to tests that use IsolateHolder. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
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() {
68 loop.PostDelayedTask(FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), 68 base::MessageLoop::current()->PostDelayedTask(
Sami 2015/05/05 13:55:54 Please use ThreadTaskRunnerHandle::Get() instead o
Primiano Tucci (use gerrit) 2015/05/05 14:26:51 Is this going to use the DummyTaskRunner? If so, i
Sami 2015/05/05 14:35:46 Actually not, because you can't have both a Messag
ssid 2015/05/05 16:00:06 No, this change here is not related to the dummy t
69 base::TimeDelta::FromMilliseconds(0)); 69 FROM_HERE, 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();
95 helper.loop.Run(); 95 base::MessageLoop::current()->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();
111 helper.loop.Run(); 111 base::MessageLoop::current()->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 base::MessageLoop::current()->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();
147 helper.runner.reset(NULL); 147 helper.runner.reset(NULL);
148 helper.loop.Run(); 148 base::MessageLoop::current()->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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698