| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/json/json_reader.h" | 6 #include "base/json/json_reader.h" |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "chrome/browser/local_discovery/privet_http_impl.h" | 9 #include "chrome/browser/local_discovery/privet_http_impl.h" |
| 10 #include "net/base/host_port_pair.h" | 10 #include "net/base/host_port_pair.h" |
| 11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 12 #include "net/url_request/test_url_fetcher_factory.h" | 12 #include "net/url_request/test_url_fetcher_factory.h" |
| 13 #include "net/url_request/url_request_test_util.h" | 13 #include "net/url_request/url_request_test_util.h" |
| 14 #include "printing/bitmap_transform_settings.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 using testing::StrictMock; | 18 using testing::StrictMock; |
| 18 using testing::NiceMock; | 19 using testing::NiceMock; |
| 19 | 20 |
| 20 namespace local_discovery { | 21 namespace local_discovery { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 const char kSampleInvalidDocumentTypeResponse[] = "{" | 183 const char kSampleInvalidDocumentTypeResponse[] = "{" |
| 183 "\"error\" : \"invalid_document_type\"" | 184 "\"error\" : \"invalid_document_type\"" |
| 184 "}"; | 185 "}"; |
| 185 | 186 |
| 186 const char kSampleCreatejobResponse[] = "{ \"job_id\": \"1234\" }"; | 187 const char kSampleCreatejobResponse[] = "{ \"job_id\": \"1234\" }"; |
| 187 | 188 |
| 188 const char kSampleEmptyJSONResponse[] = "{}"; | 189 const char kSampleEmptyJSONResponse[] = "{}"; |
| 189 | 190 |
| 190 const char kSampleCJT[] = "{ \"version\" : \"1.0\" }"; | 191 const char kSampleCJT[] = "{ \"version\" : \"1.0\" }"; |
| 191 | 192 |
| 193 const char kSampleCapabilitiesResponsePWGSettings[] = |
| 194 "{" |
| 195 "\"version\" : \"1.0\"," |
| 196 "\"printer\" : {" |
| 197 " \"pwg_raster_config\" : {" |
| 198 " \"document_sheet_back\" : \"MANUAL_TUMBLE\"," |
| 199 " \"reverse_order_streaming\": true" |
| 200 " }," |
| 201 " \"supported_content_type\" : [" |
| 202 " { \"content_type\" : \"image/pwg-raster\" }" |
| 203 " ]" |
| 204 "}" |
| 205 "}"; |
| 206 |
| 207 const char kSampleCJTDuplex[] = |
| 208 "{" |
| 209 "\"version\" : \"1.0\"," |
| 210 "\"print\": { \"duplex\": {\"type\": \"SHORT_EDGE\"} }" |
| 211 "}"; |
| 212 |
| 213 const char kSampleCJTDuplexPWGSettings[] = |
| 214 "{" |
| 215 "\"version\" : \"1.0\"," |
| 216 "\"print\": {" |
| 217 " \"pwg_raster_config\" : {" |
| 218 " \"document_sheet_back\" : \"MANUAL_TUMBLE\"," |
| 219 " \"reverse_order_streaming\": true" |
| 220 " }," |
| 221 " \"duplex\": {\"type\": \"SHORT_EDGE\"} }" |
| 222 "}"; |
| 223 |
| 192 // Return the representation of the given JSON that would be outputted by | 224 // Return the representation of the given JSON that would be outputted by |
| 193 // JSONWriter. This ensures the same JSON values are represented by the same | 225 // JSONWriter. This ensures the same JSON values are represented by the same |
| 194 // string. | 226 // string. |
| 195 std::string NormalizeJson(const std::string& json) { | 227 std::string NormalizeJson(const std::string& json) { |
| 196 std::string result = json; | 228 std::string result = json; |
| 197 scoped_ptr<base::Value> value(base::JSONReader::Read(result)); | 229 scoped_ptr<base::Value> value(base::JSONReader::Read(result)); |
| 198 DCHECK(value); | 230 DCHECK(value); |
| 199 base::JSONWriter::Write(value.get(), &result); | 231 base::JSONWriter::Write(value.get(), &result); |
| 200 return result; | 232 return result; |
| 201 } | 233 } |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 class FakePWGRasterConverter : public PWGRasterConverter { | 448 class FakePWGRasterConverter : public PWGRasterConverter { |
| 417 public: | 449 public: |
| 418 FakePWGRasterConverter() { | 450 FakePWGRasterConverter() { |
| 419 } | 451 } |
| 420 | 452 |
| 421 virtual ~FakePWGRasterConverter() { | 453 virtual ~FakePWGRasterConverter() { |
| 422 } | 454 } |
| 423 | 455 |
| 424 virtual void Start(base::RefCountedMemory* data, | 456 virtual void Start(base::RefCountedMemory* data, |
| 425 const printing::PdfRenderSettings& conversion_settings, | 457 const printing::PdfRenderSettings& conversion_settings, |
| 458 const printing::BitmapTransformSettings& bitmap_settings, |
| 426 const ResultCallback& callback) OVERRIDE { | 459 const ResultCallback& callback) OVERRIDE { |
| 460 bitmap_settings_ = bitmap_settings; |
| 427 std::string data_str(data->front_as<char>(), data->size()); | 461 std::string data_str(data->front_as<char>(), data->size()); |
| 428 callback.Run(true, base::FilePath().AppendASCII(data_str + "test.pdf")); | 462 callback.Run(true, base::FilePath().AppendASCII(data_str + "test.pdf")); |
| 429 } | 463 } |
| 464 |
| 465 const printing::BitmapTransformSettings& bitmap_settings() { |
| 466 return bitmap_settings_; |
| 467 } |
| 468 |
| 469 private: |
| 470 printing::BitmapTransformSettings bitmap_settings_; |
| 430 }; | 471 }; |
| 431 | 472 |
| 432 TEST_F(PrivetHTTPTest, CreatePrivetStorageList) { | 473 TEST_F(PrivetHTTPTest, CreatePrivetStorageList) { |
| 433 MockJSONCallback mock_callback; | 474 MockJSONCallback mock_callback; |
| 434 scoped_ptr<PrivetJSONOperation> storage_list_operation = | 475 scoped_ptr<PrivetJSONOperation> storage_list_operation = |
| 435 privet_client_->CreateStorageListOperation( | 476 privet_client_->CreateStorageListOperation( |
| 436 "/path/to/nothing", | 477 "/path/to/nothing", |
| 437 mock_callback.callback()); | 478 mock_callback.callback()); |
| 438 storage_list_operation->Start(); | 479 storage_list_operation->Start(); |
| 439 | 480 |
| (...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 769 PrivetLocalPrintTest() {} | 810 PrivetLocalPrintTest() {} |
| 770 | 811 |
| 771 virtual ~PrivetLocalPrintTest() {} | 812 virtual ~PrivetLocalPrintTest() {} |
| 772 | 813 |
| 773 virtual void SetUp() OVERRIDE { | 814 virtual void SetUp() OVERRIDE { |
| 774 PrivetURLFetcher::ResetTokenMapForTests(); | 815 PrivetURLFetcher::ResetTokenMapForTests(); |
| 775 | 816 |
| 776 local_print_operation_ = privet_client_->CreateLocalPrintOperation( | 817 local_print_operation_ = privet_client_->CreateLocalPrintOperation( |
| 777 &local_print_delegate_); | 818 &local_print_delegate_); |
| 778 | 819 |
| 820 scoped_ptr<FakePWGRasterConverter> pwg_converter( |
| 821 new FakePWGRasterConverter); |
| 822 pwg_converter_ = pwg_converter.get(); |
| 779 local_print_operation_->SetPWGRasterConverterForTesting( | 823 local_print_operation_->SetPWGRasterConverterForTesting( |
| 780 scoped_ptr<PWGRasterConverter>(new FakePWGRasterConverter)); | 824 pwg_converter.PassAs<PWGRasterConverter>()); |
| 781 } | 825 } |
| 782 | 826 |
| 783 scoped_refptr<base::RefCountedBytes> RefCountedBytesFromString( | 827 scoped_refptr<base::RefCountedBytes> RefCountedBytesFromString( |
| 784 std::string str) { | 828 std::string str) { |
| 785 std::vector<unsigned char> str_vec; | 829 std::vector<unsigned char> str_vec; |
| 786 str_vec.insert(str_vec.begin(), str.begin(), str.end()); | 830 str_vec.insert(str_vec.begin(), str.begin(), str.end()); |
| 787 return scoped_refptr<base::RefCountedBytes>( | 831 return scoped_refptr<base::RefCountedBytes>( |
| 788 base::RefCountedBytes::TakeVector(&str_vec)); | 832 base::RefCountedBytes::TakeVector(&str_vec)); |
| 789 } | 833 } |
| 790 | 834 |
| 791 protected: | 835 protected: |
| 792 scoped_ptr<PrivetLocalPrintOperation> local_print_operation_; | 836 scoped_ptr<PrivetLocalPrintOperation> local_print_operation_; |
| 793 StrictMock<MockLocalPrintDelegate> local_print_delegate_; | 837 StrictMock<MockLocalPrintDelegate> local_print_delegate_; |
| 838 FakePWGRasterConverter* pwg_converter_; |
| 794 }; | 839 }; |
| 795 | 840 |
| 796 TEST_F(PrivetLocalPrintTest, SuccessfulLocalPrint) { | 841 TEST_F(PrivetLocalPrintTest, SuccessfulLocalPrint) { |
| 797 local_print_operation_->SetUsername("sample@gmail.com"); | 842 local_print_operation_->SetUsername("sample@gmail.com"); |
| 798 local_print_operation_->SetJobname("Sample job name"); | 843 local_print_operation_->SetJobname("Sample job name"); |
| 799 local_print_operation_->SetData(RefCountedBytesFromString( | 844 local_print_operation_->SetData(RefCountedBytesFromString( |
| 800 "Sample print data")); | 845 "Sample print data")); |
| 801 local_print_operation_->SetCapabilities(kSampleCapabilitiesResponse); | 846 local_print_operation_->SetCapabilities(kSampleCapabilitiesResponse); |
| 802 local_print_operation_->Start(); | 847 local_print_operation_->Start(); |
| 803 | 848 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 863 | 908 |
| 864 EXPECT_CALL(local_print_delegate_, OnPrivetPrintingDoneInternal()); | 909 EXPECT_CALL(local_print_delegate_, OnPrivetPrintingDoneInternal()); |
| 865 | 910 |
| 866 // TODO(noamsml): Is encoding spaces as pluses standard? | 911 // TODO(noamsml): Is encoding spaces as pluses standard? |
| 867 EXPECT_TRUE(SuccessfulResponseToURLAndFilePath( | 912 EXPECT_TRUE(SuccessfulResponseToURLAndFilePath( |
| 868 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?" | 913 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?" |
| 869 "client_name=Chrome&user_name=sample%40gmail.com" | 914 "client_name=Chrome&user_name=sample%40gmail.com" |
| 870 "&job_name=Sample+job+name"), | 915 "&job_name=Sample+job+name"), |
| 871 base::FilePath(FILE_PATH_LITERAL("path/to/test.pdf")), | 916 base::FilePath(FILE_PATH_LITERAL("path/to/test.pdf")), |
| 872 kSampleLocalPrintResponse)); | 917 kSampleLocalPrintResponse)); |
| 918 |
| 919 EXPECT_EQ(printing::TRANSFORM_NORMAL, |
| 920 pwg_converter_->bitmap_settings().odd_page_transform); |
| 921 EXPECT_FALSE(pwg_converter_->bitmap_settings().rotate_all_pages); |
| 922 EXPECT_FALSE(pwg_converter_->bitmap_settings().reverse_page_order); |
| 923 }; |
| 924 |
| 925 TEST_F(PrivetLocalPrintTest, SuccessfulPWGLocalPrintDuplex) { |
| 926 local_print_operation_->SetUsername("sample@gmail.com"); |
| 927 local_print_operation_->SetJobname("Sample job name"); |
| 928 local_print_operation_->SetData(RefCountedBytesFromString("path/to/")); |
| 929 local_print_operation_->SetTicket(kSampleCJTDuplex); |
| 930 local_print_operation_->SetCapabilities( |
| 931 kSampleCapabilitiesResponsePWGSettings); |
| 932 local_print_operation_->Start(); |
| 933 |
| 934 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"), |
| 935 kSampleInfoResponseWithCreatejob)); |
| 936 |
| 937 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"), |
| 938 kSampleInfoResponse)); |
| 939 |
| 940 EXPECT_TRUE(SuccessfulResponseToURLAndJSONData( |
| 941 GURL("http://10.0.0.8:6006/privet/printer/createjob"), |
| 942 kSampleCJTDuplexPWGSettings, |
| 943 kSampleCreatejobResponse)); |
| 944 |
| 945 EXPECT_CALL(local_print_delegate_, OnPrivetPrintingDoneInternal()); |
| 946 |
| 947 // TODO(noamsml): Is encoding spaces as pluses standard? |
| 948 EXPECT_TRUE(SuccessfulResponseToURLAndFilePath( |
| 949 GURL( |
| 950 "http://10.0.0.8:6006/privet/printer/submitdoc?" |
| 951 "client_name=Chrome&user_name=sample%40gmail.com" |
| 952 "&job_name=Sample+job+name&job_id=1234"), |
| 953 base::FilePath(FILE_PATH_LITERAL("path/to/test.pdf")), |
| 954 kSampleLocalPrintResponse)); |
| 955 |
| 956 EXPECT_EQ(printing::TRANSFORM_ROTATE_180, |
| 957 pwg_converter_->bitmap_settings().odd_page_transform); |
| 958 EXPECT_FALSE(pwg_converter_->bitmap_settings().rotate_all_pages); |
| 959 EXPECT_TRUE(pwg_converter_->bitmap_settings().reverse_page_order); |
| 873 }; | 960 }; |
| 874 | 961 |
| 875 TEST_F(PrivetLocalPrintTest, SuccessfulLocalPrintWithCreatejob) { | 962 TEST_F(PrivetLocalPrintTest, SuccessfulLocalPrintWithCreatejob) { |
| 876 local_print_operation_->SetUsername("sample@gmail.com"); | 963 local_print_operation_->SetUsername("sample@gmail.com"); |
| 877 local_print_operation_->SetJobname("Sample job name"); | 964 local_print_operation_->SetJobname("Sample job name"); |
| 878 local_print_operation_->SetTicket(kSampleCJT); | 965 local_print_operation_->SetTicket(kSampleCJT); |
| 879 local_print_operation_->SetData( | 966 local_print_operation_->SetData( |
| 880 RefCountedBytesFromString("Sample print data")); | 967 RefCountedBytesFromString("Sample print data")); |
| 881 local_print_operation_->SetCapabilities(kSampleCapabilitiesResponse); | 968 local_print_operation_->SetCapabilities(kSampleCapabilitiesResponse); |
| 882 local_print_operation_->Start(); | 969 local_print_operation_->Start(); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1009 | 1096 |
| 1010 EXPECT_TRUE(SuccessfulResponseToURL( | 1097 EXPECT_TRUE(SuccessfulResponseToURL( |
| 1011 GURL("http://10.0.0.8:6006/privet/printer/createjob"), | 1098 GURL("http://10.0.0.8:6006/privet/printer/createjob"), |
| 1012 kSampleCreatejobResponse)); | 1099 kSampleCreatejobResponse)); |
| 1013 }; | 1100 }; |
| 1014 | 1101 |
| 1015 | 1102 |
| 1016 } // namespace | 1103 } // namespace |
| 1017 | 1104 |
| 1018 } // namespace local_discovery | 1105 } // namespace local_discovery |
| OLD | NEW |