Chromium Code Reviews| 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 |
| 10 namespace { | 13 namespace cloud_print { |
| 11 | 14 |
| 12 void CheckURLs(const GURL& server_base_url) { | 15 void CheckJobStatusURLs(const GURL& server_base_url) { |
|
msw
2012/11/29 19:57:36
ditto optional nit: put locals in anon namespace i
Chen Yu
2012/11/30 17:35:46
Done.
| |
| 13 GURL url = CloudPrintHelpers::GetUrlForPrinterRegistration(server_base_url); | |
| 14 std::string expected_url_base = server_base_url.spec(); | 16 std::string expected_url_base = server_base_url.spec(); |
| 15 if (expected_url_base[expected_url_base.length() - 1] != '/') { | 17 if (expected_url_base[expected_url_base.length() - 1] != '/') |
| 16 expected_url_base += "/"; | 18 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 | 19 |
| 22 url = CloudPrintHelpers::GetUrlForPrinterUpdate(server_base_url, | 20 EXPECT_EQ(base::StringPrintf("%scontrol?jobid=87654321&status=ERROR", |
| 23 "printeridfoo"); | 21 expected_url_base.c_str()), |
| 24 expected_url = base::StringPrintf("%supdate?printerid=printeridfoo", | 22 GetUrlForJobStatusUpdate(server_base_url, "87654321", |
| 25 expected_url_base.c_str()); | 23 cloud_print::PRINT_JOB_STATUS_ERROR).spec()); |
| 26 EXPECT_EQ(expected_url, url.spec()); | |
| 27 | 24 |
| 28 url = CloudPrintHelpers::GetUrlForPrinterDelete(server_base_url, | 25 PrintJobDetails details; |
| 29 "printeridbar", "deleted"); | 26 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; | 27 details.platform_status_flags = 2; |
| 69 details.status_message = "Out of Paper"; | 28 details.status_message = "Out of Paper"; |
| 70 details.total_pages = 345; | 29 details.total_pages = 345; |
| 71 details.pages_printed = 47; | 30 details.pages_printed = 47; |
| 72 url = CloudPrintHelpers::GetUrlForJobStatusUpdate(server_base_url, | 31 EXPECT_EQ(base::StringPrintf( |
| 73 "87654321", details); | 32 "%scontrol?jobid=87654321&status=IN_PROGRESS&code=2" |
| 74 expected_url = base::StringPrintf( | 33 "&message=Out%%20of%%20Paper&numpages=345&pagesprinted=47", |
| 75 "%scontrol?jobid=87654321&status=IN_PROGRESS&code=2" | 34 expected_url_base.c_str()), |
| 76 "&message=Out%%20of%%20Paper&numpages=345&pagesprinted=47", | 35 GetUrlForJobStatusUpdate( |
| 77 expected_url_base.c_str()); | 36 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 } | 37 } |
| 95 | 38 |
| 96 } // namespace | 39 TEST(CloudPrintServiceHelpersTest, GetURLs) { |
| 97 | 40 CheckJobStatusURLs(GURL("https://www.google.com/cloudprint")); |
| 98 TEST(CloudPrintHelpersTest, URLGetters) { | 41 CheckJobStatusURLs(GURL("https://www.google.com/cloudprint/")); |
| 99 CheckURLs(GURL("https://www.google.com/cloudprint")); | 42 CheckJobStatusURLs(GURL("http://www.myprinterserver.com")); |
| 100 CheckURLs(GURL("https://www.google.com/cloudprint/")); | 43 CheckJobStatusURLs(GURL("http://www.myprinterserver.com/")); |
| 101 CheckURLs(GURL("http://www.myprinterserver.com")); | |
| 102 CheckURLs(GURL("http://www.myprinterserver.com/")); | |
| 103 } | 44 } |
| 104 | 45 |
| 46 TEST(CloudPrintServiceHelpersTest, GetHashOfPrinterInfo) { | |
| 47 printing::PrinterBasicInfo printer_info; | |
| 48 printer_info.options["tag1"] = std::string("value1"); | |
| 49 printer_info.options["tag2"] = std::string("value2"); | |
| 50 | |
| 51 chrome::VersionInfo version_info; | |
| 52 std::string expected_list_string = StringPrintf( | |
| 53 "chrome_version%ssystem_name%ssystem_version%stag1value1tag2value2", | |
| 54 version_info.CreateVersionString().c_str(), | |
| 55 base::SysInfo::OperatingSystemName().c_str(), | |
| 56 base::SysInfo::OperatingSystemVersion().c_str()); | |
| 57 EXPECT_EQ(base::MD5String(expected_list_string), | |
| 58 GetHashOfPrinterInfo(printer_info)); | |
| 59 } | |
| 60 | |
| 61 TEST(CloudPrintServiceHelpersTest, GetPostDataForPrinterInfo) { | |
| 62 printing::PrinterBasicInfo printer_info; | |
| 63 printer_info.options["tag1"] = std::string("value1"); | |
| 64 printer_info.options["tag2"] = std::string("value2"); | |
| 65 | |
| 66 chrome::VersionInfo version_info; | |
| 67 std::string expected = base::StringPrintf( | |
| 68 "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\"" | |
| 69 "\r\n\r\n__cp__chrome_version=%s\r\n" | |
| 70 "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\"" | |
| 71 "\r\n\r\n__cp__system_name=%s\r\n" | |
| 72 "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\"" | |
| 73 "\r\n\r\n__cp__system_version=%s\r\n" | |
| 74 "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\"" | |
| 75 "\r\n\r\n__cp__tag1=value1\r\n" | |
| 76 "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\"" | |
| 77 "\r\n\r\n__cp__tag2=value2\r\n" | |
| 78 "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\"" | |
| 79 "\r\n\r\n__cp__tagshash=%s\r\n", | |
| 80 version_info.CreateVersionString().c_str(), | |
| 81 base::SysInfo::OperatingSystemName().c_str(), | |
| 82 base::SysInfo::OperatingSystemVersion().c_str(), | |
| 83 GetHashOfPrinterInfo(printer_info).c_str()); | |
| 84 | |
| 85 EXPECT_EQ(expected, GetPostDataForPrinterInfo( | |
| 86 printer_info, std::string("test_mime_boundary"))); | |
| 87 } | |
| 88 | |
| 89 TEST(CloudPrintServiceHelpersTest, IsDryRunJob) { | |
| 90 std::vector<std::string> tags_not_dry_run; | |
| 91 tags_not_dry_run.push_back("tag_1"); | |
| 92 EXPECT_FALSE(IsDryRunJob(tags_not_dry_run)); | |
| 93 | |
| 94 std::vector<std::string> tags_dry_run; | |
| 95 tags_dry_run.push_back("__cp__dry_run"); | |
| 96 EXPECT_TRUE(IsDryRunJob(tags_dry_run)); | |
| 97 } | |
| 98 | |
| 99 } // namespace cloud_print | |
| 100 | |
| OLD | NEW |