Chromium Code Reviews| 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 "net/http/http_response_headers.h" | |
| 17 #include "net/url_request/test_url_fetcher_factory.h" | |
| 18 #include "net/url_request/url_request_test_util.h" | |
| 19 #include "printing/backend/print_backend.h" | |
| 20 #include "testing/gmock/include/gmock/gmock.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 | |
| 23 using ::testing::AtLeast; | |
| 24 using ::testing::Exactly; | |
| 25 using ::testing::Sequence; | |
| 26 using ::testing::Return; | |
| 27 using ::testing::SaveArg; | |
| 28 using ::testing::DoAll; | |
| 29 using ::testing::_; | |
| 30 using ::testing::NiceMock; | |
| 31 using ::testing::StrictMock; | |
| 32 using ::testing::Invoke; | |
| 33 using ::testing::SetArgPointee; | |
| 34 using ::testing::InvokeWithoutArgs; | |
| 35 | |
| 36 namespace cloud_print { | |
| 37 | |
| 38 const char kExampleJobListResponse[] = "{" | |
| 39 " \"success\": true," | |
| 40 " \"jobs\": [" | |
| 41 " {" | |
| 42 " \"tags\": [" | |
| 43 " \"^own\"" | |
| 44 " ]," | |
| 45 " \"printerName\": \"Example Printer\"," | |
| 46 " \"status\": \"QUEUED\"," | |
| 47 " \"ownerId\": \"sampleuser@gmail.com\"," | |
| 48 " \"ticketUrl\": \"https://www.google.com/cloudprint/ticket?exampleURI1\"," | |
| 49 " \"printerid\": \"__example_printer_id\"," | |
| 50 " \"printerType\": \"GOOGLE\"," | |
| 51 " \"contentType\": \"text/html\"," | |
| 52 " \"fileUrl\": \"https://www.google.com/cloudprint/download?exampleURI1\"," | |
| 53 " \"id\": \"__example_job_id1\"," | |
| 54 " \"message\": \"\"," | |
| 55 " \"title\": \"Example Job 1\"," | |
| 56 " \"errorCode\": \"\"," | |
| 57 " \"numberOfPages\": 3" | |
| 58 " }" | |
| 59 " ]," | |
| 60 " \"xsrf_token\": \"AIp06DjUd3AV6BO0aujB9NvM2a9ZbogxOQ:1360021066932\"," | |
| 61 " \"request\": {" | |
| 62 " \"time\": \"0\"," | |
| 63 " \"users\": [" | |
| 64 " \"sampleuser@gmail.com\"" | |
| 65 " ]," | |
| 66 " \"params\": {" | |
| 67 " \"printerid\": [" | |
| 68 " \"__example_printer_id\"" | |
| 69 " ]" | |
| 70 " }," | |
| 71 " \"user\": \"sampleuser@gmail.com\"" | |
| 72 " }" | |
| 73 "}"; | |
| 74 | |
| 75 const char kExampleJobListResponseEmpty[] = "{" | |
| 76 " \"success\": true," | |
| 77 " \"jobs\": [" | |
| 78 " ]," | |
| 79 " \"xsrf_token\": \"AIp06DjUd3AV6BO0aujB9NvM2a9ZbogxOQ:1360021066932\"," | |
| 80 " \"request\": {" | |
| 81 " \"time\": \"0\"," | |
| 82 " \"users\": [" | |
| 83 " \"sampleuser@gmail.com\"" | |
| 84 " ]," | |
| 85 " \"params\": {" | |
| 86 " \"printerid\": [" | |
| 87 " \"__example_printer_id\"" | |
| 88 " ]" | |
| 89 " }," | |
| 90 " \"user\": \"sampleuser@gmail.com\"" | |
| 91 " }" | |
| 92 "}"; | |
| 93 | |
| 94 | |
| 95 | |
| 96 const char kExampleJobID[] = "__example_job_id1"; | |
| 97 | |
| 98 const char kExampleCloudPrintServerURL[] = "https://www.google.com/cloudprint/"; | |
| 99 | |
| 100 const char kExamplePrintTicket[] = "{\"MediaType\":\"plain\"," | |
| 101 "\"Resolution\":\"300x300dpi\",\"PageRegion\":\"Letter\"," | |
| 102 "\"InputSlot\":\"auto\",\"PageSize\":\"Letter\",\"EconoMode\":\"off\"}"; | |
| 103 | |
| 104 const char kExamplePrintTicketURI[] = | |
| 105 "https://www.google.com/cloudprint/ticket?exampleURI1"; | |
| 106 | |
| 107 const char kExamplePrintDownloadURI[] = | |
| 108 "https://www.google.com/cloudprint/download?exampleURI1"; | |
| 109 | |
| 110 // Use StringPrintf to construct | |
| 111 const char kExamplePrinterJobListURI[] = | |
| 112 "https://www.google.com/cloudprint/fetch" | |
| 113 "?printerid=__example_printer_id&deb=%s"; | |
| 114 | |
| 115 // Use StringPrintf to construct | |
| 116 const char kExamplePrinterJobControlURI[] = | |
| 117 "https://www.google.com/cloudprint/control" | |
| 118 "?jobid=__example_printer_id&status=%s"; | |
| 119 | |
| 120 | |
| 121 // Use StringPrintf to construct | |
| 122 const char kExampleControlResponse[] = "{" | |
| 123 " \"success\": true," | |
| 124 " \"message\": \"Print job updated successfully.\"," | |
| 125 " \"xsrf_token\": \"AIp06DjKgbfGalbqzj23V1bU6i-vtR2B4w:1360023068789\"," | |
| 126 " \"request\": {" | |
| 127 " \"time\": \"0\"," | |
| 128 " \"users\": [" | |
| 129 " \"sampleuser@gmail.com\"" | |
| 130 " ]," | |
| 131 " \"params\": {" | |
| 132 " \"xsrf\": [" | |
| 133 " \"AIp06DgeGIETs42Cj28QWmxGPWVDiaXwVQ:1360023041852\"" | |
| 134 " ]," | |
| 135 " \"status\": [" | |
| 136 " \"%s\"" | |
| 137 " ]," | |
| 138 " \"jobid\": [" | |
| 139 " \"__example_job_id1\"" | |
| 140 " ]" | |
| 141 " }," | |
| 142 " \"user\": \"sampleuser@gmail.com\"" | |
| 143 " }," | |
| 144 " \"job\": {" | |
| 145 " \"tags\": [" | |
| 146 " \"^own\"" | |
| 147 " ]," | |
| 148 " \"printerName\": \"Example Printer\"," | |
| 149 " \"status\": \"%s\"," | |
| 150 " \"ownerId\": \"sampleuser@gmail.com\"," | |
| 151 " \"ticketUrl\": \"https://www.google.com/cloudprint/ticket?exampleURI1\"," | |
| 152 " \"printerid\": \"__example_printer_id\"," | |
| 153 " \"contentType\": \"text/html\"," | |
| 154 " \"fileUrl\": \"https://www.google.com/cloudprint/download?exampleURI1\"," | |
| 155 " \"id\": \"__example_job_id1\"," | |
| 156 " \"message\": \"\"," | |
| 157 " \"title\": \"Example Job\"," | |
| 158 " \"errorCode\": \"\"," | |
| 159 " \"numberOfPages\": 3" | |
| 160 " }" | |
| 161 "}"; | |
| 162 | |
| 163 const char kExamplePrinterID[] = "__example_printer_id"; | |
| 164 | |
| 165 const char kExamplePrinterCapabilities[] = ""; | |
| 166 | |
| 167 const char kExampleCapsMimeType[] = ""; | |
| 168 | |
| 169 // These can stay empty | |
| 170 const char kExampleDefaults[] = ""; | |
| 171 | |
| 172 const char kExampleDefaultMimeType[] = ""; | |
| 173 | |
| 174 // Since we're not connecting to the server, this can be any non-empty string. | |
| 175 const char kExampleCloudPrintOAuthToken[] = "__SAMPLE_TOKEN"; | |
| 176 | |
| 177 | |
| 178 // Not actually printing, no need for real PDF. | |
| 179 const char kExamplePrintData[] = "__EXAMPLE_PRINT_DATA"; | |
| 180 | |
| 181 const char kExampleJobDownloadResponseHeaders[] = | |
| 182 "Content-Type: Application/PDF\n"; | |
| 183 | |
| 184 const char kExampleUpdateDoneURL[] = | |
| 185 "https://www.google.com/cloudprint/control?jobid=__example_job_id1" | |
| 186 "&status=DONE&code=0&message=&numpages=0&pagesprinted=0"; | |
| 187 | |
| 188 const char kExamplePrinterName[] = "Example Printer"; | |
| 189 | |
| 190 const char kExamplePrinterDescription[] = "Example Description"; | |
| 191 | |
| 192 class CloudPrintURLFetcherNoServiceProcess | |
| 193 : public CloudPrintURLFetcher { | |
| 194 protected: | |
| 195 virtual net::URLRequestContextGetter* GetRequestContextGetter() { | |
| 196 return new net::TestURLRequestContextGetter( | |
| 197 base::MessageLoopProxy::current()); | |
| 198 } | |
| 199 }; | |
| 200 | |
| 201 | |
| 202 class CloudPrintURLFetcherNoServiceProcessFactory | |
| 203 : public CloudPrintURLFetcherFactory { | |
| 204 public: | |
| 205 virtual CloudPrintURLFetcher* CreateCloudPrintURLFetcher() { | |
| 206 return new CloudPrintURLFetcherNoServiceProcess; | |
| 207 } | |
| 208 | |
| 209 virtual ~CloudPrintURLFetcherNoServiceProcessFactory() {} | |
| 210 }; | |
| 211 | |
| 212 | |
| 213 // This class handles the callback from FakeURLFetcher | |
| 214 // It is a separate class because callback methods must be | |
| 215 // on RefCounted classes | |
| 216 | |
| 217 class TestURLFetcherCallback { | |
| 218 public: | |
| 219 net::FakeURLFetcher* CreateURLFetcher(const GURL& url, | |
| 220 net::URLFetcherDelegate* d, | |
| 221 const std::string& response_data, | |
| 222 bool success) { | |
| 223 net::FakeURLFetcher* fetcher = | |
| 224 new net::FakeURLFetcher(url, d, response_data, success); | |
| 225 OnRequestCreate(url, fetcher); | |
| 226 return fetcher; | |
| 227 } | |
| 228 MOCK_METHOD2(OnRequestCreate, | |
| 229 void(const GURL&, net::FakeURLFetcher*)); | |
| 230 }; | |
| 231 | |
| 232 | |
| 233 class MockPrinterJobHandlerDelegate | |
| 234 : public PrinterJobHandler::Delegate { | |
| 235 public: | |
| 236 MOCK_METHOD0(OnAuthError, void()); | |
| 237 MOCK_METHOD1(OnPrinterDeleted, void(const std::string& str)); | |
| 238 | |
| 239 virtual ~MockPrinterJobHandlerDelegate() {} | |
| 240 }; | |
| 241 | |
| 242 | |
| 243 class MockPrintServerWatcher | |
| 244 : public PrintSystem::PrintServerWatcher { | |
| 245 public: | |
| 246 MOCK_METHOD1(StartWatching, | |
| 247 bool(PrintSystem::PrintServerWatcher::Delegate* d)); | |
| 248 MOCK_METHOD0(StopWatching, bool()); | |
| 249 | |
| 250 MockPrintServerWatcher(); | |
| 251 PrintSystem::PrintServerWatcher::Delegate* delegate() const { | |
| 252 return delegate_; | |
| 253 } | |
| 254 | |
| 255 virtual ~MockPrintServerWatcher() {} | |
|
gene
2013/02/13 03:43:59
Could you please move destructor next to contructo
Noam Samuel (WRONG ACCOUNT)
2013/02/13 18:49:58
Done.
| |
| 256 | |
| 257 private: | |
| 258 PrintSystem::PrintServerWatcher::Delegate* delegate_; | |
| 259 }; | |
| 260 | |
| 261 class MockPrinterWatcher : public PrintSystem::PrinterWatcher { | |
| 262 public: | |
| 263 MOCK_METHOD1(StartWatching, bool(PrintSystem::PrinterWatcher::Delegate* d)); | |
| 264 MOCK_METHOD0(StopWatching, bool()); | |
| 265 MOCK_METHOD1(GetCurrentPrinterInfo, | |
| 266 bool(printing::PrinterBasicInfo* printer_info)); | |
| 267 | |
| 268 MockPrinterWatcher(); | |
| 269 PrintSystem::PrinterWatcher::Delegate* delegate() const { return delegate_; } | |
| 270 | |
| 271 virtual ~MockPrinterWatcher() {} | |
|
gene
2013/02/13 03:43:59
Could you please move destructor next to contructo
Noam Samuel (WRONG ACCOUNT)
2013/02/13 18:49:58
Done.
| |
| 272 | |
| 273 private: | |
| 274 PrintSystem::PrinterWatcher::Delegate* delegate_; | |
| 275 }; | |
| 276 | |
| 277 | |
| 278 class MockJobSpooler : public PrintSystem::JobSpooler { | |
| 279 public: | |
| 280 MOCK_METHOD7(Spool, bool( | |
| 281 const std::string& print_ticket, | |
| 282 const FilePath& print_data_file_path, | |
| 283 const std::string& print_data_mime_type, | |
| 284 const std::string& printer_name, | |
| 285 const std::string& job_title, | |
| 286 const std::vector<std::string>& tags, | |
| 287 PrintSystem::JobSpooler::Delegate* delegate)); | |
| 288 | |
| 289 MockJobSpooler(); | |
| 290 PrintSystem::JobSpooler::Delegate* delegate() const { return delegate_; } | |
| 291 | |
| 292 virtual ~MockJobSpooler() {} | |
|
gene
2013/02/13 03:43:59
Could you please move destructor next to contructo
Noam Samuel (WRONG ACCOUNT)
2013/02/13 18:49:58
Done.
| |
| 293 private: | |
| 294 PrintSystem::JobSpooler::Delegate* delegate_; | |
| 295 }; | |
| 296 | |
| 297 | |
| 298 | |
| 299 class MockPrintSystem : public PrintSystem { | |
| 300 public: | |
| 301 MockPrintSystem(); | |
| 302 PrintSystem::PrintSystemResult succeed() { | |
| 303 return PrintSystem::PrintSystemResult(true, "success"); | |
| 304 } | |
| 305 | |
| 306 PrintSystem::PrintSystemResult fail() { | |
| 307 return PrintSystem::PrintSystemResult(false, "failure"); | |
| 308 } | |
| 309 | |
| 310 MockJobSpooler& JobSpooler() { | |
| 311 return *job_spooler_; | |
| 312 } | |
| 313 | |
| 314 MockPrinterWatcher& PrinterWatcher() { | |
| 315 return *printer_watcher_; | |
| 316 } | |
| 317 | |
| 318 MockPrintServerWatcher& PrintServerWatcher() { | |
| 319 return *print_server_watcher_; | |
| 320 } | |
| 321 | |
| 322 MOCK_METHOD0(Init, PrintSystem::PrintSystemResult()); | |
| 323 MOCK_METHOD1(EnumeratePrinters, PrintSystem::PrintSystemResult( | |
| 324 printing::PrinterList* printer_list)); | |
| 325 | |
| 326 MOCK_METHOD2( | |
| 327 GetPrinterCapsAndDefaults, | |
| 328 void(const std::string& printer_name, | |
| 329 const PrintSystem::PrinterCapsAndDefaultsCallback& callback)); | |
| 330 | |
| 331 MOCK_METHOD1(IsValidPrinter, bool(const std::string& printer_name)); | |
| 332 | |
| 333 MOCK_METHOD2(ValidatePrintTicket, bool(const std::string& printer_name, | |
| 334 const std::string& print_ticket_data)); | |
| 335 | |
| 336 MOCK_METHOD3(GetJobDetails, bool(const std::string& printer_name, | |
| 337 PlatformJobId job_id, | |
| 338 PrintJobDetails* job_details)); | |
| 339 | |
| 340 MOCK_METHOD0(CreatePrintServerWatcher, PrintSystem::PrintServerWatcher*()); | |
| 341 MOCK_METHOD1(CreatePrinterWatcher, | |
| 342 PrintSystem::PrinterWatcher*(const std::string& printer_name)); | |
| 343 MOCK_METHOD0(CreateJobSpooler, PrintSystem::JobSpooler*()); | |
| 344 | |
| 345 MOCK_METHOD0(GetSupportedMimeTypes, std::string()); | |
| 346 | |
| 347 virtual ~MockPrintSystem() {} | |
|
gene
2013/02/13 03:43:59
Could you please move destructor next to contructo
Noam Samuel (WRONG ACCOUNT)
2013/02/13 18:49:58
Done.
| |
| 348 private: | |
| 349 scoped_refptr<MockJobSpooler> job_spooler_; | |
| 350 scoped_refptr<MockPrinterWatcher> printer_watcher_; | |
| 351 scoped_refptr<MockPrintServerWatcher> print_server_watcher_; | |
| 352 }; | |
| 353 | |
| 354 | |
| 355 class PrinterJobHandlerTest : public ::testing::Test { | |
| 356 public: | |
| 357 PrinterJobHandlerTest(); | |
| 358 void SetUp() OVERRIDE; | |
| 359 void TearDown() OVERRIDE; | |
| 360 void IdleOut(); | |
| 361 bool GetPrinterInfo(printing::PrinterBasicInfo* info); | |
| 362 void SendCapsAndDefaults( | |
| 363 const std::string& printer_name, | |
| 364 const PrintSystem::PrinterCapsAndDefaultsCallback& callback); | |
| 365 void AddMimeHeader(const GURL& url, net::FakeURLFetcher* fetcher); | |
| 366 bool PostSpoolSuccess(); | |
| 367 | |
| 368 static void MessageLoopQuitNowHelper(MessageLoop* message_loop); | |
| 369 static void MessageLoopQuitSoonHelper(MessageLoop* message_loop); | |
| 370 | |
| 371 MessageLoopForIO loop_; | |
| 372 TestURLFetcherCallback url_callback_; | |
| 373 MockPrinterJobHandlerDelegate jobhandler_delegate_; | |
| 374 CloudPrintTokenStore token_store_; | |
| 375 CloudPrintURLFetcherNoServiceProcessFactory cloud_print_factory_; | |
| 376 scoped_refptr<PrinterJobHandler> job_handler_; | |
| 377 scoped_refptr<NiceMock<MockPrintSystem> > print_system_; | |
| 378 net::FakeURLFetcherFactory factory_; | |
| 379 printing::PrinterBasicInfo basic_info_; | |
| 380 printing::PrinterCapsAndDefaults caps_and_defaults_; | |
| 381 PrinterJobHandler::PrinterInfoFromCloud info_from_cloud_; | |
| 382 }; | |
| 383 | |
| 384 | |
| 385 void PrinterJobHandlerTest::SetUp() { | |
| 386 basic_info_.printer_name = kExamplePrinterName; | |
| 387 basic_info_.printer_description = kExamplePrinterDescription; | |
| 388 basic_info_.is_default = 0; | |
| 389 | |
| 390 info_from_cloud_.printer_id = kExamplePrinterID; | |
| 391 info_from_cloud_.tags_hash = GetHashOfPrinterInfo(basic_info_); | |
| 392 | |
| 393 info_from_cloud_.caps_hash = base::MD5String(kExamplePrinterCapabilities); | |
| 394 | |
| 395 caps_and_defaults_.printer_capabilities = kExamplePrinterCapabilities; | |
| 396 caps_and_defaults_.caps_mime_type = kExampleCapsMimeType; | |
| 397 caps_and_defaults_.printer_defaults = kExampleDefaults; | |
| 398 caps_and_defaults_.defaults_mime_type = kExampleDefaultMimeType; | |
| 399 | |
| 400 print_system_ = new NiceMock<MockPrintSystem>(); | |
| 401 | |
| 402 token_store_.SetToken(kExampleCloudPrintOAuthToken); | |
| 403 | |
| 404 ON_CALL(print_system_->PrinterWatcher(), GetCurrentPrinterInfo(_)) | |
| 405 .WillByDefault(Invoke(this, &PrinterJobHandlerTest::GetPrinterInfo)); | |
| 406 | |
| 407 ON_CALL(*print_system_, GetPrinterCapsAndDefaults(_, _)) | |
| 408 .WillByDefault(Invoke(this, &PrinterJobHandlerTest::SendCapsAndDefaults)); | |
| 409 | |
| 410 CloudPrintURLFetcher::set_factory(&cloud_print_factory_); | |
| 411 } | |
| 412 | |
| 413 void PrinterJobHandlerTest::MessageLoopQuitNowHelper( | |
| 414 MessageLoop* message_loop) { | |
| 415 message_loop->QuitWhenIdle(); | |
| 416 } | |
| 417 | |
| 418 void PrinterJobHandlerTest::MessageLoopQuitSoonHelper( | |
| 419 MessageLoop* message_loop) { | |
| 420 message_loop->message_loop_proxy()->PostTask( | |
| 421 FROM_HERE, | |
| 422 base::Bind(&MessageLoopQuitNowHelper, message_loop)); | |
| 423 } | |
| 424 | |
| 425 PrinterJobHandlerTest::PrinterJobHandlerTest() | |
| 426 : factory_(base::Bind(&TestURLFetcherCallback::CreateURLFetcher, | |
| 427 base::Unretained(&url_callback_)), NULL) { | |
| 428 } | |
| 429 | |
| 430 bool PrinterJobHandlerTest::PostSpoolSuccess() { | |
| 431 MessageLoop::current()->PostTask( | |
| 432 FROM_HERE, | |
| 433 base::Bind( | |
| 434 &PrinterJobHandler::OnJobSpoolSucceeded, | |
| 435 job_handler_, 0)); | |
| 436 | |
| 437 // Everything that would be posted on the printer thread queue | |
| 438 // has been posted, we can tell the main message loop to quit when idle | |
| 439 // and not worry about it idling while the print thread does work | |
| 440 MessageLoop::current()->PostTask(FROM_HERE, | |
| 441 base::Bind(&MessageLoopQuitSoonHelper, &loop_)); | |
| 442 return true; | |
| 443 } | |
| 444 | |
| 445 void PrinterJobHandlerTest::AddMimeHeader(const GURL& url, | |
| 446 net::FakeURLFetcher* fetcher) { | |
| 447 scoped_refptr<net::HttpResponseHeaders> download_headers = | |
| 448 new net::HttpResponseHeaders(kExampleJobDownloadResponseHeaders); | |
| 449 fetcher->set_response_headers(download_headers); | |
| 450 } | |
| 451 | |
| 452 | |
| 453 void PrinterJobHandlerTest::SendCapsAndDefaults( | |
| 454 const std::string& printer_name, | |
| 455 const PrintSystem::PrinterCapsAndDefaultsCallback& callback) { | |
| 456 callback.Run(true, printer_name, caps_and_defaults_); | |
| 457 } | |
| 458 | |
| 459 bool PrinterJobHandlerTest::GetPrinterInfo(printing::PrinterBasicInfo* info) { | |
| 460 *info = basic_info_; | |
| 461 return true; | |
| 462 } | |
| 463 | |
| 464 void PrinterJobHandlerTest::TearDown() { | |
| 465 IdleOut(); | |
| 466 CloudPrintURLFetcher::set_factory(NULL); | |
| 467 } | |
| 468 | |
| 469 void PrinterJobHandlerTest::IdleOut() { | |
| 470 MessageLoop::current()->RunUntilIdle(); | |
| 471 } | |
| 472 | |
| 473 MockPrintServerWatcher::MockPrintServerWatcher() : delegate_(NULL) { | |
| 474 ON_CALL(*this, StartWatching(_)) | |
| 475 .WillByDefault(DoAll(SaveArg<0>(&delegate_), Return(true))); | |
| 476 ON_CALL(*this, StopWatching()).WillByDefault(Return(true)); | |
| 477 } | |
| 478 | |
| 479 | |
| 480 MockPrinterWatcher::MockPrinterWatcher() : delegate_(NULL) { | |
| 481 ON_CALL(*this, StartWatching(_)) | |
| 482 .WillByDefault(DoAll(SaveArg<0>(&delegate_), Return(true))); | |
| 483 ON_CALL(*this, StopWatching()).WillByDefault(Return(true)); | |
| 484 } | |
| 485 | |
| 486 MockJobSpooler::MockJobSpooler() : delegate_(NULL) { | |
| 487 ON_CALL(*this, Spool(_, _, _, _, _, _, _)) | |
| 488 .WillByDefault(DoAll(SaveArg<6>(&delegate_), Return(true))); | |
| 489 } | |
| 490 | |
| 491 | |
| 492 MockPrintSystem::MockPrintSystem() | |
| 493 : job_spooler_(new NiceMock<MockJobSpooler>()), | |
| 494 printer_watcher_(new NiceMock<MockPrinterWatcher>()), | |
| 495 print_server_watcher_(new NiceMock<MockPrintServerWatcher>()) { | |
| 496 ON_CALL(*this, CreateJobSpooler()) | |
| 497 .WillByDefault(Return(job_spooler_)); | |
| 498 | |
| 499 ON_CALL(*this, CreatePrinterWatcher(_)) | |
| 500 .WillByDefault(Return(printer_watcher_)); | |
| 501 | |
| 502 ON_CALL(*this, CreatePrintServerWatcher()) | |
| 503 .WillByDefault(Return(print_server_watcher_)); | |
| 504 | |
| 505 ON_CALL(*this, IsValidPrinter(_)). | |
| 506 WillByDefault(Return(true)); | |
| 507 | |
| 508 ON_CALL(*this, ValidatePrintTicket(_, _)). | |
| 509 WillByDefault(Return(true)); | |
| 510 }; | |
| 511 | |
| 512 // This test simulates an end-to-end printing of a document | |
| 513 // but tests only non-failure cases. | |
| 514 TEST_F(PrinterJobHandlerTest, HappyPathTest) { | |
| 515 GURL InProgressURL = | |
| 516 GetUrlForJobStatusUpdate(GURL(kExampleCloudPrintServerURL), | |
| 517 kExampleJobID, | |
| 518 PRINT_JOB_STATUS_IN_PROGRESS); | |
| 519 | |
| 520 factory_.SetFakeResponse(kExamplePrintTicketURI, kExamplePrintTicket, true); | |
| 521 factory_.SetFakeResponse(kExamplePrintDownloadURI, kExamplePrintData, true); | |
| 522 factory_.SetFakeResponse( | |
| 523 StringPrintf(kExamplePrinterJobListURI, kJobFetchReasonStartup), | |
| 524 kExampleJobListResponse, true); | |
| 525 | |
| 526 | |
| 527 factory_.SetFakeResponse( | |
| 528 base::StringPrintf(kExamplePrinterJobListURI, kJobFetchReasonQueryMore), | |
| 529 kExampleJobListResponseEmpty, true); | |
| 530 | |
| 531 factory_.SetFakeResponse( | |
| 532 kExampleUpdateDoneURL, | |
| 533 base::StringPrintf(kExampleControlResponse, "DONE", "DONE"), true); | |
| 534 | |
| 535 factory_.SetFakeResponse( | |
| 536 InProgressURL.spec(), | |
| 537 base::StringPrintf(kExampleControlResponse, | |
| 538 "IN_PROGRESS", "IN_PROGRESS"), | |
| 539 true); | |
| 540 | |
| 541 | |
| 542 job_handler_ = new PrinterJobHandler(basic_info_, info_from_cloud_, | |
| 543 GURL(kExampleCloudPrintServerURL), | |
| 544 print_system_, &jobhandler_delegate_); | |
| 545 | |
| 546 EXPECT_CALL(url_callback_, OnRequestCreate( | |
| 547 GURL(base::StringPrintf(kExamplePrinterJobListURI, "startup")), _)) | |
| 548 .Times(Exactly(1)); | |
| 549 | |
| 550 EXPECT_CALL(url_callback_, OnRequestCreate( | |
| 551 GURL(base::StringPrintf(kExamplePrinterJobListURI, "querymore")), _)) | |
| 552 .Times(Exactly(1)); | |
| 553 | |
| 554 EXPECT_CALL(url_callback_, OnRequestCreate(GURL(kExamplePrintTicketURI), _)) | |
| 555 .Times(Exactly(1)); | |
| 556 | |
| 557 EXPECT_CALL(url_callback_, OnRequestCreate( | |
| 558 GURL(kExamplePrintDownloadURI), _)) | |
| 559 .Times(Exactly(1)) | |
| 560 .WillOnce(Invoke(this, &PrinterJobHandlerTest::AddMimeHeader)); | |
| 561 | |
| 562 EXPECT_CALL(url_callback_, OnRequestCreate(InProgressURL, _)) | |
| 563 .Times(Exactly(1)); | |
| 564 | |
| 565 EXPECT_CALL(url_callback_, OnRequestCreate(GURL(kExampleUpdateDoneURL), _)) | |
| 566 .Times(Exactly(1)); | |
| 567 | |
| 568 EXPECT_CALL(print_system_->JobSpooler(), | |
| 569 Spool(kExamplePrintTicket, _, _, _, _, _, _)) | |
| 570 .Times(Exactly(1)) | |
| 571 .WillOnce(InvokeWithoutArgs(this, | |
| 572 &PrinterJobHandlerTest::PostSpoolSuccess)); | |
| 573 | |
| 574 | |
| 575 job_handler_->Initialize(); | |
| 576 | |
| 577 MessageLoop::current()->PostDelayedTask( | |
| 578 FROM_HERE, | |
| 579 base::Bind(&PrinterJobHandlerTest::MessageLoopQuitSoonHelper, | |
| 580 MessageLoop::current()), | |
| 581 base::TimeDelta::FromSeconds(1)); | |
| 582 | |
| 583 MessageLoop::current()->Run(); | |
| 584 } | |
| 585 | |
| 586 } // namespace cloud_print | |
| 587 | |
| OLD | NEW |