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 virtual ~TestUpgradeDetectorImpl() {}
21 // Methods exposed for testing.
22 using UpgradeDetectorImpl::OnExperimentChangesDetected
;
23 using UpgradeDetectorImpl::NotifyOnUpgradeWithTimePassed
;
26 virtual 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 virtual ~TestUpgradeNotificationListener() {
51 const std::vector
<int>& notifications_received() const {
52 return notifications_received_
;
56 // content::NotificationObserver:
57 virtual void Observe(int type
,
58 const content::NotificationSource
& source
,
59 const content::NotificationDetails
& details
) OVERRIDE
{
60 notifications_received_
.push_back(type
);
63 // Registrar for listening to notifications.
64 content::NotificationRegistrar registrar_
;
66 // Keeps track of the number and types of notifications that were received.
67 std::vector
<int> notifications_received_
;
69 DISALLOW_COPY_AND_ASSIGN(TestUpgradeNotificationListener
);
72 TEST(UpgradeDetectorImplTest
, VariationsChanges
) {
73 content::TestBrowserThreadBundle bundle
;
75 TestUpgradeNotificationListener notifications_listener
;
76 TestUpgradeDetectorImpl detector
;
77 EXPECT_FALSE(detector
.notify_upgrade());
78 EXPECT_TRUE(notifications_listener
.notifications_received().empty());
80 detector
.OnExperimentChangesDetected(
81 chrome_variations::VariationsService::Observer::BEST_EFFORT
);
82 EXPECT_FALSE(detector
.notify_upgrade());
83 EXPECT_TRUE(notifications_listener
.notifications_received().empty());
85 detector
.NotifyOnUpgradeWithTimePassed(base::TimeDelta::FromDays(30));
86 EXPECT_TRUE(detector
.notify_upgrade());
87 ASSERT_EQ(1U, notifications_listener
.notifications_received().size());
88 EXPECT_EQ(chrome::NOTIFICATION_UPGRADE_RECOMMENDED
,
89 notifications_listener
.notifications_received().front());
90 EXPECT_EQ(0, detector
.trigger_critical_update_call_count());
93 TEST(UpgradeDetectorImplTest
, VariationsCriticalChanges
) {
94 content::TestBrowserThreadBundle bundle
;
96 TestUpgradeNotificationListener notifications_listener
;
97 TestUpgradeDetectorImpl detector
;
98 EXPECT_FALSE(detector
.notify_upgrade());
99 EXPECT_TRUE(notifications_listener
.notifications_received().empty());
101 detector
.OnExperimentChangesDetected(
102 chrome_variations::VariationsService::Observer::CRITICAL
);
103 EXPECT_FALSE(detector
.notify_upgrade());
104 EXPECT_TRUE(notifications_listener
.notifications_received().empty());
106 detector
.NotifyOnUpgradeWithTimePassed(base::TimeDelta::FromDays(30));
107 EXPECT_TRUE(detector
.notify_upgrade());
108 ASSERT_EQ(1U, notifications_listener
.notifications_received().size());
109 EXPECT_EQ(chrome::NOTIFICATION_UPGRADE_RECOMMENDED
,
110 notifications_listener
.notifications_received().front());
111 EXPECT_EQ(1, detector
.trigger_critical_update_call_count());