Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / sync / sync_stopped_reporter_unittest.cc
blobeab9f14e3b48a0557d49cb04bb5c1f4e8134782a
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 "chrome/browser/sync/glue/local_device_info_provider_impl.h"
12 #include "net/http/http_status_code.h"
13 #include "net/url_request/test_url_fetcher_factory.h"
14 #include "net/url_request/url_request_test_util.h"
15 #include "sync/protocol/sync.pb.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "url/gurl.h"
19 using browser_sync::SyncStoppedReporter;
21 const char kTestURL[] = "http://chromium.org/test";
22 const char kTestURLTrailingSlash[] = "http://chromium.org/test/";
23 const char kEventURL[] = "http://chromium.org/test/event";
25 const char kAuthToken[] = "multipass";
26 const char kCacheGuid[] = "leeloo";
27 const char kBirthday[] = "2263";
29 const char kAuthHeaderPrefix[] = "Bearer ";
31 class SyncStoppedReporterTest : public testing::Test {
32 public:
33 SyncStoppedReporterTest() {}
34 ~SyncStoppedReporterTest() override {}
36 void SetUp() override {
37 request_context_ = new net::TestURLRequestContextGetter(
38 message_loop_.task_runner());
41 void RequestFinishedCallback(const SyncStoppedReporter::Result& result) {
42 request_result_ = result;
45 GURL test_url() {
46 return GURL(kTestURL);
49 SyncStoppedReporter::ResultCallback callback() {
50 return base::Bind(&SyncStoppedReporterTest::RequestFinishedCallback,
51 base::Unretained(this));
54 const SyncStoppedReporter::Result& request_result() const {
55 return request_result_;
58 net::URLRequestContextGetter* request_context() {
59 return request_context_.get();
62 static std::string GetUserAgent() {
63 return browser_sync::LocalDeviceInfoProviderImpl::MakeUserAgentForSyncApi();
66 private:
67 base::MessageLoop message_loop_;
68 scoped_refptr<net::URLRequestContextGetter> request_context_;
69 SyncStoppedReporter::Result request_result_;
71 DISALLOW_COPY_AND_ASSIGN(SyncStoppedReporterTest);
74 // Test that the event URL gets constructed correctly.
75 TEST_F(SyncStoppedReporterTest, EventURL) {
76 net::TestURLFetcherFactory factory;
77 SyncStoppedReporter ssr(GURL(kTestURL), request_context(), callback());
78 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
79 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
80 EXPECT_EQ(kEventURL, fetcher->GetOriginalURL().spec());
83 // Test that the event URL gets constructed correctly with a trailing slash.
84 TEST_F(SyncStoppedReporterTest, EventURLWithSlash) {
85 net::TestURLFetcherFactory factory;
86 SyncStoppedReporter ssr(GURL(kTestURLTrailingSlash),
87 request_context(), callback());
88 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
89 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
90 EXPECT_EQ(kEventURL, fetcher->GetOriginalURL().spec());
93 // Test that the URLFetcher gets configured correctly.
94 TEST_F(SyncStoppedReporterTest, FetcherConfiguration) {
95 net::TestURLFetcherFactory factory;
96 SyncStoppedReporter ssr(test_url(), request_context(), callback());
97 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
98 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
100 // Ensure the headers are set correctly.
101 net::HttpRequestHeaders headers;
102 std::string header;
103 fetcher->GetExtraRequestHeaders(&headers);
104 headers.GetHeader(net::HttpRequestHeaders::kAuthorization, &header);
105 std::string auth_header(kAuthHeaderPrefix);
106 auth_header.append(kAuthToken);
107 EXPECT_EQ(auth_header, header);
108 headers.GetHeader(net::HttpRequestHeaders::kUserAgent, &header);
109 EXPECT_EQ(GetUserAgent(), header);
111 sync_pb::EventRequest event_request;
112 event_request.ParseFromString(fetcher->upload_data());
114 EXPECT_EQ(kCacheGuid, event_request.sync_disabled().cache_guid());
115 EXPECT_EQ(kBirthday, event_request.sync_disabled().store_birthday());
116 EXPECT_EQ(kEventURL, fetcher->GetOriginalURL().spec());
119 TEST_F(SyncStoppedReporterTest, HappyCase) {
120 net::TestURLFetcherFactory factory;
121 SyncStoppedReporter ssr(test_url(), request_context(), callback());
122 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
123 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
124 fetcher->set_response_code(net::HTTP_OK);
125 ssr.OnURLFetchComplete(fetcher);
126 base::RunLoop run_loop;
127 run_loop.RunUntilIdle();
128 EXPECT_EQ(SyncStoppedReporter::RESULT_SUCCESS, request_result());
131 TEST_F(SyncStoppedReporterTest, ServerNotFound) {
132 net::TestURLFetcherFactory factory;
133 SyncStoppedReporter ssr(test_url(), request_context(), callback());
134 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
135 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
136 fetcher->set_response_code(net::HTTP_NOT_FOUND);
137 ssr.OnURLFetchComplete(fetcher);
138 base::RunLoop run_loop;
139 run_loop.RunUntilIdle();
140 EXPECT_EQ(SyncStoppedReporter::RESULT_ERROR, request_result());
143 TEST_F(SyncStoppedReporterTest, DestructionDuringRequestHandler) {
144 net::TestURLFetcherFactory factory;
145 factory.set_remove_fetcher_on_delete(true);
147 SyncStoppedReporter ssr(test_url(), request_context(), callback());
148 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
149 EXPECT_FALSE(factory.GetFetcherByID(0) == nullptr);
151 EXPECT_TRUE(factory.GetFetcherByID(0) == nullptr);
154 TEST_F(SyncStoppedReporterTest, Timeout) {
155 SyncStoppedReporter ssr(test_url(), request_context(), callback());
157 // A task runner that can trigger the timeout immediately.
158 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
159 new base::TestSimpleTaskRunner());
160 ssr.SetTimerTaskRunnerForTest(task_runner);
162 // Begin request.
163 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
165 // Trigger the timeout.
166 ASSERT_TRUE(task_runner->HasPendingTask());
167 task_runner->RunPendingTasks();
169 base::RunLoop run_loop;
170 run_loop.RunUntilIdle();
171 EXPECT_EQ(SyncStoppedReporter::RESULT_TIMEOUT, request_result());
174 TEST_F(SyncStoppedReporterTest, NoCallback) {
175 net::TestURLFetcherFactory factory;
176 SyncStoppedReporter ssr(GURL(kTestURL), request_context(),
177 SyncStoppedReporter::ResultCallback());
178 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
179 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
180 fetcher->set_response_code(net::HTTP_OK);
181 ssr.OnURLFetchComplete(fetcher);
184 TEST_F(SyncStoppedReporterTest, NoCallbackTimeout) {
185 SyncStoppedReporter ssr(GURL(kTestURL), request_context(),
186 SyncStoppedReporter::ResultCallback());
188 // A task runner that can trigger the timeout immediately.
189 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
190 new base::TestSimpleTaskRunner());
191 ssr.SetTimerTaskRunnerForTest(task_runner);
193 // Begin request.
194 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
196 // Trigger the timeout.
197 ASSERT_TRUE(task_runner->HasPendingTask());
198 task_runner->RunPendingTasks();