1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/upgrade_detector_impl.h"
9 #include "content/public/browser/notification_details.h"
10 #include "content/public/browser/notification_observer.h"
11 #include "content/public/browser/notification_registrar.h"
12 #include "content/public/browser/notification_service.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 class TestUpgradeDetectorImpl
: public UpgradeDetectorImpl
{
18 TestUpgradeDetectorImpl() : trigger_critical_update_call_count_(0) {}
19 ~TestUpgradeDetectorImpl() override
{}
21 // Methods exposed for testing.
22 using UpgradeDetectorImpl::OnExperimentChangesDetected
;
23 using UpgradeDetectorImpl::NotifyOnUpgradeWithTimePassed
;
26 void TriggerCriticalUpdate() override
{
27 trigger_critical_update_call_count_
++;
30 int trigger_critical_update_call_count() const {
31 return trigger_critical_update_call_count_
;
35 // How many times TriggerCriticalUpdate() has been called. Expected to either
37 int trigger_critical_update_call_count_
;
39 DISALLOW_COPY_AND_ASSIGN(TestUpgradeDetectorImpl
);
42 class TestUpgradeNotificationListener
: public content::NotificationObserver
{
44 TestUpgradeNotificationListener() {
45 registrar_
.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED
,
46 content::NotificationService::AllSources());
48 ~TestUpgradeNotificationListener() override
{}
50 const std::vector
<int>& notifications_received() const {
51 return notifications_received_
;
55 // content::NotificationObserver:
56 void Observe(int type
,
57 const content::NotificationSource
& source
,
58 const content::NotificationDetails
& details
) override
{
59 notifications_received_
.push_back(type
);
62 // Registrar for listening to notifications.
63 content::NotificationRegistrar registrar_
;
65 // Keeps track of the number and types of notifications that were received.
66 std::vector
<int> notifications_received_
;
68 DISALLOW_COPY_AND_ASSIGN(TestUpgradeNotificationListener
);
71 TEST(UpgradeDetectorImplTest
, VariationsChanges
) {
72 content::TestBrowserThreadBundle bundle
;
74 TestUpgradeNotificationListener notifications_listener
;
75 TestUpgradeDetectorImpl detector
;
76 EXPECT_FALSE(detector
.notify_upgrade());
77 EXPECT_TRUE(notifications_listener
.notifications_received().empty());
79 detector
.OnExperimentChangesDetected(
80 variations::VariationsService::Observer::BEST_EFFORT
);
81 EXPECT_FALSE(detector
.notify_upgrade());
82 EXPECT_TRUE(notifications_listener
.notifications_received().empty());
84 detector
.NotifyOnUpgradeWithTimePassed(base::TimeDelta::FromDays(30));
85 EXPECT_TRUE(detector
.notify_upgrade());
86 ASSERT_EQ(1U, notifications_listener
.notifications_received().size());
87 EXPECT_EQ(chrome::NOTIFICATION_UPGRADE_RECOMMENDED
,
88 notifications_listener
.notifications_received().front());
89 EXPECT_EQ(0, detector
.trigger_critical_update_call_count());
92 TEST(UpgradeDetectorImplTest
, VariationsCriticalChanges
) {
93 content::TestBrowserThreadBundle bundle
;
95 TestUpgradeNotificationListener notifications_listener
;
96 TestUpgradeDetectorImpl detector
;
97 EXPECT_FALSE(detector
.notify_upgrade());
98 EXPECT_TRUE(notifications_listener
.notifications_received().empty());
100 detector
.OnExperimentChangesDetected(
101 variations::VariationsService::Observer::CRITICAL
);
102 EXPECT_FALSE(detector
.notify_upgrade());
103 EXPECT_TRUE(notifications_listener
.notifications_received().empty());
105 detector
.NotifyOnUpgradeWithTimePassed(base::TimeDelta::FromDays(30));
106 EXPECT_TRUE(detector
.notify_upgrade());
107 ASSERT_EQ(1U, notifications_listener
.notifications_received().size());
108 EXPECT_EQ(chrome::NOTIFICATION_UPGRADE_RECOMMENDED
,
109 notifications_listener
.notifications_received().front());
110 EXPECT_EQ(1, detector
.trigger_critical_update_call_count());