Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / upgrade_detector_impl_unittest.cc
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   ~TestUpgradeDetectorImpl() override {}
20
21   // Methods exposed for testing.
22   using UpgradeDetectorImpl::OnExperimentChangesDetected;
23   using UpgradeDetectorImpl::NotifyOnUpgradeWithTimePassed;
24
25   // UpgradeDetector:
26   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   ~TestUpgradeNotificationListener() override {}
49
50   const std::vector<int>& notifications_received() const {
51     return notifications_received_;
52   }
53
54  private:
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);
60   }
61
62   // Registrar for listening to notifications.
63   content::NotificationRegistrar registrar_;
64
65   // Keeps track of the number and types of notifications that were received.
66   std::vector<int> notifications_received_;
67
68   DISALLOW_COPY_AND_ASSIGN(TestUpgradeNotificationListener);
69 };
70
71 TEST(UpgradeDetectorImplTest, VariationsChanges) {
72   content::TestBrowserThreadBundle bundle;
73
74   TestUpgradeNotificationListener notifications_listener;
75   TestUpgradeDetectorImpl detector;
76   EXPECT_FALSE(detector.notify_upgrade());
77   EXPECT_TRUE(notifications_listener.notifications_received().empty());
78
79   detector.OnExperimentChangesDetected(
80       chrome_variations::VariationsService::Observer::BEST_EFFORT);
81   EXPECT_FALSE(detector.notify_upgrade());
82   EXPECT_TRUE(notifications_listener.notifications_received().empty());
83
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());
90 }
91
92 TEST(UpgradeDetectorImplTest, VariationsCriticalChanges) {
93   content::TestBrowserThreadBundle bundle;
94
95   TestUpgradeNotificationListener notifications_listener;
96   TestUpgradeDetectorImpl detector;
97   EXPECT_FALSE(detector.notify_upgrade());
98   EXPECT_TRUE(notifications_listener.notifications_received().empty());
99
100   detector.OnExperimentChangesDetected(
101       chrome_variations::VariationsService::Observer::CRITICAL);
102   EXPECT_FALSE(detector.notify_upgrade());
103   EXPECT_TRUE(notifications_listener.notifications_received().empty());
104
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());
111 }