summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/glue/model_association_manager_unittest.cc
blob: 3d1f90df0935203fcf542f8aeafddb2317f08bd6 (plain)
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright (c) 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 "base/callback.h"
#include "base/message_loop.h"
#include "chrome/browser/sync/glue/fake_data_type_controller.h"
#include "chrome/browser/sync/glue/model_association_manager.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::_;
namespace browser_sync {
class MockModelAssociationResultProcessor :
    public ModelAssociationResultProcessor {
 public:
  MockModelAssociationResultProcessor() {}
  ~MockModelAssociationResultProcessor() {}
  MOCK_METHOD1(OnModelAssociationDone, void(
      const DataTypeManager::ConfigureResult& result));
  MOCK_METHOD0(OnTypesLoaded, void());
};

FakeDataTypeController* GetController(
    const DataTypeController::TypeMap& controllers,
    syncable::ModelType model_type) {
  DataTypeController::TypeMap::const_iterator it =
      controllers.find(model_type);
  if (it == controllers.end()) {
    return NULL;
  }
  return (FakeDataTypeController*)(it->second.get());
}

ACTION_P(VerifyResult, expected_result) {
  EXPECT_EQ(arg0.status, expected_result.status);
  EXPECT_TRUE(arg0.requested_types.Equals(expected_result.requested_types));
  EXPECT_EQ(arg0.failed_data_types.size(),
            expected_result.failed_data_types.size());

  if (arg0.failed_data_types.size() ==
          expected_result.failed_data_types.size()) {
    std::list<syncer::SyncError>::const_iterator it1, it2;
    for (it1 = arg0.failed_data_types.begin(),
         it2 = expected_result.failed_data_types.begin();
         it1 != arg0.failed_data_types.end();
         ++it1, ++it2) {
      EXPECT_EQ((*it1).type(), (*it2).type());
    }
  }

  EXPECT_TRUE(arg0.waiting_to_start.Equals(expected_result.waiting_to_start));
}

class ModelAssociationManagerTest : public testing::Test {
 public:
  ModelAssociationManagerTest() :
      ui_thread_(content::BrowserThread::UI, &ui_loop_) {
  }

 protected:
  MessageLoopForUI ui_loop_;
  content::TestBrowserThread ui_thread_;
  MockModelAssociationResultProcessor result_processor_;
  DataTypeController::TypeMap controllers_;
};

// Start a type and make sure ModelAssociationManager callst the |Start|
// method and calls the callback when it is done.
TEST_F(ModelAssociationManagerTest, SimpleModelStart) {
  controllers_[syncable::BOOKMARKS] =
      new FakeDataTypeController(syncable::BOOKMARKS);
  ModelAssociationManager model_association_manager(&controllers_,
                                                    &result_processor_);
  syncable::ModelTypeSet types;
  types.Put(syncable::BOOKMARKS);
  DataTypeManager::ConfigureResult expected_result(
      DataTypeManager::OK,
      types,
      std::list<syncer::SyncError>(),
      syncable::ModelTypeSet());
  EXPECT_CALL(result_processor_, OnModelAssociationDone(_)).
              WillOnce(VerifyResult(expected_result));

  model_association_manager.Initialize(types);
  model_association_manager.StopDisabledTypes();
  model_association_manager.StartAssociationAsync();

  EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(),
            DataTypeController::MODEL_LOADED);
  GetController(controllers_, syncable::BOOKMARKS)->FinishStart(
      DataTypeController::OK);
}

// Start a type and call stop before it finishes associating.
TEST_F(ModelAssociationManagerTest, StopModelBeforeFinish) {
  controllers_[syncable::BOOKMARKS] =
      new FakeDataTypeController(syncable::BOOKMARKS);
  ModelAssociationManager model_association_manager(&controllers_,
                                                    &result_processor_);

  syncable::ModelTypeSet types;
  types.Put(syncable::BOOKMARKS);

  DataTypeManager::ConfigureResult expected_result(
      DataTypeManager::ABORTED,
      types,
      std::list<syncer::SyncError>(),
      syncable::ModelTypeSet());

  EXPECT_CALL(result_processor_, OnModelAssociationDone(_)).
              WillOnce(VerifyResult(expected_result));

  model_association_manager.Initialize(types);
  model_association_manager.StopDisabledTypes();
  model_association_manager.StartAssociationAsync();

  EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(),
            DataTypeController::MODEL_LOADED);
  model_association_manager.Stop();
  EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(),
            DataTypeController::NOT_RUNNING);
}

// Start a type, let it finish and then call stop.
TEST_F(ModelAssociationManagerTest, StopAfterFinish) {
  controllers_[syncable::BOOKMARKS] =
      new FakeDataTypeController(syncable::BOOKMARKS);
  ModelAssociationManager model_association_manager(&controllers_,
                                                    &result_processor_);
  syncable::ModelTypeSet types;
  types.Put(syncable::BOOKMARKS);
  DataTypeManager::ConfigureResult expected_result(
      DataTypeManager::OK,
      types,
      std::list<syncer::SyncError>(),
      syncable::ModelTypeSet());
  EXPECT_CALL(result_processor_, OnModelAssociationDone(_)).
              WillOnce(VerifyResult(expected_result));

  model_association_manager.Initialize(types);
  model_association_manager.StopDisabledTypes();
  model_association_manager.StartAssociationAsync();

  EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(),
            DataTypeController::MODEL_LOADED);
  GetController(controllers_, syncable::BOOKMARKS)->FinishStart(
      DataTypeController::OK);

  model_association_manager.Stop();
  EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(),
            DataTypeController::NOT_RUNNING);
}

// Make a type fail model association and verify correctness.
TEST_F(ModelAssociationManagerTest, TypeFailModelAssociation) {
  controllers_[syncable::BOOKMARKS] =
      new FakeDataTypeController(syncable::BOOKMARKS);
  ModelAssociationManager model_association_manager(&controllers_,
                                                    &result_processor_);
  syncable::ModelTypeSet types;
  types.Put(syncable::BOOKMARKS);
  std::list<syncer::SyncError> errors;
  syncer::SyncError error(FROM_HERE, "Failed", syncable::BOOKMARKS);
  errors.push_back(error);
  DataTypeManager::ConfigureResult expected_result(
      DataTypeManager::PARTIAL_SUCCESS,
      types,
      errors,
      syncable::ModelTypeSet());
  EXPECT_CALL(result_processor_, OnModelAssociationDone(_)).
              WillOnce(VerifyResult(expected_result));

  model_association_manager.Initialize(types);
  model_association_manager.StopDisabledTypes();
  model_association_manager.StartAssociationAsync();

  EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(),
            DataTypeController::MODEL_LOADED);
  GetController(controllers_, syncable::BOOKMARKS)->FinishStart(
      DataTypeController::ASSOCIATION_FAILED);
}

// Ensure configuring stops when a type returns a unrecoverable error.
TEST_F(ModelAssociationManagerTest, TypeReturnUnrecoverableError) {
  controllers_[syncable::BOOKMARKS] =
      new FakeDataTypeController(syncable::BOOKMARKS);
  ModelAssociationManager model_association_manager(&controllers_,
                                                    &result_processor_);
  syncable::ModelTypeSet types;
  types.Put(syncable::BOOKMARKS);
  std::list<syncer::SyncError> errors;
  syncer::SyncError error(FROM_HERE, "Failed", syncable::BOOKMARKS);
  errors.push_back(error);
  DataTypeManager::ConfigureResult expected_result(
      DataTypeManager::UNRECOVERABLE_ERROR,
      types,
      errors,
      syncable::ModelTypeSet());
  EXPECT_CALL(result_processor_, OnModelAssociationDone(_)).
              WillOnce(VerifyResult(expected_result));

  model_association_manager.Initialize(types);
  model_association_manager.StopDisabledTypes();
  model_association_manager.StartAssociationAsync();

  EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(),
            DataTypeController::MODEL_LOADED);
  GetController(controllers_, syncable::BOOKMARKS)->FinishStart(
      DataTypeController::UNRECOVERABLE_ERROR);
}

TEST_F(ModelAssociationManagerTest, InitializeAbortsLoad) {
  controllers_[syncable::BOOKMARKS] =
      new FakeDataTypeController(syncable::BOOKMARKS);
  controllers_[syncable::THEMES] =
      new FakeDataTypeController(syncable::THEMES);

  GetController(controllers_, syncable::BOOKMARKS)->SetDelayModelLoad();
  ModelAssociationManager model_association_manager(&controllers_,
                                                    &result_processor_);
  syncable::ModelTypeSet types(syncable::BOOKMARKS, syncable::THEMES);

  syncable::ModelTypeSet expected_types_waiting_to_load;
  expected_types_waiting_to_load.Put(syncable::BOOKMARKS);
  DataTypeManager::ConfigureResult expected_result_partially_done(
      DataTypeManager::PARTIAL_SUCCESS,
      types,
      std::list<syncer::SyncError>(),
      expected_types_waiting_to_load);

  model_association_manager.Initialize(types);
  model_association_manager.StopDisabledTypes();

  model_association_manager.StartAssociationAsync();

  EXPECT_CALL(result_processor_, OnModelAssociationDone(_)).
              WillOnce(VerifyResult(expected_result_partially_done));

  base::OneShotTimer<ModelAssociationManager>* timer =
      model_association_manager.GetTimerForTesting();

  base::Closure task = timer->user_task();
  timer->Stop();
  task.Run();  // Bookmark load times out here.

  // Apps finishes associating here.
  GetController(controllers_, syncable::THEMES)->FinishStart(
      DataTypeController::OK);

  // At this point, BOOKMARKS is still waiting to load (as evidenced by
  // expected_result_partially_done). If we schedule another Initialize (which
  // could happen in practice due to reconfiguration), this should abort
  // BOOKMARKS. Aborting will call ModelLoadCallback, but the
  // ModelAssociationManager should be smart enough to know that this is not due
  // to the type having completed loading.
  EXPECT_CALL(result_processor_, OnTypesLoaded()).Times(0);

  EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(),
            DataTypeController::MODEL_STARTING);

  model_association_manager.Initialize(types);
  EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(),
            DataTypeController::NOT_RUNNING);

  DataTypeManager::ConfigureResult expected_result_done(
      DataTypeManager::OK,
      types,
      std::list<syncer::SyncError>(),
      syncable::ModelTypeSet());
  EXPECT_CALL(result_processor_, OnModelAssociationDone(_)).
              WillOnce(VerifyResult(expected_result_done));

  model_association_manager.StopDisabledTypes();
  model_association_manager.StartAssociationAsync();

  GetController(controllers_,
                syncable::BOOKMARKS)->SimulateModelLoadFinishing();
  GetController(controllers_, syncable::BOOKMARKS)->FinishStart(
      DataTypeController::OK);
}

// Start 2 types. One of which timeout loading. Ensure that type is
// fully configured eventually.
TEST_F(ModelAssociationManagerTest, ModelStartWithSlowLoadingType) {
  controllers_[syncable::BOOKMARKS] =
      new FakeDataTypeController(syncable::BOOKMARKS);
  controllers_[syncable::APPS] =
      new FakeDataTypeController(syncable::APPS);
  GetController(controllers_, syncable::BOOKMARKS)->SetDelayModelLoad();
  ModelAssociationManager model_association_manager(&controllers_,
                                                    &result_processor_);
  syncable::ModelTypeSet types;
  types.Put(syncable::BOOKMARKS);
  types.Put(syncable::APPS);

  syncable::ModelTypeSet expected_types_waiting_to_load;
  expected_types_waiting_to_load.Put(syncable::BOOKMARKS);
  DataTypeManager::ConfigureResult expected_result_partially_done(
      DataTypeManager::PARTIAL_SUCCESS,
      types,
      std::list<syncer::SyncError>(),
      expected_types_waiting_to_load);

  DataTypeManager::ConfigureResult expected_result_done(
      DataTypeManager::OK,
      types,
      std::list<syncer::SyncError>(),
      syncable::ModelTypeSet());

  EXPECT_CALL(result_processor_, OnModelAssociationDone(_)).
              WillOnce(VerifyResult(expected_result_partially_done));
  EXPECT_CALL(result_processor_, OnTypesLoaded());

  model_association_manager.Initialize(types);
  model_association_manager.StopDisabledTypes();
  model_association_manager.StartAssociationAsync();

  base::OneShotTimer<ModelAssociationManager>* timer =
      model_association_manager.GetTimerForTesting();

  // Note: Independent of the timeout value this test is not flaky.
  // The reason is timer posts a task which would never be executed
  // as we dont let the message loop run.
  base::Closure task = timer->user_task();
  timer->Stop();
  task.Run();

  // Simulate delayed loading of bookmark model.
  GetController(controllers_, syncable::APPS)->FinishStart(
      DataTypeController::OK);

  GetController(controllers_,
                syncable::BOOKMARKS)->SimulateModelLoadFinishing();

  EXPECT_CALL(result_processor_, OnModelAssociationDone(_)).
              WillOnce(VerifyResult(expected_result_done));

  // Do it once more to associate bookmarks.
  model_association_manager.Initialize(types);
  model_association_manager.StopDisabledTypes();
  model_association_manager.StartAssociationAsync();

  GetController(controllers_,
                syncable::BOOKMARKS)->SimulateModelLoadFinishing();

  GetController(controllers_, syncable::BOOKMARKS)->FinishStart(
      DataTypeController::OK);
}


}  // namespace browser_sync