| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 | 68 |
| 69 test_clock_->SetNow(base::Time::FromDoubleT(10)); | 69 test_clock_->SetNow(base::Time::FromDoubleT(10)); |
| 70 } | 70 } |
| 71 | 71 |
| 72 void CreateAlarm(const std::string& args) { | 72 void CreateAlarm(const std::string& args) { |
| 73 RunFunction(new AlarmsCreateFunction(test_clock_), args); | 73 RunFunction(new AlarmsCreateFunction(test_clock_), args); |
| 74 } | 74 } |
| 75 | 75 |
| 76 // Takes a JSON result from a function and converts it to a vector of | 76 // Takes a JSON result from a function and converts it to a vector of |
| 77 // JsAlarms. | 77 // JsAlarms. |
| 78 std::vector<linked_ptr<JsAlarm>> ToAlarmList(base::ListValue* value) { | 78 std::vector<std::unique_ptr<JsAlarm>> ToAlarmList(base::ListValue* value) { |
| 79 std::vector<linked_ptr<JsAlarm>> list; | 79 std::vector<std::unique_ptr<JsAlarm>> list; |
| 80 for (size_t i = 0; i < value->GetSize(); ++i) { | 80 for (size_t i = 0; i < value->GetSize(); ++i) { |
| 81 linked_ptr<JsAlarm> alarm(new JsAlarm); | 81 std::unique_ptr<JsAlarm> alarm(new JsAlarm()); |
| 82 base::DictionaryValue* alarm_value; | 82 base::DictionaryValue* alarm_value; |
| 83 if (!value->GetDictionary(i, &alarm_value)) { | 83 if (!value->GetDictionary(i, &alarm_value)) { |
| 84 ADD_FAILURE() << "Expected a list of Alarm objects."; | 84 ADD_FAILURE() << "Expected a list of Alarm objects."; |
| 85 return list; | 85 return list; |
| 86 } | 86 } |
| 87 EXPECT_TRUE(JsAlarm::Populate(*alarm_value, alarm.get())); | 87 EXPECT_TRUE(JsAlarm::Populate(*alarm_value, alarm.get())); |
| 88 list.push_back(alarm); | 88 list.push_back(std::move(alarm)); |
| 89 } | 89 } |
| 90 return list; | 90 return list; |
| 91 } | 91 } |
| 92 | 92 |
| 93 // Creates up to 3 alarms using the extension API. | 93 // Creates up to 3 alarms using the extension API. |
| 94 void CreateAlarms(size_t num_alarms) { | 94 void CreateAlarms(size_t num_alarms) { |
| 95 CHECK_LE(num_alarms, 3U); | 95 CHECK_LE(num_alarms, 3U); |
| 96 | 96 |
| 97 const char* const kCreateArgs[] = { | 97 const char* const kCreateArgs[] = { |
| 98 "[null, {\"periodInMinutes\": 0.001}]", | 98 "[null, {\"periodInMinutes\": 0.001}]", |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 "[\"nobody\"]")); | 359 "[\"nobody\"]")); |
| 360 ASSERT_FALSE(result.get()); | 360 ASSERT_FALSE(result.get()); |
| 361 } | 361 } |
| 362 } | 362 } |
| 363 | 363 |
| 364 TEST_F(ExtensionAlarmsTest, GetAll) { | 364 TEST_F(ExtensionAlarmsTest, GetAll) { |
| 365 // Test getAll with 0 alarms. | 365 // Test getAll with 0 alarms. |
| 366 { | 366 { |
| 367 std::unique_ptr<base::ListValue> result( | 367 std::unique_ptr<base::ListValue> result( |
| 368 RunFunctionAndReturnList(new AlarmsGetAllFunction(), "[]")); | 368 RunFunctionAndReturnList(new AlarmsGetAllFunction(), "[]")); |
| 369 std::vector<linked_ptr<JsAlarm>> alarms = ToAlarmList(result.get()); | 369 std::vector<std::unique_ptr<JsAlarm>> alarms = ToAlarmList(result.get()); |
| 370 EXPECT_EQ(0u, alarms.size()); | 370 EXPECT_EQ(0u, alarms.size()); |
| 371 } | 371 } |
| 372 | 372 |
| 373 // Create 2 alarms, and make sure we can query them. | 373 // Create 2 alarms, and make sure we can query them. |
| 374 CreateAlarms(2); | 374 CreateAlarms(2); |
| 375 | 375 |
| 376 { | 376 { |
| 377 std::unique_ptr<base::ListValue> result( | 377 std::unique_ptr<base::ListValue> result( |
| 378 RunFunctionAndReturnList(new AlarmsGetAllFunction(), "[null]")); | 378 RunFunctionAndReturnList(new AlarmsGetAllFunction(), "[null]")); |
| 379 std::vector<linked_ptr<JsAlarm>> alarms = ToAlarmList(result.get()); | 379 std::vector<std::unique_ptr<JsAlarm>> alarms = ToAlarmList(result.get()); |
| 380 EXPECT_EQ(2u, alarms.size()); | 380 EXPECT_EQ(2u, alarms.size()); |
| 381 | 381 |
| 382 // Test the "7" alarm. | 382 // Test the "7" alarm. |
| 383 JsAlarm* alarm = alarms[0].get(); | 383 JsAlarm* alarm = alarms[0].get(); |
| 384 if (alarm->name != "7") | 384 if (alarm->name != "7") |
| 385 alarm = alarms[1].get(); | 385 alarm = alarms[1].get(); |
| 386 EXPECT_EQ("7", alarm->name); | 386 EXPECT_EQ("7", alarm->name); |
| 387 EXPECT_THAT(alarm->period_in_minutes, testing::Pointee(7)); | 387 EXPECT_THAT(alarm->period_in_minutes, testing::Pointee(7)); |
| 388 } | 388 } |
| 389 } | 389 } |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 754 | 754 |
| 755 // The next poll should be the first poll that hasn't happened and is in-line | 755 // The next poll should be the first poll that hasn't happened and is in-line |
| 756 // with the original scheduling. | 756 // with the original scheduling. |
| 757 // Last poll was at 380 seconds; next poll should be at 480 seconds. | 757 // Last poll was at 380 seconds; next poll should be at 480 seconds. |
| 758 EXPECT_DOUBLE_EQ((alarm_manager_->last_poll_time_ + | 758 EXPECT_DOUBLE_EQ((alarm_manager_->last_poll_time_ + |
| 759 base::TimeDelta::FromSeconds(100)).ToJsTime(), | 759 base::TimeDelta::FromSeconds(100)).ToJsTime(), |
| 760 alarm_manager_->next_poll_time_.ToJsTime()); | 760 alarm_manager_->next_poll_time_.ToJsTime()); |
| 761 } | 761 } |
| 762 | 762 |
| 763 } // namespace extensions | 763 } // namespace extensions |
| OLD | NEW |