| 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/test/simple_test_clock.h" | 10 #include "base/test/simple_test_clock.h" |
| 10 #include "base/values.h" | 11 #include "base/values.h" |
| 11 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.h" |
| 12 #include "content/public/test/mock_render_process_host.h" | 13 #include "content/public/test/mock_render_process_host.h" |
| 13 #include "extensions/browser/api/alarms/alarm_manager.h" | 14 #include "extensions/browser/api/alarms/alarm_manager.h" |
| 14 #include "extensions/browser/api/alarms/alarms_api.h" | 15 #include "extensions/browser/api/alarms/alarms_api.h" |
| 16 #include "extensions/browser/api/alarms/alarms_api_constants.h" |
| 15 #include "extensions/browser/api_test_utils.h" | 17 #include "extensions/browser/api_test_utils.h" |
| 16 #include "extensions/browser/api_unittest.h" | 18 #include "extensions/browser/api_unittest.h" |
| 17 #include "extensions/common/extension_messages.h" | 19 #include "extensions/common/extension_messages.h" |
| 18 #include "ipc/ipc_test_sink.h" | 20 #include "ipc/ipc_test_sink.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 23 |
| 22 typedef extensions::api::alarms::Alarm JsAlarm; | 24 typedef extensions::api::alarms::Alarm JsAlarm; |
| 23 | 25 |
| 24 namespace extensions { | 26 namespace extensions { |
| (...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 649 alarm_manager_->last_poll_time_ = base::Time::FromJsTime(0); | 651 alarm_manager_->last_poll_time_ = base::Time::FromJsTime(0); |
| 650 alarm_manager_->ScheduleNextPoll(); | 652 alarm_manager_->ScheduleNextPoll(); |
| 651 | 653 |
| 652 // The next poll time should be 12 seconds from now - the time at which the | 654 // The next poll time should be 12 seconds from now - the time at which the |
| 653 // first alarm should go off. | 655 // first alarm should go off. |
| 654 EXPECT_DOUBLE_EQ((alarm_manager_->last_poll_time_ + | 656 EXPECT_DOUBLE_EQ((alarm_manager_->last_poll_time_ + |
| 655 base::TimeDelta::FromSeconds(12)).ToJsTime(), | 657 base::TimeDelta::FromSeconds(12)).ToJsTime(), |
| 656 alarm_manager_->next_poll_time_.ToJsTime()); | 658 alarm_manager_->next_poll_time_.ToJsTime()); |
| 657 } | 659 } |
| 658 | 660 |
| 661 void FrequencyTestGetAlarmsCallback(ExtensionAlarmsTest* test, Alarm* alarm) { |
| 662 ASSERT_TRUE(alarm); |
| 663 EXPECT_EQ("hello", alarm->js_alarm->name); |
| 664 EXPECT_DOUBLE_EQ(10000, alarm->js_alarm->scheduled_time); |
| 665 EXPECT_THAT(alarm->js_alarm->period_in_minutes, |
| 666 testing::Pointee(testing::DoubleEq(0.0001))); |
| 667 |
| 668 test->test_clock_->Advance(base::TimeDelta::FromMilliseconds(10)); |
| 669 // Now wait for the alarm to fire. Our test delegate will quit the |
| 670 // MessageLoop when that happens. |
| 671 base::MessageLoop::current()->Run(); |
| 672 } |
| 673 |
| 674 // Tests that alarms with very small period written to storage are also |
| 675 // subjected to minimum polling interval. |
| 676 // Regression test for https://crbug.com/618540. |
| 677 TEST_F(ExtensionAlarmsSchedulingTest, PollFrequencyFromStoredAlarm) { |
| 678 test_clock_->SetNow(base::Time::FromDoubleT(10)); |
| 679 |
| 680 // Mimic retrieving an alarm from StateStore. |
| 681 std::string alarm_args = |
| 682 "[{\"name\": \"hello\", \"scheduledTime\": 10000, " |
| 683 "\"periodInMinutes\": 0.0001}]"; |
| 684 std::unique_ptr<base::ListValue> value = |
| 685 base::ListValue::From(base::JSONReader::Read(alarm_args)); |
| 686 alarm_manager_->ReadFromStorage(extension()->id(), std::move(value)); |
| 687 |
| 688 // Let the alarm fire once, we will verify the next polling time afterwards. |
| 689 alarm_manager_->GetAlarm(extension()->id(), "hello", |
| 690 base::Bind(FrequencyTestGetAlarmsCallback, this)); |
| 691 |
| 692 // The stored alarm's "periodInMinutes" is much smaller than allowed minimum |
| 693 // in this test (alarms_api_constants::kDevDelayMinimum). Make sure |
| 694 // our next poll time corresponds to our allowed minimum and not to the |
| 695 // StateStore specified "periodInMinutes". |
| 696 EXPECT_GE(alarm_manager_->next_poll_time_, |
| 697 // 10s initial clock. |
| 698 base::Time::FromJsTime(10000) + |
| 699 // 10ms in FrequencyTestGetAlarmsCallback. |
| 700 base::TimeDelta::FromMilliseconds(10) + |
| 701 base::TimeDelta::FromSecondsD( |
| 702 alarms_api_constants::kDevDelayMinimum * 60)); |
| 703 } |
| 704 |
| 659 // Test that scheduled alarms go off at set intervals, even if their actual | 705 // Test that scheduled alarms go off at set intervals, even if their actual |
| 660 // trigger is off. | 706 // trigger is off. |
| 661 TEST_F(ExtensionAlarmsSchedulingTest, RepeatingAlarmsScheduledPredictably) { | 707 TEST_F(ExtensionAlarmsSchedulingTest, RepeatingAlarmsScheduledPredictably) { |
| 662 test_clock_->SetNow(base::Time::FromJsTime(0)); | 708 test_clock_->SetNow(base::Time::FromJsTime(0)); |
| 663 CreateAlarm("[\"a\", {\"periodInMinutes\": 2}]"); | 709 CreateAlarm("[\"a\", {\"periodInMinutes\": 2}]"); |
| 664 | 710 |
| 665 alarm_manager_->last_poll_time_ = base::Time::FromJsTime(0); | 711 alarm_manager_->last_poll_time_ = base::Time::FromJsTime(0); |
| 666 alarm_manager_->ScheduleNextPoll(); | 712 alarm_manager_->ScheduleNextPoll(); |
| 667 | 713 |
| 668 // We expect the first poll to happen two minutes from the start. | 714 // We expect the first poll to happen two minutes from the start. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 693 | 739 |
| 694 // The next poll should be the first poll that hasn't happened and is in-line | 740 // The next poll should be the first poll that hasn't happened and is in-line |
| 695 // with the original scheduling. | 741 // with the original scheduling. |
| 696 // Last poll was at 380 seconds; next poll should be at 480 seconds. | 742 // Last poll was at 380 seconds; next poll should be at 480 seconds. |
| 697 EXPECT_DOUBLE_EQ((alarm_manager_->last_poll_time_ + | 743 EXPECT_DOUBLE_EQ((alarm_manager_->last_poll_time_ + |
| 698 base::TimeDelta::FromSeconds(100)).ToJsTime(), | 744 base::TimeDelta::FromSeconds(100)).ToJsTime(), |
| 699 alarm_manager_->next_poll_time_.ToJsTime()); | 745 alarm_manager_->next_poll_time_.ToJsTime()); |
| 700 } | 746 } |
| 701 | 747 |
| 702 } // namespace extensions | 748 } // namespace extensions |
| OLD | NEW |