| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/sync/driver/sync_stopped_reporter.h" | 5 #include "components/sync/driver/sync_stopped_reporter.h" |
| 6 | 6 |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/test/test_simple_task_runner.h" | 9 #include "base/test/scoped_mock_time_message_loop_task_runner.h" |
| 10 #include "base/threading/non_thread_safe.h" | 10 #include "base/threading/non_thread_safe.h" |
| 11 #include "components/sync/protocol/sync.pb.h" | 11 #include "components/sync/protocol/sync.pb.h" |
| 12 #include "net/http/http_status_code.h" | 12 #include "net/http/http_status_code.h" |
| 13 #include "net/url_request/test_url_fetcher_factory.h" | 13 #include "net/url_request/test_url_fetcher_factory.h" |
| 14 #include "net/url_request/url_request_test_util.h" | 14 #include "net/url_request/url_request_test_util.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 namespace syncer { | 17 namespace syncer { |
| 18 | 18 |
| 19 const char kTestURL[] = "http://chromium.org/test"; | 19 const char kTestURL[] = "http://chromium.org/test"; |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 { | 145 { |
| 146 SyncStoppedReporter ssr(test_url(), user_agent(), request_context(), | 146 SyncStoppedReporter ssr(test_url(), user_agent(), request_context(), |
| 147 callback()); | 147 callback()); |
| 148 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday); | 148 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday); |
| 149 EXPECT_NE(nullptr, factory.GetFetcherByID(0)); | 149 EXPECT_NE(nullptr, factory.GetFetcherByID(0)); |
| 150 } | 150 } |
| 151 EXPECT_EQ(nullptr, factory.GetFetcherByID(0)); | 151 EXPECT_EQ(nullptr, factory.GetFetcherByID(0)); |
| 152 } | 152 } |
| 153 | 153 |
| 154 TEST_F(SyncStoppedReporterTest, Timeout) { | 154 TEST_F(SyncStoppedReporterTest, Timeout) { |
| 155 // Mock the underlying loop's clock to trigger the timer at will. |
| 156 base::ScopedMockTimeMessageLoopTaskRunner mock_main_runner; |
| 157 |
| 155 SyncStoppedReporter ssr(test_url(), user_agent(), request_context(), | 158 SyncStoppedReporter ssr(test_url(), user_agent(), request_context(), |
| 156 callback()); | 159 callback()); |
| 157 | 160 |
| 158 // A task runner that can trigger the timeout immediately. | |
| 159 scoped_refptr<base::TestSimpleTaskRunner> task_runner( | |
| 160 new base::TestSimpleTaskRunner()); | |
| 161 ssr.SetTimerTaskRunnerForTest(task_runner); | |
| 162 | |
| 163 // Begin request. | 161 // Begin request. |
| 164 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday); | 162 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday); |
| 165 | 163 |
| 166 // Trigger the timeout. | 164 // Trigger the timeout. |
| 167 ASSERT_TRUE(task_runner->HasPendingTask()); | 165 ASSERT_TRUE(mock_main_runner->HasPendingTask()); |
| 168 task_runner->RunPendingTasks(); | 166 mock_main_runner->FastForwardUntilNoTasksRemain(); |
| 169 | |
| 170 base::RunLoop run_loop; | |
| 171 run_loop.RunUntilIdle(); | |
| 172 EXPECT_EQ(SyncStoppedReporter::RESULT_TIMEOUT, request_result()); | 167 EXPECT_EQ(SyncStoppedReporter::RESULT_TIMEOUT, request_result()); |
| 173 } | 168 } |
| 174 | 169 |
| 175 TEST_F(SyncStoppedReporterTest, NoCallback) { | 170 TEST_F(SyncStoppedReporterTest, NoCallback) { |
| 176 net::TestURLFetcherFactory factory; | 171 net::TestURLFetcherFactory factory; |
| 177 SyncStoppedReporter ssr(GURL(kTestURL), user_agent(), request_context(), | 172 SyncStoppedReporter ssr(GURL(kTestURL), user_agent(), request_context(), |
| 178 SyncStoppedReporter::ResultCallback()); | 173 SyncStoppedReporter::ResultCallback()); |
| 179 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday); | 174 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday); |
| 180 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 175 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 181 fetcher->set_response_code(net::HTTP_OK); | 176 fetcher->set_response_code(net::HTTP_OK); |
| 182 ssr.OnURLFetchComplete(fetcher); | 177 ssr.OnURLFetchComplete(fetcher); |
| 183 } | 178 } |
| 184 | 179 |
| 185 TEST_F(SyncStoppedReporterTest, NoCallbackTimeout) { | 180 TEST_F(SyncStoppedReporterTest, NoCallbackTimeout) { |
| 181 // Mock the underlying loop's clock to trigger the timer at will. |
| 182 base::ScopedMockTimeMessageLoopTaskRunner mock_main_runner; |
| 183 |
| 186 SyncStoppedReporter ssr(GURL(kTestURL), user_agent(), request_context(), | 184 SyncStoppedReporter ssr(GURL(kTestURL), user_agent(), request_context(), |
| 187 SyncStoppedReporter::ResultCallback()); | 185 SyncStoppedReporter::ResultCallback()); |
| 188 | 186 |
| 189 // A task runner that can trigger the timeout immediately. | |
| 190 scoped_refptr<base::TestSimpleTaskRunner> task_runner( | |
| 191 new base::TestSimpleTaskRunner()); | |
| 192 ssr.SetTimerTaskRunnerForTest(task_runner); | |
| 193 | |
| 194 // Begin request. | 187 // Begin request. |
| 195 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday); | 188 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday); |
| 196 | 189 |
| 197 // Trigger the timeout. | 190 // Trigger the timeout. |
| 198 ASSERT_TRUE(task_runner->HasPendingTask()); | 191 ASSERT_TRUE(mock_main_runner->HasPendingTask()); |
| 199 task_runner->RunPendingTasks(); | 192 mock_main_runner->FastForwardUntilNoTasksRemain(); |
| 200 } | 193 } |
| 201 | 194 |
| 202 } // namespace syncer | 195 } // namespace syncer |
| OLD | NEW |