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" |
| 7 #include "base/json/json_writer.h" |
6 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
7 #include "chrome/browser/local_discovery/privet_http_impl.h" | 9 #include "chrome/browser/local_discovery/privet_http_impl.h" |
8 #include "net/base/host_port_pair.h" | 10 #include "net/base/host_port_pair.h" |
9 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
10 #include "net/url_request/test_url_fetcher_factory.h" | 12 #include "net/url_request/test_url_fetcher_factory.h" |
11 #include "net/url_request/url_request_test_util.h" | 13 #include "net/url_request/url_request_test_util.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
14 | 16 |
15 using testing::StrictMock; | 17 using testing::StrictMock; |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 "}"; | 180 "}"; |
179 | 181 |
180 const char kSampleInvalidDocumentTypeResponse[] = "{" | 182 const char kSampleInvalidDocumentTypeResponse[] = "{" |
181 "\"error\" : \"invalid_document_type\"" | 183 "\"error\" : \"invalid_document_type\"" |
182 "}"; | 184 "}"; |
183 | 185 |
184 const char kSampleCreatejobResponse[] = "{ \"job_id\": \"1234\" }"; | 186 const char kSampleCreatejobResponse[] = "{ \"job_id\": \"1234\" }"; |
185 | 187 |
186 const char kSampleEmptyJSONResponse[] = "{}"; | 188 const char kSampleEmptyJSONResponse[] = "{}"; |
187 | 189 |
| 190 const char kSampleCDD[] = "{ \"version\" : \"1.0\" }"; |
| 191 |
| 192 // 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 |
| 194 // string. |
| 195 std::string NormalizeJson(const std::string& json) { |
| 196 std::string result = json; |
| 197 scoped_ptr<base::Value> value(base::JSONReader::Read(result)); |
| 198 DCHECK(value); |
| 199 base::JSONWriter::Write(value.get(), &result); |
| 200 return result; |
| 201 } |
| 202 |
188 class MockTestURLFetcherFactoryDelegate | 203 class MockTestURLFetcherFactoryDelegate |
189 : public net::TestURLFetcher::DelegateForTests { | 204 : public net::TestURLFetcher::DelegateForTests { |
190 public: | 205 public: |
191 // Callback issued correspondingly to the call to the |Start()| method. | 206 // Callback issued correspondingly to the call to the |Start()| method. |
192 MOCK_METHOD1(OnRequestStart, void(int fetcher_id)); | 207 MOCK_METHOD1(OnRequestStart, void(int fetcher_id)); |
193 | 208 |
194 // Callback issued correspondingly to the call to |AppendChunkToUpload|. | 209 // Callback issued correspondingly to the call to |AppendChunkToUpload|. |
195 // Uploaded chunks can be retrieved with the |upload_chunks()| getter. | 210 // Uploaded chunks can be retrieved with the |upload_chunks()| getter. |
196 MOCK_METHOD1(OnChunkUpload, void(int fetcher_id)); | 211 MOCK_METHOD1(OnChunkUpload, void(int fetcher_id)); |
197 | 212 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 EXPECT_EQ(url, fetcher->GetOriginalURL()); | 256 EXPECT_EQ(url, fetcher->GetOriginalURL()); |
242 | 257 |
243 if (!fetcher) return false; | 258 if (!fetcher) return false; |
244 | 259 |
245 EXPECT_EQ(data, fetcher->upload_data()); | 260 EXPECT_EQ(data, fetcher->upload_data()); |
246 if (data != fetcher->upload_data()) return false; | 261 if (data != fetcher->upload_data()) return false; |
247 | 262 |
248 return SuccessfulResponseToURL(url, response); | 263 return SuccessfulResponseToURL(url, response); |
249 } | 264 } |
250 | 265 |
| 266 bool SuccessfulResponseToURLAndJSONData(const GURL& url, |
| 267 const std::string& data, |
| 268 const std::string& response) { |
| 269 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); |
| 270 EXPECT_TRUE(fetcher); |
| 271 EXPECT_EQ(url, fetcher->GetOriginalURL()); |
| 272 |
| 273 if (!fetcher) |
| 274 return false; |
| 275 |
| 276 std::string normalized_data = NormalizeJson(data); |
| 277 std::string normalized_upload_data = NormalizeJson(fetcher->upload_data()); |
| 278 EXPECT_EQ(normalized_data, normalized_upload_data); |
| 279 if (normalized_data != normalized_upload_data) |
| 280 return false; |
| 281 |
| 282 return SuccessfulResponseToURL(url, response); |
| 283 } |
| 284 |
251 bool SuccessfulResponseToURLAndFilePath(const GURL& url, | 285 bool SuccessfulResponseToURLAndFilePath(const GURL& url, |
252 const base::FilePath& file_path, | 286 const base::FilePath& file_path, |
253 const std::string& response) { | 287 const std::string& response) { |
254 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); | 288 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); |
255 EXPECT_TRUE(fetcher); | 289 EXPECT_TRUE(fetcher); |
256 EXPECT_EQ(url, fetcher->GetOriginalURL()); | 290 EXPECT_EQ(url, fetcher->GetOriginalURL()); |
257 | 291 |
258 if (!fetcher) return false; | 292 if (!fetcher) return false; |
259 | 293 |
260 EXPECT_EQ(file_path, fetcher->upload_file_path()); | 294 EXPECT_EQ(file_path, fetcher->upload_file_path()); |
(...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
842 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?" | 876 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?" |
843 "client_name=Chrome&user_name=sample%40gmail.com" | 877 "client_name=Chrome&user_name=sample%40gmail.com" |
844 "&job_name=Sample+job+name"), | 878 "&job_name=Sample+job+name"), |
845 base::FilePath(FILE_PATH_LITERAL("path/to/test.pdf")), | 879 base::FilePath(FILE_PATH_LITERAL("path/to/test.pdf")), |
846 kSampleLocalPrintResponse)); | 880 kSampleLocalPrintResponse)); |
847 }; | 881 }; |
848 | 882 |
849 TEST_F(PrivetLocalPrintTest, SuccessfulLocalPrintWithCreatejob) { | 883 TEST_F(PrivetLocalPrintTest, SuccessfulLocalPrintWithCreatejob) { |
850 local_print_operation_->SetUsername("sample@gmail.com"); | 884 local_print_operation_->SetUsername("sample@gmail.com"); |
851 local_print_operation_->SetJobname("Sample job name"); | 885 local_print_operation_->SetJobname("Sample job name"); |
852 local_print_operation_->SetTicket("Sample print ticket"); | 886 local_print_operation_->SetTicket(kSampleCDD); |
853 local_print_operation_->SetData( | 887 local_print_operation_->SetData( |
854 RefCountedBytesFromString("Sample print data")); | 888 RefCountedBytesFromString("Sample print data")); |
855 local_print_operation_->Start(); | 889 local_print_operation_->Start(); |
856 | 890 |
857 EXPECT_TRUE(SuccessfulResponseToURL( | 891 EXPECT_TRUE(SuccessfulResponseToURL( |
858 GURL("http://10.0.0.8:6006/privet/info"), | 892 GURL("http://10.0.0.8:6006/privet/info"), |
859 kSampleInfoResponseWithCreatejob)); | 893 kSampleInfoResponseWithCreatejob)); |
860 | 894 |
861 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"), | 895 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"), |
862 kSampleInfoResponse)); | 896 kSampleInfoResponse)); |
863 | 897 |
864 EXPECT_TRUE( | 898 EXPECT_TRUE( |
865 SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/capabilities"), | 899 SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/capabilities"), |
866 kSampleCapabilitiesResponse)); | 900 kSampleCapabilitiesResponse)); |
867 | 901 |
868 EXPECT_TRUE(SuccessfulResponseToURLAndData( | 902 EXPECT_TRUE(SuccessfulResponseToURLAndJSONData( |
869 GURL("http://10.0.0.8:6006/privet/printer/createjob"), | 903 GURL("http://10.0.0.8:6006/privet/printer/createjob"), |
870 "Sample print ticket", | 904 kSampleCDD, |
871 kSampleCreatejobResponse)); | 905 kSampleCreatejobResponse)); |
872 | 906 |
873 EXPECT_CALL(local_print_delegate_, OnPrivetPrintingDoneInternal()); | 907 EXPECT_CALL(local_print_delegate_, OnPrivetPrintingDoneInternal()); |
874 | 908 |
875 // TODO(noamsml): Is encoding spaces as pluses standard? | 909 // TODO(noamsml): Is encoding spaces as pluses standard? |
876 EXPECT_TRUE(SuccessfulResponseToURLAndData( | 910 EXPECT_TRUE(SuccessfulResponseToURLAndData( |
877 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?" | 911 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?" |
878 "client_name=Chrome&user_name=sample%40gmail.com&" | 912 "client_name=Chrome&user_name=sample%40gmail.com&" |
879 "job_name=Sample+job+name&job_id=1234"), | 913 "job_name=Sample+job+name&job_id=1234"), |
880 "Sample print data", | 914 "Sample print data", |
881 kSampleLocalPrintResponse)); | 915 kSampleLocalPrintResponse)); |
882 }; | 916 }; |
883 | 917 |
884 TEST_F(PrivetLocalPrintTest, SuccessfulLocalPrintWithOverlongName) { | 918 TEST_F(PrivetLocalPrintTest, SuccessfulLocalPrintWithOverlongName) { |
885 local_print_operation_->SetUsername("sample@gmail.com"); | 919 local_print_operation_->SetUsername("sample@gmail.com"); |
886 local_print_operation_->SetJobname( | 920 local_print_operation_->SetJobname( |
887 "123456789:123456789:123456789:123456789:123456789:123456789:123456789:"); | 921 "123456789:123456789:123456789:123456789:123456789:123456789:123456789:"); |
888 local_print_operation_->SetTicket("Sample print ticket"); | 922 local_print_operation_->SetTicket(kSampleCDD); |
889 local_print_operation_->SetData( | 923 local_print_operation_->SetData( |
890 RefCountedBytesFromString("Sample print data")); | 924 RefCountedBytesFromString("Sample print data")); |
891 local_print_operation_->Start(); | 925 local_print_operation_->Start(); |
892 | 926 |
893 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"), | 927 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"), |
894 kSampleInfoResponseWithCreatejob)); | 928 kSampleInfoResponseWithCreatejob)); |
895 | 929 |
896 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"), | 930 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"), |
897 kSampleInfoResponse)); | 931 kSampleInfoResponse)); |
898 | 932 |
899 EXPECT_TRUE( | 933 EXPECT_TRUE( |
900 SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/capabilities"), | 934 SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/capabilities"), |
901 kSampleCapabilitiesResponse)); | 935 kSampleCapabilitiesResponse)); |
902 | 936 |
903 EXPECT_TRUE(SuccessfulResponseToURLAndData( | 937 EXPECT_TRUE(SuccessfulResponseToURLAndJSONData( |
904 GURL("http://10.0.0.8:6006/privet/printer/createjob"), | 938 GURL("http://10.0.0.8:6006/privet/printer/createjob"), |
905 "Sample print ticket", | 939 kSampleCDD, |
906 kSampleCreatejobResponse)); | 940 kSampleCreatejobResponse)); |
907 | 941 |
908 EXPECT_CALL(local_print_delegate_, OnPrivetPrintingDoneInternal()); | 942 EXPECT_CALL(local_print_delegate_, OnPrivetPrintingDoneInternal()); |
909 | 943 |
910 // TODO(noamsml): Is encoding spaces as pluses standard? | 944 // TODO(noamsml): Is encoding spaces as pluses standard? |
911 EXPECT_TRUE(SuccessfulResponseToURLAndData( | 945 EXPECT_TRUE(SuccessfulResponseToURLAndData( |
912 GURL( | 946 GURL( |
913 "http://10.0.0.8:6006/privet/printer/submitdoc?" | 947 "http://10.0.0.8:6006/privet/printer/submitdoc?" |
914 "client_name=Chrome&user_name=sample%40gmail.com&" | 948 "client_name=Chrome&user_name=sample%40gmail.com&" |
915 "job_name=123456789%3A123456789%3A123456789%3A1...123456789" | 949 "job_name=123456789%3A123456789%3A123456789%3A1...123456789" |
916 "%3A123456789%3A123456789%3A&job_id=1234"), | 950 "%3A123456789%3A123456789%3A&job_id=1234"), |
917 "Sample print data", | 951 "Sample print data", |
918 kSampleLocalPrintResponse)); | 952 kSampleLocalPrintResponse)); |
919 }; | 953 }; |
920 | 954 |
921 TEST_F(PrivetLocalPrintTest, PDFPrintInvalidDocumentTypeRetry) { | 955 TEST_F(PrivetLocalPrintTest, PDFPrintInvalidDocumentTypeRetry) { |
922 local_print_operation_->SetUsername("sample@gmail.com"); | 956 local_print_operation_->SetUsername("sample@gmail.com"); |
923 local_print_operation_->SetJobname("Sample job name"); | 957 local_print_operation_->SetJobname("Sample job name"); |
924 local_print_operation_->SetTicket("Sample print ticket"); | 958 local_print_operation_->SetTicket(kSampleCDD); |
925 local_print_operation_->SetData( | 959 local_print_operation_->SetData( |
926 RefCountedBytesFromString("sample/path/")); | 960 RefCountedBytesFromString("sample/path/")); |
927 local_print_operation_->Start(); | 961 local_print_operation_->Start(); |
928 | 962 |
929 EXPECT_TRUE(SuccessfulResponseToURL( | 963 EXPECT_TRUE(SuccessfulResponseToURL( |
930 GURL("http://10.0.0.8:6006/privet/info"), | 964 GURL("http://10.0.0.8:6006/privet/info"), |
931 kSampleInfoResponseWithCreatejob)); | 965 kSampleInfoResponseWithCreatejob)); |
932 | 966 |
933 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"), | 967 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"), |
934 kSampleInfoResponse)); | 968 kSampleInfoResponse)); |
935 | 969 |
936 EXPECT_TRUE( | 970 EXPECT_TRUE( |
937 SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/capabilities"), | 971 SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/capabilities"), |
938 kSampleCapabilitiesResponse)); | 972 kSampleCapabilitiesResponse)); |
939 | 973 |
940 EXPECT_TRUE(SuccessfulResponseToURLAndData( | 974 EXPECT_TRUE(SuccessfulResponseToURLAndJSONData( |
941 GURL("http://10.0.0.8:6006/privet/printer/createjob"), | 975 GURL("http://10.0.0.8:6006/privet/printer/createjob"), |
942 "Sample print ticket", | 976 kSampleCDD, |
943 kSampleCreatejobResponse)); | 977 kSampleCreatejobResponse)); |
944 | 978 |
945 // TODO(noamsml): Is encoding spaces as pluses standard? | 979 // TODO(noamsml): Is encoding spaces as pluses standard? |
946 EXPECT_TRUE(SuccessfulResponseToURLAndData( | 980 EXPECT_TRUE(SuccessfulResponseToURLAndData( |
947 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?" | 981 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?" |
948 "client_name=Chrome&user_name=sample%40gmail.com&" | 982 "client_name=Chrome&user_name=sample%40gmail.com&" |
949 "job_name=Sample+job+name&job_id=1234"), | 983 "job_name=Sample+job+name&job_id=1234"), |
950 "sample/path/", | 984 "sample/path/", |
951 kSampleInvalidDocumentTypeResponse)); | 985 kSampleInvalidDocumentTypeResponse)); |
952 | 986 |
953 EXPECT_CALL(local_print_delegate_, OnPrivetPrintingDoneInternal()); | 987 EXPECT_CALL(local_print_delegate_, OnPrivetPrintingDoneInternal()); |
954 | 988 |
955 EXPECT_TRUE(SuccessfulResponseToURLAndFilePath( | 989 EXPECT_TRUE(SuccessfulResponseToURLAndFilePath( |
956 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?" | 990 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?" |
957 "client_name=Chrome&user_name=sample%40gmail.com&" | 991 "client_name=Chrome&user_name=sample%40gmail.com&" |
958 "job_name=Sample+job+name&job_id=1234"), | 992 "job_name=Sample+job+name&job_id=1234"), |
959 base::FilePath(FILE_PATH_LITERAL("sample/path/test.pdf")), | 993 base::FilePath(FILE_PATH_LITERAL("sample/path/test.pdf")), |
960 kSampleLocalPrintResponse)); | 994 kSampleLocalPrintResponse)); |
961 }; | 995 }; |
962 | 996 |
963 TEST_F(PrivetLocalPrintTest, LocalPrintRetryOnInvalidJobID) { | 997 TEST_F(PrivetLocalPrintTest, LocalPrintRetryOnInvalidJobID) { |
964 local_print_operation_->SetUsername("sample@gmail.com"); | 998 local_print_operation_->SetUsername("sample@gmail.com"); |
965 local_print_operation_->SetJobname("Sample job name"); | 999 local_print_operation_->SetJobname("Sample job name"); |
966 local_print_operation_->SetTicket("Sample print ticket"); | 1000 local_print_operation_->SetTicket(kSampleCDD); |
967 local_print_operation_->SetData( | 1001 local_print_operation_->SetData( |
968 RefCountedBytesFromString("Sample print data")); | 1002 RefCountedBytesFromString("Sample print data")); |
969 local_print_operation_->Start(); | 1003 local_print_operation_->Start(); |
970 | 1004 |
971 EXPECT_TRUE(SuccessfulResponseToURL( | 1005 EXPECT_TRUE(SuccessfulResponseToURL( |
972 GURL("http://10.0.0.8:6006/privet/info"), | 1006 GURL("http://10.0.0.8:6006/privet/info"), |
973 kSampleInfoResponseWithCreatejob)); | 1007 kSampleInfoResponseWithCreatejob)); |
974 | 1008 |
975 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"), | 1009 EXPECT_TRUE(SuccessfulResponseToURL(GURL("http://10.0.0.8:6006/privet/info"), |
976 kSampleInfoResponse)); | 1010 kSampleInfoResponse)); |
977 | 1011 |
978 EXPECT_TRUE(SuccessfulResponseToURL( | 1012 EXPECT_TRUE(SuccessfulResponseToURL( |
979 GURL("http://10.0.0.8:6006/privet/capabilities"), | 1013 GURL("http://10.0.0.8:6006/privet/capabilities"), |
980 kSampleCapabilitiesResponse)); | 1014 kSampleCapabilitiesResponse)); |
981 | 1015 |
982 EXPECT_TRUE(SuccessfulResponseToURLAndData( | 1016 EXPECT_TRUE(SuccessfulResponseToURLAndJSONData( |
983 GURL("http://10.0.0.8:6006/privet/printer/createjob"), | 1017 GURL("http://10.0.0.8:6006/privet/printer/createjob"), |
984 "Sample print ticket", | 1018 kSampleCDD, |
985 kSampleCreatejobResponse)); | 1019 kSampleCreatejobResponse)); |
986 | 1020 |
987 EXPECT_TRUE(SuccessfulResponseToURLAndData( | 1021 EXPECT_TRUE(SuccessfulResponseToURLAndData( |
988 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?" | 1022 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?" |
989 "client_name=Chrome&user_name=sample%40gmail.com&" | 1023 "client_name=Chrome&user_name=sample%40gmail.com&" |
990 "job_name=Sample+job+name&job_id=1234"), | 1024 "job_name=Sample+job+name&job_id=1234"), |
991 "Sample print data", | 1025 "Sample print data", |
992 kSampleErrorResponsePrinterBusy)); | 1026 kSampleErrorResponsePrinterBusy)); |
993 | 1027 |
994 RunFor(base::TimeDelta::FromSeconds(3)); | 1028 RunFor(base::TimeDelta::FromSeconds(3)); |
995 | 1029 |
996 EXPECT_TRUE(SuccessfulResponseToURL( | 1030 EXPECT_TRUE(SuccessfulResponseToURL( |
997 GURL("http://10.0.0.8:6006/privet/printer/createjob"), | 1031 GURL("http://10.0.0.8:6006/privet/printer/createjob"), |
998 kSampleCreatejobResponse)); | 1032 kSampleCreatejobResponse)); |
999 }; | 1033 }; |
1000 | 1034 |
1001 | 1035 |
1002 } // namespace | 1036 } // namespace |
1003 | 1037 |
1004 } // namespace local_discovery | 1038 } // namespace local_discovery |
OLD | NEW |