Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(214)

Side by Side Diff: components/sync/driver/sync_stopped_reporter_unittest.cc

Issue 2203673002: [Sync] Move //components/sync_driver to //components/sync/driver. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@sd-a
Patch Set: Full change rebased on static lib. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <string> 7 #include <string>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "base/test/test_simple_task_runner.h" 12 #include "base/test/test_simple_task_runner.h"
13 #include "base/threading/non_thread_safe.h" 13 #include "base/threading/non_thread_safe.h"
14 #include "components/sync/protocol/sync.pb.h" 14 #include "components/sync/protocol/sync.pb.h"
15 #include "net/http/http_status_code.h" 15 #include "net/http/http_status_code.h"
(...skipping 14 matching lines...) Expand all
30 const char kBirthday[] = "2263"; 30 const char kBirthday[] = "2263";
31 31
32 const char kAuthHeaderPrefix[] = "Bearer "; 32 const char kAuthHeaderPrefix[] = "Bearer ";
33 33
34 class SyncStoppedReporterTest : public testing::Test { 34 class SyncStoppedReporterTest : public testing::Test {
35 public: 35 public:
36 SyncStoppedReporterTest() {} 36 SyncStoppedReporterTest() {}
37 ~SyncStoppedReporterTest() override {} 37 ~SyncStoppedReporterTest() override {}
38 38
39 void SetUp() override { 39 void SetUp() override {
40 request_context_ = new net::TestURLRequestContextGetter( 40 request_context_ =
41 message_loop_.task_runner()); 41 new net::TestURLRequestContextGetter(message_loop_.task_runner());
42 } 42 }
43 43
44 void RequestFinishedCallback(const SyncStoppedReporter::Result& result) { 44 void RequestFinishedCallback(const SyncStoppedReporter::Result& result) {
45 request_result_ = result; 45 request_result_ = result;
46 } 46 }
47 47
48 GURL test_url() { 48 GURL test_url() { return GURL(kTestURL); }
49 return GURL(kTestURL);
50 }
51 49
52 std::string user_agent() const { 50 std::string user_agent() const { return std::string(kTestUserAgent); }
53 return std::string(kTestUserAgent);
54 }
55 51
56 SyncStoppedReporter::ResultCallback callback() { 52 SyncStoppedReporter::ResultCallback callback() {
57 return base::Bind(&SyncStoppedReporterTest::RequestFinishedCallback, 53 return base::Bind(&SyncStoppedReporterTest::RequestFinishedCallback,
58 base::Unretained(this)); 54 base::Unretained(this));
59 } 55 }
60 56
61 const SyncStoppedReporter::Result& request_result() const { 57 const SyncStoppedReporter::Result& request_result() const {
62 return request_result_; 58 return request_result_;
63 } 59 }
64 60
65 net::URLRequestContextGetter* request_context() { 61 net::URLRequestContextGetter* request_context() {
66 return request_context_.get(); 62 return request_context_.get();
67 } 63 }
68 64
69 private: 65 private:
70 base::MessageLoop message_loop_; 66 base::MessageLoop message_loop_;
71 scoped_refptr<net::URLRequestContextGetter> request_context_; 67 scoped_refptr<net::URLRequestContextGetter> request_context_;
72 SyncStoppedReporter::Result request_result_; 68 SyncStoppedReporter::Result request_result_;
73 69
74 DISALLOW_COPY_AND_ASSIGN(SyncStoppedReporterTest); 70 DISALLOW_COPY_AND_ASSIGN(SyncStoppedReporterTest);
75 }; 71 };
76 72
77 // Test that the event URL gets constructed correctly. 73 // Test that the event URL gets constructed correctly.
78 TEST_F(SyncStoppedReporterTest, EventURL) { 74 TEST_F(SyncStoppedReporterTest, EventURL) {
79 net::TestURLFetcherFactory factory; 75 net::TestURLFetcherFactory factory;
80 SyncStoppedReporter ssr(GURL(kTestURL), user_agent(), 76 SyncStoppedReporter ssr(GURL(kTestURL), user_agent(), request_context(),
81 request_context(), callback()); 77 callback());
82 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday); 78 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
83 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 79 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
84 EXPECT_EQ(kEventURL, fetcher->GetOriginalURL().spec()); 80 EXPECT_EQ(kEventURL, fetcher->GetOriginalURL().spec());
85 } 81 }
86 82
87 // Test that the event URL gets constructed correctly with a trailing slash. 83 // Test that the event URL gets constructed correctly with a trailing slash.
88 TEST_F(SyncStoppedReporterTest, EventURLWithSlash) { 84 TEST_F(SyncStoppedReporterTest, EventURLWithSlash) {
89 net::TestURLFetcherFactory factory; 85 net::TestURLFetcherFactory factory;
90 SyncStoppedReporter ssr(GURL(kTestURLTrailingSlash), user_agent(), 86 SyncStoppedReporter ssr(GURL(kTestURLTrailingSlash), user_agent(),
91 request_context(), callback()); 87 request_context(), callback());
92 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday); 88 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
93 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 89 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
94 EXPECT_EQ(kEventURL, fetcher->GetOriginalURL().spec()); 90 EXPECT_EQ(kEventURL, fetcher->GetOriginalURL().spec());
95 } 91 }
96 92
97 // Test that the URLFetcher gets configured correctly. 93 // Test that the URLFetcher gets configured correctly.
98 TEST_F(SyncStoppedReporterTest, FetcherConfiguration) { 94 TEST_F(SyncStoppedReporterTest, FetcherConfiguration) {
99 net::TestURLFetcherFactory factory; 95 net::TestURLFetcherFactory factory;
100 SyncStoppedReporter ssr(test_url(), user_agent(), 96 SyncStoppedReporter ssr(test_url(), user_agent(), request_context(),
101 request_context(), callback()); 97 callback());
102 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday); 98 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
103 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 99 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
104 100
105 // Ensure the headers are set correctly. 101 // Ensure the headers are set correctly.
106 net::HttpRequestHeaders headers; 102 net::HttpRequestHeaders headers;
107 std::string header; 103 std::string header;
108 fetcher->GetExtraRequestHeaders(&headers); 104 fetcher->GetExtraRequestHeaders(&headers);
109 headers.GetHeader(net::HttpRequestHeaders::kAuthorization, &header); 105 headers.GetHeader(net::HttpRequestHeaders::kAuthorization, &header);
110 std::string auth_header(kAuthHeaderPrefix); 106 std::string auth_header(kAuthHeaderPrefix);
111 auth_header.append(kAuthToken); 107 auth_header.append(kAuthToken);
112 EXPECT_EQ(auth_header, header); 108 EXPECT_EQ(auth_header, header);
113 headers.GetHeader(net::HttpRequestHeaders::kUserAgent, &header); 109 headers.GetHeader(net::HttpRequestHeaders::kUserAgent, &header);
114 EXPECT_EQ(user_agent(), header); 110 EXPECT_EQ(user_agent(), header);
115 111
116 sync_pb::EventRequest event_request; 112 sync_pb::EventRequest event_request;
117 event_request.ParseFromString(fetcher->upload_data()); 113 event_request.ParseFromString(fetcher->upload_data());
118 114
119 EXPECT_EQ(kCacheGuid, event_request.sync_disabled().cache_guid()); 115 EXPECT_EQ(kCacheGuid, event_request.sync_disabled().cache_guid());
120 EXPECT_EQ(kBirthday, event_request.sync_disabled().store_birthday()); 116 EXPECT_EQ(kBirthday, event_request.sync_disabled().store_birthday());
121 EXPECT_EQ(kEventURL, fetcher->GetOriginalURL().spec()); 117 EXPECT_EQ(kEventURL, fetcher->GetOriginalURL().spec());
122 } 118 }
123 119
124 TEST_F(SyncStoppedReporterTest, HappyCase) { 120 TEST_F(SyncStoppedReporterTest, HappyCase) {
125 net::TestURLFetcherFactory factory; 121 net::TestURLFetcherFactory factory;
126 SyncStoppedReporter ssr(test_url(), user_agent(), 122 SyncStoppedReporter ssr(test_url(), user_agent(), request_context(),
127 request_context(), callback()); 123 callback());
128 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday); 124 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
129 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 125 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
130 fetcher->set_response_code(net::HTTP_OK); 126 fetcher->set_response_code(net::HTTP_OK);
131 ssr.OnURLFetchComplete(fetcher); 127 ssr.OnURLFetchComplete(fetcher);
132 base::RunLoop run_loop; 128 base::RunLoop run_loop;
133 run_loop.RunUntilIdle(); 129 run_loop.RunUntilIdle();
134 EXPECT_EQ(SyncStoppedReporter::RESULT_SUCCESS, request_result()); 130 EXPECT_EQ(SyncStoppedReporter::RESULT_SUCCESS, request_result());
135 } 131 }
136 132
137 TEST_F(SyncStoppedReporterTest, ServerNotFound) { 133 TEST_F(SyncStoppedReporterTest, ServerNotFound) {
138 net::TestURLFetcherFactory factory; 134 net::TestURLFetcherFactory factory;
139 SyncStoppedReporter ssr(test_url(), user_agent(), 135 SyncStoppedReporter ssr(test_url(), user_agent(), request_context(),
140 request_context(), callback()); 136 callback());
141 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday); 137 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
142 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 138 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
143 fetcher->set_response_code(net::HTTP_NOT_FOUND); 139 fetcher->set_response_code(net::HTTP_NOT_FOUND);
144 ssr.OnURLFetchComplete(fetcher); 140 ssr.OnURLFetchComplete(fetcher);
145 base::RunLoop run_loop; 141 base::RunLoop run_loop;
146 run_loop.RunUntilIdle(); 142 run_loop.RunUntilIdle();
147 EXPECT_EQ(SyncStoppedReporter::RESULT_ERROR, request_result()); 143 EXPECT_EQ(SyncStoppedReporter::RESULT_ERROR, request_result());
148 } 144 }
149 145
150 TEST_F(SyncStoppedReporterTest, DestructionDuringRequestHandler) { 146 TEST_F(SyncStoppedReporterTest, DestructionDuringRequestHandler) {
151 net::TestURLFetcherFactory factory; 147 net::TestURLFetcherFactory factory;
152 factory.set_remove_fetcher_on_delete(true); 148 factory.set_remove_fetcher_on_delete(true);
153 { 149 {
154 SyncStoppedReporter ssr(test_url(), user_agent(), 150 SyncStoppedReporter ssr(test_url(), user_agent(), request_context(),
155 request_context(), callback()); 151 callback());
156 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday); 152 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
157 EXPECT_FALSE(factory.GetFetcherByID(0) == nullptr); 153 EXPECT_FALSE(factory.GetFetcherByID(0) == nullptr);
158 } 154 }
159 EXPECT_TRUE(factory.GetFetcherByID(0) == nullptr); 155 EXPECT_TRUE(factory.GetFetcherByID(0) == nullptr);
160 } 156 }
161 157
162 TEST_F(SyncStoppedReporterTest, Timeout) { 158 TEST_F(SyncStoppedReporterTest, Timeout) {
163 SyncStoppedReporter ssr(test_url(), user_agent(), 159 SyncStoppedReporter ssr(test_url(), user_agent(), request_context(),
164 request_context(), callback()); 160 callback());
165 161
166 // A task runner that can trigger the timeout immediately. 162 // A task runner that can trigger the timeout immediately.
167 scoped_refptr<base::TestSimpleTaskRunner> task_runner( 163 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
168 new base::TestSimpleTaskRunner()); 164 new base::TestSimpleTaskRunner());
169 ssr.SetTimerTaskRunnerForTest(task_runner); 165 ssr.SetTimerTaskRunnerForTest(task_runner);
170 166
171 // Begin request. 167 // Begin request.
172 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday); 168 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
173 169
174 // Trigger the timeout. 170 // Trigger the timeout.
(...skipping 24 matching lines...) Expand all
199 new base::TestSimpleTaskRunner()); 195 new base::TestSimpleTaskRunner());
200 ssr.SetTimerTaskRunnerForTest(task_runner); 196 ssr.SetTimerTaskRunnerForTest(task_runner);
201 197
202 // Begin request. 198 // Begin request.
203 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday); 199 ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
204 200
205 // Trigger the timeout. 201 // Trigger the timeout.
206 ASSERT_TRUE(task_runner->HasPendingTask()); 202 ASSERT_TRUE(task_runner->HasPendingTask());
207 task_runner->RunPendingTasks(); 203 task_runner->RunPendingTasks();
208 } 204 }
OLDNEW
« no previous file with comments | « components/sync/driver/sync_stopped_reporter.cc ('k') | components/sync/driver/sync_type_preference_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698