| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/upgrade_detector_impl.h" | 5 #include "chrome/browser/upgrade_detector_impl.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "chrome/browser/upgrade_observer.h" |
| 10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/notification_details.h" | |
| 12 #include "content/public/browser/notification_observer.h" | |
| 13 #include "content/public/browser/notification_registrar.h" | |
| 14 #include "content/public/browser/notification_service.h" | |
| 15 #include "content/public/test/test_browser_thread_bundle.h" | 12 #include "content/public/test/test_browser_thread_bundle.h" |
| 16 #include "content/public/test/test_utils.h" | 13 #include "content/public/test/test_utils.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 15 |
| 19 class TestUpgradeDetectorImpl : public UpgradeDetectorImpl { | 16 class TestUpgradeDetectorImpl : public UpgradeDetectorImpl { |
| 20 public: | 17 public: |
| 21 TestUpgradeDetectorImpl() : trigger_critical_update_call_count_(0) {} | 18 TestUpgradeDetectorImpl() : trigger_critical_update_call_count_(0) {} |
| 22 ~TestUpgradeDetectorImpl() override {} | 19 ~TestUpgradeDetectorImpl() override {} |
| 23 | 20 |
| 24 // Methods exposed for testing. | 21 // Methods exposed for testing. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 35 } | 32 } |
| 36 | 33 |
| 37 private: | 34 private: |
| 38 // How many times TriggerCriticalUpdate() has been called. Expected to either | 35 // How many times TriggerCriticalUpdate() has been called. Expected to either |
| 39 // be 0 or 1. | 36 // be 0 or 1. |
| 40 int trigger_critical_update_call_count_; | 37 int trigger_critical_update_call_count_; |
| 41 | 38 |
| 42 DISALLOW_COPY_AND_ASSIGN(TestUpgradeDetectorImpl); | 39 DISALLOW_COPY_AND_ASSIGN(TestUpgradeDetectorImpl); |
| 43 }; | 40 }; |
| 44 | 41 |
| 45 class TestUpgradeNotificationListener : public content::NotificationObserver { | 42 class TestUpgradeNotificationListener : public UpgradeObserver { |
| 46 public: | 43 public: |
| 47 TestUpgradeNotificationListener() { | 44 explicit TestUpgradeNotificationListener(UpgradeDetector* detector) |
| 48 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED, | 45 : notifications_count_(0), detector_(detector) { |
| 49 content::NotificationService::AllSources()); | 46 detector_->AddObserver(this); |
| 50 } | 47 } |
| 51 ~TestUpgradeNotificationListener() override {} | 48 ~TestUpgradeNotificationListener() override { |
| 52 | 49 if (detector_) |
| 53 const std::vector<int>& notifications_received() const { | 50 detector_->RemoveObserver(this); |
| 54 return notifications_received_; | |
| 55 } | 51 } |
| 56 | 52 |
| 53 int get_notifications_count() const { return notifications_count_; } |
| 54 |
| 57 private: | 55 private: |
| 58 // content::NotificationObserver: | 56 // UpgradeObserver implementation. |
| 59 void Observe(int type, | 57 void OnUpgradeRecommended() override { notifications_count_ += 1; } |
| 60 const content::NotificationSource& source, | |
| 61 const content::NotificationDetails& details) override { | |
| 62 notifications_received_.push_back(type); | |
| 63 } | |
| 64 | 58 |
| 65 // Registrar for listening to notifications. | 59 // Keeps track of the number of upgrade recommended notifications that were |
| 66 content::NotificationRegistrar registrar_; | 60 // received. |
| 61 int notifications_count_; |
| 67 | 62 |
| 68 // Keeps track of the number and types of notifications that were received. | 63 UpgradeDetector* detector_; |
| 69 std::vector<int> notifications_received_; | |
| 70 | 64 |
| 71 DISALLOW_COPY_AND_ASSIGN(TestUpgradeNotificationListener); | 65 DISALLOW_COPY_AND_ASSIGN(TestUpgradeNotificationListener); |
| 72 }; | 66 }; |
| 73 | 67 |
| 74 TEST(UpgradeDetectorImplTest, VariationsChanges) { | 68 TEST(UpgradeDetectorImplTest, VariationsChanges) { |
| 75 content::TestBrowserThreadBundle bundle; | 69 content::TestBrowserThreadBundle bundle; |
| 76 | 70 |
| 77 TestUpgradeNotificationListener notifications_listener; | |
| 78 TestUpgradeDetectorImpl detector; | 71 TestUpgradeDetectorImpl detector; |
| 72 TestUpgradeNotificationListener notifications_listener(&detector); |
| 79 EXPECT_FALSE(detector.notify_upgrade()); | 73 EXPECT_FALSE(detector.notify_upgrade()); |
| 80 EXPECT_TRUE(notifications_listener.notifications_received().empty()); | 74 EXPECT_EQ(0, notifications_listener.get_notifications_count()); |
| 81 | 75 |
| 82 detector.OnExperimentChangesDetected( | 76 detector.OnExperimentChangesDetected( |
| 83 variations::VariationsService::Observer::BEST_EFFORT); | 77 variations::VariationsService::Observer::BEST_EFFORT); |
| 84 EXPECT_FALSE(detector.notify_upgrade()); | 78 EXPECT_FALSE(detector.notify_upgrade()); |
| 85 EXPECT_TRUE(notifications_listener.notifications_received().empty()); | 79 EXPECT_EQ(0, notifications_listener.get_notifications_count()); |
| 86 | 80 |
| 87 detector.NotifyOnUpgradeWithTimePassed(base::TimeDelta::FromDays(30)); | 81 detector.NotifyOnUpgradeWithTimePassed(base::TimeDelta::FromDays(30)); |
| 88 EXPECT_TRUE(detector.notify_upgrade()); | 82 EXPECT_TRUE(detector.notify_upgrade()); |
| 89 ASSERT_EQ(1U, notifications_listener.notifications_received().size()); | 83 EXPECT_EQ(1, notifications_listener.get_notifications_count()); |
| 90 EXPECT_EQ(chrome::NOTIFICATION_UPGRADE_RECOMMENDED, | |
| 91 notifications_listener.notifications_received().front()); | |
| 92 EXPECT_EQ(0, detector.trigger_critical_update_call_count()); | 84 EXPECT_EQ(0, detector.trigger_critical_update_call_count()); |
| 93 | 85 |
| 94 // Execute tasks sent to FILE thread by |detector| referencing it | 86 // Execute tasks sent to FILE thread by |detector| referencing it |
| 95 // while it's still in scope. | 87 // while it's still in scope. |
| 96 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | 88 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); |
| 97 } | 89 } |
| 98 | 90 |
| 99 TEST(UpgradeDetectorImplTest, VariationsCriticalChanges) { | 91 TEST(UpgradeDetectorImplTest, VariationsCriticalChanges) { |
| 100 content::TestBrowserThreadBundle bundle; | 92 content::TestBrowserThreadBundle bundle; |
| 101 | 93 |
| 102 TestUpgradeNotificationListener notifications_listener; | |
| 103 TestUpgradeDetectorImpl detector; | 94 TestUpgradeDetectorImpl detector; |
| 95 TestUpgradeNotificationListener notifications_listener(&detector); |
| 104 EXPECT_FALSE(detector.notify_upgrade()); | 96 EXPECT_FALSE(detector.notify_upgrade()); |
| 105 EXPECT_TRUE(notifications_listener.notifications_received().empty()); | 97 EXPECT_EQ(0, notifications_listener.get_notifications_count()); |
| 106 | 98 |
| 107 detector.OnExperimentChangesDetected( | 99 detector.OnExperimentChangesDetected( |
| 108 variations::VariationsService::Observer::CRITICAL); | 100 variations::VariationsService::Observer::CRITICAL); |
| 109 EXPECT_FALSE(detector.notify_upgrade()); | 101 EXPECT_FALSE(detector.notify_upgrade()); |
| 110 EXPECT_TRUE(notifications_listener.notifications_received().empty()); | 102 EXPECT_EQ(0, notifications_listener.get_notifications_count()); |
| 111 | 103 |
| 112 detector.NotifyOnUpgradeWithTimePassed(base::TimeDelta::FromDays(30)); | 104 detector.NotifyOnUpgradeWithTimePassed(base::TimeDelta::FromDays(30)); |
| 113 EXPECT_TRUE(detector.notify_upgrade()); | 105 EXPECT_TRUE(detector.notify_upgrade()); |
| 114 ASSERT_EQ(1U, notifications_listener.notifications_received().size()); | 106 EXPECT_EQ(1, notifications_listener.get_notifications_count()); |
| 115 EXPECT_EQ(chrome::NOTIFICATION_UPGRADE_RECOMMENDED, | |
| 116 notifications_listener.notifications_received().front()); | |
| 117 EXPECT_EQ(1, detector.trigger_critical_update_call_count()); | 107 EXPECT_EQ(1, detector.trigger_critical_update_call_count()); |
| 118 | 108 |
| 119 // Execute tasks sent to FILE thread by |detector| referencing it | 109 // Execute tasks sent to FILE thread by |detector| referencing it |
| 120 // while it's still in scope. | 110 // while it's still in scope. |
| 121 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | 111 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); |
| 122 } | 112 } |
| OLD | NEW |