1 // Copyright 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.
6 #include "base/json/json_reader.h"
7 #include "base/json/json_writer.h"
8 #include "base/location.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "chrome/browser/local_discovery/privet_http_impl.h"
13 #include "net/base/host_port_pair.h"
14 #include "net/base/net_errors.h"
15 #include "net/url_request/test_url_fetcher_factory.h"
16 #include "net/url_request/url_request_test_util.h"
17 #include "printing/pwg_raster_settings.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 #if defined(ENABLE_PRINT_PREVIEW)
22 #include "chrome/browser/local_discovery/pwg_raster_converter.h"
23 #endif // ENABLE_PRINT_PREVIEW
25 using testing::StrictMock
;
26 using testing::NiceMock
;
28 namespace local_discovery
{
32 const char kSampleInfoResponse
[] = "{"
33 " \"version\": \"1.0\","
34 " \"name\": \"Common printer\","
35 " \"description\": \"Printer connected through Chrome connector\","
36 " \"url\": \"https://www.google.com/cloudprint\","
41 " \"device_state\": \"idle\","
42 " \"connection_state\": \"online\","
43 " \"manufacturer\": \"Google\","
44 " \"model\": \"Google Chrome\","
45 " \"serial_number\": \"1111-22222-33333-4444\","
46 " \"firmware\": \"24.0.1312.52\","
48 " \"setup_url\": \"http://support.google.com/\","
49 " \"support_url\": \"http://support.google.com/cloudprint/?hl=en\","
50 " \"update_url\": \"http://support.google.com/cloudprint/?hl=en\","
51 " \"x-privet-token\": \"SampleTokenForTesting\","
53 " \"/privet/accesstoken\","
54 " \"/privet/capabilities\","
55 " \"/privet/printer/submitdoc\","
59 const char kSampleInfoResponseRegistered
[] = "{"
60 " \"version\": \"1.0\","
61 " \"name\": \"Common printer\","
62 " \"description\": \"Printer connected through Chrome connector\","
63 " \"url\": \"https://www.google.com/cloudprint\","
67 " \"id\": \"MyDeviceID\","
68 " \"device_state\": \"idle\","
69 " \"connection_state\": \"online\","
70 " \"manufacturer\": \"Google\","
71 " \"model\": \"Google Chrome\","
72 " \"serial_number\": \"1111-22222-33333-4444\","
73 " \"firmware\": \"24.0.1312.52\","
75 " \"setup_url\": \"http://support.google.com/\","
76 " \"support_url\": \"http://support.google.com/cloudprint/?hl=en\","
77 " \"update_url\": \"http://support.google.com/cloudprint/?hl=en\","
78 " \"x-privet-token\": \"SampleTokenForTesting\","
80 " \"/privet/accesstoken\","
81 " \"/privet/capabilities\","
82 " \"/privet/printer/submitdoc\","
86 const char kSampleRegisterStartResponse
[] = "{"
87 "\"user\": \"example@google.com\","
88 "\"action\": \"start\""
91 const char kSampleRegisterGetClaimTokenResponse
[] = "{"
92 " \"action\": \"getClaimToken\","
93 " \"user\": \"example@google.com\","
94 " \"token\": \"MySampleToken\","
95 " \"claim_url\": \"https://domain.com/SoMeUrL\""
98 const char kSampleRegisterCompleteResponse
[] = "{"
99 "\"user\": \"example@google.com\","
100 "\"action\": \"complete\","
101 "\"device_id\": \"MyDeviceID\""
104 const char kSampleXPrivetErrorResponse
[] =
105 "{ \"error\": \"invalid_x_privet_token\" }";
107 const char kSampleRegisterErrorTransient
[] =
108 "{ \"error\": \"device_busy\", \"timeout\": 1}";
110 const char kSampleRegisterErrorPermanent
[] =
111 "{ \"error\": \"user_cancel\" }";
113 const char kSampleInfoResponseBadJson
[] = "{";
115 const char kSampleRegisterCancelResponse
[] = "{"
116 "\"user\": \"example@google.com\","
117 "\"action\": \"cancel\""
120 const char kSampleCapabilitiesResponse
[] = "{"
121 "\"version\" : \"1.0\","
123 " \"supported_content_type\" : ["
124 " { \"content_type\" : \"application/pdf\" },"
125 " { \"content_type\" : \"image/pwg-raster\" }"
130 #if defined(ENABLE_PRINT_PREVIEW)
131 const char kSampleInfoResponseWithCreatejob
[] = "{"
132 " \"version\": \"1.0\","
133 " \"name\": \"Common printer\","
134 " \"description\": \"Printer connected through Chrome connector\","
135 " \"url\": \"https://www.google.com/cloudprint\","
140 " \"device_state\": \"idle\","
141 " \"connection_state\": \"online\","
142 " \"manufacturer\": \"Google\","
143 " \"model\": \"Google Chrome\","
144 " \"serial_number\": \"1111-22222-33333-4444\","
145 " \"firmware\": \"24.0.1312.52\","
147 " \"setup_url\": \"http://support.google.com/\","
148 " \"support_url\": \"http://support.google.com/cloudprint/?hl=en\","
149 " \"update_url\": \"http://support.google.com/cloudprint/?hl=en\","
150 " \"x-privet-token\": \"SampleTokenForTesting\","
152 " \"/privet/accesstoken\","
153 " \"/privet/capabilities\","
154 " \"/privet/printer/createjob\","
155 " \"/privet/printer/submitdoc\","
159 const char kSampleLocalPrintResponse
[] = "{"
160 "\"job_id\": \"123\","
161 "\"expires_in\": 500,"
162 "\"job_type\": \"application/pdf\","
164 "\"job_name\": \"Sample job name\","
167 const char kSampleCapabilitiesResponsePWGOnly
[] = "{"
168 "\"version\" : \"1.0\","
170 " \"supported_content_type\" : ["
171 " { \"content_type\" : \"image/pwg-raster\" }"
176 const char kSampleErrorResponsePrinterBusy
[] = "{"
177 "\"error\": \"invalid_print_job\","
181 const char kSampleInvalidDocumentTypeResponse
[] = "{"
182 "\"error\" : \"invalid_document_type\""
185 const char kSampleCreatejobResponse
[] = "{ \"job_id\": \"1234\" }";
187 const char kSampleCapabilitiesResponseWithAnyMimetype
[] = "{"
188 "\"version\" : \"1.0\","
190 " \"supported_content_type\" : ["
191 " { \"content_type\" : \"*/*\" },"
192 " { \"content_type\" : \"image/pwg-raster\" }"
197 const char kSampleCJT
[] = "{ \"version\" : \"1.0\" }";
199 const char kSampleCapabilitiesResponsePWGSettings
[] =
201 "\"version\" : \"1.0\","
203 " \"pwg_raster_config\" : {"
204 " \"document_sheet_back\" : \"MANUAL_TUMBLE\","
205 " \"reverse_order_streaming\": true"
207 " \"supported_content_type\" : ["
208 " { \"content_type\" : \"image/pwg-raster\" }"
213 const char kSampleCJTDuplex
[] =
215 "\"version\" : \"1.0\","
216 "\"print\": { \"duplex\": {\"type\": \"SHORT_EDGE\"} }"
218 #endif // ENABLE_PRINT_PREVIEW
220 // Return the representation of the given JSON that would be outputted by
221 // JSONWriter. This ensures the same JSON values are represented by the same
223 std::string
NormalizeJson(const std::string
& json
) {
224 std::string result
= json
;
225 scoped_ptr
<base::Value
> value
= base::JSONReader::Read(result
);
227 base::JSONWriter::Write(*value
, &result
);
231 class MockTestURLFetcherFactoryDelegate
232 : public net::TestURLFetcher::DelegateForTests
{
234 // Callback issued correspondingly to the call to the |Start()| method.
235 MOCK_METHOD1(OnRequestStart
, void(int fetcher_id
));
237 // Callback issued correspondingly to the call to |AppendChunkToUpload|.
238 // Uploaded chunks can be retrieved with the |upload_chunks()| getter.
239 MOCK_METHOD1(OnChunkUpload
, void(int fetcher_id
));
241 // Callback issued correspondingly to the destructor.
242 MOCK_METHOD1(OnRequestEnd
, void(int fetcher_id
));
245 class PrivetHTTPTest
: public ::testing::Test
{
248 PrivetURLFetcher::ResetTokenMapForTests();
250 request_context_
= new net::TestURLRequestContextGetter(
251 base::ThreadTaskRunnerHandle::Get());
253 PrivetV1HTTPClient::CreateDefault(make_scoped_ptr
<PrivetHTTPClient
>(
254 new PrivetHTTPClientImpl("sampleDevice._privet._tcp.local",
255 net::HostPortPair("10.0.0.8", 6006),
256 request_context_
.get())));
257 fetcher_factory_
.SetDelegateForTests(&fetcher_delegate_
);
260 virtual ~PrivetHTTPTest() {
263 bool SuccessfulResponseToURL(const GURL
& url
,
264 const std::string
& response
) {
265 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
266 EXPECT_TRUE(fetcher
);
267 EXPECT_EQ(url
, fetcher
->GetOriginalURL());
269 if (!fetcher
|| url
!= fetcher
->GetOriginalURL())
272 fetcher
->SetResponseString(response
);
273 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
275 fetcher
->set_response_code(200);
276 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
280 bool SuccessfulResponseToURLAndData(const GURL
& url
,
281 const std::string
& data
,
282 const std::string
& response
) {
283 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
284 EXPECT_TRUE(fetcher
);
285 EXPECT_EQ(url
, fetcher
->GetOriginalURL());
287 if (!fetcher
) return false;
289 EXPECT_EQ(data
, fetcher
->upload_data());
290 if (data
!= fetcher
->upload_data()) return false;
292 return SuccessfulResponseToURL(url
, response
);
295 bool SuccessfulResponseToURLAndJSONData(const GURL
& url
,
296 const std::string
& data
,
297 const std::string
& response
) {
298 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
299 EXPECT_TRUE(fetcher
);
300 EXPECT_EQ(url
, fetcher
->GetOriginalURL());
305 std::string normalized_data
= NormalizeJson(data
);
306 std::string normalized_upload_data
= NormalizeJson(fetcher
->upload_data());
307 EXPECT_EQ(normalized_data
, normalized_upload_data
);
308 if (normalized_data
!= normalized_upload_data
)
311 return SuccessfulResponseToURL(url
, response
);
314 bool SuccessfulResponseToURLAndFilePath(const GURL
& url
,
315 const base::FilePath
& file_path
,
316 const std::string
& response
) {
317 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
318 EXPECT_TRUE(fetcher
);
319 EXPECT_EQ(url
, fetcher
->GetOriginalURL());
321 if (!fetcher
) return false;
323 EXPECT_EQ(file_path
, fetcher
->upload_file_path());
324 if (file_path
!= fetcher
->upload_file_path()) return false;
326 return SuccessfulResponseToURL(url
, response
);
330 void RunFor(base::TimeDelta time_period
) {
331 base::CancelableCallback
<void()> callback(base::Bind(
332 &PrivetHTTPTest::Stop
, base::Unretained(this)));
333 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
334 FROM_HERE
, callback
.callback(), time_period
);
336 base::MessageLoop::current()->Run();
341 base::MessageLoop::current()->Quit();
345 base::MessageLoop loop_
;
346 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_
;
347 net::TestURLFetcherFactory fetcher_factory_
;
348 scoped_ptr
<PrivetV1HTTPClient
> privet_client_
;
349 NiceMock
<MockTestURLFetcherFactoryDelegate
> fetcher_delegate_
;
352 class MockJSONCallback
{
354 MockJSONCallback() {}
355 ~MockJSONCallback() {}
357 void OnPrivetJSONDone(const base::DictionaryValue
* value
) {
361 value_
.reset(value
->DeepCopy());
364 OnPrivetJSONDoneInternal();
367 MOCK_METHOD0(OnPrivetJSONDoneInternal
, void());
369 const base::DictionaryValue
* value() { return value_
.get(); }
370 PrivetJSONOperation::ResultCallback
callback() {
371 return base::Bind(&MockJSONCallback::OnPrivetJSONDone
,
372 base::Unretained(this));
375 scoped_ptr
<base::DictionaryValue
> value_
;
378 class MockRegisterDelegate
: public PrivetRegisterOperation::Delegate
{
380 MockRegisterDelegate() {
382 ~MockRegisterDelegate() {
385 void OnPrivetRegisterClaimToken(
386 PrivetRegisterOperation
* operation
,
387 const std::string
& token
,
388 const GURL
& url
) override
{
389 OnPrivetRegisterClaimTokenInternal(token
, url
);
392 MOCK_METHOD2(OnPrivetRegisterClaimTokenInternal
, void(
393 const std::string
& token
,
396 void OnPrivetRegisterError(
397 PrivetRegisterOperation
* operation
,
398 const std::string
& action
,
399 PrivetRegisterOperation::FailureReason reason
,
400 int printer_http_code
,
401 const base::DictionaryValue
* json
) override
{
402 // TODO(noamsml): Save and test for JSON?
403 OnPrivetRegisterErrorInternal(action
, reason
, printer_http_code
);
406 MOCK_METHOD3(OnPrivetRegisterErrorInternal
,
407 void(const std::string
& action
,
408 PrivetRegisterOperation::FailureReason reason
,
409 int printer_http_code
));
411 void OnPrivetRegisterDone(
412 PrivetRegisterOperation
* operation
,
413 const std::string
& device_id
) override
{
414 OnPrivetRegisterDoneInternal(device_id
);
417 MOCK_METHOD1(OnPrivetRegisterDoneInternal
,
418 void(const std::string
& device_id
));
421 class MockLocalPrintDelegate
: public PrivetLocalPrintOperation::Delegate
{
423 MockLocalPrintDelegate() {}
424 ~MockLocalPrintDelegate() {}
426 virtual void OnPrivetPrintingDone(
427 const PrivetLocalPrintOperation
* print_operation
) {
428 OnPrivetPrintingDoneInternal();
431 MOCK_METHOD0(OnPrivetPrintingDoneInternal
, void());
433 virtual void OnPrivetPrintingError(
434 const PrivetLocalPrintOperation
* print_operation
, int http_code
) {
435 OnPrivetPrintingErrorInternal(http_code
);
438 MOCK_METHOD1(OnPrivetPrintingErrorInternal
, void(int http_code
));
441 class PrivetInfoTest
: public PrivetHTTPTest
{
445 ~PrivetInfoTest() override
{}
447 void SetUp() override
{
448 info_operation_
= privet_client_
->CreateInfoOperation(
449 info_callback_
.callback());
453 scoped_ptr
<PrivetJSONOperation
> info_operation_
;
454 StrictMock
<MockJSONCallback
> info_callback_
;
457 TEST_F(PrivetInfoTest
, SuccessfulInfo
) {
458 info_operation_
->Start();
460 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
461 ASSERT_TRUE(fetcher
!= NULL
);
462 EXPECT_EQ(GURL("http://10.0.0.8:6006/privet/info"),
463 fetcher
->GetOriginalURL());
465 fetcher
->SetResponseString(kSampleInfoResponse
);
466 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
468 fetcher
->set_response_code(200);
470 EXPECT_CALL(info_callback_
, OnPrivetJSONDoneInternal());
471 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
474 TEST_F(PrivetInfoTest
, InfoFailureHTTP
) {
475 info_operation_
->Start();
477 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
478 ASSERT_TRUE(fetcher
!= NULL
);
479 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
481 fetcher
->set_response_code(404);
483 EXPECT_CALL(info_callback_
, OnPrivetJSONDoneInternal());
484 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
487 class PrivetRegisterTest
: public PrivetHTTPTest
{
489 PrivetRegisterTest() {
491 ~PrivetRegisterTest() override
{
494 void SetUp() override
{
495 info_operation_
= privet_client_
->CreateInfoOperation(
496 info_callback_
.callback());
497 register_operation_
=
498 privet_client_
->CreateRegisterOperation("example@google.com",
499 ®ister_delegate_
);
503 bool SuccessfulResponseToURL(const GURL
& url
,
504 const std::string
& response
) {
505 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
506 EXPECT_TRUE(fetcher
);
507 EXPECT_EQ(url
, fetcher
->GetOriginalURL());
508 if (!fetcher
|| url
!= fetcher
->GetOriginalURL())
511 fetcher
->SetResponseString(response
);
512 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
514 fetcher
->set_response_code(200);
515 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
519 scoped_ptr
<PrivetJSONOperation
> info_operation_
;
520 NiceMock
<MockJSONCallback
> info_callback_
;
521 scoped_ptr
<PrivetRegisterOperation
> register_operation_
;
522 StrictMock
<MockRegisterDelegate
> register_delegate_
;
525 TEST_F(PrivetRegisterTest
, RegisterSuccessSimple
) {
526 register_operation_
->Start();
528 EXPECT_TRUE(SuccessfulResponseToURL(
529 GURL("http://10.0.0.8:6006/privet/info"),
530 kSampleInfoResponse
));
532 EXPECT_TRUE(SuccessfulResponseToURL(
533 GURL("http://10.0.0.8:6006/privet/register?"
534 "action=start&user=example%40google.com"),
535 kSampleRegisterStartResponse
));
537 EXPECT_CALL(register_delegate_
, OnPrivetRegisterClaimTokenInternal(
539 GURL("https://domain.com/SoMeUrL")));
541 EXPECT_TRUE(SuccessfulResponseToURL(
542 GURL("http://10.0.0.8:6006/privet/register?"
543 "action=getClaimToken&user=example%40google.com"),
544 kSampleRegisterGetClaimTokenResponse
));
546 register_operation_
->CompleteRegistration();
548 EXPECT_TRUE(SuccessfulResponseToURL(
549 GURL("http://10.0.0.8:6006/privet/register?"
550 "action=complete&user=example%40google.com"),
551 kSampleRegisterCompleteResponse
));
553 EXPECT_CALL(register_delegate_
, OnPrivetRegisterDoneInternal(
556 EXPECT_TRUE(SuccessfulResponseToURL(
557 GURL("http://10.0.0.8:6006/privet/info"),
558 kSampleInfoResponseRegistered
));
561 TEST_F(PrivetRegisterTest
, RegisterXSRFFailure
) {
562 register_operation_
->Start();
564 EXPECT_TRUE(SuccessfulResponseToURL(
565 GURL("http://10.0.0.8:6006/privet/info"),
566 kSampleInfoResponse
));
568 EXPECT_TRUE(SuccessfulResponseToURL(
569 GURL("http://10.0.0.8:6006/privet/register?"
570 "action=start&user=example%40google.com"),
571 kSampleRegisterStartResponse
));
573 EXPECT_TRUE(SuccessfulResponseToURL(
574 GURL("http://10.0.0.8:6006/privet/register?"
575 "action=getClaimToken&user=example%40google.com"),
576 kSampleXPrivetErrorResponse
));
578 EXPECT_TRUE(SuccessfulResponseToURL(
579 GURL("http://10.0.0.8:6006/privet/info"),
580 kSampleInfoResponse
));
582 EXPECT_CALL(register_delegate_
, OnPrivetRegisterClaimTokenInternal(
583 "MySampleToken", GURL("https://domain.com/SoMeUrL")));
585 EXPECT_TRUE(SuccessfulResponseToURL(
586 GURL("http://10.0.0.8:6006/privet/register?"
587 "action=getClaimToken&user=example%40google.com"),
588 kSampleRegisterGetClaimTokenResponse
));
591 TEST_F(PrivetRegisterTest
, TransientFailure
) {
592 register_operation_
->Start();
594 EXPECT_TRUE(SuccessfulResponseToURL(
595 GURL("http://10.0.0.8:6006/privet/info"),
596 kSampleInfoResponse
));
598 EXPECT_TRUE(SuccessfulResponseToURL(
599 GURL("http://10.0.0.8:6006/privet/register?"
600 "action=start&user=example%40google.com"),
601 kSampleRegisterErrorTransient
));
603 EXPECT_CALL(fetcher_delegate_
, OnRequestStart(0));
605 RunFor(base::TimeDelta::FromSeconds(2));
607 testing::Mock::VerifyAndClearExpectations(&fetcher_delegate_
);
609 EXPECT_TRUE(SuccessfulResponseToURL(
610 GURL("http://10.0.0.8:6006/privet/register?"
611 "action=start&user=example%40google.com"),
612 kSampleRegisterStartResponse
));
615 TEST_F(PrivetRegisterTest
, PermanentFailure
) {
616 register_operation_
->Start();
618 EXPECT_TRUE(SuccessfulResponseToURL(
619 GURL("http://10.0.0.8:6006/privet/info"),
620 kSampleInfoResponse
));
622 EXPECT_TRUE(SuccessfulResponseToURL(
623 GURL("http://10.0.0.8:6006/privet/register?"
624 "action=start&user=example%40google.com"),
625 kSampleRegisterStartResponse
));
627 EXPECT_CALL(register_delegate_
,
628 OnPrivetRegisterErrorInternal(
630 PrivetRegisterOperation::FAILURE_JSON_ERROR
,
633 EXPECT_TRUE(SuccessfulResponseToURL(
634 GURL("http://10.0.0.8:6006/privet/register?"
635 "action=getClaimToken&user=example%40google.com"),
636 kSampleRegisterErrorPermanent
));
639 TEST_F(PrivetRegisterTest
, InfoFailure
) {
640 register_operation_
->Start();
642 EXPECT_CALL(register_delegate_
,
643 OnPrivetRegisterErrorInternal(
645 PrivetRegisterOperation::FAILURE_TOKEN
,
648 EXPECT_TRUE(SuccessfulResponseToURL(
649 GURL("http://10.0.0.8:6006/privet/info"),
650 kSampleInfoResponseBadJson
));
653 TEST_F(PrivetRegisterTest
, RegisterCancel
) {
654 register_operation_
->Start();
656 EXPECT_TRUE(SuccessfulResponseToURL(
657 GURL("http://10.0.0.8:6006/privet/info"),
658 kSampleInfoResponse
));
660 EXPECT_TRUE(SuccessfulResponseToURL(
661 GURL("http://10.0.0.8:6006/privet/register?"
662 "action=start&user=example%40google.com"),
663 kSampleRegisterStartResponse
));
665 register_operation_
->Cancel();
667 EXPECT_TRUE(SuccessfulResponseToURL(
668 GURL("http://10.0.0.8:6006/privet/register?"
669 "action=cancel&user=example%40google.com"),
670 kSampleRegisterCancelResponse
));
672 // Must keep mocks alive for 3 seconds so the cancelation object can be
674 RunFor(base::TimeDelta::FromSeconds(3));
677 class PrivetCapabilitiesTest
: public PrivetHTTPTest
{
679 PrivetCapabilitiesTest() {}
681 ~PrivetCapabilitiesTest() override
{}
683 void SetUp() override
{
684 capabilities_operation_
= privet_client_
->CreateCapabilitiesOperation(
685 capabilities_callback_
.callback());
689 scoped_ptr
<PrivetJSONOperation
> capabilities_operation_
;
690 StrictMock
<MockJSONCallback
> capabilities_callback_
;
693 TEST_F(PrivetCapabilitiesTest
, SuccessfulCapabilities
) {
694 capabilities_operation_
->Start();
696 EXPECT_TRUE(SuccessfulResponseToURL(
697 GURL("http://10.0.0.8:6006/privet/info"),
698 kSampleInfoResponse
));
700 EXPECT_CALL(capabilities_callback_
, OnPrivetJSONDoneInternal());
702 EXPECT_TRUE(SuccessfulResponseToURL(
703 GURL("http://10.0.0.8:6006/privet/capabilities"),
704 kSampleCapabilitiesResponse
));
707 EXPECT_TRUE(capabilities_callback_
.value()->GetString("version", &version
));
708 EXPECT_EQ("1.0", version
);
711 TEST_F(PrivetCapabilitiesTest
, CacheToken
) {
712 capabilities_operation_
->Start();
714 EXPECT_TRUE(SuccessfulResponseToURL(
715 GURL("http://10.0.0.8:6006/privet/info"),
716 kSampleInfoResponse
));
718 EXPECT_CALL(capabilities_callback_
, OnPrivetJSONDoneInternal());
720 EXPECT_TRUE(SuccessfulResponseToURL(
721 GURL("http://10.0.0.8:6006/privet/capabilities"),
722 kSampleCapabilitiesResponse
));
724 capabilities_operation_
= privet_client_
->CreateCapabilitiesOperation(
725 capabilities_callback_
.callback());
727 capabilities_operation_
->Start();
729 EXPECT_CALL(capabilities_callback_
, OnPrivetJSONDoneInternal());
731 EXPECT_TRUE(SuccessfulResponseToURL(
732 GURL("http://10.0.0.8:6006/privet/capabilities"),
733 kSampleCapabilitiesResponse
));
736 TEST_F(PrivetCapabilitiesTest
, BadToken
) {
737 capabilities_operation_
->Start();
739 EXPECT_TRUE(SuccessfulResponseToURL(
740 GURL("http://10.0.0.8:6006/privet/info"),
741 kSampleInfoResponse
));
743 EXPECT_TRUE(SuccessfulResponseToURL(
744 GURL("http://10.0.0.8:6006/privet/capabilities"),
745 kSampleXPrivetErrorResponse
));
747 EXPECT_TRUE(SuccessfulResponseToURL(
748 GURL("http://10.0.0.8:6006/privet/info"),
749 kSampleInfoResponse
));
751 EXPECT_CALL(capabilities_callback_
, OnPrivetJSONDoneInternal());
753 EXPECT_TRUE(SuccessfulResponseToURL(
754 GURL("http://10.0.0.8:6006/privet/capabilities"),
755 kSampleCapabilitiesResponse
));
758 #if defined(ENABLE_PRINT_PREVIEW)
759 // A note on PWG raster conversion: The PWG raster converter used simply
760 // converts strings to file paths based on them by appending "test.pdf", since
761 // it's easier to test that way. Instead of using a mock, we simply check if the
762 // request is uploading a file that is based on this pattern.
763 class FakePWGRasterConverter
: public PWGRasterConverter
{
765 FakePWGRasterConverter() {
768 ~FakePWGRasterConverter() override
{}
770 void Start(base::RefCountedMemory
* data
,
771 const printing::PdfRenderSettings
& conversion_settings
,
772 const printing::PwgRasterSettings
& bitmap_settings
,
773 const ResultCallback
& callback
) override
{
774 bitmap_settings_
= bitmap_settings
;
775 std::string
data_str(data
->front_as
<char>(), data
->size());
776 callback
.Run(true, base::FilePath().AppendASCII(data_str
+ "test.pdf"));
779 const printing::PwgRasterSettings
& bitmap_settings() {
780 return bitmap_settings_
;
784 printing::PwgRasterSettings bitmap_settings_
;
787 class PrivetLocalPrintTest
: public PrivetHTTPTest
{
789 PrivetLocalPrintTest() {}
791 ~PrivetLocalPrintTest() override
{}
793 void SetUp() override
{
794 PrivetURLFetcher::ResetTokenMapForTests();
796 local_print_operation_
= privet_client_
->CreateLocalPrintOperation(
797 &local_print_delegate_
);
799 scoped_ptr
<FakePWGRasterConverter
> pwg_converter(
800 new FakePWGRasterConverter
);
801 pwg_converter_
= pwg_converter
.get();
802 local_print_operation_
->SetPWGRasterConverterForTesting(
803 pwg_converter
.Pass());
806 scoped_refptr
<base::RefCountedBytes
> RefCountedBytesFromString(
808 std::vector
<unsigned char> str_vec
;
809 str_vec
.insert(str_vec
.begin(), str
.begin(), str
.end());
810 return scoped_refptr
<base::RefCountedBytes
>(
811 base::RefCountedBytes::TakeVector(&str_vec
));
815 scoped_ptr
<PrivetLocalPrintOperation
> local_print_operation_
;
816 StrictMock
<MockLocalPrintDelegate
> local_print_delegate_
;
817 FakePWGRasterConverter
* pwg_converter_
;
820 TEST_F(PrivetLocalPrintTest
, SuccessfulLocalPrint
) {
821 local_print_operation_
->SetUsername("sample@gmail.com");
822 local_print_operation_
->SetJobname("Sample job name");
823 local_print_operation_
->SetData(RefCountedBytesFromString(
824 "Sample print data"));
825 local_print_operation_
->SetCapabilities(kSampleCapabilitiesResponse
);
826 local_print_operation_
->Start();
828 EXPECT_TRUE(SuccessfulResponseToURL(
829 GURL("http://10.0.0.8:6006/privet/info"),
830 kSampleInfoResponse
));
832 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
833 kSampleInfoResponse
));
835 EXPECT_CALL(local_print_delegate_
, OnPrivetPrintingDoneInternal());
837 // TODO(noamsml): Is encoding spaces as pluses standard?
838 EXPECT_TRUE(SuccessfulResponseToURLAndData(
839 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?"
840 "client_name=Chrome&user_name=sample%40gmail.com&"
841 "job_name=Sample+job+name"),
843 kSampleLocalPrintResponse
));
846 TEST_F(PrivetLocalPrintTest
, SuccessfulLocalPrintWithAnyMimetype
) {
847 local_print_operation_
->SetUsername("sample@gmail.com");
848 local_print_operation_
->SetJobname("Sample job name");
849 local_print_operation_
->SetData(
850 RefCountedBytesFromString("Sample print data"));
851 local_print_operation_
->SetCapabilities(
852 kSampleCapabilitiesResponseWithAnyMimetype
);
853 local_print_operation_
->Start();
855 EXPECT_TRUE(SuccessfulResponseToURL(
856 GURL("http://10.0.0.8:6006/privet/info"),
857 kSampleInfoResponse
));
859 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
860 kSampleInfoResponse
));
862 EXPECT_CALL(local_print_delegate_
, OnPrivetPrintingDoneInternal());
864 // TODO(noamsml): Is encoding spaces as pluses standard?
865 EXPECT_TRUE(SuccessfulResponseToURLAndData(
866 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?"
867 "client_name=Chrome&user_name=sample%40gmail.com&"
868 "job_name=Sample+job+name"),
870 kSampleLocalPrintResponse
));
873 TEST_F(PrivetLocalPrintTest
, SuccessfulPWGLocalPrint
) {
874 local_print_operation_
->SetUsername("sample@gmail.com");
875 local_print_operation_
->SetJobname("Sample job name");
876 local_print_operation_
->SetData(
877 RefCountedBytesFromString("path/to/"));
878 local_print_operation_
->SetCapabilities(kSampleCapabilitiesResponsePWGOnly
);
879 local_print_operation_
->Start();
881 EXPECT_TRUE(SuccessfulResponseToURL(
882 GURL("http://10.0.0.8:6006/privet/info"),
883 kSampleInfoResponse
));
885 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
886 kSampleInfoResponse
));
888 EXPECT_CALL(local_print_delegate_
, OnPrivetPrintingDoneInternal());
890 // TODO(noamsml): Is encoding spaces as pluses standard?
891 EXPECT_TRUE(SuccessfulResponseToURLAndFilePath(
892 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?"
893 "client_name=Chrome&user_name=sample%40gmail.com"
894 "&job_name=Sample+job+name"),
895 base::FilePath(FILE_PATH_LITERAL("path/to/test.pdf")),
896 kSampleLocalPrintResponse
));
898 EXPECT_EQ(printing::TRANSFORM_NORMAL
,
899 pwg_converter_
->bitmap_settings().odd_page_transform
);
900 EXPECT_FALSE(pwg_converter_
->bitmap_settings().rotate_all_pages
);
901 EXPECT_FALSE(pwg_converter_
->bitmap_settings().reverse_page_order
);
904 TEST_F(PrivetLocalPrintTest
, SuccessfulPWGLocalPrintDuplex
) {
905 local_print_operation_
->SetUsername("sample@gmail.com");
906 local_print_operation_
->SetJobname("Sample job name");
907 local_print_operation_
->SetData(RefCountedBytesFromString("path/to/"));
908 local_print_operation_
->SetTicket(kSampleCJTDuplex
);
909 local_print_operation_
->SetCapabilities(
910 kSampleCapabilitiesResponsePWGSettings
);
911 local_print_operation_
->Start();
913 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
914 kSampleInfoResponseWithCreatejob
));
916 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
917 kSampleInfoResponse
));
919 EXPECT_TRUE(SuccessfulResponseToURLAndJSONData(
920 GURL("http://10.0.0.8:6006/privet/printer/createjob"),
922 kSampleCreatejobResponse
));
924 EXPECT_CALL(local_print_delegate_
, OnPrivetPrintingDoneInternal());
926 // TODO(noamsml): Is encoding spaces as pluses standard?
927 EXPECT_TRUE(SuccessfulResponseToURLAndFilePath(
929 "http://10.0.0.8:6006/privet/printer/submitdoc?"
930 "client_name=Chrome&user_name=sample%40gmail.com"
931 "&job_name=Sample+job+name&job_id=1234"),
932 base::FilePath(FILE_PATH_LITERAL("path/to/test.pdf")),
933 kSampleLocalPrintResponse
));
935 EXPECT_EQ(printing::TRANSFORM_ROTATE_180
,
936 pwg_converter_
->bitmap_settings().odd_page_transform
);
937 EXPECT_FALSE(pwg_converter_
->bitmap_settings().rotate_all_pages
);
938 EXPECT_TRUE(pwg_converter_
->bitmap_settings().reverse_page_order
);
941 TEST_F(PrivetLocalPrintTest
, SuccessfulLocalPrintWithCreatejob
) {
942 local_print_operation_
->SetUsername("sample@gmail.com");
943 local_print_operation_
->SetJobname("Sample job name");
944 local_print_operation_
->SetTicket(kSampleCJT
);
945 local_print_operation_
->SetData(
946 RefCountedBytesFromString("Sample print data"));
947 local_print_operation_
->SetCapabilities(kSampleCapabilitiesResponse
);
948 local_print_operation_
->Start();
950 EXPECT_TRUE(SuccessfulResponseToURL(
951 GURL("http://10.0.0.8:6006/privet/info"),
952 kSampleInfoResponseWithCreatejob
));
954 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
955 kSampleInfoResponse
));
957 EXPECT_TRUE(SuccessfulResponseToURLAndJSONData(
958 GURL("http://10.0.0.8:6006/privet/printer/createjob"),
960 kSampleCreatejobResponse
));
962 EXPECT_CALL(local_print_delegate_
, OnPrivetPrintingDoneInternal());
964 // TODO(noamsml): Is encoding spaces as pluses standard?
965 EXPECT_TRUE(SuccessfulResponseToURLAndData(
966 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?"
967 "client_name=Chrome&user_name=sample%40gmail.com&"
968 "job_name=Sample+job+name&job_id=1234"),
970 kSampleLocalPrintResponse
));
973 TEST_F(PrivetLocalPrintTest
, SuccessfulLocalPrintWithOverlongName
) {
974 local_print_operation_
->SetUsername("sample@gmail.com");
975 local_print_operation_
->SetJobname(
976 "123456789:123456789:123456789:123456789:123456789:123456789:123456789:");
977 local_print_operation_
->SetTicket(kSampleCJT
);
978 local_print_operation_
->SetCapabilities(kSampleCapabilitiesResponse
);
979 local_print_operation_
->SetData(
980 RefCountedBytesFromString("Sample print data"));
981 local_print_operation_
->Start();
983 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
984 kSampleInfoResponseWithCreatejob
));
986 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
987 kSampleInfoResponse
));
989 EXPECT_TRUE(SuccessfulResponseToURLAndJSONData(
990 GURL("http://10.0.0.8:6006/privet/printer/createjob"),
992 kSampleCreatejobResponse
));
994 EXPECT_CALL(local_print_delegate_
, OnPrivetPrintingDoneInternal());
996 // TODO(noamsml): Is encoding spaces as pluses standard?
997 EXPECT_TRUE(SuccessfulResponseToURLAndData(
999 "http://10.0.0.8:6006/privet/printer/submitdoc?"
1000 "client_name=Chrome&user_name=sample%40gmail.com&"
1001 "job_name=123456789%3A123456789%3A123456789%3A1...123456789"
1002 "%3A123456789%3A123456789%3A&job_id=1234"),
1003 "Sample print data",
1004 kSampleLocalPrintResponse
));
1007 TEST_F(PrivetLocalPrintTest
, PDFPrintInvalidDocumentTypeRetry
) {
1008 local_print_operation_
->SetUsername("sample@gmail.com");
1009 local_print_operation_
->SetJobname("Sample job name");
1010 local_print_operation_
->SetTicket(kSampleCJT
);
1011 local_print_operation_
->SetCapabilities(kSampleCapabilitiesResponse
);
1012 local_print_operation_
->SetData(
1013 RefCountedBytesFromString("sample/path/"));
1014 local_print_operation_
->Start();
1016 EXPECT_TRUE(SuccessfulResponseToURL(
1017 GURL("http://10.0.0.8:6006/privet/info"),
1018 kSampleInfoResponseWithCreatejob
));
1020 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
1021 kSampleInfoResponse
));
1023 EXPECT_TRUE(SuccessfulResponseToURLAndJSONData(
1024 GURL("http://10.0.0.8:6006/privet/printer/createjob"),
1026 kSampleCreatejobResponse
));
1028 // TODO(noamsml): Is encoding spaces as pluses standard?
1029 EXPECT_TRUE(SuccessfulResponseToURLAndData(
1030 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?"
1031 "client_name=Chrome&user_name=sample%40gmail.com&"
1032 "job_name=Sample+job+name&job_id=1234"),
1034 kSampleInvalidDocumentTypeResponse
));
1036 EXPECT_CALL(local_print_delegate_
, OnPrivetPrintingDoneInternal());
1038 EXPECT_TRUE(SuccessfulResponseToURLAndFilePath(
1039 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?"
1040 "client_name=Chrome&user_name=sample%40gmail.com&"
1041 "job_name=Sample+job+name&job_id=1234"),
1042 base::FilePath(FILE_PATH_LITERAL("sample/path/test.pdf")),
1043 kSampleLocalPrintResponse
));
1046 TEST_F(PrivetLocalPrintTest
, LocalPrintRetryOnInvalidJobID
) {
1047 local_print_operation_
->SetUsername("sample@gmail.com");
1048 local_print_operation_
->SetJobname("Sample job name");
1049 local_print_operation_
->SetTicket(kSampleCJT
);
1050 local_print_operation_
->SetCapabilities(kSampleCapabilitiesResponse
);
1051 local_print_operation_
->SetData(
1052 RefCountedBytesFromString("Sample print data"));
1053 local_print_operation_
->Start();
1055 EXPECT_TRUE(SuccessfulResponseToURL(
1056 GURL("http://10.0.0.8:6006/privet/info"),
1057 kSampleInfoResponseWithCreatejob
));
1059 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
1060 kSampleInfoResponse
));
1062 EXPECT_TRUE(SuccessfulResponseToURLAndJSONData(
1063 GURL("http://10.0.0.8:6006/privet/printer/createjob"),
1065 kSampleCreatejobResponse
));
1067 EXPECT_TRUE(SuccessfulResponseToURLAndData(
1068 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?"
1069 "client_name=Chrome&user_name=sample%40gmail.com&"
1070 "job_name=Sample+job+name&job_id=1234"),
1071 "Sample print data",
1072 kSampleErrorResponsePrinterBusy
));
1074 RunFor(base::TimeDelta::FromSeconds(3));
1076 EXPECT_TRUE(SuccessfulResponseToURL(
1077 GURL("http://10.0.0.8:6006/privet/printer/createjob"),
1078 kSampleCreatejobResponse
));
1080 #endif // ENABLE_PRINT_PREVIEW
1084 } // namespace local_discovery