summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/glue/data_type_manager_impl_unittest.cc
blob: cc0db2b9808bf1209c365787783cd29a68792d9e (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
// 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 "chrome/browser/sync/glue/data_type_manager_impl.h"

#include "base/compiler_specific.h"
#include "base/message_loop.h"
#include "chrome/browser/sync/glue/backend_data_type_configurer.h"
#include "chrome/browser/sync/glue/data_type_controller.h"
#include "chrome/browser/sync/glue/fake_data_type_controller.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/mock_notification_observer.h"
#include "content/public/test/test_browser_thread.h"
#include "sync/internal_api/configure_reason.h"
#include "sync/internal_api/public/syncable/model_type.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace browser_sync {

using syncable::ModelType;
using syncable::ModelTypeSet;
using syncable::ModelTypeToString;
using syncable::BOOKMARKS;
using syncable::APPS;
using syncable::PASSWORDS;
using syncable::PREFERENCES;
using testing::_;
using testing::Mock;
using testing::ResultOf;

// Fake BackendDataTypeConfigurer implementation that simply stores
// away the nigori state and callback passed into ConfigureDataTypes.
class FakeBackendDataTypeConfigurer : public BackendDataTypeConfigurer {
 public:
  FakeBackendDataTypeConfigurer() : last_nigori_state_(WITHOUT_NIGORI) {}
  virtual ~FakeBackendDataTypeConfigurer() {}

  virtual void ConfigureDataTypes(
      sync_api::ConfigureReason reason,
      const ModelTypeSet& types_to_add,
      const ModelTypeSet& types_to_remove,
      NigoriState nigori_state,
      const base::Callback<void(ModelTypeSet)>& ready_task,
      const base::Callback<void()>& retry_callback) OVERRIDE {
    last_nigori_state_ = nigori_state;
    last_ready_task_ = ready_task;
  }

  base::Callback<void(ModelTypeSet)> last_ready_task() const {
    return last_ready_task_;
  }

  NigoriState last_nigori_state() const {
    return last_nigori_state_;
  }

 private:
  base::Callback<void(ModelTypeSet)> last_ready_task_;
  NigoriState last_nigori_state_;
};


// Used by SetConfigureDoneExpectation.
DataTypeManager::ConfigureStatus GetStatus(
    const content::NotificationDetails& details) {
  const DataTypeManager::ConfigureResult* result =
      content::Details<const DataTypeManager::ConfigureResult>(
      details).ptr();
  return result->status;
}

// The actual test harness class, parametrized on NigoriState (i.e.,
// tests are run both configuring with nigori, and configuring
// without).
class SyncDataTypeManagerImplTest
    : public testing::TestWithParam<BackendDataTypeConfigurer::NigoriState> {
 public:
  SyncDataTypeManagerImplTest()
      : ui_thread_(content::BrowserThread::UI, &ui_loop_) {}

  virtual ~SyncDataTypeManagerImplTest() {
  }

 protected:
  virtual void SetUp() {
    registrar_.Add(&observer_,
                   chrome::NOTIFICATION_SYNC_CONFIGURE_START,
                   content::NotificationService::AllSources());
    registrar_.Add(&observer_,
                   chrome::NOTIFICATION_SYNC_CONFIGURE_DONE,
                   content::NotificationService::AllSources());
  }

  // A clearer name for the param accessor.
  BackendDataTypeConfigurer::NigoriState GetNigoriState() {
    return GetParam();
  }

  void SetConfigureStartExpectation() {
    EXPECT_CALL(
        observer_,
        Observe(int(chrome::NOTIFICATION_SYNC_CONFIGURE_START),
                _, _));
  }

  void SetConfigureDoneExpectation(DataTypeManager::ConfigureStatus status) {
    EXPECT_CALL(
        observer_,
        Observe(int(chrome::NOTIFICATION_SYNC_CONFIGURE_DONE), _,
        ResultOf(&GetStatus, status)));
  }

  // Configure the given DTM with the given desired types.
  void Configure(DataTypeManagerImpl* dtm,
                 const DataTypeManager::TypeSet& desired_types) {
    const sync_api::ConfigureReason kReason =
        sync_api::CONFIGURE_REASON_RECONFIGURATION;
    if (GetNigoriState() == BackendDataTypeConfigurer::WITH_NIGORI) {
      dtm->Configure(desired_types, kReason);
    } else {
      dtm->ConfigureWithoutNigori(desired_types, kReason);
    }
  }

  // Finish downloading for the given DTM.  Should be done only after
  // a call to Configure().
  void FinishDownload(const DataTypeManager& dtm,
                      ModelTypeSet failed_download_types) {
    EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());
    EXPECT_EQ(GetNigoriState(), configurer_.last_nigori_state());
    ASSERT_FALSE(configurer_.last_ready_task().is_null());
    configurer_.last_ready_task().Run(failed_download_types);
  }

  // Adds a fake controller for the given type to |controllers_|.
  // Should be called only before setting up the DTM.
  void AddController(ModelType model_type) {
    controllers_[model_type] = new FakeDataTypeController(model_type);
  }

  // Gets the fake controller for the given type, which should have
  // been previously added via AddController().
  scoped_refptr<FakeDataTypeController> GetController(
      ModelType model_type) const {
    DataTypeController::TypeMap::const_iterator it =
        controllers_.find(model_type);
    if (it == controllers_.end()) {
      return NULL;
    }
    return make_scoped_refptr(
        static_cast<FakeDataTypeController*>(it->second.get()));
  }

  MessageLoopForUI ui_loop_;
  content::TestBrowserThread ui_thread_;
  DataTypeController::TypeMap controllers_;
  FakeBackendDataTypeConfigurer configurer_;
  content::MockNotificationObserver observer_;
  content::NotificationRegistrar registrar_;
};

// Set up a DTM with no controllers, configure it, finish downloading,
// and then stop it.
TEST_P(SyncDataTypeManagerImplTest, NoControllers) {
  DataTypeManagerImpl dtm(&configurer_, &controllers_);
  SetConfigureStartExpectation();
  SetConfigureDoneExpectation(DataTypeManager::OK);

  Configure(&dtm, ModelTypeSet());
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  FinishDownload(dtm, ModelTypeSet());
  EXPECT_EQ(DataTypeManager::CONFIGURED, dtm.state());

  dtm.Stop();
  EXPECT_EQ(DataTypeManager::STOPPED, dtm.state());
}

// Set up a DTM with a single controller, configure it, finish
// downloading, finish starting the controller, and then stop the DTM.
TEST_P(SyncDataTypeManagerImplTest, ConfigureOne) {
  AddController(BOOKMARKS);

  DataTypeManagerImpl dtm(&configurer_, &controllers_);
  SetConfigureStartExpectation();
  SetConfigureDoneExpectation(DataTypeManager::OK);

  Configure(&dtm, ModelTypeSet(BOOKMARKS));
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  FinishDownload(dtm, ModelTypeSet());
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

  GetController(BOOKMARKS)->FinishStart(DataTypeController::OK);
  EXPECT_EQ(DataTypeManager::CONFIGURED, dtm.state());

  dtm.Stop();
  EXPECT_EQ(DataTypeManager::STOPPED, dtm.state());
}

// Set up a DTM with 2 controllers. configure it. One of them finishes loading
// after the  timeout. Make sure eventually all are configured.
TEST_P(SyncDataTypeManagerImplTest, ConfigureSlowLoadingType) {
  AddController(BOOKMARKS);
  AddController(APPS);

  GetController(BOOKMARKS)->SetDelayModelLoad();

  DataTypeManagerImpl dtm(&configurer_, &controllers_);
  SetConfigureStartExpectation();
  SetConfigureDoneExpectation(DataTypeManager::PARTIAL_SUCCESS);

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

  Configure(&dtm, types);
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  FinishDownload(dtm, ModelTypeSet());
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

  base::OneShotTimer<ModelAssociationManager>* timer =
    dtm.GetModelAssociationManagerForTesting()->GetTimerForTesting();

  base::Closure task = timer->user_task();
  timer->Stop();
  task.Run();

  SetConfigureDoneExpectation(DataTypeManager::OK);
  GetController(APPS)->FinishStart(DataTypeController::OK);

  SetConfigureStartExpectation();
  GetController(BOOKMARKS)->SimulateModelLoadFinishing();

  FinishDownload(dtm, ModelTypeSet());
  GetController(BOOKMARKS)->SimulateModelLoadFinishing();

  GetController(BOOKMARKS)->FinishStart(DataTypeController::OK);
  EXPECT_EQ(DataTypeManager::CONFIGURED, dtm.state());

  dtm.Stop();
  EXPECT_EQ(DataTypeManager::STOPPED, dtm.state());
}


// Set up a DTM with a single controller, configure it, but stop it
// before finishing the download.  It should still be safe to run the
// download callback even after the DTM is stopped and destroyed.
TEST_P(SyncDataTypeManagerImplTest, ConfigureOneStopWhileDownloadPending) {
  AddController(BOOKMARKS);

  {
    DataTypeManagerImpl dtm(&configurer_, &controllers_);
    SetConfigureStartExpectation();
    SetConfigureDoneExpectation(DataTypeManager::ABORTED);

    Configure(&dtm, ModelTypeSet(BOOKMARKS));
    EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

    dtm.Stop();
    EXPECT_EQ(DataTypeManager::STOPPED, dtm.state());
  }

  configurer_.last_ready_task().Run(ModelTypeSet());
}

// Set up a DTM with a single controller, configure it, finish
// downloading, but stop the DTM before the controller finishes
// starting up.  It should still be safe to finish starting up the
// controller even after the DTM is stopped and destroyed.
TEST_P(SyncDataTypeManagerImplTest, ConfigureOneStopWhileStartingModel) {
  AddController(BOOKMARKS);

  {
    DataTypeManagerImpl dtm(&configurer_, &controllers_);
    SetConfigureStartExpectation();
    SetConfigureDoneExpectation(DataTypeManager::ABORTED);

    Configure(&dtm, ModelTypeSet(BOOKMARKS));
    EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

    FinishDownload(dtm, ModelTypeSet());
    EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

    dtm.Stop();
    EXPECT_EQ(DataTypeManager::STOPPED, dtm.state());
  }

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

// Set up a DTM with a single controller, configure it, finish
// downloading, start the controller's model, but stop the DTM before
// the controller finishes starting up.  It should still be safe to
// finish starting up the controller even after the DTM is stopped and
// destroyed.
TEST_P(SyncDataTypeManagerImplTest, ConfigureOneStopWhileAssociating) {
  AddController(BOOKMARKS);
  {
    DataTypeManagerImpl dtm(&configurer_, &controllers_);
    SetConfigureStartExpectation();
    SetConfigureDoneExpectation(DataTypeManager::ABORTED);

    Configure(&dtm, ModelTypeSet(BOOKMARKS));
    EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

    FinishDownload(dtm, ModelTypeSet());
    EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

    dtm.Stop();
    EXPECT_EQ(DataTypeManager::STOPPED, dtm.state());
  }

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

// Set up a DTM with a single controller.  Then:
//
//   1) Configure.
//   2) Finish the download for step 1.
//   3) Finish starting the controller with the NEEDS_CRYPTO status.
//   4) Configure again.
//   5) Finish the download for step 4.
//   6) Finish starting the controller successfully.
//   7) Stop the DTM.
TEST_P(SyncDataTypeManagerImplTest, OneWaitingForCrypto) {
  AddController(PASSWORDS);

  DataTypeManagerImpl dtm(&configurer_, &controllers_);
  SetConfigureStartExpectation();

  const ModelTypeSet types(PASSWORDS);

  // Step 1.
  Configure(&dtm, types);
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  // Step 2.
  FinishDownload(dtm, ModelTypeSet());
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

  // Step 3.
  GetController(PASSWORDS)->FinishStart(DataTypeController::NEEDS_CRYPTO);
  EXPECT_EQ(DataTypeManager::BLOCKED, dtm.state());

  Mock::VerifyAndClearExpectations(&observer_);
  SetConfigureDoneExpectation(DataTypeManager::OK);

  // Step 4.
  Configure(&dtm, types);
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  // Step 5.
  FinishDownload(dtm, ModelTypeSet());
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

  // Step 6.
  GetController(PASSWORDS)->FinishStart(DataTypeController::OK);
  EXPECT_EQ(DataTypeManager::CONFIGURED, dtm.state());

  // Step 7.
  dtm.Stop();
  EXPECT_EQ(DataTypeManager::STOPPED, dtm.state());
}

// Set up a DTM with two controllers.  Then:
//
//   1) Configure with first controller.
//   2) Finish the download for step 1.
//   3) Finish starting the first controller.
//   4) Configure with both controllers.
//   5) Finish the download for step 4.
//   6) Finish starting the second controller.
//   7) Stop the DTM.
TEST_P(SyncDataTypeManagerImplTest, ConfigureOneThenBoth) {
  AddController(BOOKMARKS);
  AddController(PREFERENCES);

  DataTypeManagerImpl dtm(&configurer_, &controllers_);
  SetConfigureStartExpectation();
  SetConfigureDoneExpectation(DataTypeManager::OK);

  // Step 1.
  Configure(&dtm, ModelTypeSet(BOOKMARKS));
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  // Step 2.
  FinishDownload(dtm, ModelTypeSet());
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

  // Step 3.
  GetController(BOOKMARKS)->FinishStart(DataTypeController::OK);
  EXPECT_EQ(DataTypeManager::CONFIGURED, dtm.state());

  Mock::VerifyAndClearExpectations(&observer_);
  SetConfigureStartExpectation();
  SetConfigureDoneExpectation(DataTypeManager::OK);

  // Step 4.
  Configure(&dtm, ModelTypeSet(BOOKMARKS, PREFERENCES));
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  // Step 5.
  FinishDownload(dtm, ModelTypeSet());
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

  // Step 6.
  GetController(PREFERENCES)->FinishStart(DataTypeController::OK);
  EXPECT_EQ(DataTypeManager::CONFIGURED, dtm.state());

  // Step 7.
  dtm.Stop();
  EXPECT_EQ(DataTypeManager::STOPPED, dtm.state());
}

// Set up a DTM with two controllers.  Then:
//
//   1) Configure with first controller.
//   2) Finish the download for step 1.
//   3) Finish starting the first controller.
//   4) Configure with second controller.
//   5) Finish the download for step 4.
//   6) Finish starting the second controller.
//   7) Stop the DTM.
TEST_P(SyncDataTypeManagerImplTest, ConfigureOneThenSwitch) {
  AddController(BOOKMARKS);
  AddController(PREFERENCES);

  DataTypeManagerImpl dtm(&configurer_, &controllers_);
  SetConfigureStartExpectation();
  SetConfigureDoneExpectation(DataTypeManager::OK);

  // Step 1.
  Configure(&dtm, ModelTypeSet(BOOKMARKS));
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  // Step 2.
  FinishDownload(dtm, ModelTypeSet());
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

  // Step 3.
  GetController(BOOKMARKS)->FinishStart(DataTypeController::OK);
  EXPECT_EQ(DataTypeManager::CONFIGURED, dtm.state());

  Mock::VerifyAndClearExpectations(&observer_);
  SetConfigureStartExpectation();
  SetConfigureDoneExpectation(DataTypeManager::OK);

  // Step 4.
  Configure(&dtm, ModelTypeSet(PREFERENCES));
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  // Step 5.
  FinishDownload(dtm, ModelTypeSet());
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

  // Step 6.
  GetController(PREFERENCES)->FinishStart(DataTypeController::OK);
  EXPECT_EQ(DataTypeManager::CONFIGURED, dtm.state());

  // Step 7.
  dtm.Stop();
  EXPECT_EQ(DataTypeManager::STOPPED, dtm.state());
}

// Set up a DTM with two controllers.  Then:
//
//   1) Configure with first controller.
//   2) Finish the download for step 1.
//   3) Configure with both controllers.
//   4) Finish starting the first controller.
//   5) Finish the download for step 3.
//   6) Finish starting the second controller.
//   7) Stop the DTM.
TEST_P(SyncDataTypeManagerImplTest, ConfigureWhileOneInFlight) {
  AddController(BOOKMARKS);
  AddController(PREFERENCES);

  DataTypeManagerImpl dtm(&configurer_, &controllers_);
  SetConfigureStartExpectation();
  SetConfigureDoneExpectation(DataTypeManager::OK);

  // Step 1.
  Configure(&dtm, ModelTypeSet(BOOKMARKS));
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  // Step 2.
  FinishDownload(dtm, ModelTypeSet());
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

  // Step 3.
  Configure(&dtm, ModelTypeSet(BOOKMARKS, PREFERENCES));
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

  // Step 4.
  GetController(BOOKMARKS)->FinishStart(DataTypeController::OK);
  EXPECT_EQ(DataTypeManager::BLOCKED, dtm.state());

  // Pump the loop to run the posted DTMI::ConfigureImpl() task from
  // DTMI::ProcessReconfigure() (triggered by FinishStart()).
  ui_loop_.RunAllPending();
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  // Step 5.
  FinishDownload(dtm, ModelTypeSet());
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

  // Step 6.
  GetController(PREFERENCES)->FinishStart(DataTypeController::OK);
  EXPECT_EQ(DataTypeManager::CONFIGURED, dtm.state());

  // Step 7.
  dtm.Stop();
  EXPECT_EQ(DataTypeManager::STOPPED, dtm.state());
}

// Set up a DTM with one controller.  Then configure, finish
// downloading, and start the controller with an unrecoverable error.
// The unrecoverable error should cause the DTM to stop.
TEST_P(SyncDataTypeManagerImplTest, OneFailingController) {
  AddController(BOOKMARKS);

  DataTypeManagerImpl dtm(&configurer_, &controllers_);
  SetConfigureStartExpectation();
  SetConfigureDoneExpectation(DataTypeManager::UNRECOVERABLE_ERROR);

  Configure(&dtm, ModelTypeSet(BOOKMARKS));
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  FinishDownload(dtm, ModelTypeSet());
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

  GetController(BOOKMARKS)->FinishStart(
      DataTypeController::UNRECOVERABLE_ERROR);
  EXPECT_EQ(DataTypeManager::STOPPED, dtm.state());
}

// Set up a DTM with two controllers.  Then:
//
//   1) Configure with both controllers.
//   2) Finish the download for step 1.
//   3) Finish starting the first controller successfully.
//   4) Finish starting the second controller with an unrecoverable error.
//
// The failure from step 4 should cause the DTM to stop.
TEST_P(SyncDataTypeManagerImplTest, SecondControllerFails) {
  AddController(BOOKMARKS);
  AddController(PREFERENCES);

  DataTypeManagerImpl dtm(&configurer_, &controllers_);
  SetConfigureStartExpectation();
  SetConfigureDoneExpectation(DataTypeManager::UNRECOVERABLE_ERROR);

  // Step 1.
  Configure(&dtm, ModelTypeSet(BOOKMARKS, PREFERENCES));
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  // Step 2.
  FinishDownload(dtm, ModelTypeSet());
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

  // Step 3.
  GetController(BOOKMARKS)->FinishStart(DataTypeController::OK);
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

  // Step 4.
  GetController(PREFERENCES)->FinishStart(
      DataTypeController::UNRECOVERABLE_ERROR);
  EXPECT_EQ(DataTypeManager::STOPPED, dtm.state());
}

// Set up a DTM with two controllers.  Then:
//
//   1) Configure with both controllers.
//   2) Finish the download for step 1.
//   3) Finish starting the first controller with an association failure.
//   4) Finish starting the second controller successfully.
//   5) Stop the DTM.
//
// The association failure from step 3 should be ignored.
//
// TODO(akalin): Check that the data type that failed association is
// recorded in the CONFIGURE_DONE notification.
TEST_P(SyncDataTypeManagerImplTest, OneControllerFailsAssociation) {
  AddController(BOOKMARKS);
  AddController(PREFERENCES);

  DataTypeManagerImpl dtm(&configurer_, &controllers_);
  SetConfigureStartExpectation();
  SetConfigureDoneExpectation(DataTypeManager::PARTIAL_SUCCESS);

  // Step 1.
  Configure(&dtm, ModelTypeSet(BOOKMARKS, PREFERENCES));
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  // Step 2.
  FinishDownload(dtm, ModelTypeSet());
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

  // Step 3.
  GetController(BOOKMARKS)->FinishStart(DataTypeController::OK);
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

  // Step 4.
  GetController(PREFERENCES)->FinishStart(
      DataTypeController::ASSOCIATION_FAILED);
  EXPECT_EQ(DataTypeManager::CONFIGURED, dtm.state());

  // Step 5.
  dtm.Stop();
  EXPECT_EQ(DataTypeManager::STOPPED, dtm.state());
}

// Set up a DTM with two controllers.  Then:
//
//   1) Configure with first controller.
//   2) Configure with both controllers.
//   3) Finish the download for step 1.
//   4) Finish the download for step 2.
//   5) Finish starting both controllers.
//   6) Stop the DTM.
TEST_P(SyncDataTypeManagerImplTest, ConfigureWhileDownloadPending) {
  AddController(BOOKMARKS);
  AddController(PREFERENCES);

  DataTypeManagerImpl dtm(&configurer_, &controllers_);
  SetConfigureStartExpectation();
  SetConfigureDoneExpectation(DataTypeManager::OK);

  // Step 1.
  Configure(&dtm, ModelTypeSet(BOOKMARKS));
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  // Step 2.
  Configure(&dtm, ModelTypeSet(BOOKMARKS, PREFERENCES));
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  // Step 3.
  FinishDownload(dtm, ModelTypeSet());
  EXPECT_EQ(DataTypeManager::BLOCKED, dtm.state());

  // Pump the loop to run the posted DTMI::ConfigureImpl() task from
  // DTMI::ProcessReconfigure() (triggered by step 3).
  ui_loop_.RunAllPending();
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  // Step 4.
  FinishDownload(dtm, ModelTypeSet());
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

  // Step 5.
  GetController(BOOKMARKS)->FinishStart(DataTypeController::OK);
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());
  GetController(PREFERENCES)->FinishStart(DataTypeController::OK);
  EXPECT_EQ(DataTypeManager::CONFIGURED, dtm.state());

  // Step 6.
  dtm.Stop();
  EXPECT_EQ(DataTypeManager::STOPPED, dtm.state());
}

// Set up a DTM with two controllers.  Then:
//
//   1) Configure with first controller.
//   2) Configure with both controllers.
//   3) Finish the download for step 1 with a failed data type.
//   4) Finish the download for step 2 successfully.
//   5) Finish starting both controllers.
//   6) Stop the DTM.
//
// The failure from step 3 should be ignored since there's a
// reconfigure pending from step 2.
TEST_P(SyncDataTypeManagerImplTest, ConfigureWhileDownloadPendingWithFailure) {
  AddController(BOOKMARKS);
  AddController(PREFERENCES);

  DataTypeManagerImpl dtm(&configurer_, &controllers_);
  SetConfigureStartExpectation();
  SetConfigureDoneExpectation(DataTypeManager::OK);

  // Step 1.
  Configure(&dtm, ModelTypeSet(BOOKMARKS));
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  // Step 2.
  Configure(&dtm, ModelTypeSet(BOOKMARKS, PREFERENCES));
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  // Step 3.
  FinishDownload(dtm, ModelTypeSet(BOOKMARKS));
  EXPECT_EQ(DataTypeManager::BLOCKED, dtm.state());

  // Pump the loop to run the posted DTMI::ConfigureImpl() task from
  // DTMI::ProcessReconfigure() (triggered by step 3).
  ui_loop_.RunAllPending();
  EXPECT_EQ(DataTypeManager::DOWNLOAD_PENDING, dtm.state());

  // Step 4.
  FinishDownload(dtm, ModelTypeSet());
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());

  // Step 5.
  GetController(BOOKMARKS)->FinishStart(DataTypeController::OK);
  EXPECT_EQ(DataTypeManager::CONFIGURING, dtm.state());
  GetController(PREFERENCES)->FinishStart(DataTypeController::OK);
  EXPECT_EQ(DataTypeManager::CONFIGURED, dtm.state());

  // Step 6.
  dtm.Stop();
  EXPECT_EQ(DataTypeManager::STOPPED, dtm.state());
}

INSTANTIATE_TEST_CASE_P(
    WithoutNigori, SyncDataTypeManagerImplTest,
    ::testing::Values(BackendDataTypeConfigurer::WITHOUT_NIGORI));

INSTANTIATE_TEST_CASE_P(
    WithNigori, SyncDataTypeManagerImplTest,
    ::testing::Values(BackendDataTypeConfigurer::WITH_NIGORI));

}  // namespace browser_sync