Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(548)

Side by Side Diff: components/ukm/observers/sync_disable_observer_unittest.cc

Issue 2653693004: UKM Sync Observer (Closed)
Patch Set: MSVC struct initializer Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/ukm/observers/sync_disable_observer.cc ('k') | components/ukm/test_ukm_service.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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 "components/ukm/observers/sync_disable_observer.h"
6
7 #include "base/observer_list.h"
8 #include "components/sync/driver/fake_sync_service.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace ukm {
12
13 namespace {
14
15 class MockSyncService : public syncer::FakeSyncService {
16 public:
17 MockSyncService() {}
18 ~MockSyncService() override { Shutdown(); }
19
20 void SetStatus(bool has_passphrase, bool enabled) {
21 initialized_ = true;
22 has_passphrase_ = has_passphrase;
23 preferred_data_types_ =
24 enabled ? syncer::ModelTypeSet(syncer::HISTORY_DELETE_DIRECTIVES)
25 : syncer::ModelTypeSet();
26 for (auto& observer : observers_) {
27 observer.OnStateChanged(this);
28 }
29 }
30
31 void Shutdown() {
32 for (auto& observer : observers_) {
33 observer.OnSyncShutdown(this);
34 }
35 }
36
37 private:
38 // syncer::FakeSyncService:
39 void AddObserver(syncer::SyncServiceObserver* observer) override {
40 observers_.AddObserver(observer);
41 }
42 void RemoveObserver(syncer::SyncServiceObserver* observer) override {
43 observers_.RemoveObserver(observer);
44 }
45 bool IsEngineInitialized() const override { return initialized_; }
46 bool IsUsingSecondaryPassphrase() const override { return has_passphrase_; }
47 syncer::ModelTypeSet GetPreferredDataTypes() const override {
48 return preferred_data_types_;
49 }
50
51 bool initialized_ = false;
52 bool has_passphrase_ = false;
53 syncer::ModelTypeSet preferred_data_types_;
54
55 // The list of observers of the SyncService state.
56 base::ObserverList<syncer::SyncServiceObserver> observers_;
57
58 DISALLOW_COPY_AND_ASSIGN(MockSyncService);
59 };
60
61 class TestSyncDisableObserver : public SyncDisableObserver {
62 public:
63 TestSyncDisableObserver() : purged_(false), notified_(false) {}
64 ~TestSyncDisableObserver() override {}
65
66 bool ResetPurged() {
67 bool was_purged = purged_;
68 purged_ = false;
69 return was_purged;
70 }
71
72 bool ResetNotified() {
73 bool notified = notified_;
74 notified_ = false;
75 return notified;
76 }
77
78 private:
79 // SyncDisableObserver:
80 void OnSyncPrefsChanged(bool must_purge) override {
81 notified_ = true;
82 purged_ = purged_ || must_purge;
83 }
84 bool purged_;
85 bool notified_;
86 DISALLOW_COPY_AND_ASSIGN(TestSyncDisableObserver);
87 };
88
89 class SyncDisableObserverTest : public testing::Test {
90 public:
91 SyncDisableObserverTest() {}
92
93 private:
94 DISALLOW_COPY_AND_ASSIGN(SyncDisableObserverTest);
95 };
96
97 } // namespace
98
99 TEST_F(SyncDisableObserverTest, NoProfiles) {
100 TestSyncDisableObserver observer;
101 EXPECT_FALSE(observer.IsHistorySyncEnabledOnAllProfiles());
102 EXPECT_FALSE(observer.ResetNotified());
103 EXPECT_FALSE(observer.ResetPurged());
104 }
105
106 TEST_F(SyncDisableObserverTest, OneEnabled) {
107 TestSyncDisableObserver observer;
108 MockSyncService sync;
109 sync.SetStatus(false, true);
110 observer.ObserveServiceForSyncDisables(&sync);
111 EXPECT_TRUE(observer.IsHistorySyncEnabledOnAllProfiles());
112 EXPECT_TRUE(observer.ResetNotified());
113 EXPECT_FALSE(observer.ResetPurged());
114 }
115
116 TEST_F(SyncDisableObserverTest, Passphrase) {
117 TestSyncDisableObserver observer;
118 MockSyncService sync;
119 sync.SetStatus(true, true);
120 observer.ObserveServiceForSyncDisables(&sync);
121 EXPECT_FALSE(observer.IsHistorySyncEnabledOnAllProfiles());
122 EXPECT_FALSE(observer.ResetNotified());
123 EXPECT_FALSE(observer.ResetPurged());
124 }
125
126 TEST_F(SyncDisableObserverTest, HistoryDisabled) {
127 TestSyncDisableObserver observer;
128 MockSyncService sync;
129 sync.SetStatus(false, false);
130 observer.ObserveServiceForSyncDisables(&sync);
131 EXPECT_FALSE(observer.IsHistorySyncEnabledOnAllProfiles());
132 EXPECT_FALSE(observer.ResetNotified());
133 EXPECT_FALSE(observer.ResetPurged());
134 }
135
136 TEST_F(SyncDisableObserverTest, MixedProfiles1) {
137 TestSyncDisableObserver observer;
138 MockSyncService sync1;
139 sync1.SetStatus(false, false);
140 observer.ObserveServiceForSyncDisables(&sync1);
141 MockSyncService sync2;
142 sync2.SetStatus(false, true);
143 observer.ObserveServiceForSyncDisables(&sync2);
144 EXPECT_FALSE(observer.IsHistorySyncEnabledOnAllProfiles());
145 EXPECT_FALSE(observer.ResetNotified());
146 EXPECT_FALSE(observer.ResetPurged());
147 }
148
149 TEST_F(SyncDisableObserverTest, MixedProfiles2) {
150 TestSyncDisableObserver observer;
151 MockSyncService sync1;
152 sync1.SetStatus(false, true);
153 observer.ObserveServiceForSyncDisables(&sync1);
154 EXPECT_TRUE(observer.ResetNotified());
155 MockSyncService sync2;
156 sync2.SetStatus(false, false);
157 observer.ObserveServiceForSyncDisables(&sync2);
158 EXPECT_FALSE(observer.IsHistorySyncEnabledOnAllProfiles());
159 EXPECT_TRUE(observer.ResetNotified());
160 EXPECT_FALSE(observer.ResetPurged());
161 sync2.Shutdown();
162 EXPECT_TRUE(observer.IsHistorySyncEnabledOnAllProfiles());
163 EXPECT_TRUE(observer.ResetNotified());
164 EXPECT_FALSE(observer.ResetPurged());
165 }
166
167 TEST_F(SyncDisableObserverTest, TwoEnabled) {
168 TestSyncDisableObserver observer;
169 MockSyncService sync1;
170 sync1.SetStatus(false, true);
171 observer.ObserveServiceForSyncDisables(&sync1);
172 EXPECT_TRUE(observer.ResetNotified());
173 MockSyncService sync2;
174 sync2.SetStatus(false, true);
175 observer.ObserveServiceForSyncDisables(&sync2);
176 EXPECT_TRUE(observer.IsHistorySyncEnabledOnAllProfiles());
177 EXPECT_FALSE(observer.ResetNotified());
178 EXPECT_FALSE(observer.ResetPurged());
179 }
180
181 TEST_F(SyncDisableObserverTest, OneAddRemove) {
182 TestSyncDisableObserver observer;
183 MockSyncService sync;
184 observer.ObserveServiceForSyncDisables(&sync);
185 EXPECT_FALSE(observer.IsHistorySyncEnabledOnAllProfiles());
186 EXPECT_FALSE(observer.ResetNotified());
187 EXPECT_FALSE(observer.ResetPurged());
188 sync.SetStatus(false, true);
189 EXPECT_TRUE(observer.IsHistorySyncEnabledOnAllProfiles());
190 EXPECT_TRUE(observer.ResetNotified());
191 EXPECT_FALSE(observer.ResetPurged());
192 sync.Shutdown();
193 EXPECT_FALSE(observer.IsHistorySyncEnabledOnAllProfiles());
194 EXPECT_TRUE(observer.ResetNotified());
195 EXPECT_FALSE(observer.ResetPurged());
196 }
197
198 TEST_F(SyncDisableObserverTest, PurgeOnDisable) {
199 TestSyncDisableObserver observer;
200 MockSyncService sync;
201 sync.SetStatus(false, true);
202 observer.ObserveServiceForSyncDisables(&sync);
203 EXPECT_TRUE(observer.IsHistorySyncEnabledOnAllProfiles());
204 EXPECT_TRUE(observer.ResetNotified());
205 EXPECT_FALSE(observer.ResetPurged());
206 sync.SetStatus(false, false);
207 EXPECT_FALSE(observer.IsHistorySyncEnabledOnAllProfiles());
208 EXPECT_TRUE(observer.ResetNotified());
209 EXPECT_TRUE(observer.ResetPurged());
210 sync.Shutdown();
211 EXPECT_FALSE(observer.IsHistorySyncEnabledOnAllProfiles());
212 EXPECT_FALSE(observer.ResetNotified());
213 EXPECT_FALSE(observer.ResetPurged());
214 }
215
216 } // namespace ukm
OLDNEW
« no previous file with comments | « components/ukm/observers/sync_disable_observer.cc ('k') | components/ukm/test_ukm_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698