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
);
63 themes_sync_proxy
.Enable(GetProxy());
65 base::RunLoop run_loop_
;
66 run_loop_
.RunUntilIdle();
67 EXPECT_FALSE(themes_sync_proxy
.IsConnected());
70 // Try to connect a type to a SyncContext as it shuts down.
71 TEST_F(SyncContextProxyImplTest
, FailToConnect2
) {
72 ModelTypeProcessorImpl
themes_sync_proxy(syncer::THEMES
);
73 themes_sync_proxy
.Enable(GetProxy());
76 base::RunLoop run_loop_
;
77 run_loop_
.RunUntilIdle();
78 EXPECT_FALSE(themes_sync_proxy
.IsConnected());
81 // Tests the case where the type's sync proxy shuts down first.
82 TEST_F(SyncContextProxyImplTest
, TypeDisconnectsFirst
) {
83 scoped_ptr
<ModelTypeProcessorImpl
> themes_sync_proxy(
84 new ModelTypeProcessorImpl(syncer::THEMES
));
85 themes_sync_proxy
->Enable(GetProxy());
87 base::RunLoop run_loop_
;
88 run_loop_
.RunUntilIdle();
90 EXPECT_TRUE(themes_sync_proxy
->IsConnected());
91 themes_sync_proxy
.reset();
94 // Tests the case where the sync thread shuts down first.
95 TEST_F(SyncContextProxyImplTest
, SyncDisconnectsFirst
) {
96 scoped_ptr
<ModelTypeProcessorImpl
> themes_sync_proxy(
97 new ModelTypeProcessorImpl(syncer::THEMES
));
98 themes_sync_proxy
->Enable(GetProxy());
100 base::RunLoop run_loop_
;
101 run_loop_
.RunUntilIdle();
103 EXPECT_TRUE(themes_sync_proxy
->IsConnected());
107 } // namespace syncer