OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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. | |
4 | |
5 #include "base/file_path.h" | |
6 #include "base/md5.h" | |
7 #include "base/memory/ref_counted.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/message_loop_proxy.h" | |
10 #include "base/stringprintf.h" | |
11 #include "chrome/common/cloud_print/cloud_print_constants.h" | |
12 #include "chrome/service/cloud_print/cloud_print_helpers.h" | |
13 #include "chrome/service/cloud_print/cloud_print_token_store.h" | |
14 #include "chrome/service/cloud_print/print_system.h" | |
15 #include "chrome/service/cloud_print/printer_job_handler.h" | |
16 #include "chrome/service/cloud_print/printer_job_handler_unittest_constants.h" | |
17 #include "net/http/http_response_headers.h" | |
18 #include "net/url_request/test_url_fetcher_factory.h" | |
19 #include "net/url_request/url_request_test_util.h" | |
20 #include "printing/backend/print_backend.h" | |
21 #include "testing/gmock/include/gmock/gmock.h" | |
22 #include "testing/gtest/include/gtest/gtest.h" | |
23 | |
24 using ::testing::AtLeast; | |
25 using ::testing::Exactly; | |
26 using ::testing::Sequence; | |
27 using ::testing::Return; | |
28 using ::testing::SaveArg; | |
29 using ::testing::DoAll; | |
30 using ::testing::_; | |
31 using ::testing::NiceMock; | |
32 using ::testing::StrictMock; | |
33 using ::testing::Invoke; | |
34 using ::testing::SetArgPointee; | |
35 using ::testing::InvokeWithoutArgs; | |
36 | |
37 namespace cloud_print { | |
38 | |
39 class CloudPrintURLFetcherNoServiceProcess | |
40 : public CloudPrintURLFetcher { | |
41 protected: | |
42 net::URLRequestContextGetter* GetRequestContextGetter() { | |
43 return new net::TestURLRequestContextGetter( | |
44 base::MessageLoopProxy::current()); | |
45 } | |
46 }; | |
47 | |
48 | |
49 class CloudPrintURLFetcherNoServiceProcessFactory | |
50 : public CloudPrintURLFetcherFactory { | |
51 public: | |
52 CloudPrintURLFetcher* CreateCloudPrintURLFetcher() { | |
53 return new CloudPrintURLFetcherNoServiceProcess; | |
54 } | |
55 }; | |
56 | |
57 | |
58 // This class handles the callback from FakeURLFetcher | |
59 // It is a separate class because callback methods must be | |
60 // on RefCounted classes | |
61 | |
62 class TestURLFetcherCallback | |
63 : public base::RefCounted<TestURLFetcherCallback> { | |
64 public: | |
65 net::FakeURLFetcher* CreateURLFetcher( | |
66 const GURL& url, | |
67 net::URLFetcherDelegate* d, | |
68 const std::string& response_data, bool success) { | |
69 net::FakeURLFetcher* fetcher = | |
70 new net::FakeURLFetcher(url, d, response_data, success); | |
71 OnRequestCreate(url, fetcher); | |
72 return fetcher; | |
73 } | |
74 MOCK_METHOD2(OnRequestCreate, | |
75 void(const GURL&, net::FakeURLFetcher*)); | |
76 }; | |
77 | |
78 | |
79 class MockPrinterJobHandlerDelegate | |
80 : public PrinterJobHandler::Delegate { | |
81 public: | |
82 MOCK_METHOD0(OnAuthError, void()); | |
83 MOCK_METHOD1(OnPrinterDeleted, void(const std::string& str)); | |
84 }; | |
85 | |
86 | |
87 class MockPrintServerWatcher | |
88 : public PrintSystem::PrintServerWatcher { | |
89 protected: | |
90 PrintSystem::PrintServerWatcher::Delegate* delegate_; | |
91 | |
92 bool SetDelegate(PrintSystem::PrintServerWatcher::Delegate* d) { | |
93 delegate_ = d; | |
94 return true; | |
95 } | |
96 | |
97 public: | |
98 MOCK_METHOD1(StartWatching, | |
99 bool(PrintSystem::PrintServerWatcher::Delegate* d)); | |
100 MOCK_METHOD0(StopWatching, bool()); | |
101 | |
102 MockPrintServerWatcher(); | |
103 PrintSystem::PrintServerWatcher::Delegate* Delegate() { | |
104 return delegate_; | |
105 } | |
106 }; | |
107 | |
108 class MockPrinterWatcher : public PrintSystem::PrinterWatcher { | |
109 protected: | |
110 PrintSystem::PrinterWatcher::Delegate* delegate_; | |
111 | |
112 bool SetDelegate(PrintSystem::PrinterWatcher::Delegate* d) { | |
113 delegate_ = d; | |
114 return true; | |
115 } | |
116 | |
117 public: | |
118 MOCK_METHOD1(StartWatching, bool(PrintSystem::PrinterWatcher::Delegate* d)); | |
119 MOCK_METHOD0(StopWatching, bool()); | |
120 MOCK_METHOD1(GetCurrentPrinterInfo, | |
121 bool(printing::PrinterBasicInfo* printer_info)); | |
122 | |
123 MockPrinterWatcher(); | |
124 PrintSystem::PrinterWatcher::Delegate* Delegate() { return delegate_; } | |
125 }; | |
126 | |
127 | |
128 class MockJobSpooler : public PrintSystem::JobSpooler { | |
129 protected: | |
130 PrintSystem::JobSpooler::Delegate* delegate_; | |
131 public: | |
132 MOCK_METHOD7(Spool, bool( | |
133 const std::string& print_ticket, | |
134 const FilePath& print_data_file_path, | |
135 const std::string& print_data_mime_type, | |
136 const std::string& printer_name, | |
137 const std::string& job_title, | |
138 const std::vector<std::string>& tags, | |
139 PrintSystem::JobSpooler::Delegate* delegate)); | |
140 | |
141 MockJobSpooler(); | |
142 PrintSystem::JobSpooler::Delegate* Delegate() { return delegate_; } | |
143 }; | |
144 | |
145 | |
146 | |
147 class MockPrintSystem : public PrintSystem { | |
148 protected: | |
149 scoped_refptr<MockJobSpooler> job_spooler_; | |
150 scoped_refptr<MockPrinterWatcher> printer_watcher_; | |
151 scoped_refptr<MockPrintServerWatcher> print_server_watcher_; | |
152 | |
153 public: | |
154 MockPrintSystem(); | |
155 PrintSystem::PrintSystemResult succeed() { | |
156 return PrintSystem::PrintSystemResult(true, "success"); | |
157 } | |
158 | |
159 PrintSystem::PrintSystemResult fail() { | |
160 return PrintSystem::PrintSystemResult(false, "failure"); | |
161 } | |
162 | |
163 MockJobSpooler& JobSpooler() { | |
164 return *job_spooler_; | |
165 } | |
166 | |
167 MockPrinterWatcher& PrinterWatcher() { | |
168 return *printer_watcher_; | |
169 } | |
170 | |
171 MockPrintServerWatcher& PrintServerWatcher() { | |
172 return *print_server_watcher_; | |
173 } | |
174 | |
175 | |
176 MOCK_METHOD0(Init, PrintSystem::PrintSystemResult()); | |
177 MOCK_METHOD1(EnumeratePrinters, PrintSystem::PrintSystemResult( | |
178 printing::PrinterList* printer_list)); | |
179 | |
180 MOCK_METHOD2( | |
181 GetPrinterCapsAndDefaults, | |
182 void(const std::string& printer_name, | |
183 const PrintSystem::PrinterCapsAndDefaultsCallback& callback)); | |
184 | |
185 | |
186 MOCK_METHOD1(IsValidPrinter, bool(const std::string& printer_name)); | |
187 | |
188 | |
189 MOCK_METHOD2(ValidatePrintTicket, bool(const std::string& printer_name, | |
190 const std::string& print_ticket_data)); | |
191 | |
192 MOCK_METHOD3(GetJobDetails, bool(const std::string& printer_name, | |
193 PlatformJobId job_id, | |
194 PrintJobDetails* job_details)); | |
195 | |
196 | |
197 | |
198 MOCK_METHOD0(CreatePrintServerWatcher, PrintSystem::PrintServerWatcher*()); | |
199 MOCK_METHOD1(CreatePrinterWatcher, | |
200 PrintSystem::PrinterWatcher*(const std::string& printer_name)); | |
201 MOCK_METHOD0(CreateJobSpooler, PrintSystem::JobSpooler*()); | |
202 | |
203 | |
204 MOCK_METHOD0(GetSupportedMimeTypes, std::string()); | |
205 }; | |
206 | |
207 | |
208 class PrinterJobHandlerTest : public ::testing::Test { | |
209 public: | |
210 MessageLoopForIO loop_; | |
211 scoped_refptr<TestURLFetcherCallback > url_callback_; | |
Vitaly Buka (NO REVIEWS)
2013/02/12 21:57:08
no need space before >
Noam Samuel (WRONG ACCOUNT)
2013/02/12 22:50:18
Done.
| |
212 MockPrinterJobHandlerDelegate jobhandler_delegate_; | |
213 CloudPrintTokenStore token_store_; | |
214 | |
215 CloudPrintURLFetcherNoServiceProcessFactory cloud_print_factory_; | |
216 scoped_refptr<PrinterJobHandler> job_handler_; | |
217 scoped_refptr<NiceMock<MockPrintSystem> > print_system_; | |
218 net::FakeURLFetcherFactory factory_; | |
219 printing::PrinterBasicInfo basic_info_; | |
220 printing::PrinterCapsAndDefaults caps_and_defaults_; | |
221 PrinterJobHandler::PrinterInfoFromCloud info_from_cloud_; | |
222 | |
223 PrinterJobHandlerTest(); | |
224 void TearDown(); | |
225 void IdleOut(); | |
226 bool GetPrinterInfo(printing::PrinterBasicInfo* info); | |
227 void SendCapsAndDefaults( | |
228 const std::string& printer_name, | |
229 const PrintSystem::PrinterCapsAndDefaultsCallback& callback); | |
230 void AddMimeHeader(const GURL& url, net::FakeURLFetcher* fetcher); | |
231 bool PostSpoolSuccess(); | |
232 | |
233 static void MessageLoopQuitNowHelper(MessageLoop* message_loop); | |
234 static void MessageLoopQuitSoonHelper(MessageLoop* message_loop); | |
235 }; | |
236 | |
237 | |
238 | |
239 void PrinterJobHandlerTest::MessageLoopQuitNowHelper( | |
240 MessageLoop* message_loop) { | |
241 message_loop->QuitWhenIdle(); | |
242 } | |
243 | |
244 void PrinterJobHandlerTest::MessageLoopQuitSoonHelper( | |
245 MessageLoop* message_loop) { | |
246 message_loop->message_loop_proxy()->PostTask( | |
247 FROM_HERE, | |
248 base::Bind(&MessageLoopQuitNowHelper, message_loop)); | |
249 } | |
250 | |
251 PrinterJobHandlerTest::PrinterJobHandlerTest() | |
252 : url_callback_(new TestURLFetcherCallback), | |
253 factory_(base::Bind( | |
254 &TestURLFetcherCallback::CreateURLFetcher, | |
Vitaly Buka (NO REVIEWS)
2013/02/12 21:57:08
You can avoid base::RefCounted for TestURLFetcherC
Noam Samuel (WRONG ACCOUNT)
2013/02/12 22:50:18
Done.
| |
255 url_callback_), NULL) { | |
256 basic_info_.printer_name = kExamplePrinterName; | |
257 basic_info_.printer_description = kExamplePrinterDescription; | |
258 basic_info_.is_default = 0; | |
259 | |
Vitaly Buka (NO REVIEWS)
2013/02/12 21:57:08
recomended places for gtest initialization and shu
Noam Samuel (WRONG ACCOUNT)
2013/02/12 22:50:18
Done.
| |
260 info_from_cloud_.printer_id = kExamplePrinterID; | |
261 info_from_cloud_.tags_hash = GetHashOfPrinterInfo(basic_info_); | |
262 | |
263 info_from_cloud_.caps_hash = base::MD5String(kExamplePrinterCapabilities); | |
264 | |
265 caps_and_defaults_.printer_capabilities = kExamplePrinterCapabilities; | |
266 caps_and_defaults_.caps_mime_type = kExampleCapsMimeType; | |
267 caps_and_defaults_.printer_defaults = kExampleDefaults; | |
268 caps_and_defaults_.defaults_mime_type = kExampleDefaultMimeType; | |
269 | |
270 print_system_ = new NiceMock<MockPrintSystem>(); | |
271 | |
272 token_store_.SetToken(kExampleCloudPrintOAuthToken); | |
273 | |
274 ON_CALL(print_system_->PrinterWatcher(), GetCurrentPrinterInfo(_)) | |
275 .WillByDefault(Invoke(this, &PrinterJobHandlerTest::GetPrinterInfo)); | |
276 | |
277 ON_CALL(*print_system_, GetPrinterCapsAndDefaults(_, _)) | |
278 .WillByDefault(Invoke(this, &PrinterJobHandlerTest::SendCapsAndDefaults)); | |
279 | |
280 CloudPrintURLFetcher::set_factory(&cloud_print_factory_); | |
281 } | |
282 | |
283 bool PrinterJobHandlerTest::PostSpoolSuccess() { | |
284 MessageLoop::current()->PostTask( | |
285 FROM_HERE, | |
286 base::Bind( | |
287 &PrinterJobHandler::OnJobSpoolSucceeded, | |
288 job_handler_, 0)); | |
289 | |
290 // Everything that would be posted on the printer thread queue | |
291 // has been posted, we can tell the main message loop to quit when idle | |
292 // and not worry about it idling while the print thread does work | |
293 MessageLoop::current()->PostTask(FROM_HERE, | |
294 base::Bind(&MessageLoopQuitSoonHelper, &loop_)); | |
295 return true; | |
296 } | |
297 | |
298 void PrinterJobHandlerTest::AddMimeHeader(const GURL& url, | |
299 net::FakeURLFetcher* fetcher) { | |
300 scoped_refptr<net::HttpResponseHeaders> download_headers = | |
301 new net::HttpResponseHeaders(kExampleJobDownloadResponseHeaders); | |
302 fetcher->set_response_headers(download_headers); | |
303 } | |
304 | |
305 | |
306 void PrinterJobHandlerTest::SendCapsAndDefaults( | |
307 const std::string& printer_name, | |
308 const PrintSystem::PrinterCapsAndDefaultsCallback& callback) { | |
309 callback.Run(true, printer_name, caps_and_defaults_); | |
310 } | |
311 | |
312 bool PrinterJobHandlerTest::GetPrinterInfo(printing::PrinterBasicInfo* info) { | |
313 *info = basic_info_; | |
314 return true; | |
315 } | |
316 | |
317 void PrinterJobHandlerTest::TearDown() { | |
318 IdleOut(); | |
319 } | |
320 | |
321 void PrinterJobHandlerTest::IdleOut() { | |
322 MessageLoop::current()->RunUntilIdle(); | |
323 } | |
324 | |
325 MockPrintServerWatcher::MockPrintServerWatcher() : delegate_(NULL) { | |
326 ON_CALL(*this, StartWatching(_)) | |
327 .WillByDefault(Invoke(this, &MockPrintServerWatcher::SetDelegate)); | |
328 ON_CALL(*this, StopWatching()).WillByDefault(Return(true)); | |
329 } | |
330 | |
331 | |
332 MockPrinterWatcher::MockPrinterWatcher() : delegate_(NULL) { | |
333 ON_CALL(*this, StartWatching(_)) | |
334 .WillByDefault(Invoke(this, &MockPrinterWatcher::SetDelegate)); | |
335 ON_CALL(*this, StopWatching()).WillByDefault(Return(true)); | |
336 } | |
337 | |
338 MockJobSpooler::MockJobSpooler() : delegate_(NULL) { | |
339 ON_CALL(*this, Spool(_, _, _, _, _, _, _)) | |
340 .WillByDefault(DoAll(SaveArg<6>(&delegate_), Return(true))); | |
341 } | |
342 | |
343 | |
344 MockPrintSystem::MockPrintSystem() | |
345 : job_spooler_(new NiceMock<MockJobSpooler>()), | |
346 printer_watcher_(new NiceMock<MockPrinterWatcher>()), | |
347 print_server_watcher_(new NiceMock<MockPrintServerWatcher>()) { | |
348 ON_CALL(*this, CreateJobSpooler()) | |
349 .WillByDefault(Return(job_spooler_)); | |
350 | |
351 ON_CALL(*this, CreatePrinterWatcher(_)) | |
352 .WillByDefault(Return(printer_watcher_)); | |
353 | |
354 ON_CALL(*this, CreatePrintServerWatcher()) | |
355 .WillByDefault(Return(print_server_watcher_)); | |
356 | |
357 ON_CALL(*this, IsValidPrinter(_)). | |
358 WillByDefault(Return(true)); | |
359 | |
360 ON_CALL(*this, ValidatePrintTicket(_, _)). | |
361 WillByDefault(Return(true)); | |
362 }; | |
363 | |
364 // This test simulates an end-to-end printing of a document | |
365 // but tests only non-failure cases. | |
366 TEST_F(PrinterJobHandlerTest, HappyPathTest) { | |
367 GURL InProgressURL = | |
368 GetUrlForJobStatusUpdate(GURL(kExampleCloudPrintServerURL), | |
369 kExampleJobID, | |
370 PRINT_JOB_STATUS_IN_PROGRESS); | |
371 | |
372 factory_.SetFakeResponse(kExamplePrintTicketURI, kExamplePrintTicket, true); | |
373 factory_.SetFakeResponse(kExamplePrintDownloadURI, kExamplePrintData, true); | |
374 factory_.SetFakeResponse( | |
375 StringPrintf(kExamplePrinterJobListURI, kJobFetchReasonStartup), | |
376 kExampleJobListResponse, true); | |
377 | |
378 | |
379 factory_.SetFakeResponse( | |
380 base::StringPrintf(kExamplePrinterJobListURI, kJobFetchReasonQueryMore), | |
381 kExampleJobListResponseEmpty, true); | |
382 | |
383 factory_.SetFakeResponse( | |
384 kExampleUpdateDoneURL, | |
385 base::StringPrintf(kExampleControlResponse, "DONE", "DONE"), true); | |
386 | |
387 factory_.SetFakeResponse( | |
388 InProgressURL.spec(), | |
389 base::StringPrintf(kExampleControlResponse, | |
390 "IN_PROGRESS", "IN_PROGRESS"), | |
391 true); | |
392 | |
393 | |
394 job_handler_ = new PrinterJobHandler(basic_info_, info_from_cloud_, | |
395 GURL(kExampleCloudPrintServerURL), | |
396 print_system_, &jobhandler_delegate_); | |
397 | |
398 EXPECT_CALL(*url_callback_, OnRequestCreate( | |
399 GURL(base::StringPrintf(kExamplePrinterJobListURI, "startup")), _)) | |
400 .Times(Exactly(1)); | |
401 | |
402 EXPECT_CALL(*url_callback_, OnRequestCreate( | |
403 GURL(base::StringPrintf(kExamplePrinterJobListURI, "querymore")), _)) | |
404 .Times(Exactly(1)); | |
405 | |
406 EXPECT_CALL(*url_callback_, OnRequestCreate(GURL(kExamplePrintTicketURI), _)) | |
407 .Times(Exactly(1)); | |
408 | |
409 EXPECT_CALL(*url_callback_, OnRequestCreate( | |
410 GURL(kExamplePrintDownloadURI), _)) | |
411 .Times(Exactly(1)) | |
412 .WillOnce(Invoke(this, &PrinterJobHandlerTest::AddMimeHeader)); | |
413 | |
414 EXPECT_CALL(*url_callback_, OnRequestCreate(InProgressURL, _)) | |
415 .Times(Exactly(1)); | |
416 | |
417 EXPECT_CALL(*url_callback_, OnRequestCreate(GURL(kExampleUpdateDoneURL), _)) | |
418 .Times(Exactly(1)); | |
419 | |
420 EXPECT_CALL(print_system_->JobSpooler(), | |
421 Spool(kExamplePrintTicket, _, _, _, _, _, _)) | |
422 .Times(Exactly(1)) | |
423 .WillOnce(InvokeWithoutArgs(this, | |
424 &PrinterJobHandlerTest::PostSpoolSuccess)); | |
425 | |
426 | |
427 job_handler_->Initialize(); | |
428 | |
429 MessageLoop::current()->PostDelayedTask( | |
430 FROM_HERE, | |
431 base::Bind(&PrinterJobHandlerTest::MessageLoopQuitSoonHelper, | |
432 MessageLoop::current()), | |
433 base::TimeDelta::FromSeconds(1)); | |
434 | |
435 MessageLoop::current()->Run(); | |
436 } | |
Vitaly Buka (NO REVIEWS)
2013/02/12 21:57:08
empty line before } // namespace cloud_print
Noam Samuel (WRONG ACCOUNT)
2013/02/12 22:50:18
Done.
| |
437 } // namespace cloud_print | |
438 | |
OLD | NEW |