File: sync_backend_registrar_unittest.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (274 lines) | stat: -rw-r--r-- 10,408 bytes parent folder | download
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright 2012 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 "components/sync/engine/sync_backend_registrar.h"

#include <memory>

#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread.h"
#include "components/sync/engine/browser_thread_model_worker.h"
#include "components/sync/engine/passive_model_worker.h"
#include "components/sync/model/change_processor_mock.h"
#include "components/sync/syncable/test_user_share.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace syncer {

namespace {

using ::testing::_;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::StrictMock;

class SyncBackendRegistrarTest : public testing::Test {
 public:
  SyncBackendRegistrarTest()
      : db_thread_("DBThreadForTest"),
        file_thread_("FileThreadForTest"),
        sync_thread_("SyncThreadForTest") {}

  void SetUp() override {
    db_thread_.StartAndWaitForTesting();
    file_thread_.StartAndWaitForTesting();
    sync_thread_.StartAndWaitForTesting();
    test_user_share_.SetUp();
    registrar_ = base::MakeUnique<SyncBackendRegistrar>(
        "test", base::Bind(&SyncBackendRegistrarTest::CreateModelWorkerForGroup,
                           base::Unretained(this)));
  }

  void TearDown() override {
    registrar_->RequestWorkerStopOnUIThread();
    test_user_share_.TearDown();
    sync_thread_.task_runner()->DeleteSoon(FROM_HERE, registrar_.release());
    sync_thread_.FlushForTesting();
  }

  void TriggerChanges(ModelType type) {
    registrar_->OnChangesApplied(type, 0, nullptr, ImmutableChangeRecordList());
    registrar_->OnChangesComplete(type);
  }

  void ExpectRoutingInfo(const ModelSafeRoutingInfo& expected_routing_info) {
    ModelSafeRoutingInfo actual_routing_info;
    registrar_->GetModelSafeRoutingInfo(&actual_routing_info);
    EXPECT_EQ(expected_routing_info, actual_routing_info);
  }

  void ExpectHasProcessorsForTypes(ModelTypeSet types) {
    for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) {
      ModelType model_type = ModelTypeFromInt(i);
      EXPECT_EQ(types.Has(model_type),
                registrar_->IsTypeActivatedForTest(model_type));
    }
  }

  size_t GetWorkersSize() {
    std::vector<scoped_refptr<ModelSafeWorker>> workers;
    registrar_->GetWorkers(&workers);
    return workers.size();
  }

  // Part of the ActivateDeactivateNonUIDataType test below.
  void TestNonUIDataTypeActivationAsync(ChangeProcessor* processor,
                                        base::WaitableEvent* done) {
    registrar_->ActivateDataType(AUTOFILL, GROUP_DB, processor, user_share());
    ExpectRoutingInfo({{AUTOFILL, GROUP_DB}});
    ExpectHasProcessorsForTypes(ModelTypeSet(AUTOFILL));
    TriggerChanges(AUTOFILL);
    done->Signal();
  }

  SyncBackendRegistrar* registrar() { return registrar_.get(); }
  UserShare* user_share() { return test_user_share_.user_share(); }
  scoped_refptr<base::SingleThreadTaskRunner> db_task_runner() {
    return db_thread_.task_runner();
  }

 private:
  scoped_refptr<ModelSafeWorker> CreateModelWorkerForGroup(
      ModelSafeGroup group) {
    switch (group) {
      case GROUP_UI:
        return new BrowserThreadModelWorker(message_loop_.task_runner(), group);
      case GROUP_DB:
        return new BrowserThreadModelWorker(db_thread_.task_runner(), group);
      case GROUP_FILE:
        return new BrowserThreadModelWorker(file_thread_.task_runner(), group);
      case GROUP_PASSIVE:
        return new PassiveModelWorker();
      default:
        return nullptr;
    }
  }

  base::MessageLoop message_loop_;
  base::Thread db_thread_;
  base::Thread file_thread_;
  base::Thread sync_thread_;

  TestUserShare test_user_share_;
  std::unique_ptr<SyncBackendRegistrar> registrar_;
};

TEST_F(SyncBackendRegistrarTest, ConstructorEmpty) {
  registrar()->SetInitialTypes(ModelTypeSet());
  EXPECT_FALSE(registrar()->IsNigoriEnabled());
  EXPECT_EQ(4u, GetWorkersSize());
  ExpectRoutingInfo(ModelSafeRoutingInfo());
  ExpectHasProcessorsForTypes(ModelTypeSet());
}

TEST_F(SyncBackendRegistrarTest, ConstructorNonEmpty) {
  registrar()->RegisterNonBlockingType(BOOKMARKS);
  registrar()->SetInitialTypes(ModelTypeSet(BOOKMARKS, NIGORI, PASSWORDS));
  EXPECT_TRUE(registrar()->IsNigoriEnabled());
  EXPECT_EQ(4u, GetWorkersSize());
  EXPECT_EQ(ModelTypeSet(NIGORI), registrar()->GetLastConfiguredTypes());
  // Bookmarks dropped because it is nonblocking.
  // Passwords dropped because of no password store.
  ExpectRoutingInfo({{NIGORI, GROUP_PASSIVE}});
  ExpectHasProcessorsForTypes(ModelTypeSet());
}

TEST_F(SyncBackendRegistrarTest, ConstructorNonEmptyReversedInitialization) {
  // The blocking types get to set initial types before NonBlocking types here.
  registrar()->SetInitialTypes(ModelTypeSet(BOOKMARKS, NIGORI, PASSWORDS));
  registrar()->RegisterNonBlockingType(BOOKMARKS);
  EXPECT_TRUE(registrar()->IsNigoriEnabled());
  EXPECT_EQ(4u, GetWorkersSize());
  EXPECT_EQ(ModelTypeSet(NIGORI), registrar()->GetLastConfiguredTypes());
  // Bookmarks dropped because it is nonblocking.
  // Passwords dropped because of no password store.
  ExpectRoutingInfo({{NIGORI, GROUP_PASSIVE}});
  ExpectHasProcessorsForTypes(ModelTypeSet());
}

TEST_F(SyncBackendRegistrarTest, ConfigureDataTypes) {
  registrar()->RegisterNonBlockingType(BOOKMARKS);
  registrar()->SetInitialTypes(ModelTypeSet());

  // Add.
  const ModelTypeSet types1(BOOKMARKS, NIGORI, AUTOFILL);
  EXPECT_EQ(types1, registrar()->ConfigureDataTypes(types1, ModelTypeSet()));
  ExpectRoutingInfo({{BOOKMARKS, GROUP_NON_BLOCKING},
                     {NIGORI, GROUP_PASSIVE},
                     {AUTOFILL, GROUP_PASSIVE}});
  ExpectHasProcessorsForTypes(ModelTypeSet());
  EXPECT_EQ(types1, registrar()->GetLastConfiguredTypes());

  // Add and remove.
  const ModelTypeSet types2(PREFERENCES, THEMES);
  EXPECT_EQ(types2, registrar()->ConfigureDataTypes(types2, types1));

  ExpectRoutingInfo({{PREFERENCES, GROUP_PASSIVE}, {THEMES, GROUP_PASSIVE}});
  ExpectHasProcessorsForTypes(ModelTypeSet());
  EXPECT_EQ(types2, registrar()->GetLastConfiguredTypes());

  // Remove.
  EXPECT_TRUE(registrar()->ConfigureDataTypes(ModelTypeSet(), types2).Empty());
  ExpectRoutingInfo(ModelSafeRoutingInfo());
  ExpectHasProcessorsForTypes(ModelTypeSet());
  EXPECT_EQ(ModelTypeSet(), registrar()->GetLastConfiguredTypes());
}

TEST_F(SyncBackendRegistrarTest, ActivateDeactivateUIDataType) {
  InSequence in_sequence;
  registrar()->SetInitialTypes(ModelTypeSet());

  // Should do nothing.
  TriggerChanges(BOOKMARKS);

  StrictMock<ChangeProcessorMock> change_processor_mock;
  EXPECT_CALL(change_processor_mock, StartImpl());
  EXPECT_CALL(change_processor_mock, IsRunning()).WillRepeatedly(Return(true));
  EXPECT_CALL(change_processor_mock, ApplyChangesFromSyncModel(nullptr, _, _));
  EXPECT_CALL(change_processor_mock, IsRunning()).WillRepeatedly(Return(true));
  EXPECT_CALL(change_processor_mock, CommitChangesFromSyncModel());
  EXPECT_CALL(change_processor_mock, IsRunning()).WillRepeatedly(Return(false));

  const ModelTypeSet types(BOOKMARKS);
  EXPECT_EQ(types, registrar()->ConfigureDataTypes(types, ModelTypeSet()));
  registrar()->ActivateDataType(BOOKMARKS, GROUP_UI, &change_processor_mock,
                                user_share());
  ExpectRoutingInfo({{BOOKMARKS, GROUP_UI}});
  ExpectHasProcessorsForTypes(types);

  TriggerChanges(BOOKMARKS);

  registrar()->DeactivateDataType(BOOKMARKS);
  ExpectRoutingInfo(ModelSafeRoutingInfo());
  ExpectHasProcessorsForTypes(ModelTypeSet());

  // Should do nothing.
  TriggerChanges(BOOKMARKS);
}

TEST_F(SyncBackendRegistrarTest, ActivateDeactivateNonUIDataType) {
  InSequence in_sequence;
  registrar()->SetInitialTypes(ModelTypeSet());

  // Should do nothing.
  TriggerChanges(AUTOFILL);

  StrictMock<ChangeProcessorMock> change_processor_mock;
  EXPECT_CALL(change_processor_mock, StartImpl());
  EXPECT_CALL(change_processor_mock, IsRunning()).WillRepeatedly(Return(true));
  EXPECT_CALL(change_processor_mock, ApplyChangesFromSyncModel(nullptr, _, _));
  EXPECT_CALL(change_processor_mock, IsRunning()).WillRepeatedly(Return(true));
  EXPECT_CALL(change_processor_mock, CommitChangesFromSyncModel());
  EXPECT_CALL(change_processor_mock, IsRunning()).WillRepeatedly(Return(false));

  const ModelTypeSet types(AUTOFILL);
  EXPECT_EQ(types, registrar()->ConfigureDataTypes(types, ModelTypeSet()));

  base::WaitableEvent done(base::WaitableEvent::ResetPolicy::AUTOMATIC,
                           base::WaitableEvent::InitialState::NOT_SIGNALED);
  db_task_runner()->PostTask(
      FROM_HERE,
      base::Bind(&SyncBackendRegistrarTest::TestNonUIDataTypeActivationAsync,
                 base::Unretained(this), &change_processor_mock, &done));
  done.Wait();

  registrar()->DeactivateDataType(AUTOFILL);
  ExpectRoutingInfo(ModelSafeRoutingInfo());
  ExpectHasProcessorsForTypes(ModelTypeSet());

  // Should do nothing.
  TriggerChanges(AUTOFILL);
}

// Tests that registration and configuration of non-blocking data types is
// handled correctly in SyncBackendRegistrar.
TEST_F(SyncBackendRegistrarTest, ConfigureNonBlockingDataType) {
  registrar()->RegisterNonBlockingType(AUTOFILL);
  registrar()->RegisterNonBlockingType(BOOKMARKS);

  ExpectRoutingInfo(ModelSafeRoutingInfo());
  // Simulate that initial sync was already done for AUTOFILL.
  registrar()->AddRestoredNonBlockingType(AUTOFILL);
  // It should be added to routing info and set of configured types.
  EXPECT_EQ(ModelTypeSet(AUTOFILL), registrar()->GetLastConfiguredTypes());
  ExpectRoutingInfo({{AUTOFILL, GROUP_NON_BLOCKING}});

  // Configure two non-blocking types. Initial sync wasn't done for BOOKMARKS so
  // it should be included in types to be downloaded.
  ModelTypeSet types_to_add(AUTOFILL, BOOKMARKS);
  ModelTypeSet newly_added_types =
      registrar()->ConfigureDataTypes(types_to_add, ModelTypeSet());
  EXPECT_EQ(ModelTypeSet(BOOKMARKS), newly_added_types);
  EXPECT_EQ(types_to_add, registrar()->GetLastConfiguredTypes());
  ExpectRoutingInfo(
      {{AUTOFILL, GROUP_NON_BLOCKING}, {BOOKMARKS, GROUP_NON_BLOCKING}});
}

}  // namespace

}  // namespace syncer