Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / service / cloud_print / printer_job_queue_handler_unittest.cc
blob134242e95036c1cdd7602a044e7d54bd9946f709
1 // Copyright (c) 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/printer_job_queue_handler.h"
7 #include <set>
9 #include "base/json/json_reader.h"
10 #include "base/values.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using ::testing::Return;
15 using ::testing::AtLeast;
17 namespace cloud_print {
19 const char kJobListResponse[] =
20 "{"
21 " \"success\" : true, "
22 " \"jobs\" : ["
23 "{"
24 " \"tags\" : [ \"^own\", \"\"], "
25 " \"title\" : \"test1\","
26 " \"ticketUrl\" : \"http://example.com/job1ticket\","
27 " \"fileUrl\" : \"http://example.com/job1data\","
28 " \"id\" : \"__testjob1\""
29 "},"
30 "{"
31 " \"tags\" : [ \"^own\", \"\"], "
32 " \"title\" : \"test2\","
33 " \"ticketUrl\" : \"http://example.com/job2ticket\","
34 " \"fileUrl\" : \"http://example.com/job2data\","
35 " \"id\" : \"__testjob2\""
36 "},"
37 "{"
38 " \"tags\" : [ \"^own\", \"\"], "
39 " \"title\" : \"test3\","
40 " \"ticketUrl\" : \"http://example.com/job3ticket\","
41 " \"fileUrl\" : \"http://example.com/job3data\","
42 " \"id\" : \"__testjob3\""
43 "}"
44 "]"
45 "}";
47 class TimeProviderMock : public PrinterJobQueueHandler::TimeProvider {
48 public:
49 MOCK_METHOD0(GetNow, base::Time());
52 class PrinterJobQueueHandlerTest : public ::testing::Test {
53 protected:
54 base::Value* data_;
55 base::DictionaryValue* json_data_;
56 void SetUp() override {
57 base::JSONReader json_reader;
58 data_ = json_reader.DeprecatedRead(kJobListResponse);
59 data_->GetAsDictionary(&json_data_);
62 void TearDown() override { delete data_; }
65 TEST_F(PrinterJobQueueHandlerTest, BasicJobReadTest) {
66 PrinterJobQueueHandler job_queue_handler;
67 std::vector<JobDetails> jobs;
69 job_queue_handler.GetJobsFromQueue(json_data_, &jobs);
71 ASSERT_EQ((size_t)3, jobs.size());
73 EXPECT_EQ(std::string("__testjob1"), jobs[0].job_id_);
74 EXPECT_EQ(std::string("test1"), jobs[0].job_title_);
75 EXPECT_EQ(std::string("http://example.com/job1ticket"),
76 jobs[0].print_ticket_url_);
77 EXPECT_EQ(std::string("http://example.com/job1data"),
78 jobs[0].print_data_url_);
80 std::set<std::string> expected_tags;
81 expected_tags.insert("^own");
82 expected_tags.insert(std::string());
83 std::set<std::string> actual_tags;
84 actual_tags.insert(jobs[0].tags_.begin(), jobs[0].tags_.end());
86 EXPECT_EQ(expected_tags, actual_tags);
87 EXPECT_EQ(base::TimeDelta(), jobs[0].time_remaining_);
90 TEST_F(PrinterJobQueueHandlerTest, PreferNonFailureTest) {
91 TimeProviderMock* time_mock = new TimeProviderMock();
92 PrinterJobQueueHandler job_queue_handler(time_mock);
93 EXPECT_CALL((*time_mock), GetNow())
94 .Times(AtLeast(2))
95 .WillRepeatedly(Return(base::Time::UnixEpoch()));
97 // must fail twice for backoff to kick in
98 job_queue_handler.JobFetchFailed("__testjob1");
99 job_queue_handler.JobFetchFailed("__testjob1");
101 std::vector<JobDetails> jobs;
102 job_queue_handler.GetJobsFromQueue(json_data_, &jobs);
104 EXPECT_EQ(std::string("__testjob2"), jobs[0].job_id_);
105 EXPECT_EQ(base::TimeDelta(), jobs[0].time_remaining_);
108 TEST_F(PrinterJobQueueHandlerTest, PreferNoTimeTest) {
109 TimeProviderMock* time_mock = new TimeProviderMock();
110 PrinterJobQueueHandler job_queue_handler(time_mock);
111 EXPECT_CALL((*time_mock), GetNow()).
112 Times(AtLeast(8));
114 ON_CALL((*time_mock), GetNow())
115 .WillByDefault(Return(base::Time::UnixEpoch()));
117 for (int i = 0; i < 4; i++)
118 job_queue_handler.JobFetchFailed("__testjob1");
120 ON_CALL((*time_mock), GetNow())
121 .WillByDefault(Return(base::Time::UnixEpoch() +
122 base::TimeDelta::FromMinutes(4)));
124 for (int i = 0; i < 2; i++)
125 job_queue_handler.JobFetchFailed("__testjob2");
127 for (int i = 0; i < 2; i++)
128 job_queue_handler.JobFetchFailed("__testjob3");
130 std::vector<JobDetails> jobs;
131 job_queue_handler.GetJobsFromQueue(json_data_, &jobs);
133 EXPECT_EQ(base::TimeDelta(), jobs[0].time_remaining_);
134 EXPECT_EQ(std::string("__testjob1"), jobs[0].job_id_);
137 TEST_F(PrinterJobQueueHandlerTest, PreferLowerTimeTest) {
138 TimeProviderMock* time_mock = new TimeProviderMock();
139 PrinterJobQueueHandler job_queue_handler(time_mock);
141 EXPECT_CALL((*time_mock), GetNow()).
142 Times(AtLeast(8));
144 ON_CALL((*time_mock), GetNow())
145 .WillByDefault(Return(base::Time::UnixEpoch()));
147 for (int i = 0; i < 4; i++)
148 job_queue_handler.JobFetchFailed("__testjob1");
150 ON_CALL((*time_mock), GetNow())
151 .WillByDefault(Return(base::Time::UnixEpoch() +
152 base::TimeDelta::FromSeconds(4)));
154 for (int i = 0; i < 2; i++)
155 job_queue_handler.JobFetchFailed("__testjob2");
157 for (int i = 0; i < 2; i++)
158 job_queue_handler.JobFetchFailed("__testjob3");
160 std::vector<JobDetails> jobs;
161 job_queue_handler.GetJobsFromQueue(json_data_,
162 &jobs);
164 base::TimeDelta time_to_wait = jobs[0].time_remaining_;
165 EXPECT_NE(base::TimeDelta(), time_to_wait);
167 jobs.clear();
168 ON_CALL((*time_mock), GetNow())
169 .WillByDefault(Return(base::Time::UnixEpoch() +
170 base::TimeDelta::FromSeconds(4) + time_to_wait));
172 job_queue_handler.GetJobsFromQueue(json_data_,
173 &jobs);
175 EXPECT_EQ(base::TimeDelta(), jobs[0].time_remaining_);
176 EXPECT_EQ(std::string("__testjob2"), jobs[0].job_id_);
179 } // namespace cloud_print