OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "sync/internal_api/model_type_connector_proxy.h" | |
6 | |
7 #include <utility> | |
8 #include <vector> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/memory/ptr_util.h" | |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/run_loop.h" | |
14 #include "base/sequenced_task_runner.h" | |
15 #include "base/threading/thread_task_runner_handle.h" | |
16 #include "sync/api/fake_model_type_service.h" | |
17 #include "sync/internal_api/public/activation_context.h" | |
18 #include "sync/internal_api/public/base/model_type.h" | |
19 #include "sync/internal_api/public/model_type_connector.h" | |
20 #include "sync/internal_api/public/shared_model_type_processor.h" | |
21 #include "sync/internal_api/public/test/data_type_error_handler_mock.h" | |
22 #include "sync/sessions/model_type_registry.h" | |
23 #include "sync/test/engine/mock_nudge_handler.h" | |
24 #include "sync/test/engine/test_directory_setter_upper.h" | |
25 #include "testing/gtest/include/gtest/gtest.h" | |
26 | |
27 namespace syncer_v2 { | |
28 | |
29 class ModelTypeConnectorProxyTest : public ::testing::Test, | |
30 FakeModelTypeService { | |
31 public: | |
32 ModelTypeConnectorProxyTest() | |
33 : sync_task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
34 type_task_runner_(base::ThreadTaskRunnerHandle::Get()) {} | |
35 | |
36 void SetUp() override { | |
37 dir_maker_.SetUp(); | |
38 registry_.reset(new syncer::ModelTypeRegistry( | |
39 workers_, dir_maker_.directory(), &nudge_handler_)); | |
40 connector_proxy_.reset( | |
41 new ModelTypeConnectorProxy(sync_task_runner_, registry_->AsWeakPtr())); | |
42 } | |
43 | |
44 void TearDown() override { | |
45 connector_proxy_.reset(); | |
46 registry_.reset(); | |
47 dir_maker_.TearDown(); | |
48 } | |
49 | |
50 // The sync thread could be shut down at any time without warning. This | |
51 // function simulates such an event. | |
52 void DisableSync() { registry_.reset(); } | |
53 | |
54 void OnSyncStarting(SharedModelTypeProcessor* processor) { | |
55 processor->OnSyncStarting( | |
56 &error_handler_, | |
57 base::Bind(&ModelTypeConnectorProxyTest::OnReadyToConnect, | |
58 base::Unretained(this))); | |
59 } | |
60 | |
61 void OnReadyToConnect(syncer::SyncError error, | |
62 std::unique_ptr<ActivationContext> context) { | |
63 connector_proxy_->ConnectType(syncer::THEMES, std::move(context)); | |
64 } | |
65 | |
66 std::unique_ptr<SharedModelTypeProcessor> CreateModelTypeProcessor() { | |
67 std::unique_ptr<SharedModelTypeProcessor> processor = | |
68 base::WrapUnique(new SharedModelTypeProcessor(syncer::THEMES, this)); | |
69 processor->OnMetadataLoaded(syncer::SyncError(), | |
70 base::WrapUnique(new MetadataBatch())); | |
71 return processor; | |
72 } | |
73 | |
74 private: | |
75 base::MessageLoop loop_; | |
76 scoped_refptr<base::SingleThreadTaskRunner> sync_task_runner_; | |
77 scoped_refptr<base::SingleThreadTaskRunner> type_task_runner_; | |
78 | |
79 std::vector<scoped_refptr<syncer::ModelSafeWorker>> workers_; | |
80 syncer::TestDirectorySetterUpper dir_maker_; | |
81 syncer::MockNudgeHandler nudge_handler_; | |
82 std::unique_ptr<syncer::ModelTypeRegistry> registry_; | |
83 syncer::DataTypeErrorHandlerMock error_handler_; | |
84 | |
85 std::unique_ptr<ModelTypeConnectorProxy> connector_proxy_; | |
86 }; | |
87 | |
88 // Try to connect a type to a ModelTypeConnector that has already shut down. | |
89 TEST_F(ModelTypeConnectorProxyTest, FailToConnect1) { | |
90 std::unique_ptr<SharedModelTypeProcessor> processor = | |
91 CreateModelTypeProcessor(); | |
92 DisableSync(); | |
93 OnSyncStarting(processor.get()); | |
94 | |
95 base::RunLoop run_loop_; | |
96 run_loop_.RunUntilIdle(); | |
97 EXPECT_FALSE(processor->IsConnected()); | |
98 } | |
99 | |
100 // Try to connect a type to a ModelTypeConnector as it shuts down. | |
101 TEST_F(ModelTypeConnectorProxyTest, FailToConnect2) { | |
102 std::unique_ptr<SharedModelTypeProcessor> processor = | |
103 CreateModelTypeProcessor(); | |
104 OnSyncStarting(processor.get()); | |
105 DisableSync(); | |
106 | |
107 base::RunLoop run_loop_; | |
108 run_loop_.RunUntilIdle(); | |
109 EXPECT_FALSE(processor->IsConnected()); | |
110 } | |
111 | |
112 // Tests the case where the type's sync proxy shuts down first. | |
113 TEST_F(ModelTypeConnectorProxyTest, TypeDisconnectsFirst) { | |
114 std::unique_ptr<SharedModelTypeProcessor> processor = | |
115 CreateModelTypeProcessor(); | |
116 OnSyncStarting(processor.get()); | |
117 | |
118 base::RunLoop run_loop_; | |
119 run_loop_.RunUntilIdle(); | |
120 | |
121 EXPECT_TRUE(processor->IsConnected()); | |
122 processor.reset(); | |
123 } | |
124 | |
125 // Tests the case where the sync thread shuts down first. | |
126 TEST_F(ModelTypeConnectorProxyTest, SyncDisconnectsFirst) { | |
127 std::unique_ptr<SharedModelTypeProcessor> processor = | |
128 CreateModelTypeProcessor(); | |
129 OnSyncStarting(processor.get()); | |
130 | |
131 base::RunLoop run_loop_; | |
132 run_loop_.RunUntilIdle(); | |
133 | |
134 EXPECT_TRUE(processor->IsConnected()); | |
135 DisableSync(); | |
136 } | |
137 | |
138 } // namespace syncer_v2 | |
OLD | NEW |