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
3 // found in the LICENSE file.
5 #include "chrome/browser/sync/sync_stopped_reporter.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "base/test/test_simple_task_runner.h"
10 #include "base/threading/non_thread_safe.h"
11 #include "net/http/http_status_code.h"
12 #include "net/url_request/test_url_fetcher_factory.h"
13 #include "net/url_request/url_request_test_util.h"
14 #include "sync/protocol/sync.pb.h"
15 #include "testing/gtest/include/gtest/gtest.h"
18 using browser_sync::SyncStoppedReporter
;
20 const char kTestURL
[] = "http://chromium.org/test";
21 const char kTestURLTrailingSlash
[] = "http://chromium.org/test/";
22 const char kEventURL
[] = "http://chromium.org/test/event";
24 const char kAuthToken
[] = "multipass";
25 const char kCacheGuid
[] = "leeloo";
26 const char kBirthday
[] = "2263";
28 class SyncStoppedReporterTest
: public testing::Test
{
30 SyncStoppedReporterTest() {}
31 ~SyncStoppedReporterTest() override
{}
33 void SetUp() override
{
34 request_context_
= new net::TestURLRequestContextGetter(
35 message_loop_
.task_runner());
38 void RequestFinishedCallback(const SyncStoppedReporter::Result
& result
) {
39 request_result_
= result
;
43 return GURL(kTestURL
);
46 SyncStoppedReporter::ResultCallback
callback() {
47 return base::Bind(&SyncStoppedReporterTest::RequestFinishedCallback
,
48 base::Unretained(this));
51 const SyncStoppedReporter::Result
& request_result() const {
52 return request_result_
;
55 net::URLRequestContextGetter
* request_context() {
56 return request_context_
.get();
60 base::MessageLoop message_loop_
;
61 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
62 SyncStoppedReporter::Result request_result_
;
64 DISALLOW_COPY_AND_ASSIGN(SyncStoppedReporterTest
);
67 // Test that the event URL gets constructed correctly.
68 TEST_F(SyncStoppedReporterTest
, EventURL
) {
69 net::TestURLFetcherFactory factory
;
70 SyncStoppedReporter
ssr(GURL(kTestURL
), request_context(), callback());
71 ssr
.ReportSyncStopped(kAuthToken
, kCacheGuid
, kBirthday
);
72 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
73 EXPECT_EQ(kEventURL
, fetcher
->GetOriginalURL().spec());
76 // Test that the event URL gets constructed correctly with a trailing slash.
77 TEST_F(SyncStoppedReporterTest
, EventURLWithSlash
) {
78 net::TestURLFetcherFactory factory
;
79 SyncStoppedReporter
ssr(GURL(kTestURLTrailingSlash
),
80 request_context(), callback());
81 ssr
.ReportSyncStopped(kAuthToken
, kCacheGuid
, kBirthday
);
82 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
83 EXPECT_EQ(kEventURL
, fetcher
->GetOriginalURL().spec());
86 // Test that the URLFetcher gets configured correctly.
87 TEST_F(SyncStoppedReporterTest
, FetcherConfiguration
) {
88 net::TestURLFetcherFactory factory
;
89 SyncStoppedReporter
ssr(test_url(), request_context(), callback());
90 ssr
.ReportSyncStopped(kAuthToken
, kCacheGuid
, kBirthday
);
91 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
93 sync_pb::EventRequest event_request
;
94 event_request
.ParseFromString(fetcher
->upload_data());
96 EXPECT_EQ(kCacheGuid
, event_request
.sync_disabled().cache_guid());
97 EXPECT_EQ(kBirthday
, event_request
.sync_disabled().store_birthday());
98 EXPECT_EQ(kEventURL
, fetcher
->GetOriginalURL().spec());
101 TEST_F(SyncStoppedReporterTest
, HappyCase
) {
102 net::TestURLFetcherFactory factory
;
103 SyncStoppedReporter
ssr(test_url(), request_context(), callback());
104 ssr
.ReportSyncStopped(kAuthToken
, kCacheGuid
, kBirthday
);
105 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
106 fetcher
->set_response_code(net::HTTP_OK
);
107 ssr
.OnURLFetchComplete(fetcher
);
108 base::RunLoop run_loop
;
109 run_loop
.RunUntilIdle();
110 EXPECT_EQ(SyncStoppedReporter::RESULT_SUCCESS
, request_result());
113 TEST_F(SyncStoppedReporterTest
, ServerNotFound
) {
114 net::TestURLFetcherFactory factory
;
115 SyncStoppedReporter
ssr(test_url(), request_context(), callback());
116 ssr
.ReportSyncStopped(kAuthToken
, kCacheGuid
, kBirthday
);
117 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
118 fetcher
->set_response_code(net::HTTP_NOT_FOUND
);
119 ssr
.OnURLFetchComplete(fetcher
);
120 base::RunLoop run_loop
;
121 run_loop
.RunUntilIdle();
122 EXPECT_EQ(SyncStoppedReporter::RESULT_ERROR
, request_result());
125 TEST_F(SyncStoppedReporterTest
, DestructionDuringRequestHandler
) {
126 net::TestURLFetcherFactory factory
;
127 factory
.set_remove_fetcher_on_delete(true);
129 SyncStoppedReporter
ssr(test_url(), request_context(), callback());
130 ssr
.ReportSyncStopped(kAuthToken
, kCacheGuid
, kBirthday
);
131 EXPECT_FALSE(factory
.GetFetcherByID(0) == nullptr);
133 EXPECT_TRUE(factory
.GetFetcherByID(0) == nullptr);
136 TEST_F(SyncStoppedReporterTest
, Timeout
) {
137 SyncStoppedReporter
ssr(test_url(), request_context(), callback());
139 // A task runner that can trigger the timeout immediately.
140 scoped_refptr
<base::TestSimpleTaskRunner
> task_runner(
141 new base::TestSimpleTaskRunner());
142 ssr
.SetTimerTaskRunnerForTest(task_runner
);
145 ssr
.ReportSyncStopped(kAuthToken
, kCacheGuid
, kBirthday
);
147 // Trigger the timeout.
148 ASSERT_TRUE(task_runner
->HasPendingTask());
149 task_runner
->RunPendingTasks();
151 base::RunLoop run_loop
;
152 run_loop
.RunUntilIdle();
153 EXPECT_EQ(SyncStoppedReporter::RESULT_TIMEOUT
, request_result());
156 TEST_F(SyncStoppedReporterTest
, NoCallback
) {
157 net::TestURLFetcherFactory factory
;
158 SyncStoppedReporter
ssr(GURL(kTestURL
), request_context(),
159 SyncStoppedReporter::ResultCallback());
160 ssr
.ReportSyncStopped(kAuthToken
, kCacheGuid
, kBirthday
);
161 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
162 fetcher
->set_response_code(net::HTTP_OK
);
163 ssr
.OnURLFetchComplete(fetcher
);
166 TEST_F(SyncStoppedReporterTest
, NoCallbackTimeout
) {
167 SyncStoppedReporter
ssr(GURL(kTestURL
), request_context(),
168 SyncStoppedReporter::ResultCallback());
170 // A task runner that can trigger the timeout immediately.
171 scoped_refptr
<base::TestSimpleTaskRunner
> task_runner(
172 new base::TestSimpleTaskRunner());
173 ssr
.SetTimerTaskRunnerForTest(task_runner
);
176 ssr
.ReportSyncStopped(kAuthToken
, kCacheGuid
, kBirthday
);
178 // Trigger the timeout.
179 ASSERT_TRUE(task_runner
->HasPendingTask());
180 task_runner
->RunPendingTasks();