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

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

Issue 2051573003: Perform alarm's period limit check while reading alarm info from StateStore. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase @tott Created 4 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
« no previous file with comments | « extensions/browser/api/alarms/alarms_api_constants.cc ('k') | extensions/extensions.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <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
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 struct {
679 bool is_unpacked;
680 double delay_minimum;
681 } test_data[] = {
682 {true, alarms_api_constants::kDevDelayMinimum},
683 {false, alarms_api_constants::kReleaseDelayMinimum},
684 };
685
686 // Test once for unpacked and once for crx extension.
687 for (size_t i = 0; i < arraysize(test_data); ++i) {
688 test_clock_->SetNow(base::Time::FromDoubleT(10));
689
690 // Mimic retrieving an alarm from StateStore.
691 std::string alarm_args =
692 "[{\"name\": \"hello\", \"scheduledTime\": 10000, "
693 "\"periodInMinutes\": 0.0001}]";
694 std::unique_ptr<base::ListValue> value =
695 base::ListValue::From(base::JSONReader::Read(alarm_args));
696 alarm_manager_->ReadFromStorage(extension()->id(), test_data[i].is_unpacked,
697 std::move(value));
698
699 // Let the alarm fire once, we will verify the next polling time afterwards.
700 alarm_manager_->GetAlarm(extension()->id(), "hello",
701 base::Bind(FrequencyTestGetAlarmsCallback, this));
702
703 // The stored alarm's "periodInMinutes" is much smaller than allowed minimum
704 // in this test (alarms_api_constants::kDevDelayMinimum or
705 // alarms_api_constants::kReleaseDelayMinimum). Make sure
706 // our next poll time corresponds to our allowed minimum and not to the
707 // StateStore specified "periodInMinutes".
708 EXPECT_GE(
709 alarm_manager_->next_poll_time_,
710 // 10s initial clock.
711 base::Time::FromJsTime(10000) +
712 // 10ms in FrequencyTestGetAlarmsCallback.
713 base::TimeDelta::FromMilliseconds(10) +
714 base::TimeDelta::FromSecondsD(test_data[i].delay_minimum * 60));
715 RemoveAlarm("hello");
716 }
717 }
718
659 // Test that scheduled alarms go off at set intervals, even if their actual 719 // Test that scheduled alarms go off at set intervals, even if their actual
660 // trigger is off. 720 // trigger is off.
661 TEST_F(ExtensionAlarmsSchedulingTest, RepeatingAlarmsScheduledPredictably) { 721 TEST_F(ExtensionAlarmsSchedulingTest, RepeatingAlarmsScheduledPredictably) {
662 test_clock_->SetNow(base::Time::FromJsTime(0)); 722 test_clock_->SetNow(base::Time::FromJsTime(0));
663 CreateAlarm("[\"a\", {\"periodInMinutes\": 2}]"); 723 CreateAlarm("[\"a\", {\"periodInMinutes\": 2}]");
664 724
665 alarm_manager_->last_poll_time_ = base::Time::FromJsTime(0); 725 alarm_manager_->last_poll_time_ = base::Time::FromJsTime(0);
666 alarm_manager_->ScheduleNextPoll(); 726 alarm_manager_->ScheduleNextPoll();
667 727
668 // We expect the first poll to happen two minutes from the start. 728 // We expect the first poll to happen two minutes from the start.
(...skipping 24 matching lines...) Expand all
693 753
694 // The next poll should be the first poll that hasn't happened and is in-line 754 // The next poll should be the first poll that hasn't happened and is in-line
695 // with the original scheduling. 755 // with the original scheduling.
696 // Last poll was at 380 seconds; next poll should be at 480 seconds. 756 // Last poll was at 380 seconds; next poll should be at 480 seconds.
697 EXPECT_DOUBLE_EQ((alarm_manager_->last_poll_time_ + 757 EXPECT_DOUBLE_EQ((alarm_manager_->last_poll_time_ +
698 base::TimeDelta::FromSeconds(100)).ToJsTime(), 758 base::TimeDelta::FromSeconds(100)).ToJsTime(),
699 alarm_manager_->next_poll_time_.ToJsTime()); 759 alarm_manager_->next_poll_time_.ToJsTime());
700 } 760 }
701 761
702 } // namespace extensions 762 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/api/alarms/alarms_api_constants.cc ('k') | extensions/extensions.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698