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. 4 5 #include "chrome/browser/upgrade_detector_impl.h" 6 7 #include <vector> 8 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" 15 16 class TestUpgradeDetectorImpl : public UpgradeDetectorImpl { 17 public: 18 TestUpgradeDetectorImpl() : trigger_critical_update_call_count_(0) {} 19 virtual ~TestUpgradeDetectorImpl() {} 20 21 // Methods exposed for testing. 22 using UpgradeDetectorImpl::OnExperimentChangesDetected; 23 using UpgradeDetectorImpl::NotifyOnUpgradeWithTimePassed; 24 25 // UpgradeDetector: 26 virtual void TriggerCriticalUpdate() OVERRIDE { 27 trigger_critical_update_call_count_++; 28 } 29 30 int trigger_critical_update_call_count() const { 31 return trigger_critical_update_call_count_; 32 } 33 34 private: 35 // How many times TriggerCriticalUpdate() has been called. Expected to either 36 // be 0 or 1. 37 int trigger_critical_update_call_count_; 38 39 DISALLOW_COPY_AND_ASSIGN(TestUpgradeDetectorImpl); 40 }; 41 42 class TestUpgradeNotificationListener : public content::NotificationObserver { 43 public: 44 TestUpgradeNotificationListener() { 45 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED, 46 content::NotificationService::AllSources()); 47 } 48 virtual ~TestUpgradeNotificationListener() { 49 } 50 51 const std::vector<int>& notifications_received() const { 52 return notifications_received_; 53 } 54 55 private: 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); 61 } 62 63 // Registrar for listening to notifications. 64 content::NotificationRegistrar registrar_; 65 66 // Keeps track of the number and types of notifications that were received. 67 std::vector<int> notifications_received_; 68 69 DISALLOW_COPY_AND_ASSIGN(TestUpgradeNotificationListener); 70 }; 71 72 TEST(UpgradeDetectorImplTest, VariationsChanges) { 73 content::TestBrowserThreadBundle bundle; 74 75 TestUpgradeNotificationListener notifications_listener; 76 TestUpgradeDetectorImpl detector; 77 EXPECT_FALSE(detector.notify_upgrade()); 78 EXPECT_TRUE(notifications_listener.notifications_received().empty()); 79 80 detector.OnExperimentChangesDetected( 81 chrome_variations::VariationsService::Observer::BEST_EFFORT); 82 EXPECT_FALSE(detector.notify_upgrade()); 83 EXPECT_TRUE(notifications_listener.notifications_received().empty()); 84 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()); 91 } 92 93 TEST(UpgradeDetectorImplTest, VariationsCriticalChanges) { 94 content::TestBrowserThreadBundle bundle; 95 96 TestUpgradeNotificationListener notifications_listener; 97 TestUpgradeDetectorImpl detector; 98 EXPECT_FALSE(detector.notify_upgrade()); 99 EXPECT_TRUE(notifications_listener.notifications_received().empty()); 100 101 detector.OnExperimentChangesDetected( 102 chrome_variations::VariationsService::Observer::CRITICAL); 103 EXPECT_FALSE(detector.notify_upgrade()); 104 EXPECT_TRUE(notifications_listener.notifications_received().empty()); 105 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()); 112 } 113