OLD | NEW |
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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 return list; | 82 return list; |
83 } | 83 } |
84 EXPECT_TRUE(JsAlarm::Populate(*alarm_value, alarm.get())); | 84 EXPECT_TRUE(JsAlarm::Populate(*alarm_value, alarm.get())); |
85 list.push_back(alarm); | 85 list.push_back(alarm); |
86 } | 86 } |
87 return list; | 87 return list; |
88 } | 88 } |
89 | 89 |
90 // Creates up to 3 alarms using the extension API. | 90 // Creates up to 3 alarms using the extension API. |
91 void CreateAlarms(size_t num_alarms) { | 91 void CreateAlarms(size_t num_alarms) { |
92 CHECK(num_alarms <= 3); | 92 CHECK_LE(num_alarms, 3U); |
93 | 93 |
94 const char* kCreateArgs[] = { | 94 const char* const kCreateArgs[] = { |
95 "[null, {\"periodInMinutes\": 0.001}]", | 95 "[null, {\"periodInMinutes\": 0.001}]", |
96 "[\"7\", {\"periodInMinutes\": 7}]", | 96 "[\"7\", {\"periodInMinutes\": 7}]", |
97 "[\"0\", {\"delayInMinutes\": 0}]", | 97 "[\"0\", {\"delayInMinutes\": 0}]", |
98 }; | 98 }; |
99 for (size_t i = 0; i < num_alarms; ++i) { | 99 for (size_t i = 0; i < num_alarms; ++i) { |
100 scoped_ptr<base::DictionaryValue> result(RunFunctionAndReturnDictionary( | 100 scoped_ptr<base::DictionaryValue> result(RunFunctionAndReturnDictionary( |
101 new AlarmsCreateFunction(test_clock_), kCreateArgs[i])); | 101 new AlarmsCreateFunction(test_clock_), kCreateArgs[i])); |
102 EXPECT_FALSE(result.get()); | 102 EXPECT_FALSE(result.get()); |
103 } | 103 } |
104 } | 104 } |
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 } | 453 } |
454 | 454 |
455 class ExtensionAlarmsSchedulingTest : public ExtensionAlarmsTest { | 455 class ExtensionAlarmsSchedulingTest : public ExtensionAlarmsTest { |
456 void GetAlarmCallback(Alarm* alarm) { | 456 void GetAlarmCallback(Alarm* alarm) { |
457 CHECK(alarm); | 457 CHECK(alarm); |
458 const base::Time scheduled_time = | 458 const base::Time scheduled_time = |
459 base::Time::FromJsTime(alarm->js_alarm->scheduled_time); | 459 base::Time::FromJsTime(alarm->js_alarm->scheduled_time); |
460 EXPECT_EQ(scheduled_time, alarm_manager_->next_poll_time_); | 460 EXPECT_EQ(scheduled_time, alarm_manager_->next_poll_time_); |
461 } | 461 } |
462 | 462 |
463 static void RemoveAlarmCallback (bool success) { EXPECT_TRUE(success); } | 463 static void RemoveAlarmCallback(bool success) { EXPECT_TRUE(success); } |
464 static void RemoveAllAlarmsCallback () {} | 464 static void RemoveAllAlarmsCallback() {} |
| 465 |
465 public: | 466 public: |
466 // Get the time that the alarm named is scheduled to run. | 467 // Get the time that the alarm named is scheduled to run. |
467 void VerifyScheduledTime(const std::string& alarm_name) { | 468 void VerifyScheduledTime(const std::string& alarm_name) { |
468 alarm_manager_->GetAlarm(extension()->id(), alarm_name, base::Bind( | 469 alarm_manager_->GetAlarm(extension()->id(), alarm_name, base::Bind( |
469 &ExtensionAlarmsSchedulingTest::GetAlarmCallback, | 470 &ExtensionAlarmsSchedulingTest::GetAlarmCallback, |
470 base::Unretained(this))); | 471 base::Unretained(this))); |
471 } | 472 } |
472 | 473 |
473 void RemoveAlarm(const std::string& name) { | 474 void RemoveAlarm(const std::string& name) { |
474 alarm_manager_->RemoveAlarm( | 475 alarm_manager_->RemoveAlarm( |
475 extension()->id(), | 476 extension()->id(), |
476 name, | 477 name, |
477 base::Bind(&ExtensionAlarmsSchedulingTest::RemoveAlarmCallback)); | 478 base::Bind(&ExtensionAlarmsSchedulingTest::RemoveAlarmCallback)); |
478 } | 479 } |
479 | 480 |
480 void RemoveAllAlarms () { | 481 void RemoveAllAlarms() { |
481 alarm_manager_->RemoveAllAlarms(extension()->id(), base::Bind( | 482 alarm_manager_->RemoveAllAlarms(extension()->id(), base::Bind( |
482 &ExtensionAlarmsSchedulingTest::RemoveAllAlarmsCallback)); | 483 &ExtensionAlarmsSchedulingTest::RemoveAllAlarmsCallback)); |
483 } | 484 } |
484 }; | 485 }; |
485 | 486 |
486 TEST_F(ExtensionAlarmsSchedulingTest, PollScheduling) { | 487 TEST_F(ExtensionAlarmsSchedulingTest, PollScheduling) { |
487 { | 488 { |
488 CreateAlarm("[\"a\", {\"periodInMinutes\": 6}]"); | 489 CreateAlarm("[\"a\", {\"periodInMinutes\": 6}]"); |
489 CreateAlarm("[\"bb\", {\"periodInMinutes\": 8}]"); | 490 CreateAlarm("[\"bb\", {\"periodInMinutes\": 8}]"); |
490 VerifyScheduledTime("a"); | 491 VerifyScheduledTime("a"); |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
657 | 658 |
658 // The next poll should be the first poll that hasn't happened and is in-line | 659 // The next poll should be the first poll that hasn't happened and is in-line |
659 // with the original scheduling. | 660 // with the original scheduling. |
660 // Last poll was at 380 seconds; next poll should be at 480 seconds. | 661 // Last poll was at 380 seconds; next poll should be at 480 seconds. |
661 EXPECT_DOUBLE_EQ((alarm_manager_->last_poll_time_ + | 662 EXPECT_DOUBLE_EQ((alarm_manager_->last_poll_time_ + |
662 base::TimeDelta::FromSeconds(100)).ToJsTime(), | 663 base::TimeDelta::FromSeconds(100)).ToJsTime(), |
663 alarm_manager_->next_poll_time_.ToJsTime()); | 664 alarm_manager_->next_poll_time_.ToJsTime()); |
664 } | 665 } |
665 | 666 |
666 } // namespace extensions | 667 } // namespace extensions |
OLD | NEW |