1 // Copyright 2014 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.
5 #include "base/message_loop/message_loop.h"
6 #include "base/message_loop/message_loop_proxy.h"
7 #include "base/run_loop.h"
8 #include "base/sequenced_task_runner.h"
9 #include "sync/engine/non_blocking_type_processor.h"
10 #include "sync/internal_api/public/base/model_type.h"
11 #include "sync/internal_api/sync_core.h"
12 #include "sync/internal_api/sync_core_proxy_impl.h"
13 #include "sync/sessions/model_type_registry.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 class SyncCoreProxyImplTest
: public ::testing::Test
{
20 SyncCoreProxyImplTest()
21 : sync_task_runner_(base::MessageLoopProxy::current()),
22 type_task_runner_(base::MessageLoopProxy::current()),
23 core_(new SyncCore(®istry_
)),
26 core_
->AsWeakPtr()) {}
28 // The sync thread could be shut down at any time without warning. This
29 // function simulates such an event.
34 scoped_ptr
<SyncCoreProxy
> GetProxy() { return core_proxy_
.Clone(); }
37 base::MessageLoop loop_
;
38 scoped_refptr
<base::SequencedTaskRunner
> sync_task_runner_
;
39 scoped_refptr
<base::SequencedTaskRunner
> type_task_runner_
;
40 ModelTypeRegistry registry_
;
41 scoped_ptr
<SyncCore
> core_
;
42 SyncCoreProxyImpl core_proxy_
;
45 // Try to connect a type to a SyncCore that has already shut down.
46 TEST_F(SyncCoreProxyImplTest
, FailToConnect1
) {
47 NonBlockingTypeProcessor
themes_processor(syncer::THEMES
);
49 themes_processor
.Enable(GetProxy());
51 base::RunLoop run_loop_
;
52 run_loop_
.RunUntilIdle();
53 EXPECT_FALSE(themes_processor
.IsConnected());
56 // Try to connect a type to a SyncCore as it shuts down.
57 TEST_F(SyncCoreProxyImplTest
, FailToConnect2
) {
58 NonBlockingTypeProcessor
themes_processor(syncer::THEMES
);
59 themes_processor
.Enable(GetProxy());
62 base::RunLoop run_loop_
;
63 run_loop_
.RunUntilIdle();
64 EXPECT_FALSE(themes_processor
.IsConnected());
67 // Tests the case where the type's processor shuts down first.
68 TEST_F(SyncCoreProxyImplTest
, TypeDisconnectsFirst
) {
69 scoped_ptr
<NonBlockingTypeProcessor
> themes_processor
70 (new NonBlockingTypeProcessor(syncer::THEMES
));
71 themes_processor
->Enable(GetProxy());
73 base::RunLoop run_loop_
;
74 run_loop_
.RunUntilIdle();
76 EXPECT_TRUE(themes_processor
->IsConnected());
77 themes_processor
.reset();
80 // Tests the case where the sync thread shuts down first.
81 TEST_F(SyncCoreProxyImplTest
, SyncDisconnectsFirst
) {
82 scoped_ptr
<NonBlockingTypeProcessor
> themes_processor
83 (new NonBlockingTypeProcessor(syncer::THEMES
));
84 themes_processor
->Enable(GetProxy());
86 base::RunLoop run_loop_
;
87 run_loop_
.RunUntilIdle();
89 EXPECT_TRUE(themes_processor
->IsConnected());