OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_SYNC_PROFILE_SYNC_TEST_UTIL_H_ | 5 #ifndef CHROME_BROWSER_SYNC_PROFILE_SYNC_TEST_UTIL_H_ |
6 #define CHROME_BROWSER_SYNC_PROFILE_SYNC_TEST_UTIL_H_ | 6 #define CHROME_BROWSER_SYNC_PROFILE_SYNC_TEST_UTIL_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/ref_counted.h" | 13 #include "base/ref_counted.h" |
14 #include "base/scoped_ptr.h" | 14 #include "base/scoped_ptr.h" |
15 #include "base/task.h" | 15 #include "base/task.h" |
16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
17 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
19 #include "chrome/browser/browser_thread.h" | 19 #include "chrome/browser/browser_thread.h" |
20 #include "chrome/browser/webdata/web_database.h" | 20 #include "chrome/browser/webdata/web_database.h" |
21 #include "chrome/browser/sync/abstract_profile_sync_service_test.h" | 21 #include "chrome/browser/sync/abstract_profile_sync_service_test.h" |
22 #include "chrome/browser/sync/glue/bookmark_change_processor.h" | 22 #include "chrome/browser/sync/glue/bookmark_change_processor.h" |
23 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h" | 23 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h" |
24 #include "chrome/browser/sync/glue/bookmark_model_associator.h" | 24 #include "chrome/browser/sync/glue/bookmark_model_associator.h" |
25 #include "chrome/browser/sync/glue/change_processor.h" | 25 #include "chrome/browser/sync/glue/change_processor.h" |
26 #include "chrome/browser/sync/glue/data_type_manager_impl.h" | 26 #include "chrome/browser/sync/glue/data_type_manager_impl.h" |
27 #include "chrome/browser/sync/glue/sync_backend_host.h" | 27 #include "chrome/browser/sync/glue/sync_backend_host.h" |
28 #include "chrome/browser/sync/profile_sync_factory.h" | 28 #include "chrome/browser/sync/profile_sync_factory.h" |
29 #include "chrome/browser/sync/profile_sync_service.h" | 29 #include "chrome/browser/sync/profile_sync_service.h" |
| 30 #include "chrome/browser/sync/syncable/model_type.h" |
30 #include "chrome/browser/sync/unrecoverable_error_handler.h" | 31 #include "chrome/browser/sync/unrecoverable_error_handler.h" |
31 #include "chrome/common/notification_details.h" | 32 #include "chrome/common/notification_details.h" |
32 #include "chrome/common/notification_service.h" | 33 #include "chrome/common/notification_service.h" |
33 #include "chrome/common/notification_source.h" | 34 #include "chrome/common/notification_source.h" |
34 #include "chrome/common/notification_type.h" | 35 #include "chrome/common/notification_type.h" |
35 #include "chrome/test/sync/test_http_bridge_factory.h" | 36 #include "chrome/test/sync/test_http_bridge_factory.h" |
36 #include "testing/gmock/include/gmock/gmock.h" | 37 #include "testing/gmock/include/gmock/gmock.h" |
37 | 38 |
38 ACTION_P(Notify, type) { | 39 ACTION_P(Notify, type) { |
39 NotificationService::current()->Notify(type, | 40 NotificationService::current()->Notify(type, |
40 NotificationService::AllSources(), | 41 NotificationService::AllSources(), |
41 NotificationService::NoDetails()); | 42 NotificationService::NoDetails()); |
42 } | 43 } |
43 | 44 |
44 ACTION(QuitUIMessageLoop) { | 45 ACTION(QuitUIMessageLoop) { |
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
46 MessageLoop::current()->Quit(); | 47 MessageLoop::current()->Quit(); |
47 } | 48 } |
48 | 49 |
49 class TestModelAssociatorHelper { | |
50 public: | |
51 explicit TestModelAssociatorHelper(browser_sync::TestIdFactory* id_factory) | |
52 : id_factory_(id_factory) { | |
53 } | |
54 | |
55 template <class ModelAssociatorImpl> | |
56 bool GetSyncIdForTaggedNode(ModelAssociatorImpl* associator, | |
57 const std::string& tag, int64* sync_id) { | |
58 std::wstring tag_wide; | |
59 if (!UTF8ToWide(tag.c_str(), tag.length(), &tag_wide)) { | |
60 NOTREACHED() << "Unable to convert UTF8 to wide for string: " << tag; | |
61 return false; | |
62 } | |
63 | |
64 browser_sync::SyncBackendHost::UserShareHandle share( | |
65 associator->sync_service()->backend()->GetUserShareHandle()); | |
66 bool root_exists = false; | |
67 ModelType type = ModelAssociatorImpl::model_type(); | |
68 { | |
69 sync_api::WriteTransaction trans(share); | |
70 sync_api::ReadNode uber_root(&trans); | |
71 uber_root.InitByRootLookup(); | |
72 | |
73 sync_api::ReadNode root(&trans); | |
74 root_exists = root.InitByTagLookup( | |
75 ProfileSyncServiceTestHelper::GetTagForType(type)); | |
76 } | |
77 | |
78 if (!root_exists) { | |
79 bool created = ProfileSyncServiceTestHelper::CreateRoot( | |
80 type, | |
81 associator->sync_service(), | |
82 id_factory_); | |
83 if (!created) | |
84 return false; | |
85 } | |
86 | |
87 sync_api::WriteTransaction trans(share); | |
88 sync_api::ReadNode root(&trans); | |
89 EXPECT_TRUE(root.InitByTagLookup( | |
90 ProfileSyncServiceTestHelper::GetTagForType(type))); | |
91 | |
92 // First, try to find a node with the title among the root's children. | |
93 // This will be the case if we are testing model persistence, and | |
94 // are reloading a sync repository created earlier in the test. | |
95 int64 last_child_id = sync_api::kInvalidId; | |
96 for (int64 id = root.GetFirstChildId(); id != sync_api::kInvalidId; /***/) { | |
97 sync_api::ReadNode child(&trans); | |
98 child.InitByIdLookup(id); | |
99 last_child_id = id; | |
100 if (tag_wide == child.GetTitle()) { | |
101 *sync_id = id; | |
102 return true; | |
103 } | |
104 id = child.GetSuccessorId(); | |
105 } | |
106 | |
107 sync_api::ReadNode predecessor_node(&trans); | |
108 sync_api::ReadNode* predecessor = NULL; | |
109 if (last_child_id != sync_api::kInvalidId) { | |
110 predecessor_node.InitByIdLookup(last_child_id); | |
111 predecessor = &predecessor_node; | |
112 } | |
113 sync_api::WriteNode node(&trans); | |
114 // Create new fake tagged nodes at the end of the ordering. | |
115 node.InitByCreation(ModelAssociatorImpl::model_type(), root, predecessor); | |
116 node.SetIsFolder(true); | |
117 node.SetTitle(tag_wide); | |
118 node.SetExternalId(0); | |
119 *sync_id = node.GetId(); | |
120 return true; | |
121 } | |
122 | |
123 ~TestModelAssociatorHelper() {} | |
124 private: | |
125 browser_sync::TestIdFactory* id_factory_; | |
126 }; | |
127 | |
128 class ProfileSyncServiceObserverMock : public ProfileSyncServiceObserver { | 50 class ProfileSyncServiceObserverMock : public ProfileSyncServiceObserver { |
129 public: | 51 public: |
130 MOCK_METHOD0(OnStateChanged, void()); | 52 MOCK_METHOD0(OnStateChanged, void()); |
131 }; | 53 }; |
132 | 54 |
133 class ThreadNotificationService | 55 class ThreadNotificationService |
134 : public base::RefCountedThreadSafe<ThreadNotificationService> { | 56 : public base::RefCountedThreadSafe<ThreadNotificationService> { |
135 public: | 57 public: |
136 explicit ThreadNotificationService(base::Thread* notification_thread) | 58 explicit ThreadNotificationService(base::Thread* notification_thread) |
137 : done_event_(false, false), | 59 : done_event_(false, false), |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 const NotificationDetails& details) { | 127 const NotificationDetails& details) { |
206 NotificationService::current()->Notify(type, source, details); | 128 NotificationService::current()->Notify(type, source, details); |
207 done_event_.Signal(); | 129 done_event_.Signal(); |
208 } | 130 } |
209 | 131 |
210 base::WaitableEvent done_event_; | 132 base::WaitableEvent done_event_; |
211 base::Thread* notify_thread_; | 133 base::Thread* notify_thread_; |
212 }; | 134 }; |
213 | 135 |
214 #endif // CHROME_BROWSER_SYNC_PROFILE_SYNC_TEST_UTIL_H_ | 136 #endif // CHROME_BROWSER_SYNC_PROFILE_SYNC_TEST_UTIL_H_ |
OLD | NEW |