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.
5 #include "chrome/service/cloud_print/cloud_print_service_helpers.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/sys_info.h"
10 #include "chrome/common/channel_info.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace cloud_print
{
17 void CheckJobStatusURLs(const GURL
& server_base_url
) {
18 std::string expected_url_base
= server_base_url
.spec();
19 if (expected_url_base
[expected_url_base
.length() - 1] != '/')
20 expected_url_base
+= "/";
22 EXPECT_EQ(base::StringPrintf(
23 "%scontrol?jobid=87654321&status=ERROR&connector_code=1",
24 expected_url_base
.c_str()),
25 GetUrlForJobStatusUpdate(server_base_url
, "87654321",
26 PRINT_JOB_STATUS_ERROR
, 1).spec());
28 PrintJobDetails details
;
29 details
.status
= PRINT_JOB_STATUS_IN_PROGRESS
;
30 details
.platform_status_flags
= 2;
31 details
.status_message
= "Out of Paper";
32 details
.total_pages
= 345;
33 details
.pages_printed
= 47;
34 EXPECT_EQ(base::StringPrintf(
35 "%scontrol?jobid=87654321&status=IN_PROGRESS&code=2"
36 "&message=Out%%20of%%20Paper&numpages=345&pagesprinted=47",
37 expected_url_base
.c_str()),
38 GetUrlForJobStatusUpdate(
39 server_base_url
, "87654321", details
).spec());
44 TEST(CloudPrintServiceHelpersTest
, GetURLs
) {
45 CheckJobStatusURLs(GURL("https://www.google.com/cloudprint"));
46 CheckJobStatusURLs(GURL("https://www.google.com/cloudprint/"));
47 CheckJobStatusURLs(GURL("http://www.myprinterserver.com"));
48 CheckJobStatusURLs(GURL("http://www.myprinterserver.com/"));
51 TEST(CloudPrintServiceHelpersTest
, GetHashOfPrinterInfo
) {
52 printing::PrinterBasicInfo printer_info
;
53 printer_info
.options
["tag1"] = std::string("value1");
54 printer_info
.options
["tag2"] = std::string("value2");
56 std::string expected_list_string
= base::StringPrintf(
57 "chrome_version%ssystem_name%ssystem_version%stag1value1tag2value2",
58 chrome::GetVersionString().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
));
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");
70 std::string expected
= base::StringPrintf(
71 "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\""
72 "\r\n\r\n__cp__chrome_version=%s\r\n"
73 "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\""
74 "\r\n\r\n__cp__system_name=%s\r\n"
75 "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\""
76 "\r\n\r\n__cp__system_version=%s\r\n"
77 "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\""
78 "\r\n\r\n__cp__tag1=value1\r\n"
79 "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\""
80 "\r\n\r\n__cp__tag2=value2\r\n"
81 "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\""
82 "\r\n\r\n__cp__tagshash=%s\r\n",
83 chrome::GetVersionString().c_str(),
84 base::SysInfo::OperatingSystemName().c_str(),
85 base::SysInfo::OperatingSystemVersion().c_str(),
86 GetHashOfPrinterInfo(printer_info
).c_str());
88 EXPECT_EQ(expected
, GetPostDataForPrinterInfo(
89 printer_info
, std::string("test_mime_boundary")));
92 TEST(CloudPrintServiceHelpersTest
, IsDryRunJob
) {
93 std::vector
<std::string
> tags_not_dry_run
;
94 tags_not_dry_run
.push_back("tag_1");
95 EXPECT_FALSE(IsDryRunJob(tags_not_dry_run
));
97 std::vector
<std::string
> tags_dry_run
;
98 tags_dry_run
.push_back("__cp__dry_run");
99 EXPECT_TRUE(IsDryRunJob(tags_dry_run
));
102 } // namespace cloud_print