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

Side by Side Diff: chrome/browser/extensions/api/alarms/alarms_api_unittest.cc

Issue 14113053: chrome: Use base::MessageLoop. (Part 3) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase again Created 7 years, 6 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // This file tests the chrome.alarms extension API. 5 // This file tests the chrome.alarms extension API.
6 6
7 #include "base/test/simple_test_clock.h" 7 #include "base/test/simple_test_clock.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "chrome/browser/extensions/api/alarms/alarm_manager.h" 9 #include "chrome/browser/extensions/api/alarms/alarm_manager.h"
10 #include "chrome/browser/extensions/api/alarms/alarms_api.h" 10 #include "chrome/browser/extensions/api/alarms/alarms_api.h"
(...skipping 18 matching lines...) Expand all
29 29
30 namespace { 30 namespace {
31 31
32 // Test delegate which quits the message loop when an alarm fires. 32 // Test delegate which quits the message loop when an alarm fires.
33 class AlarmDelegate : public AlarmManager::Delegate { 33 class AlarmDelegate : public AlarmManager::Delegate {
34 public: 34 public:
35 virtual ~AlarmDelegate() {} 35 virtual ~AlarmDelegate() {}
36 virtual void OnAlarm(const std::string& extension_id, 36 virtual void OnAlarm(const std::string& extension_id,
37 const Alarm& alarm) OVERRIDE { 37 const Alarm& alarm) OVERRIDE {
38 alarms_seen.push_back(alarm.js_alarm->name); 38 alarms_seen.push_back(alarm.js_alarm->name);
39 MessageLoop::current()->Quit(); 39 base::MessageLoop::current()->Quit();
40 } 40 }
41 41
42 std::vector<std::string> alarms_seen; 42 std::vector<std::string> alarms_seen;
43 }; 43 };
44 44
45 } // namespace 45 } // namespace
46 46
47 class ExtensionAlarmsTest : public BrowserWithTestWindowTest { 47 class ExtensionAlarmsTest : public BrowserWithTestWindowTest {
48 public: 48 public:
49 virtual void SetUp() { 49 virtual void SetUp() {
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 150
151 const Alarm* alarm = 151 const Alarm* alarm =
152 alarm_manager_->GetAlarm(extension_->id(), std::string()); 152 alarm_manager_->GetAlarm(extension_->id(), std::string());
153 ASSERT_TRUE(alarm); 153 ASSERT_TRUE(alarm);
154 EXPECT_EQ("", alarm->js_alarm->name); 154 EXPECT_EQ("", alarm->js_alarm->name);
155 EXPECT_DOUBLE_EQ(10000, alarm->js_alarm->scheduled_time); 155 EXPECT_DOUBLE_EQ(10000, alarm->js_alarm->scheduled_time);
156 EXPECT_FALSE(alarm->js_alarm->period_in_minutes.get()); 156 EXPECT_FALSE(alarm->js_alarm->period_in_minutes.get());
157 157
158 // Now wait for the alarm to fire. Our test delegate will quit the 158 // Now wait for the alarm to fire. Our test delegate will quit the
159 // MessageLoop when that happens. 159 // MessageLoop when that happens.
160 MessageLoop::current()->Run(); 160 base::MessageLoop::current()->Run();
161 161
162 ASSERT_EQ(1u, alarm_delegate_->alarms_seen.size()); 162 ASSERT_EQ(1u, alarm_delegate_->alarms_seen.size());
163 EXPECT_EQ("", alarm_delegate_->alarms_seen[0]); 163 EXPECT_EQ("", alarm_delegate_->alarms_seen[0]);
164 164
165 // Ensure the alarm is gone. 165 // Ensure the alarm is gone.
166 { 166 {
167 const AlarmManager::AlarmList* alarms = 167 const AlarmManager::AlarmList* alarms =
168 alarm_manager_->GetAllAlarms(extension_->id()); 168 alarm_manager_->GetAllAlarms(extension_->id());
169 ASSERT_FALSE(alarms); 169 ASSERT_FALSE(alarms);
170 } 170 }
171 } 171 }
172 172
173 TEST_F(ExtensionAlarmsTest, CreateRepeating) { 173 TEST_F(ExtensionAlarmsTest, CreateRepeating) {
174 test_clock_->SetNow(base::Time::FromDoubleT(10)); 174 test_clock_->SetNow(base::Time::FromDoubleT(10));
175 175
176 // Create 1 repeating alarm. 176 // Create 1 repeating alarm.
177 CreateAlarm("[null, {\"periodInMinutes\": 0.001}]"); 177 CreateAlarm("[null, {\"periodInMinutes\": 0.001}]");
178 178
179 const Alarm* alarm = 179 const Alarm* alarm =
180 alarm_manager_->GetAlarm(extension_->id(), std::string()); 180 alarm_manager_->GetAlarm(extension_->id(), std::string());
181 ASSERT_TRUE(alarm); 181 ASSERT_TRUE(alarm);
182 EXPECT_EQ("", alarm->js_alarm->name); 182 EXPECT_EQ("", alarm->js_alarm->name);
183 EXPECT_DOUBLE_EQ(10060, alarm->js_alarm->scheduled_time); 183 EXPECT_DOUBLE_EQ(10060, alarm->js_alarm->scheduled_time);
184 EXPECT_THAT(alarm->js_alarm->period_in_minutes, 184 EXPECT_THAT(alarm->js_alarm->period_in_minutes,
185 testing::Pointee(testing::DoubleEq(0.001))); 185 testing::Pointee(testing::DoubleEq(0.001)));
186 186
187 test_clock_->Advance(base::TimeDelta::FromSeconds(1)); 187 test_clock_->Advance(base::TimeDelta::FromSeconds(1));
188 // Now wait for the alarm to fire. Our test delegate will quit the 188 // Now wait for the alarm to fire. Our test delegate will quit the
189 // MessageLoop when that happens. 189 // MessageLoop when that happens.
190 MessageLoop::current()->Run(); 190 base::MessageLoop::current()->Run();
191 191
192 test_clock_->Advance(base::TimeDelta::FromSeconds(1)); 192 test_clock_->Advance(base::TimeDelta::FromSeconds(1));
193 // Wait again, and ensure the alarm fires again. 193 // Wait again, and ensure the alarm fires again.
194 alarm_manager_->ScheduleNextPoll(); 194 alarm_manager_->ScheduleNextPoll();
195 MessageLoop::current()->Run(); 195 base::MessageLoop::current()->Run();
196 196
197 ASSERT_EQ(2u, alarm_delegate_->alarms_seen.size()); 197 ASSERT_EQ(2u, alarm_delegate_->alarms_seen.size());
198 EXPECT_EQ("", alarm_delegate_->alarms_seen[0]); 198 EXPECT_EQ("", alarm_delegate_->alarms_seen[0]);
199 } 199 }
200 200
201 TEST_F(ExtensionAlarmsTest, CreateAbsolute) { 201 TEST_F(ExtensionAlarmsTest, CreateAbsolute) {
202 test_clock_->SetNow(base::Time::FromDoubleT(9.99)); 202 test_clock_->SetNow(base::Time::FromDoubleT(9.99));
203 CreateAlarm("[null, {\"when\": 10001}]"); 203 CreateAlarm("[null, {\"when\": 10001}]");
204 204
205 const Alarm* alarm = 205 const Alarm* alarm =
206 alarm_manager_->GetAlarm(extension_->id(), std::string()); 206 alarm_manager_->GetAlarm(extension_->id(), std::string());
207 ASSERT_TRUE(alarm); 207 ASSERT_TRUE(alarm);
208 EXPECT_EQ("", alarm->js_alarm->name); 208 EXPECT_EQ("", alarm->js_alarm->name);
209 EXPECT_DOUBLE_EQ(10001, alarm->js_alarm->scheduled_time); 209 EXPECT_DOUBLE_EQ(10001, alarm->js_alarm->scheduled_time);
210 EXPECT_THAT(alarm->js_alarm->period_in_minutes, 210 EXPECT_THAT(alarm->js_alarm->period_in_minutes,
211 testing::IsNull()); 211 testing::IsNull());
212 212
213 test_clock_->SetNow(base::Time::FromDoubleT(10.1)); 213 test_clock_->SetNow(base::Time::FromDoubleT(10.1));
214 // Now wait for the alarm to fire. Our test delegate will quit the 214 // Now wait for the alarm to fire. Our test delegate will quit the
215 // MessageLoop when that happens. 215 // MessageLoop when that happens.
216 MessageLoop::current()->Run(); 216 base::MessageLoop::current()->Run();
217 217
218 ASSERT_FALSE(alarm_manager_->GetAlarm(extension_->id(), std::string())); 218 ASSERT_FALSE(alarm_manager_->GetAlarm(extension_->id(), std::string()));
219 219
220 ASSERT_EQ(1u, alarm_delegate_->alarms_seen.size()); 220 ASSERT_EQ(1u, alarm_delegate_->alarms_seen.size());
221 EXPECT_EQ("", alarm_delegate_->alarms_seen[0]); 221 EXPECT_EQ("", alarm_delegate_->alarms_seen[0]);
222 } 222 }
223 223
224 TEST_F(ExtensionAlarmsTest, CreateRepeatingWithQuickFirstCall) { 224 TEST_F(ExtensionAlarmsTest, CreateRepeatingWithQuickFirstCall) {
225 test_clock_->SetNow(base::Time::FromDoubleT(9.99)); 225 test_clock_->SetNow(base::Time::FromDoubleT(9.99));
226 CreateAlarm("[null, {\"when\": 10001, \"periodInMinutes\": 0.001}]"); 226 CreateAlarm("[null, {\"when\": 10001, \"periodInMinutes\": 0.001}]");
227 227
228 const Alarm* alarm = 228 const Alarm* alarm =
229 alarm_manager_->GetAlarm(extension_->id(), std::string()); 229 alarm_manager_->GetAlarm(extension_->id(), std::string());
230 ASSERT_TRUE(alarm); 230 ASSERT_TRUE(alarm);
231 EXPECT_EQ("", alarm->js_alarm->name); 231 EXPECT_EQ("", alarm->js_alarm->name);
232 EXPECT_DOUBLE_EQ(10001, alarm->js_alarm->scheduled_time); 232 EXPECT_DOUBLE_EQ(10001, alarm->js_alarm->scheduled_time);
233 EXPECT_THAT(alarm->js_alarm->period_in_minutes, 233 EXPECT_THAT(alarm->js_alarm->period_in_minutes,
234 testing::Pointee(testing::DoubleEq(0.001))); 234 testing::Pointee(testing::DoubleEq(0.001)));
235 235
236 test_clock_->SetNow(base::Time::FromDoubleT(10.1)); 236 test_clock_->SetNow(base::Time::FromDoubleT(10.1));
237 // Now wait for the alarm to fire. Our test delegate will quit the 237 // Now wait for the alarm to fire. Our test delegate will quit the
238 // MessageLoop when that happens. 238 // MessageLoop when that happens.
239 MessageLoop::current()->Run(); 239 base::MessageLoop::current()->Run();
240 240
241 ASSERT_TRUE(alarm_manager_->GetAlarm(extension_->id(), std::string())); 241 ASSERT_TRUE(alarm_manager_->GetAlarm(extension_->id(), std::string()));
242 EXPECT_THAT(alarm_delegate_->alarms_seen, testing::ElementsAre("")); 242 EXPECT_THAT(alarm_delegate_->alarms_seen, testing::ElementsAre(""));
243 243
244 test_clock_->SetNow(base::Time::FromDoubleT(10.7)); 244 test_clock_->SetNow(base::Time::FromDoubleT(10.7));
245 MessageLoop::current()->Run(); 245 base::MessageLoop::current()->Run();
246 246
247 ASSERT_TRUE(alarm_manager_->GetAlarm(extension_->id(), std::string())); 247 ASSERT_TRUE(alarm_manager_->GetAlarm(extension_->id(), std::string()));
248 EXPECT_THAT(alarm_delegate_->alarms_seen, testing::ElementsAre("", "")); 248 EXPECT_THAT(alarm_delegate_->alarms_seen, testing::ElementsAre("", ""));
249 } 249 }
250 250
251 TEST_F(ExtensionAlarmsTest, CreateDupe) { 251 TEST_F(ExtensionAlarmsTest, CreateDupe) {
252 test_clock_->SetNow(base::Time::FromDoubleT(10)); 252 test_clock_->SetNow(base::Time::FromDoubleT(10));
253 253
254 // Create 2 duplicate alarms. The first should be overridden. 254 // Create 2 duplicate alarms. The first should be overridden.
255 CreateAlarm("[\"dup\", {\"delayInMinutes\": 1}]"); 255 CreateAlarm("[\"dup\", {\"delayInMinutes\": 1}]");
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 alarm_manager_->GetAllAlarms(extension_->id()); 365 alarm_manager_->GetAllAlarms(extension_->id());
366 ASSERT_TRUE(alarms); 366 ASSERT_TRUE(alarms);
367 EXPECT_EQ(1u, alarms->size()); 367 EXPECT_EQ(1u, alarms->size());
368 EXPECT_THAT((*alarms)[0].js_alarm->period_in_minutes, 368 EXPECT_THAT((*alarms)[0].js_alarm->period_in_minutes,
369 testing::Pointee(0.001)); 369 testing::Pointee(0.001));
370 } 370 }
371 371
372 // Now wait for the alarms to fire, and ensure the cancelled alarms don't 372 // Now wait for the alarms to fire, and ensure the cancelled alarms don't
373 // fire. 373 // fire.
374 alarm_manager_->ScheduleNextPoll(); 374 alarm_manager_->ScheduleNextPoll();
375 MessageLoop::current()->Run(); 375 base::MessageLoop::current()->Run();
376 376
377 ASSERT_EQ(1u, alarm_delegate_->alarms_seen.size()); 377 ASSERT_EQ(1u, alarm_delegate_->alarms_seen.size());
378 EXPECT_EQ("", alarm_delegate_->alarms_seen[0]); 378 EXPECT_EQ("", alarm_delegate_->alarms_seen[0]);
379 379
380 // Ensure the 0.001-minute alarm is still there, since it's repeating. 380 // Ensure the 0.001-minute alarm is still there, since it's repeating.
381 { 381 {
382 const AlarmManager::AlarmList* alarms = 382 const AlarmManager::AlarmList* alarms =
383 alarm_manager_->GetAllAlarms(extension_->id()); 383 alarm_manager_->GetAllAlarms(extension_->id());
384 ASSERT_TRUE(alarms); 384 ASSERT_TRUE(alarms);
385 EXPECT_EQ(1u, alarms->size()); 385 EXPECT_EQ(1u, alarms->size());
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 alarm_manager_->next_poll_time_.ToDoubleT()); 450 alarm_manager_->next_poll_time_.ToDoubleT());
451 alarm_manager_->RemoveAllAlarms(extension_->id()); 451 alarm_manager_->RemoveAllAlarms(extension_->id());
452 } 452 }
453 { 453 {
454 test_clock_->SetNow(base::Time::FromDoubleT(3 * 60 + 1)); 454 test_clock_->SetNow(base::Time::FromDoubleT(3 * 60 + 1));
455 Alarm alarm; 455 Alarm alarm;
456 alarm.js_alarm->name = "bb"; 456 alarm.js_alarm->name = "bb";
457 alarm.js_alarm->scheduled_time = 3 * 60000; 457 alarm.js_alarm->scheduled_time = 3 * 60000;
458 alarm.js_alarm->period_in_minutes.reset(new double(3)); 458 alarm.js_alarm->period_in_minutes.reset(new double(3));
459 alarm_manager_->AddAlarmImpl(extension_->id(), alarm); 459 alarm_manager_->AddAlarmImpl(extension_->id(), alarm);
460 MessageLoop::current()->Run(); 460 base::MessageLoop::current()->Run();
461 EXPECT_EQ(alarm_manager_->last_poll_time_ + base::TimeDelta::FromMinutes(3), 461 EXPECT_EQ(alarm_manager_->last_poll_time_ + base::TimeDelta::FromMinutes(3),
462 alarm_manager_->next_poll_time_); 462 alarm_manager_->next_poll_time_);
463 alarm_manager_->RemoveAllAlarms(extension_->id()); 463 alarm_manager_->RemoveAllAlarms(extension_->id());
464 } 464 }
465 { 465 {
466 test_clock_->SetNow(base::Time::FromDoubleT(4 * 60 + 1)); 466 test_clock_->SetNow(base::Time::FromDoubleT(4 * 60 + 1));
467 CreateAlarm("[\"a\", {\"periodInMinutes\": 2}]"); 467 CreateAlarm("[\"a\", {\"periodInMinutes\": 2}]");
468 alarm_manager_->RemoveAlarm(extension_->id(), "a"); 468 alarm_manager_->RemoveAlarm(extension_->id(), "a");
469 Alarm alarm2; 469 Alarm alarm2;
470 alarm2.js_alarm->name = "bb"; 470 alarm2.js_alarm->name = "bb";
471 alarm2.js_alarm->scheduled_time = 4 * 60000; 471 alarm2.js_alarm->scheduled_time = 4 * 60000;
472 alarm2.js_alarm->period_in_minutes.reset(new double(4)); 472 alarm2.js_alarm->period_in_minutes.reset(new double(4));
473 alarm_manager_->AddAlarmImpl(extension_->id(), alarm2); 473 alarm_manager_->AddAlarmImpl(extension_->id(), alarm2);
474 Alarm alarm3; 474 Alarm alarm3;
475 alarm3.js_alarm->name = "ccc"; 475 alarm3.js_alarm->name = "ccc";
476 alarm3.js_alarm->scheduled_time = 25 * 60000; 476 alarm3.js_alarm->scheduled_time = 25 * 60000;
477 alarm3.js_alarm->period_in_minutes.reset(new double(25)); 477 alarm3.js_alarm->period_in_minutes.reset(new double(25));
478 alarm_manager_->AddAlarmImpl(extension_->id(), alarm3); 478 alarm_manager_->AddAlarmImpl(extension_->id(), alarm3);
479 MessageLoop::current()->Run(); 479 base::MessageLoop::current()->Run();
480 EXPECT_EQ(alarm_manager_->last_poll_time_ + base::TimeDelta::FromMinutes(4), 480 EXPECT_EQ(alarm_manager_->last_poll_time_ + base::TimeDelta::FromMinutes(4),
481 alarm_manager_->next_poll_time_); 481 alarm_manager_->next_poll_time_);
482 alarm_manager_->RemoveAllAlarms(extension_->id()); 482 alarm_manager_->RemoveAllAlarms(extension_->id());
483 } 483 }
484 } 484 }
485 485
486 TEST_F(ExtensionAlarmsSchedulingTest, ReleasedExtensionPollsInfrequently) { 486 TEST_F(ExtensionAlarmsSchedulingTest, ReleasedExtensionPollsInfrequently) {
487 extension_ = utils::CreateEmptyExtensionWithLocation( 487 extension_ = utils::CreateEmptyExtensionWithLocation(
488 extensions::Manifest::INTERNAL); 488 extensions::Manifest::INTERNAL);
489 test_clock_->SetNow(base::Time::FromJsTime(300000)); 489 test_clock_->SetNow(base::Time::FromJsTime(300000));
(...skipping 11 matching lines...) Expand all
501 alarm_manager_->ScheduleNextPoll(); 501 alarm_manager_->ScheduleNextPoll();
502 EXPECT_DOUBLE_EQ((alarm_manager_->last_poll_time_ + 502 EXPECT_DOUBLE_EQ((alarm_manager_->last_poll_time_ +
503 base::TimeDelta::FromMinutes(1)).ToJsTime(), 503 base::TimeDelta::FromMinutes(1)).ToJsTime(),
504 alarm_manager_->next_poll_time_.ToJsTime()); 504 alarm_manager_->next_poll_time_.ToJsTime());
505 } 505 }
506 506
507 TEST_F(ExtensionAlarmsSchedulingTest, TimerRunning) { 507 TEST_F(ExtensionAlarmsSchedulingTest, TimerRunning) {
508 EXPECT_FALSE(alarm_manager_->timer_.IsRunning()); 508 EXPECT_FALSE(alarm_manager_->timer_.IsRunning());
509 CreateAlarm("[\"a\", {\"delayInMinutes\": 0.001}]"); 509 CreateAlarm("[\"a\", {\"delayInMinutes\": 0.001}]");
510 EXPECT_TRUE(alarm_manager_->timer_.IsRunning()); 510 EXPECT_TRUE(alarm_manager_->timer_.IsRunning());
511 MessageLoop::current()->Run(); 511 base::MessageLoop::current()->Run();
512 EXPECT_FALSE(alarm_manager_->timer_.IsRunning()); 512 EXPECT_FALSE(alarm_manager_->timer_.IsRunning());
513 CreateAlarm("[\"bb\", {\"delayInMinutes\": 10}]"); 513 CreateAlarm("[\"bb\", {\"delayInMinutes\": 10}]");
514 EXPECT_TRUE(alarm_manager_->timer_.IsRunning()); 514 EXPECT_TRUE(alarm_manager_->timer_.IsRunning());
515 alarm_manager_->RemoveAllAlarms(extension_->id()); 515 alarm_manager_->RemoveAllAlarms(extension_->id());
516 EXPECT_FALSE(alarm_manager_->timer_.IsRunning()); 516 EXPECT_FALSE(alarm_manager_->timer_.IsRunning());
517 } 517 }
518 518
519 } // namespace extensions 519 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698