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

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

Issue 2653693004: UKM Sync Observer (Closed)
Patch Set: Suppress recording and fix tests 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
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() : initialized_(false), has_passphrase_(false) {}
Alexei Svitkine (slow) 2017/02/04 00:52:23 Nit: Initialize these in their private sections si
Steven Holte 2017/02/06 21:57:54 Done.
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 bool initialized_;
Alexei Svitkine (slow) 2017/02/04 00:52:23 Add a newline before this.
Steven Holte 2017/02/06 21:57:54 Done.
51 bool has_passphrase_;
52 syncer::ModelTypeSet preferred_data_types_;
53
54 // The list of observers of the SyncService state.
55 base::ObserverList<syncer::SyncServiceObserver> observers_;
56 };
Alexei Svitkine (slow) 2017/02/04 00:52:23 DISALLOW_COPY_AND_ASSIGN();
Steven Holte 2017/02/06 21:57:54 Done.
57
58 class TestSyncDisableObserver : public SyncDisableObserver {
59 public:
60 TestSyncDisableObserver() : purged_(false), notified_(false) {}
61 ~TestSyncDisableObserver() override {}
62
63 bool ResetPurged() {
64 bool was_purged = purged_;
65 purged_ = false;
66 return was_purged;
67 }
68
69 bool ResetNotified() {
70 bool notified = notified_;
71 notified_ = false;
72 return notified;
73 }
74
75 private:
76 // SyncDisableObserver:
77 void OnSyncPrefsChanged(bool must_purge) override {
78 notified_ = true;
79 purged_ = purged_ || must_purge;
80 }
81 bool purged_;
82 bool notified_;
83 DISALLOW_COPY_AND_ASSIGN(TestSyncDisableObserver);
84 };
85
86 class SyncDisableObserverTest : public testing::Test {
87 public:
88 SyncDisableObserverTest() {}
89
90 private:
91 DISALLOW_COPY_AND_ASSIGN(SyncDisableObserverTest);
92 };
93
94 } // namespace
95
96 TEST_F(SyncDisableObserverTest, NoProfiles) {
97 TestSyncDisableObserver observer;
98 EXPECT_FALSE(observer.IsHistorySyncEnabledOnAllProfiles());
99 EXPECT_FALSE(observer.ResetNotified());
100 EXPECT_FALSE(observer.ResetPurged());
101 }
102
103 TEST_F(SyncDisableObserverTest, OneEnabled) {
104 TestSyncDisableObserver observer;
105 MockSyncService sync;
106 sync.SetStatus(false, true);
107 observer.ObserveServiceForSyncDisables(&sync);
108 EXPECT_TRUE(observer.IsHistorySyncEnabledOnAllProfiles());
109 EXPECT_TRUE(observer.ResetNotified());
110 EXPECT_FALSE(observer.ResetPurged());
111 }
112
113 TEST_F(SyncDisableObserverTest, Passphrase) {
114 TestSyncDisableObserver observer;
115 MockSyncService sync;
116 sync.SetStatus(true, true);
117 observer.ObserveServiceForSyncDisables(&sync);
118 EXPECT_FALSE(observer.IsHistorySyncEnabledOnAllProfiles());
119 EXPECT_FALSE(observer.ResetNotified());
120 EXPECT_FALSE(observer.ResetPurged());
121 }
122
123 TEST_F(SyncDisableObserverTest, HistoryDisabled) {
124 TestSyncDisableObserver observer;
125 MockSyncService sync;
126 sync.SetStatus(false, false);
127 observer.ObserveServiceForSyncDisables(&sync);
128 EXPECT_FALSE(observer.IsHistorySyncEnabledOnAllProfiles());
129 EXPECT_FALSE(observer.ResetNotified());
130 EXPECT_FALSE(observer.ResetPurged());
131 }
132
133 TEST_F(SyncDisableObserverTest, MixedProfiles1) {
134 TestSyncDisableObserver observer;
135 MockSyncService sync1;
136 sync1.SetStatus(false, false);
137 observer.ObserveServiceForSyncDisables(&sync1);
138 MockSyncService sync2;
139 sync2.SetStatus(false, true);
140 observer.ObserveServiceForSyncDisables(&sync2);
141 EXPECT_FALSE(observer.IsHistorySyncEnabledOnAllProfiles());
142 EXPECT_FALSE(observer.ResetNotified());
143 EXPECT_FALSE(observer.ResetPurged());
144 }
145
146 TEST_F(SyncDisableObserverTest, MixedProfiles2) {
147 TestSyncDisableObserver observer;
148 MockSyncService sync1;
149 sync1.SetStatus(false, true);
150 observer.ObserveServiceForSyncDisables(&sync1);
151 EXPECT_TRUE(observer.ResetNotified());
152 MockSyncService sync2;
153 sync2.SetStatus(false, false);
154 observer.ObserveServiceForSyncDisables(&sync2);
155 EXPECT_FALSE(observer.IsHistorySyncEnabledOnAllProfiles());
156 EXPECT_TRUE(observer.ResetNotified());
157 EXPECT_FALSE(observer.ResetPurged());
158 sync2.Shutdown();
159 EXPECT_TRUE(observer.IsHistorySyncEnabledOnAllProfiles());
160 EXPECT_TRUE(observer.ResetNotified());
161 EXPECT_FALSE(observer.ResetPurged());
162 }
163
164 TEST_F(SyncDisableObserverTest, TwoEnabled) {
165 TestSyncDisableObserver observer;
166 MockSyncService sync1;
167 sync1.SetStatus(false, true);
168 observer.ObserveServiceForSyncDisables(&sync1);
169 EXPECT_TRUE(observer.ResetNotified());
170 MockSyncService sync2;
171 sync2.SetStatus(false, true);
172 observer.ObserveServiceForSyncDisables(&sync2);
173 EXPECT_TRUE(observer.IsHistorySyncEnabledOnAllProfiles());
174 EXPECT_FALSE(observer.ResetNotified());
175 EXPECT_FALSE(observer.ResetPurged());
176 }
177
178 TEST_F(SyncDisableObserverTest, OneAddRemove) {
179 TestSyncDisableObserver observer;
180 MockSyncService sync;
181 observer.ObserveServiceForSyncDisables(&sync);
182 EXPECT_FALSE(observer.IsHistorySyncEnabledOnAllProfiles());
183 EXPECT_FALSE(observer.ResetNotified());
184 EXPECT_FALSE(observer.ResetPurged());
185 sync.SetStatus(false, true);
186 EXPECT_TRUE(observer.IsHistorySyncEnabledOnAllProfiles());
187 EXPECT_TRUE(observer.ResetNotified());
188 EXPECT_FALSE(observer.ResetPurged());
189 sync.Shutdown();
190 EXPECT_FALSE(observer.IsHistorySyncEnabledOnAllProfiles());
191 EXPECT_TRUE(observer.ResetNotified());
192 EXPECT_FALSE(observer.ResetPurged());
193 }
194
195 TEST_F(SyncDisableObserverTest, PurgeOnDisable) {
196 TestSyncDisableObserver observer;
197 MockSyncService sync;
198 sync.SetStatus(false, true);
199 observer.ObserveServiceForSyncDisables(&sync);
200 EXPECT_TRUE(observer.IsHistorySyncEnabledOnAllProfiles());
201 EXPECT_TRUE(observer.ResetNotified());
202 EXPECT_FALSE(observer.ResetPurged());
203 sync.SetStatus(false, false);
204 EXPECT_FALSE(observer.IsHistorySyncEnabledOnAllProfiles());
205 EXPECT_TRUE(observer.ResetNotified());
206 EXPECT_TRUE(observer.ResetPurged());
207 sync.Shutdown();
208 EXPECT_FALSE(observer.IsHistorySyncEnabledOnAllProfiles());
209 EXPECT_FALSE(observer.ResetNotified());
210 EXPECT_FALSE(observer.ResetPurged());
211 }
212
213 } // namespace ukm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698