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/run_loop.h"
7 #include "base/sequenced_task_runner.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "sync/engine/model_type_processor_impl.h"
10 #include "sync/internal_api/public/base/model_type.h"
11 #include "sync/internal_api/public/sync_context.h"
12 #include "sync/internal_api/sync_context_proxy_impl.h"
13 #include "sync/sessions/model_type_registry.h"
14 #include "sync/test/engine/mock_nudge_handler.h"
15 #include "sync/test/engine/test_directory_setter_upper.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 class SyncContextProxyImplTest
: public ::testing::Test
{
22 SyncContextProxyImplTest()
23 : sync_task_runner_(base::ThreadTaskRunnerHandle::Get()),
24 type_task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
26 void SetUp() override
{
28 registry_
.reset(new syncer::ModelTypeRegistry(
29 workers_
, dir_maker_
.directory(), &nudge_handler_
));
31 new SyncContextProxyImpl(sync_task_runner_
, registry_
->AsWeakPtr()));
34 void TearDown() override
{
35 context_proxy_
.reset();
37 dir_maker_
.TearDown();
40 // The sync thread could be shut down at any time without warning. This
41 // function simulates such an event.
42 void DisableSync() { registry_
.reset(); }
44 scoped_ptr
<SyncContextProxy
> GetProxy() { return context_proxy_
->Clone(); }
47 base::MessageLoop loop_
;
48 scoped_refptr
<base::SequencedTaskRunner
> sync_task_runner_
;
49 scoped_refptr
<base::SequencedTaskRunner
> type_task_runner_
;
51 std::vector
<scoped_refptr
<syncer::ModelSafeWorker
>> workers_
;
52 syncer::TestDirectorySetterUpper dir_maker_
;
53 syncer::MockNudgeHandler nudge_handler_
;
54 scoped_ptr
<syncer::ModelTypeRegistry
> registry_
;
56 scoped_ptr
<SyncContextProxyImpl
> context_proxy_
;
59 // Try to connect a type to a SyncContext that has already shut down.
60 TEST_F(SyncContextProxyImplTest
, FailToConnect1
) {
61 ModelTypeProcessorImpl
themes_sync_proxy(syncer::THEMES
,
62 base::WeakPtr
<ModelTypeStore
>());
64 themes_sync_proxy
.Enable(GetProxy());
66 base::RunLoop run_loop_
;
67 run_loop_
.RunUntilIdle();
68 EXPECT_FALSE(themes_sync_proxy
.IsConnected());
71 // Try to connect a type to a SyncContext as it shuts down.
72 TEST_F(SyncContextProxyImplTest
, FailToConnect2
) {
73 ModelTypeProcessorImpl
themes_sync_proxy(syncer::THEMES
,
74 base::WeakPtr
<ModelTypeStore
>());
75 themes_sync_proxy
.Enable(GetProxy());
78 base::RunLoop run_loop_
;
79 run_loop_
.RunUntilIdle();
80 EXPECT_FALSE(themes_sync_proxy
.IsConnected());
83 // Tests the case where the type's sync proxy shuts down first.
84 TEST_F(SyncContextProxyImplTest
, TypeDisconnectsFirst
) {
85 scoped_ptr
<ModelTypeProcessorImpl
> themes_sync_proxy(
86 new ModelTypeProcessorImpl(syncer::THEMES
,
87 base::WeakPtr
<ModelTypeStore
>()));
88 themes_sync_proxy
->Enable(GetProxy());
90 base::RunLoop run_loop_
;
91 run_loop_
.RunUntilIdle();
93 EXPECT_TRUE(themes_sync_proxy
->IsConnected());
94 themes_sync_proxy
.reset();
97 // Tests the case where the sync thread shuts down first.
98 TEST_F(SyncContextProxyImplTest
, SyncDisconnectsFirst
) {
99 scoped_ptr
<ModelTypeProcessorImpl
> themes_sync_proxy(
100 new ModelTypeProcessorImpl(syncer::THEMES
,
101 base::WeakPtr
<ModelTypeStore
>()));
102 themes_sync_proxy
->Enable(GetProxy());
104 base::RunLoop run_loop_
;
105 run_loop_
.RunUntilIdle();
107 EXPECT_TRUE(themes_sync_proxy
->IsConnected());
111 } // namespace syncer