OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chrome/browser/invalidation/invalidation_service_factory.h" | 11 #include "chrome/browser/invalidation/invalidation_service_factory.h" |
12 #include "chrome/browser/signin/signin_manager.h" | 12 #include "chrome/browser/signin/signin_manager.h" |
13 #include "chrome/browser/signin/signin_manager_factory.h" | 13 #include "chrome/browser/signin/signin_manager_factory.h" |
14 #include "chrome/browser/signin/token_service.h" | 14 #include "chrome/browser/signin/token_service.h" |
15 #include "chrome/browser/signin/token_service_factory.h" | 15 #include "chrome/browser/signin/token_service_factory.h" |
16 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h" | 16 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h" |
17 #include "chrome/browser/sync/glue/data_type_controller.h" | 17 #include "chrome/browser/sync/glue/data_type_controller.h" |
18 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" | 18 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" |
19 #include "chrome/browser/sync/test_profile_sync_service.h" | 19 #include "chrome/browser/sync/test_profile_sync_service.h" |
20 #include "chrome/common/chrome_version_info.h" | 20 #include "chrome/common/chrome_version_info.h" |
21 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
22 #include "chrome/test/base/testing_pref_service_syncable.h" | 22 #include "chrome/test/base/testing_pref_service_syncable.h" |
23 #include "chrome/test/base/testing_profile.h" | 23 #include "chrome/test/base/testing_profile.h" |
24 #include "content/public/test/test_browser_thread.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
| 25 #include "content/public/test/test_utils.h" |
25 #include "google/cacheinvalidation/include/types.h" | 26 #include "google/cacheinvalidation/include/types.h" |
26 #include "google_apis/gaia/gaia_constants.h" | 27 #include "google_apis/gaia/gaia_constants.h" |
27 #include "sync/js/js_arg_list.h" | 28 #include "sync/js/js_arg_list.h" |
28 #include "sync/js/js_event_details.h" | 29 #include "sync/js/js_event_details.h" |
29 #include "sync/js/js_test_util.h" | 30 #include "sync/js/js_test_util.h" |
30 #include "testing/gmock/include/gmock/gmock.h" | 31 #include "testing/gmock/include/gmock/gmock.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
32 | 33 |
33 // TODO(akalin): Add tests here that exercise the whole | 34 // TODO(akalin): Add tests here that exercise the whole |
34 // ProfileSyncService/SyncBackendHost stack while mocking out as | 35 // ProfileSyncService/SyncBackendHost stack while mocking out as |
35 // little as possible. | 36 // little as possible. |
36 | 37 |
37 namespace browser_sync { | 38 namespace browser_sync { |
38 | 39 |
39 namespace { | 40 namespace { |
40 | 41 |
41 using content::BrowserThread; | |
42 using testing::_; | 42 using testing::_; |
43 using testing::AtLeast; | 43 using testing::AtLeast; |
44 using testing::AtMost; | 44 using testing::AtMost; |
45 using testing::Mock; | 45 using testing::Mock; |
46 using testing::Return; | 46 using testing::Return; |
47 using testing::StrictMock; | 47 using testing::StrictMock; |
48 | 48 |
49 void SignalDone(base::WaitableEvent* done) { | 49 void SignalDone(base::WaitableEvent* done) { |
50 done->Signal(); | 50 done->Signal(); |
51 } | 51 } |
52 | 52 |
53 class ProfileSyncServiceTestHarness { | 53 class ProfileSyncServiceTestHarness { |
54 public: | 54 public: |
55 ProfileSyncServiceTestHarness() | 55 ProfileSyncServiceTestHarness() |
56 : ui_thread_(BrowserThread::UI, &ui_loop_), | 56 : thread_bundle_(content::TestBrowserThreadBundle::REAL_DB_THREAD | |
57 db_thread_(BrowserThread::DB), | 57 content::TestBrowserThreadBundle::REAL_FILE_THREAD | |
58 file_thread_(BrowserThread::FILE), | 58 content::TestBrowserThreadBundle::REAL_IO_THREAD) { |
59 io_thread_(BrowserThread::IO) {} | 59 } |
60 | 60 |
61 ~ProfileSyncServiceTestHarness() {} | 61 ~ProfileSyncServiceTestHarness() {} |
62 | 62 |
63 void SetUp() { | 63 void SetUp() { |
64 db_thread_.Start(); | |
65 file_thread_.Start(); | |
66 io_thread_.StartIOThread(); | |
67 profile.reset(new TestingProfile()); | 64 profile.reset(new TestingProfile()); |
68 profile->CreateRequestContext(); | |
69 invalidation::InvalidationServiceFactory::GetInstance()-> | 65 invalidation::InvalidationServiceFactory::GetInstance()-> |
70 SetBuildOnlyFakeInvalidatorsForTest(true); | 66 SetBuildOnlyFakeInvalidatorsForTest(true); |
71 ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory( | 67 ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory( |
72 profile.get(), FakeOAuth2TokenService::BuildTokenService); | 68 profile.get(), FakeOAuth2TokenService::BuildTokenService); |
73 } | 69 } |
74 | 70 |
75 void TearDown() { | 71 void TearDown() { |
76 // Kill the service before the profile. | 72 // Kill the service before the profile. |
77 if (service) { | 73 if (service) { |
78 service->Shutdown(); | 74 service->Shutdown(); |
79 } | 75 } |
80 service.reset(); | 76 service.reset(); |
81 profile.reset(); | 77 profile.reset(); |
82 // Pump messages posted by the sync thread (which may end up | 78 // Pump messages posted by the sync thread (which may end up |
83 // posting on the IO thread). | 79 // posting on the IO thread). |
84 ui_loop_.RunUntilIdle(); | 80 base::RunLoop().RunUntilIdle(); |
85 io_thread_.Stop(); | 81 content::RunAllPendingInMessageLoop(content::BrowserThread::IO); |
86 file_thread_.Stop(); | 82 base::RunLoop().RunUntilIdle(); |
87 db_thread_.Stop(); | |
88 // Ensure that the sync objects destruct to avoid memory leaks. | |
89 ui_loop_.RunUntilIdle(); | |
90 } | 83 } |
91 | 84 |
92 // TODO(akalin): Refactor the StartSyncService*() functions below. | 85 // TODO(akalin): Refactor the StartSyncService*() functions below. |
93 | 86 |
94 void StartSyncService() { | 87 void StartSyncService() { |
95 StartSyncServiceAndSetInitialSyncEnded( | 88 StartSyncServiceAndSetInitialSyncEnded( |
96 true, true, false, true, syncer::STORAGE_IN_MEMORY); | 89 true, true, false, true, syncer::STORAGE_IN_MEMORY); |
97 } | 90 } |
98 | 91 |
99 void StartSyncServiceAndSetInitialSyncEnded( | 92 void StartSyncServiceAndSetInitialSyncEnded( |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 } | 126 } |
134 } | 127 } |
135 | 128 |
136 void WaitForBackendInitDone() { | 129 void WaitForBackendInitDone() { |
137 for (int i = 0; i < 5; ++i) { | 130 for (int i = 0; i < 5; ++i) { |
138 base::WaitableEvent done(false, false); | 131 base::WaitableEvent done(false, false); |
139 service->GetBackendForTest()->GetSyncLoopForTesting() | 132 service->GetBackendForTest()->GetSyncLoopForTesting() |
140 ->PostTask(FROM_HERE, | 133 ->PostTask(FROM_HERE, |
141 base::Bind(&SignalDone, &done)); | 134 base::Bind(&SignalDone, &done)); |
142 done.Wait(); | 135 done.Wait(); |
143 ui_loop_.RunUntilIdle(); | 136 base::RunLoop().RunUntilIdle(); |
144 if (service->sync_initialized()) { | 137 if (service->sync_initialized()) { |
145 return; | 138 return; |
146 } | 139 } |
147 } | 140 } |
148 LOG(ERROR) << "Backend not initialized."; | 141 LOG(ERROR) << "Backend not initialized."; |
149 } | 142 } |
150 | 143 |
151 void IssueTestTokens() { | 144 void IssueTestTokens() { |
152 TokenService* token_service = | 145 TokenService* token_service = |
153 TokenServiceFactory::GetForProfile(profile.get()); | 146 TokenServiceFactory::GetForProfile(profile.get()); |
154 token_service->IssueAuthTokenForTest( | 147 token_service->IssueAuthTokenForTest( |
155 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token"); | 148 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token"); |
156 token_service->IssueAuthTokenForTest( | 149 token_service->IssueAuthTokenForTest( |
157 GaiaConstants::kSyncService, "token"); | 150 GaiaConstants::kSyncService, "token"); |
158 } | 151 } |
159 | 152 |
160 scoped_ptr<TestProfileSyncService> service; | 153 scoped_ptr<TestProfileSyncService> service; |
161 scoped_ptr<TestingProfile> profile; | 154 scoped_ptr<TestingProfile> profile; |
162 | 155 |
163 private: | 156 private: |
164 base::MessageLoop ui_loop_; | 157 content::TestBrowserThreadBundle thread_bundle_; |
165 // Needed by |service|. | |
166 content::TestBrowserThread ui_thread_; | |
167 content::TestBrowserThread db_thread_; | |
168 // Needed by DisableAndEnableSyncTemporarily test case. | |
169 content::TestBrowserThread file_thread_; | |
170 // Needed by |service| and |profile|'s request context. | |
171 content::TestBrowserThread io_thread_; | |
172 }; | 158 }; |
173 | 159 |
174 class TestProfileSyncServiceObserver : public ProfileSyncServiceObserver { | 160 class TestProfileSyncServiceObserver : public ProfileSyncServiceObserver { |
175 public: | 161 public: |
176 explicit TestProfileSyncServiceObserver(ProfileSyncService* service) | 162 explicit TestProfileSyncServiceObserver(ProfileSyncService* service) |
177 : service_(service), first_setup_in_progress_(false) {} | 163 : service_(service), first_setup_in_progress_(false) {} |
178 virtual void OnStateChanged() OVERRIDE { | 164 virtual void OnStateChanged() OVERRIDE { |
179 first_setup_in_progress_ = service_->FirstSetupInProgress(); | 165 first_setup_in_progress_ = service_->FirstSetupInProgress(); |
180 } | 166 } |
181 bool first_setup_in_progress() const { return first_setup_in_progress_; } | 167 bool first_setup_in_progress() const { return first_setup_in_progress_; } |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 TEST_F(ProfileSyncServiceTest, FailToDownloadControlTypes) { | 479 TEST_F(ProfileSyncServiceTest, FailToDownloadControlTypes) { |
494 harness_.StartSyncServiceAndSetInitialSyncEnded(false, true, true, true, | 480 harness_.StartSyncServiceAndSetInitialSyncEnded(false, true, true, true, |
495 syncer::STORAGE_IN_MEMORY); | 481 syncer::STORAGE_IN_MEMORY); |
496 | 482 |
497 // The backend is not ready. Ensure the PSS knows this. | 483 // The backend is not ready. Ensure the PSS knows this. |
498 EXPECT_FALSE(harness_.service->sync_initialized()); | 484 EXPECT_FALSE(harness_.service->sync_initialized()); |
499 } | 485 } |
500 | 486 |
501 } // namespace | 487 } // namespace |
502 } // namespace browser_sync | 488 } // namespace browser_sync |
OLD | NEW |