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/message_loop/message_loop.h"
9 #include "chrome/browser/local_discovery/privet_http_impl.h"
10 #include "net/base/host_port_pair.h"
11 #include "net/base/net_errors.h"
12 #include "net/url_request/test_url_fetcher_factory.h"
13 #include "net/url_request/url_request_test_util.h"
14 #include "printing/pwg_raster_settings.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 #if defined(ENABLE_FULL_PRINTING)
19 #include "chrome/browser/local_discovery/pwg_raster_converter.h"
20 #endif // ENABLE_FULL_PRINTING
22 using testing::StrictMock
;
23 using testing::NiceMock
;
25 namespace local_discovery
{
29 const char kSampleInfoResponse
[] = "{"
30 " \"version\": \"1.0\","
31 " \"name\": \"Common printer\","
32 " \"description\": \"Printer connected through Chrome connector\","
33 " \"url\": \"https://www.google.com/cloudprint\","
38 " \"device_state\": \"idle\","
39 " \"connection_state\": \"online\","
40 " \"manufacturer\": \"Google\","
41 " \"model\": \"Google Chrome\","
42 " \"serial_number\": \"1111-22222-33333-4444\","
43 " \"firmware\": \"24.0.1312.52\","
45 " \"setup_url\": \"http://support.google.com/\","
46 " \"support_url\": \"http://support.google.com/cloudprint/?hl=en\","
47 " \"update_url\": \"http://support.google.com/cloudprint/?hl=en\","
48 " \"x-privet-token\": \"SampleTokenForTesting\","
50 " \"/privet/accesstoken\","
51 " \"/privet/capabilities\","
52 " \"/privet/printer/submitdoc\","
56 const char kSampleInfoResponseRegistered
[] = "{"
57 " \"version\": \"1.0\","
58 " \"name\": \"Common printer\","
59 " \"description\": \"Printer connected through Chrome connector\","
60 " \"url\": \"https://www.google.com/cloudprint\","
64 " \"id\": \"MyDeviceID\","
65 " \"device_state\": \"idle\","
66 " \"connection_state\": \"online\","
67 " \"manufacturer\": \"Google\","
68 " \"model\": \"Google Chrome\","
69 " \"serial_number\": \"1111-22222-33333-4444\","
70 " \"firmware\": \"24.0.1312.52\","
72 " \"setup_url\": \"http://support.google.com/\","
73 " \"support_url\": \"http://support.google.com/cloudprint/?hl=en\","
74 " \"update_url\": \"http://support.google.com/cloudprint/?hl=en\","
75 " \"x-privet-token\": \"SampleTokenForTesting\","
77 " \"/privet/accesstoken\","
78 " \"/privet/capabilities\","
79 " \"/privet/printer/submitdoc\","
83 const char kSampleRegisterStartResponse
[] = "{"
84 "\"user\": \"example@google.com\","
85 "\"action\": \"start\""
88 const char kSampleRegisterGetClaimTokenResponse
[] = "{"
89 " \"action\": \"getClaimToken\","
90 " \"user\": \"example@google.com\","
91 " \"token\": \"MySampleToken\","
92 " \"claim_url\": \"https://domain.com/SoMeUrL\""
95 const char kSampleRegisterCompleteResponse
[] = "{"
96 "\"user\": \"example@google.com\","
97 "\"action\": \"complete\","
98 "\"device_id\": \"MyDeviceID\""
101 const char kSampleXPrivetErrorResponse
[] =
102 "{ \"error\": \"invalid_x_privet_token\" }";
104 const char kSampleRegisterErrorTransient
[] =
105 "{ \"error\": \"device_busy\", \"timeout\": 1}";
107 const char kSampleRegisterErrorPermanent
[] =
108 "{ \"error\": \"user_cancel\" }";
110 const char kSampleInfoResponseBadJson
[] = "{";
112 const char kSampleRegisterCancelResponse
[] = "{"
113 "\"user\": \"example@google.com\","
114 "\"action\": \"cancel\""
117 const char kSampleCapabilitiesResponse
[] = "{"
118 "\"version\" : \"1.0\","
120 " \"supported_content_type\" : ["
121 " { \"content_type\" : \"application/pdf\" },"
122 " { \"content_type\" : \"image/pwg-raster\" }"
127 #if defined(ENABLE_FULL_PRINTING)
128 const char kSampleInfoResponseWithCreatejob
[] = "{"
129 " \"version\": \"1.0\","
130 " \"name\": \"Common printer\","
131 " \"description\": \"Printer connected through Chrome connector\","
132 " \"url\": \"https://www.google.com/cloudprint\","
137 " \"device_state\": \"idle\","
138 " \"connection_state\": \"online\","
139 " \"manufacturer\": \"Google\","
140 " \"model\": \"Google Chrome\","
141 " \"serial_number\": \"1111-22222-33333-4444\","
142 " \"firmware\": \"24.0.1312.52\","
144 " \"setup_url\": \"http://support.google.com/\","
145 " \"support_url\": \"http://support.google.com/cloudprint/?hl=en\","
146 " \"update_url\": \"http://support.google.com/cloudprint/?hl=en\","
147 " \"x-privet-token\": \"SampleTokenForTesting\","
149 " \"/privet/accesstoken\","
150 " \"/privet/capabilities\","
151 " \"/privet/printer/createjob\","
152 " \"/privet/printer/submitdoc\","
156 const char kSampleLocalPrintResponse
[] = "{"
157 "\"job_id\": \"123\","
158 "\"expires_in\": 500,"
159 "\"job_type\": \"application/pdf\","
161 "\"job_name\": \"Sample job name\","
164 const char kSampleCapabilitiesResponsePWGOnly
[] = "{"
165 "\"version\" : \"1.0\","
167 " \"supported_content_type\" : ["
168 " { \"content_type\" : \"image/pwg-raster\" }"
173 const char kSampleErrorResponsePrinterBusy
[] = "{"
174 "\"error\": \"invalid_print_job\","
178 const char kSampleInvalidDocumentTypeResponse
[] = "{"
179 "\"error\" : \"invalid_document_type\""
182 const char kSampleCreatejobResponse
[] = "{ \"job_id\": \"1234\" }";
184 const char kSampleCapabilitiesResponseWithAnyMimetype
[] = "{"
185 "\"version\" : \"1.0\","
187 " \"supported_content_type\" : ["
188 " { \"content_type\" : \"*/*\" },"
189 " { \"content_type\" : \"image/pwg-raster\" }"
194 const char kSampleCJT
[] = "{ \"version\" : \"1.0\" }";
196 const char kSampleCapabilitiesResponsePWGSettings
[] =
198 "\"version\" : \"1.0\","
200 " \"pwg_raster_config\" : {"
201 " \"document_sheet_back\" : \"MANUAL_TUMBLE\","
202 " \"reverse_order_streaming\": true"
204 " \"supported_content_type\" : ["
205 " { \"content_type\" : \"image/pwg-raster\" }"
210 const char kSampleCJTDuplex
[] =
212 "\"version\" : \"1.0\","
213 "\"print\": { \"duplex\": {\"type\": \"SHORT_EDGE\"} }"
215 #endif // ENABLE_FULL_PRINTING
217 // Return the representation of the given JSON that would be outputted by
218 // JSONWriter. This ensures the same JSON values are represented by the same
220 std::string
NormalizeJson(const std::string
& json
) {
221 std::string result
= json
;
222 scoped_ptr
<base::Value
> value(base::JSONReader::Read(result
));
224 base::JSONWriter::Write(value
.get(), &result
);
228 class MockTestURLFetcherFactoryDelegate
229 : public net::TestURLFetcher::DelegateForTests
{
231 // Callback issued correspondingly to the call to the |Start()| method.
232 MOCK_METHOD1(OnRequestStart
, void(int fetcher_id
));
234 // Callback issued correspondingly to the call to |AppendChunkToUpload|.
235 // Uploaded chunks can be retrieved with the |upload_chunks()| getter.
236 MOCK_METHOD1(OnChunkUpload
, void(int fetcher_id
));
238 // Callback issued correspondingly to the destructor.
239 MOCK_METHOD1(OnRequestEnd
, void(int fetcher_id
));
242 class PrivetHTTPTest
: public ::testing::Test
{
245 PrivetURLFetcher::ResetTokenMapForTests();
247 request_context_
= new net::TestURLRequestContextGetter(
248 base::MessageLoopProxy::current());
250 PrivetV1HTTPClient::CreateDefault(make_scoped_ptr
<PrivetHTTPClient
>(
251 new PrivetHTTPClientImpl("sampleDevice._privet._tcp.local",
252 net::HostPortPair("10.0.0.8", 6006),
253 request_context_
.get())));
254 fetcher_factory_
.SetDelegateForTests(&fetcher_delegate_
);
257 virtual ~PrivetHTTPTest() {
260 bool SuccessfulResponseToURL(const GURL
& url
,
261 const std::string
& response
) {
262 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
263 EXPECT_TRUE(fetcher
);
264 EXPECT_EQ(url
, fetcher
->GetOriginalURL());
266 if (!fetcher
|| url
!= fetcher
->GetOriginalURL())
269 fetcher
->SetResponseString(response
);
270 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
272 fetcher
->set_response_code(200);
273 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
277 bool SuccessfulResponseToURLAndData(const GURL
& url
,
278 const std::string
& data
,
279 const std::string
& response
) {
280 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
281 EXPECT_TRUE(fetcher
);
282 EXPECT_EQ(url
, fetcher
->GetOriginalURL());
284 if (!fetcher
) return false;
286 EXPECT_EQ(data
, fetcher
->upload_data());
287 if (data
!= fetcher
->upload_data()) return false;
289 return SuccessfulResponseToURL(url
, response
);
292 bool SuccessfulResponseToURLAndJSONData(const GURL
& url
,
293 const std::string
& data
,
294 const std::string
& response
) {
295 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
296 EXPECT_TRUE(fetcher
);
297 EXPECT_EQ(url
, fetcher
->GetOriginalURL());
302 std::string normalized_data
= NormalizeJson(data
);
303 std::string normalized_upload_data
= NormalizeJson(fetcher
->upload_data());
304 EXPECT_EQ(normalized_data
, normalized_upload_data
);
305 if (normalized_data
!= normalized_upload_data
)
308 return SuccessfulResponseToURL(url
, response
);
311 bool SuccessfulResponseToURLAndFilePath(const GURL
& url
,
312 const base::FilePath
& file_path
,
313 const std::string
& response
) {
314 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
315 EXPECT_TRUE(fetcher
);
316 EXPECT_EQ(url
, fetcher
->GetOriginalURL());
318 if (!fetcher
) return false;
320 EXPECT_EQ(file_path
, fetcher
->upload_file_path());
321 if (file_path
!= fetcher
->upload_file_path()) return false;
323 return SuccessfulResponseToURL(url
, response
);
327 void RunFor(base::TimeDelta time_period
) {
328 base::CancelableCallback
<void()> callback(base::Bind(
329 &PrivetHTTPTest::Stop
, base::Unretained(this)));
330 base::MessageLoop::current()->PostDelayedTask(
331 FROM_HERE
, callback
.callback(), time_period
);
333 base::MessageLoop::current()->Run();
338 base::MessageLoop::current()->Quit();
342 base::MessageLoop loop_
;
343 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_
;
344 net::TestURLFetcherFactory fetcher_factory_
;
345 scoped_ptr
<PrivetV1HTTPClient
> privet_client_
;
346 NiceMock
<MockTestURLFetcherFactoryDelegate
> fetcher_delegate_
;
349 class MockJSONCallback
{
351 MockJSONCallback() {}
352 ~MockJSONCallback() {}
354 void OnPrivetJSONDone(const base::DictionaryValue
* value
) {
358 value_
.reset(value
->DeepCopy());
361 OnPrivetJSONDoneInternal();
364 MOCK_METHOD0(OnPrivetJSONDoneInternal
, void());
366 const base::DictionaryValue
* value() { return value_
.get(); }
367 PrivetJSONOperation::ResultCallback
callback() {
368 return base::Bind(&MockJSONCallback::OnPrivetJSONDone
,
369 base::Unretained(this));
372 scoped_ptr
<base::DictionaryValue
> value_
;
375 class MockRegisterDelegate
: public PrivetRegisterOperation::Delegate
{
377 MockRegisterDelegate() {
379 ~MockRegisterDelegate() {
382 virtual void OnPrivetRegisterClaimToken(
383 PrivetRegisterOperation
* operation
,
384 const std::string
& token
,
385 const GURL
& url
) OVERRIDE
{
386 OnPrivetRegisterClaimTokenInternal(token
, url
);
389 MOCK_METHOD2(OnPrivetRegisterClaimTokenInternal
, void(
390 const std::string
& token
,
393 virtual void OnPrivetRegisterError(
394 PrivetRegisterOperation
* operation
,
395 const std::string
& action
,
396 PrivetRegisterOperation::FailureReason reason
,
397 int printer_http_code
,
398 const base::DictionaryValue
* json
) OVERRIDE
{
399 // TODO(noamsml): Save and test for JSON?
400 OnPrivetRegisterErrorInternal(action
, reason
, printer_http_code
);
403 MOCK_METHOD3(OnPrivetRegisterErrorInternal
,
404 void(const std::string
& action
,
405 PrivetRegisterOperation::FailureReason reason
,
406 int printer_http_code
));
408 virtual void OnPrivetRegisterDone(
409 PrivetRegisterOperation
* operation
,
410 const std::string
& device_id
) OVERRIDE
{
411 OnPrivetRegisterDoneInternal(device_id
);
414 MOCK_METHOD1(OnPrivetRegisterDoneInternal
,
415 void(const std::string
& device_id
));
418 class MockLocalPrintDelegate
: public PrivetLocalPrintOperation::Delegate
{
420 MockLocalPrintDelegate() {}
421 ~MockLocalPrintDelegate() {}
423 virtual void OnPrivetPrintingDone(
424 const PrivetLocalPrintOperation
* print_operation
) {
425 OnPrivetPrintingDoneInternal();
428 MOCK_METHOD0(OnPrivetPrintingDoneInternal
, void());
430 virtual void OnPrivetPrintingError(
431 const PrivetLocalPrintOperation
* print_operation
, int http_code
) {
432 OnPrivetPrintingErrorInternal(http_code
);
435 MOCK_METHOD1(OnPrivetPrintingErrorInternal
, void(int http_code
));
438 class PrivetInfoTest
: public PrivetHTTPTest
{
442 virtual ~PrivetInfoTest() {}
444 virtual void SetUp() OVERRIDE
{
445 info_operation_
= privet_client_
->CreateInfoOperation(
446 info_callback_
.callback());
450 scoped_ptr
<PrivetJSONOperation
> info_operation_
;
451 StrictMock
<MockJSONCallback
> info_callback_
;
454 TEST_F(PrivetInfoTest
, SuccessfulInfo
) {
455 info_operation_
->Start();
457 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
458 ASSERT_TRUE(fetcher
!= NULL
);
459 EXPECT_EQ(GURL("http://10.0.0.8:6006/privet/info"),
460 fetcher
->GetOriginalURL());
462 fetcher
->SetResponseString(kSampleInfoResponse
);
463 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
465 fetcher
->set_response_code(200);
467 EXPECT_CALL(info_callback_
, OnPrivetJSONDoneInternal());
468 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
471 TEST_F(PrivetInfoTest
, InfoFailureHTTP
) {
472 info_operation_
->Start();
474 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
475 ASSERT_TRUE(fetcher
!= NULL
);
476 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
478 fetcher
->set_response_code(404);
480 EXPECT_CALL(info_callback_
, OnPrivetJSONDoneInternal());
481 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
484 class PrivetRegisterTest
: public PrivetHTTPTest
{
486 PrivetRegisterTest() {
488 virtual ~PrivetRegisterTest() {
491 virtual void SetUp() OVERRIDE
{
492 info_operation_
= privet_client_
->CreateInfoOperation(
493 info_callback_
.callback());
494 register_operation_
=
495 privet_client_
->CreateRegisterOperation("example@google.com",
496 ®ister_delegate_
);
500 bool SuccessfulResponseToURL(const GURL
& url
,
501 const std::string
& response
) {
502 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
503 EXPECT_TRUE(fetcher
);
504 EXPECT_EQ(url
, fetcher
->GetOriginalURL());
505 if (!fetcher
|| url
!= fetcher
->GetOriginalURL())
508 fetcher
->SetResponseString(response
);
509 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
511 fetcher
->set_response_code(200);
512 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
516 scoped_ptr
<PrivetJSONOperation
> info_operation_
;
517 NiceMock
<MockJSONCallback
> info_callback_
;
518 scoped_ptr
<PrivetRegisterOperation
> register_operation_
;
519 StrictMock
<MockRegisterDelegate
> register_delegate_
;
522 TEST_F(PrivetRegisterTest
, RegisterSuccessSimple
) {
523 register_operation_
->Start();
525 EXPECT_TRUE(SuccessfulResponseToURL(
526 GURL("http://10.0.0.8:6006/privet/info"),
527 kSampleInfoResponse
));
529 EXPECT_TRUE(SuccessfulResponseToURL(
530 GURL("http://10.0.0.8:6006/privet/register?"
531 "action=start&user=example%40google.com"),
532 kSampleRegisterStartResponse
));
534 EXPECT_CALL(register_delegate_
, OnPrivetRegisterClaimTokenInternal(
536 GURL("https://domain.com/SoMeUrL")));
538 EXPECT_TRUE(SuccessfulResponseToURL(
539 GURL("http://10.0.0.8:6006/privet/register?"
540 "action=getClaimToken&user=example%40google.com"),
541 kSampleRegisterGetClaimTokenResponse
));
543 register_operation_
->CompleteRegistration();
545 EXPECT_TRUE(SuccessfulResponseToURL(
546 GURL("http://10.0.0.8:6006/privet/register?"
547 "action=complete&user=example%40google.com"),
548 kSampleRegisterCompleteResponse
));
550 EXPECT_CALL(register_delegate_
, OnPrivetRegisterDoneInternal(
553 EXPECT_TRUE(SuccessfulResponseToURL(
554 GURL("http://10.0.0.8:6006/privet/info"),
555 kSampleInfoResponseRegistered
));
558 TEST_F(PrivetRegisterTest
, RegisterXSRFFailure
) {
559 register_operation_
->Start();
561 EXPECT_TRUE(SuccessfulResponseToURL(
562 GURL("http://10.0.0.8:6006/privet/info"),
563 kSampleInfoResponse
));
565 EXPECT_TRUE(SuccessfulResponseToURL(
566 GURL("http://10.0.0.8:6006/privet/register?"
567 "action=start&user=example%40google.com"),
568 kSampleRegisterStartResponse
));
570 EXPECT_TRUE(SuccessfulResponseToURL(
571 GURL("http://10.0.0.8:6006/privet/register?"
572 "action=getClaimToken&user=example%40google.com"),
573 kSampleXPrivetErrorResponse
));
575 EXPECT_TRUE(SuccessfulResponseToURL(
576 GURL("http://10.0.0.8:6006/privet/info"),
577 kSampleInfoResponse
));
579 EXPECT_CALL(register_delegate_
, OnPrivetRegisterClaimTokenInternal(
580 "MySampleToken", GURL("https://domain.com/SoMeUrL")));
582 EXPECT_TRUE(SuccessfulResponseToURL(
583 GURL("http://10.0.0.8:6006/privet/register?"
584 "action=getClaimToken&user=example%40google.com"),
585 kSampleRegisterGetClaimTokenResponse
));
588 TEST_F(PrivetRegisterTest
, TransientFailure
) {
589 register_operation_
->Start();
591 EXPECT_TRUE(SuccessfulResponseToURL(
592 GURL("http://10.0.0.8:6006/privet/info"),
593 kSampleInfoResponse
));
595 EXPECT_TRUE(SuccessfulResponseToURL(
596 GURL("http://10.0.0.8:6006/privet/register?"
597 "action=start&user=example%40google.com"),
598 kSampleRegisterErrorTransient
));
600 EXPECT_CALL(fetcher_delegate_
, OnRequestStart(0));
602 RunFor(base::TimeDelta::FromSeconds(2));
604 testing::Mock::VerifyAndClearExpectations(&fetcher_delegate_
);
606 EXPECT_TRUE(SuccessfulResponseToURL(
607 GURL("http://10.0.0.8:6006/privet/register?"
608 "action=start&user=example%40google.com"),
609 kSampleRegisterStartResponse
));
612 TEST_F(PrivetRegisterTest
, PermanentFailure
) {
613 register_operation_
->Start();
615 EXPECT_TRUE(SuccessfulResponseToURL(
616 GURL("http://10.0.0.8:6006/privet/info"),
617 kSampleInfoResponse
));
619 EXPECT_TRUE(SuccessfulResponseToURL(
620 GURL("http://10.0.0.8:6006/privet/register?"
621 "action=start&user=example%40google.com"),
622 kSampleRegisterStartResponse
));
624 EXPECT_CALL(register_delegate_
,
625 OnPrivetRegisterErrorInternal(
627 PrivetRegisterOperation::FAILURE_JSON_ERROR
,
630 EXPECT_TRUE(SuccessfulResponseToURL(
631 GURL("http://10.0.0.8:6006/privet/register?"
632 "action=getClaimToken&user=example%40google.com"),
633 kSampleRegisterErrorPermanent
));
636 TEST_F(PrivetRegisterTest
, InfoFailure
) {
637 register_operation_
->Start();
639 EXPECT_CALL(register_delegate_
,
640 OnPrivetRegisterErrorInternal(
642 PrivetRegisterOperation::FAILURE_TOKEN
,
645 EXPECT_TRUE(SuccessfulResponseToURL(
646 GURL("http://10.0.0.8:6006/privet/info"),
647 kSampleInfoResponseBadJson
));
650 TEST_F(PrivetRegisterTest
, RegisterCancel
) {
651 register_operation_
->Start();
653 EXPECT_TRUE(SuccessfulResponseToURL(
654 GURL("http://10.0.0.8:6006/privet/info"),
655 kSampleInfoResponse
));
657 EXPECT_TRUE(SuccessfulResponseToURL(
658 GURL("http://10.0.0.8:6006/privet/register?"
659 "action=start&user=example%40google.com"),
660 kSampleRegisterStartResponse
));
662 register_operation_
->Cancel();
664 EXPECT_TRUE(SuccessfulResponseToURL(
665 GURL("http://10.0.0.8:6006/privet/register?"
666 "action=cancel&user=example%40google.com"),
667 kSampleRegisterCancelResponse
));
669 // Must keep mocks alive for 3 seconds so the cancelation object can be
671 RunFor(base::TimeDelta::FromSeconds(3));
674 class PrivetCapabilitiesTest
: public PrivetHTTPTest
{
676 PrivetCapabilitiesTest() {}
678 virtual ~PrivetCapabilitiesTest() {}
680 virtual void SetUp() OVERRIDE
{
681 capabilities_operation_
= privet_client_
->CreateCapabilitiesOperation(
682 capabilities_callback_
.callback());
686 scoped_ptr
<PrivetJSONOperation
> capabilities_operation_
;
687 StrictMock
<MockJSONCallback
> capabilities_callback_
;
690 TEST_F(PrivetCapabilitiesTest
, SuccessfulCapabilities
) {
691 capabilities_operation_
->Start();
693 EXPECT_TRUE(SuccessfulResponseToURL(
694 GURL("http://10.0.0.8:6006/privet/info"),
695 kSampleInfoResponse
));
697 EXPECT_CALL(capabilities_callback_
, OnPrivetJSONDoneInternal());
699 EXPECT_TRUE(SuccessfulResponseToURL(
700 GURL("http://10.0.0.8:6006/privet/capabilities"),
701 kSampleCapabilitiesResponse
));
704 EXPECT_TRUE(capabilities_callback_
.value()->GetString("version", &version
));
705 EXPECT_EQ("1.0", version
);
708 TEST_F(PrivetCapabilitiesTest
, CacheToken
) {
709 capabilities_operation_
->Start();
711 EXPECT_TRUE(SuccessfulResponseToURL(
712 GURL("http://10.0.0.8:6006/privet/info"),
713 kSampleInfoResponse
));
715 EXPECT_CALL(capabilities_callback_
, OnPrivetJSONDoneInternal());
717 EXPECT_TRUE(SuccessfulResponseToURL(
718 GURL("http://10.0.0.8:6006/privet/capabilities"),
719 kSampleCapabilitiesResponse
));
721 capabilities_operation_
= privet_client_
->CreateCapabilitiesOperation(
722 capabilities_callback_
.callback());
724 capabilities_operation_
->Start();
726 EXPECT_CALL(capabilities_callback_
, OnPrivetJSONDoneInternal());
728 EXPECT_TRUE(SuccessfulResponseToURL(
729 GURL("http://10.0.0.8:6006/privet/capabilities"),
730 kSampleCapabilitiesResponse
));
733 TEST_F(PrivetCapabilitiesTest
, BadToken
) {
734 capabilities_operation_
->Start();
736 EXPECT_TRUE(SuccessfulResponseToURL(
737 GURL("http://10.0.0.8:6006/privet/info"),
738 kSampleInfoResponse
));
740 EXPECT_TRUE(SuccessfulResponseToURL(
741 GURL("http://10.0.0.8:6006/privet/capabilities"),
742 kSampleXPrivetErrorResponse
));
744 EXPECT_TRUE(SuccessfulResponseToURL(
745 GURL("http://10.0.0.8:6006/privet/info"),
746 kSampleInfoResponse
));
748 EXPECT_CALL(capabilities_callback_
, OnPrivetJSONDoneInternal());
750 EXPECT_TRUE(SuccessfulResponseToURL(
751 GURL("http://10.0.0.8:6006/privet/capabilities"),
752 kSampleCapabilitiesResponse
));
755 #if defined(ENABLE_FULL_PRINTING)
756 // A note on PWG raster conversion: The PWG raster converter used simply
757 // converts strings to file paths based on them by appending "test.pdf", since
758 // it's easier to test that way. Instead of using a mock, we simply check if the
759 // request is uploading a file that is based on this pattern.
760 class FakePWGRasterConverter
: public PWGRasterConverter
{
762 FakePWGRasterConverter() {
765 virtual ~FakePWGRasterConverter() {
768 virtual void Start(base::RefCountedMemory
* data
,
769 const printing::PdfRenderSettings
& conversion_settings
,
770 const printing::PwgRasterSettings
& bitmap_settings
,
771 const ResultCallback
& callback
) OVERRIDE
{
772 bitmap_settings_
= bitmap_settings
;
773 std::string
data_str(data
->front_as
<char>(), data
->size());
774 callback
.Run(true, base::FilePath().AppendASCII(data_str
+ "test.pdf"));
777 const printing::PwgRasterSettings
& bitmap_settings() {
778 return bitmap_settings_
;
782 printing::PwgRasterSettings bitmap_settings_
;
785 class PrivetLocalPrintTest
: public PrivetHTTPTest
{
787 PrivetLocalPrintTest() {}
789 virtual ~PrivetLocalPrintTest() {}
791 virtual void SetUp() OVERRIDE
{
792 PrivetURLFetcher::ResetTokenMapForTests();
794 local_print_operation_
= privet_client_
->CreateLocalPrintOperation(
795 &local_print_delegate_
);
797 scoped_ptr
<FakePWGRasterConverter
> pwg_converter(
798 new FakePWGRasterConverter
);
799 pwg_converter_
= pwg_converter
.get();
800 local_print_operation_
->SetPWGRasterConverterForTesting(
801 pwg_converter
.PassAs
<PWGRasterConverter
>());
804 scoped_refptr
<base::RefCountedBytes
> RefCountedBytesFromString(
806 std::vector
<unsigned char> str_vec
;
807 str_vec
.insert(str_vec
.begin(), str
.begin(), str
.end());
808 return scoped_refptr
<base::RefCountedBytes
>(
809 base::RefCountedBytes::TakeVector(&str_vec
));
813 scoped_ptr
<PrivetLocalPrintOperation
> local_print_operation_
;
814 StrictMock
<MockLocalPrintDelegate
> local_print_delegate_
;
815 FakePWGRasterConverter
* pwg_converter_
;
818 TEST_F(PrivetLocalPrintTest
, SuccessfulLocalPrint
) {
819 local_print_operation_
->SetUsername("sample@gmail.com");
820 local_print_operation_
->SetJobname("Sample job name");
821 local_print_operation_
->SetData(RefCountedBytesFromString(
822 "Sample print data"));
823 local_print_operation_
->SetCapabilities(kSampleCapabilitiesResponse
);
824 local_print_operation_
->Start();
826 EXPECT_TRUE(SuccessfulResponseToURL(
827 GURL("http://10.0.0.8:6006/privet/info"),
828 kSampleInfoResponse
));
830 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
831 kSampleInfoResponse
));
833 EXPECT_CALL(local_print_delegate_
, OnPrivetPrintingDoneInternal());
835 // TODO(noamsml): Is encoding spaces as pluses standard?
836 EXPECT_TRUE(SuccessfulResponseToURLAndData(
837 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?"
838 "client_name=Chrome&user_name=sample%40gmail.com&"
839 "job_name=Sample+job+name"),
841 kSampleLocalPrintResponse
));
844 TEST_F(PrivetLocalPrintTest
, SuccessfulLocalPrintWithAnyMimetype
) {
845 local_print_operation_
->SetUsername("sample@gmail.com");
846 local_print_operation_
->SetJobname("Sample job name");
847 local_print_operation_
->SetData(
848 RefCountedBytesFromString("Sample print data"));
849 local_print_operation_
->SetCapabilities(
850 kSampleCapabilitiesResponseWithAnyMimetype
);
851 local_print_operation_
->Start();
853 EXPECT_TRUE(SuccessfulResponseToURL(
854 GURL("http://10.0.0.8:6006/privet/info"),
855 kSampleInfoResponse
));
857 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
858 kSampleInfoResponse
));
860 EXPECT_CALL(local_print_delegate_
, OnPrivetPrintingDoneInternal());
862 // TODO(noamsml): Is encoding spaces as pluses standard?
863 EXPECT_TRUE(SuccessfulResponseToURLAndData(
864 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?"
865 "client_name=Chrome&user_name=sample%40gmail.com&"
866 "job_name=Sample+job+name"),
868 kSampleLocalPrintResponse
));
871 TEST_F(PrivetLocalPrintTest
, SuccessfulPWGLocalPrint
) {
872 local_print_operation_
->SetUsername("sample@gmail.com");
873 local_print_operation_
->SetJobname("Sample job name");
874 local_print_operation_
->SetData(
875 RefCountedBytesFromString("path/to/"));
876 local_print_operation_
->SetCapabilities(kSampleCapabilitiesResponsePWGOnly
);
877 local_print_operation_
->Start();
879 EXPECT_TRUE(SuccessfulResponseToURL(
880 GURL("http://10.0.0.8:6006/privet/info"),
881 kSampleInfoResponse
));
883 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
884 kSampleInfoResponse
));
886 EXPECT_CALL(local_print_delegate_
, OnPrivetPrintingDoneInternal());
888 // TODO(noamsml): Is encoding spaces as pluses standard?
889 EXPECT_TRUE(SuccessfulResponseToURLAndFilePath(
890 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?"
891 "client_name=Chrome&user_name=sample%40gmail.com"
892 "&job_name=Sample+job+name"),
893 base::FilePath(FILE_PATH_LITERAL("path/to/test.pdf")),
894 kSampleLocalPrintResponse
));
896 EXPECT_EQ(printing::TRANSFORM_NORMAL
,
897 pwg_converter_
->bitmap_settings().odd_page_transform
);
898 EXPECT_FALSE(pwg_converter_
->bitmap_settings().rotate_all_pages
);
899 EXPECT_FALSE(pwg_converter_
->bitmap_settings().reverse_page_order
);
902 TEST_F(PrivetLocalPrintTest
, SuccessfulPWGLocalPrintDuplex
) {
903 local_print_operation_
->SetUsername("sample@gmail.com");
904 local_print_operation_
->SetJobname("Sample job name");
905 local_print_operation_
->SetData(RefCountedBytesFromString("path/to/"));
906 local_print_operation_
->SetTicket(kSampleCJTDuplex
);
907 local_print_operation_
->SetCapabilities(
908 kSampleCapabilitiesResponsePWGSettings
);
909 local_print_operation_
->Start();
911 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
912 kSampleInfoResponseWithCreatejob
));
914 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
915 kSampleInfoResponse
));
917 EXPECT_TRUE(SuccessfulResponseToURLAndJSONData(
918 GURL("http://10.0.0.8:6006/privet/printer/createjob"),
920 kSampleCreatejobResponse
));
922 EXPECT_CALL(local_print_delegate_
, OnPrivetPrintingDoneInternal());
924 // TODO(noamsml): Is encoding spaces as pluses standard?
925 EXPECT_TRUE(SuccessfulResponseToURLAndFilePath(
927 "http://10.0.0.8:6006/privet/printer/submitdoc?"
928 "client_name=Chrome&user_name=sample%40gmail.com"
929 "&job_name=Sample+job+name&job_id=1234"),
930 base::FilePath(FILE_PATH_LITERAL("path/to/test.pdf")),
931 kSampleLocalPrintResponse
));
933 EXPECT_EQ(printing::TRANSFORM_ROTATE_180
,
934 pwg_converter_
->bitmap_settings().odd_page_transform
);
935 EXPECT_FALSE(pwg_converter_
->bitmap_settings().rotate_all_pages
);
936 EXPECT_TRUE(pwg_converter_
->bitmap_settings().reverse_page_order
);
939 TEST_F(PrivetLocalPrintTest
, SuccessfulLocalPrintWithCreatejob
) {
940 local_print_operation_
->SetUsername("sample@gmail.com");
941 local_print_operation_
->SetJobname("Sample job name");
942 local_print_operation_
->SetTicket(kSampleCJT
);
943 local_print_operation_
->SetData(
944 RefCountedBytesFromString("Sample print data"));
945 local_print_operation_
->SetCapabilities(kSampleCapabilitiesResponse
);
946 local_print_operation_
->Start();
948 EXPECT_TRUE(SuccessfulResponseToURL(
949 GURL("http://10.0.0.8:6006/privet/info"),
950 kSampleInfoResponseWithCreatejob
));
952 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
953 kSampleInfoResponse
));
955 EXPECT_TRUE(SuccessfulResponseToURLAndJSONData(
956 GURL("http://10.0.0.8:6006/privet/printer/createjob"),
958 kSampleCreatejobResponse
));
960 EXPECT_CALL(local_print_delegate_
, OnPrivetPrintingDoneInternal());
962 // TODO(noamsml): Is encoding spaces as pluses standard?
963 EXPECT_TRUE(SuccessfulResponseToURLAndData(
964 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?"
965 "client_name=Chrome&user_name=sample%40gmail.com&"
966 "job_name=Sample+job+name&job_id=1234"),
968 kSampleLocalPrintResponse
));
971 TEST_F(PrivetLocalPrintTest
, SuccessfulLocalPrintWithOverlongName
) {
972 local_print_operation_
->SetUsername("sample@gmail.com");
973 local_print_operation_
->SetJobname(
974 "123456789:123456789:123456789:123456789:123456789:123456789:123456789:");
975 local_print_operation_
->SetTicket(kSampleCJT
);
976 local_print_operation_
->SetCapabilities(kSampleCapabilitiesResponse
);
977 local_print_operation_
->SetData(
978 RefCountedBytesFromString("Sample print data"));
979 local_print_operation_
->Start();
981 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
982 kSampleInfoResponseWithCreatejob
));
984 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
985 kSampleInfoResponse
));
987 EXPECT_TRUE(SuccessfulResponseToURLAndJSONData(
988 GURL("http://10.0.0.8:6006/privet/printer/createjob"),
990 kSampleCreatejobResponse
));
992 EXPECT_CALL(local_print_delegate_
, OnPrivetPrintingDoneInternal());
994 // TODO(noamsml): Is encoding spaces as pluses standard?
995 EXPECT_TRUE(SuccessfulResponseToURLAndData(
997 "http://10.0.0.8:6006/privet/printer/submitdoc?"
998 "client_name=Chrome&user_name=sample%40gmail.com&"
999 "job_name=123456789%3A123456789%3A123456789%3A1...123456789"
1000 "%3A123456789%3A123456789%3A&job_id=1234"),
1001 "Sample print data",
1002 kSampleLocalPrintResponse
));
1005 TEST_F(PrivetLocalPrintTest
, PDFPrintInvalidDocumentTypeRetry
) {
1006 local_print_operation_
->SetUsername("sample@gmail.com");
1007 local_print_operation_
->SetJobname("Sample job name");
1008 local_print_operation_
->SetTicket(kSampleCJT
);
1009 local_print_operation_
->SetCapabilities(kSampleCapabilitiesResponse
);
1010 local_print_operation_
->SetData(
1011 RefCountedBytesFromString("sample/path/"));
1012 local_print_operation_
->Start();
1014 EXPECT_TRUE(SuccessfulResponseToURL(
1015 GURL("http://10.0.0.8:6006/privet/info"),
1016 kSampleInfoResponseWithCreatejob
));
1018 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
1019 kSampleInfoResponse
));
1021 EXPECT_TRUE(SuccessfulResponseToURLAndJSONData(
1022 GURL("http://10.0.0.8:6006/privet/printer/createjob"),
1024 kSampleCreatejobResponse
));
1026 // TODO(noamsml): Is encoding spaces as pluses standard?
1027 EXPECT_TRUE(SuccessfulResponseToURLAndData(
1028 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?"
1029 "client_name=Chrome&user_name=sample%40gmail.com&"
1030 "job_name=Sample+job+name&job_id=1234"),
1032 kSampleInvalidDocumentTypeResponse
));
1034 EXPECT_CALL(local_print_delegate_
, OnPrivetPrintingDoneInternal());
1036 EXPECT_TRUE(SuccessfulResponseToURLAndFilePath(
1037 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?"
1038 "client_name=Chrome&user_name=sample%40gmail.com&"
1039 "job_name=Sample+job+name&job_id=1234"),
1040 base::FilePath(FILE_PATH_LITERAL("sample/path/test.pdf")),
1041 kSampleLocalPrintResponse
));
1044 TEST_F(PrivetLocalPrintTest
, LocalPrintRetryOnInvalidJobID
) {
1045 local_print_operation_
->SetUsername("sample@gmail.com");
1046 local_print_operation_
->SetJobname("Sample job name");
1047 local_print_operation_
->SetTicket(kSampleCJT
);
1048 local_print_operation_
->SetCapabilities(kSampleCapabilitiesResponse
);
1049 local_print_operation_
->SetData(
1050 RefCountedBytesFromString("Sample print data"));
1051 local_print_operation_
->Start();
1053 EXPECT_TRUE(SuccessfulResponseToURL(
1054 GURL("http://10.0.0.8:6006/privet/info"),
1055 kSampleInfoResponseWithCreatejob
));
1057 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"),
1058 kSampleInfoResponse
));
1060 EXPECT_TRUE(SuccessfulResponseToURLAndJSONData(
1061 GURL("http://10.0.0.8:6006/privet/printer/createjob"),
1063 kSampleCreatejobResponse
));
1065 EXPECT_TRUE(SuccessfulResponseToURLAndData(
1066 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?"
1067 "client_name=Chrome&user_name=sample%40gmail.com&"
1068 "job_name=Sample+job+name&job_id=1234"),
1069 "Sample print data",
1070 kSampleErrorResponsePrinterBusy
));
1072 RunFor(base::TimeDelta::FromSeconds(3));
1074 EXPECT_TRUE(SuccessfulResponseToURL(
1075 GURL("http://10.0.0.8:6006/privet/printer/createjob"),
1076 kSampleCreatejobResponse
));
1078 #endif // ENABLE_FULL_PRINTING
1082 } // namespace local_discovery