| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/service/cloud_print/cloud_print_helpers.h" | 5 #include "chrome/service/cloud_print/cloud_print_helpers.h" |
| 6 | 6 |
| 7 #include "base/md5.h" |
| 7 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
| 9 #include "base/sys_info.h" |
| 10 #include "chrome/common/chrome_version_info.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 12 |
| 13 namespace cloud_print { |
| 14 |
| 10 namespace { | 15 namespace { |
| 11 | 16 |
| 12 void CheckURLs(const GURL& server_base_url) { | 17 void CheckJobStatusURLs(const GURL& server_base_url) { |
| 13 GURL url = CloudPrintHelpers::GetUrlForPrinterRegistration(server_base_url); | |
| 14 std::string expected_url_base = server_base_url.spec(); | 18 std::string expected_url_base = server_base_url.spec(); |
| 15 if (expected_url_base[expected_url_base.length() - 1] != '/') { | 19 if (expected_url_base[expected_url_base.length() - 1] != '/') |
| 16 expected_url_base += "/"; | 20 expected_url_base += "/"; |
| 17 } | |
| 18 std::string expected_url = base::StringPrintf("%sregister", | |
| 19 expected_url_base.c_str()); | |
| 20 EXPECT_EQ(expected_url, url.spec()); | |
| 21 | 21 |
| 22 url = CloudPrintHelpers::GetUrlForPrinterUpdate(server_base_url, | 22 EXPECT_EQ(base::StringPrintf("%scontrol?jobid=87654321&status=ERROR", |
| 23 "printeridfoo"); | 23 expected_url_base.c_str()), |
| 24 expected_url = base::StringPrintf("%supdate?printerid=printeridfoo", | 24 GetUrlForJobStatusUpdate(server_base_url, "87654321", |
| 25 expected_url_base.c_str()); | 25 PRINT_JOB_STATUS_ERROR).spec()); |
| 26 EXPECT_EQ(expected_url, url.spec()); | |
| 27 | 26 |
| 28 url = CloudPrintHelpers::GetUrlForPrinterDelete(server_base_url, | 27 PrintJobDetails details; |
| 29 "printeridbar", "deleted"); | 28 details.status = PRINT_JOB_STATUS_IN_PROGRESS; |
| 30 expected_url = base::StringPrintf( | |
| 31 "%sdelete?printerid=printeridbar&reason=deleted", | |
| 32 expected_url_base.c_str()); | |
| 33 EXPECT_EQ(expected_url, url.spec()); | |
| 34 | |
| 35 url = CloudPrintHelpers::GetUrlForPrinterList(server_base_url, "demoproxy"); | |
| 36 expected_url = base::StringPrintf("%slist?proxy=demoproxy", | |
| 37 expected_url_base.c_str()); | |
| 38 EXPECT_EQ(expected_url, url.spec()); | |
| 39 | |
| 40 url = CloudPrintHelpers::GetUrlForJobFetch(server_base_url, | |
| 41 "myprinter", | |
| 42 "nogoodreason"); | |
| 43 expected_url = base::StringPrintf( | |
| 44 "%sfetch?printerid=myprinter&deb=nogoodreason", | |
| 45 expected_url_base.c_str()); | |
| 46 EXPECT_EQ(expected_url, url.spec()); | |
| 47 | |
| 48 url = CloudPrintHelpers::GetUrlForJobStatusUpdate( | |
| 49 server_base_url, "12345678", cloud_print::PRINT_JOB_STATUS_IN_PROGRESS); | |
| 50 expected_url = base::StringPrintf( | |
| 51 "%scontrol?jobid=12345678&status=IN_PROGRESS", expected_url_base.c_str()); | |
| 52 EXPECT_EQ(expected_url, url.spec()); | |
| 53 | |
| 54 url = CloudPrintHelpers::GetUrlForJobStatusUpdate( | |
| 55 server_base_url, "12345678", cloud_print::PRINT_JOB_STATUS_ERROR); | |
| 56 expected_url = base::StringPrintf("%scontrol?jobid=12345678&status=ERROR", | |
| 57 expected_url_base.c_str()); | |
| 58 EXPECT_EQ(expected_url, url.spec()); | |
| 59 | |
| 60 url = CloudPrintHelpers::GetUrlForJobStatusUpdate( | |
| 61 server_base_url, "12345678", cloud_print::PRINT_JOB_STATUS_COMPLETED); | |
| 62 expected_url = base::StringPrintf("%scontrol?jobid=12345678&status=DONE", | |
| 63 expected_url_base.c_str()); | |
| 64 EXPECT_EQ(expected_url, url.spec()); | |
| 65 | |
| 66 cloud_print::PrintJobDetails details; | |
| 67 details.status = cloud_print::PRINT_JOB_STATUS_IN_PROGRESS; | |
| 68 details.platform_status_flags = 2; | 29 details.platform_status_flags = 2; |
| 69 details.status_message = "Out of Paper"; | 30 details.status_message = "Out of Paper"; |
| 70 details.total_pages = 345; | 31 details.total_pages = 345; |
| 71 details.pages_printed = 47; | 32 details.pages_printed = 47; |
| 72 url = CloudPrintHelpers::GetUrlForJobStatusUpdate(server_base_url, | 33 EXPECT_EQ(base::StringPrintf( |
| 73 "87654321", details); | 34 "%scontrol?jobid=87654321&status=IN_PROGRESS&code=2" |
| 74 expected_url = base::StringPrintf( | 35 "&message=Out%%20of%%20Paper&numpages=345&pagesprinted=47", |
| 75 "%scontrol?jobid=87654321&status=IN_PROGRESS&code=2" | 36 expected_url_base.c_str()), |
| 76 "&message=Out%%20of%%20Paper&numpages=345&pagesprinted=47", | 37 GetUrlForJobStatusUpdate( |
| 77 expected_url_base.c_str()); | 38 server_base_url, "87654321", details).spec()); |
| 78 EXPECT_EQ(expected_url, url.spec()); | |
| 79 | |
| 80 url = CloudPrintHelpers::GetUrlForUserMessage(server_base_url, | |
| 81 "blahmessageid"); | |
| 82 expected_url = base::StringPrintf("%smessage?code=blahmessageid", | |
| 83 expected_url_base.c_str()); | |
| 84 EXPECT_EQ(expected_url, url.spec()); | |
| 85 | |
| 86 url = CloudPrintHelpers::GetUrlForGetAuthCode( | |
| 87 server_base_url, | |
| 88 "fooclientid.apps.googleusercontent.com", | |
| 89 "test_proxy"); | |
| 90 expected_url = base::StringPrintf( | |
| 91 "%screaterobot?oauth_client_id=fooclientid.apps.googleusercontent.com&" | |
| 92 "proxy=test_proxy", expected_url_base.c_str()); | |
| 93 EXPECT_EQ(expected_url, url.spec()); | |
| 94 } | 39 } |
| 95 | 40 |
| 96 } // namespace | 41 } // namespace |
| 97 | 42 |
| 98 TEST(CloudPrintHelpersTest, URLGetters) { | 43 TEST(CloudPrintServiceHelpersTest, GetURLs) { |
| 99 CheckURLs(GURL("https://www.google.com/cloudprint")); | 44 CheckJobStatusURLs(GURL("https://www.google.com/cloudprint")); |
| 100 CheckURLs(GURL("https://www.google.com/cloudprint/")); | 45 CheckJobStatusURLs(GURL("https://www.google.com/cloudprint/")); |
| 101 CheckURLs(GURL("http://www.myprinterserver.com")); | 46 CheckJobStatusURLs(GURL("http://www.myprinterserver.com")); |
| 102 CheckURLs(GURL("http://www.myprinterserver.com/")); | 47 CheckJobStatusURLs(GURL("http://www.myprinterserver.com/")); |
| 103 } | 48 } |
| 104 | 49 |
| 50 TEST(CloudPrintServiceHelpersTest, GetHashOfPrinterInfo) { |
| 51 printing::PrinterBasicInfo printer_info; |
| 52 printer_info.options["tag1"] = std::string("value1"); |
| 53 printer_info.options["tag2"] = std::string("value2"); |
| 54 |
| 55 chrome::VersionInfo version_info; |
| 56 std::string expected_list_string = StringPrintf( |
| 57 "chrome_version%ssystem_name%ssystem_version%stag1value1tag2value2", |
| 58 version_info.CreateVersionString().c_str(), |
| 59 base::SysInfo::OperatingSystemName().c_str(), |
| 60 base::SysInfo::OperatingSystemVersion().c_str()); |
| 61 EXPECT_EQ(base::MD5String(expected_list_string), |
| 62 GetHashOfPrinterInfo(printer_info)); |
| 63 } |
| 64 |
| 65 TEST(CloudPrintServiceHelpersTest, GetPostDataForPrinterInfo) { |
| 66 printing::PrinterBasicInfo printer_info; |
| 67 printer_info.options["tag1"] = std::string("value1"); |
| 68 printer_info.options["tag2"] = std::string("value2"); |
| 69 |
| 70 chrome::VersionInfo version_info; |
| 71 std::string expected = base::StringPrintf( |
| 72 "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\"" |
| 73 "\r\n\r\n__cp__chrome_version=%s\r\n" |
| 74 "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\"" |
| 75 "\r\n\r\n__cp__system_name=%s\r\n" |
| 76 "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\"" |
| 77 "\r\n\r\n__cp__system_version=%s\r\n" |
| 78 "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\"" |
| 79 "\r\n\r\n__cp__tag1=value1\r\n" |
| 80 "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\"" |
| 81 "\r\n\r\n__cp__tag2=value2\r\n" |
| 82 "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\"" |
| 83 "\r\n\r\n__cp__tagshash=%s\r\n", |
| 84 version_info.CreateVersionString().c_str(), |
| 85 base::SysInfo::OperatingSystemName().c_str(), |
| 86 base::SysInfo::OperatingSystemVersion().c_str(), |
| 87 GetHashOfPrinterInfo(printer_info).c_str()); |
| 88 |
| 89 EXPECT_EQ(expected, GetPostDataForPrinterInfo( |
| 90 printer_info, std::string("test_mime_boundary"))); |
| 91 } |
| 92 |
| 93 TEST(CloudPrintServiceHelpersTest, IsDryRunJob) { |
| 94 std::vector<std::string> tags_not_dry_run; |
| 95 tags_not_dry_run.push_back("tag_1"); |
| 96 EXPECT_FALSE(IsDryRunJob(tags_not_dry_run)); |
| 97 |
| 98 std::vector<std::string> tags_dry_run; |
| 99 tags_dry_run.push_back("__cp__dry_run"); |
| 100 EXPECT_TRUE(IsDryRunJob(tags_dry_run)); |
| 101 } |
| 102 |
| 103 } // namespace cloud_print |
| 104 |
| OLD | NEW |