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