Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / chrome / service / cloud_print / printer_job_queue_handler_unittest.cc
blob0e54257fdcb39602e557f9126f8a2fc3bbf4d749
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 scoped_ptr<base::Value> data_;
56 const base::DictionaryValue* GetAsDictionary() const {
57 const base::DictionaryValue* json_data_ = nullptr;
58 EXPECT_TRUE(data_->GetAsDictionary(&json_data_));
59 return json_data_;
62 void SetUp() override {
63 base::JSONReader json_reader;
64 data_ = json_reader.Read(kJobListResponse);
68 TEST_F(PrinterJobQueueHandlerTest, BasicJobReadTest) {
69 PrinterJobQueueHandler job_queue_handler;
70 std::vector<JobDetails> jobs;
72 job_queue_handler.GetJobsFromQueue(GetAsDictionary(), &jobs);
74 ASSERT_EQ((size_t)3, jobs.size());
76 EXPECT_EQ(std::string("__testjob1"), jobs[0].job_id_);
77 EXPECT_EQ(std::string("test1"), jobs[0].job_title_);
78 EXPECT_EQ(std::string("http://example.com/job1ticket"),
79 jobs[0].print_ticket_url_);
80 EXPECT_EQ(std::string("http://example.com/job1data"),
81 jobs[0].print_data_url_);
83 std::set<std::string> expected_tags;
84 expected_tags.insert("^own");
85 expected_tags.insert(std::string());
86 std::set<std::string> actual_tags;
87 actual_tags.insert(jobs[0].tags_.begin(), jobs[0].tags_.end());
89 EXPECT_EQ(expected_tags, actual_tags);
90 EXPECT_EQ(base::TimeDelta(), jobs[0].time_remaining_);
93 TEST_F(PrinterJobQueueHandlerTest, PreferNonFailureTest) {
94 TimeProviderMock* time_mock = new TimeProviderMock();
95 PrinterJobQueueHandler job_queue_handler(time_mock);
96 EXPECT_CALL((*time_mock), GetNow())
97 .Times(AtLeast(2))
98 .WillRepeatedly(Return(base::Time::UnixEpoch()));
100 // must fail twice for backoff to kick in
101 job_queue_handler.JobFetchFailed("__testjob1");
102 job_queue_handler.JobFetchFailed("__testjob1");
104 std::vector<JobDetails> jobs;
105 job_queue_handler.GetJobsFromQueue(GetAsDictionary(), &jobs);
107 EXPECT_EQ(std::string("__testjob2"), jobs[0].job_id_);
108 EXPECT_EQ(base::TimeDelta(), jobs[0].time_remaining_);
111 TEST_F(PrinterJobQueueHandlerTest, PreferNoTimeTest) {
112 TimeProviderMock* time_mock = new TimeProviderMock();
113 PrinterJobQueueHandler job_queue_handler(time_mock);
114 EXPECT_CALL((*time_mock), GetNow()).
115 Times(AtLeast(8));
117 ON_CALL((*time_mock), GetNow())
118 .WillByDefault(Return(base::Time::UnixEpoch()));
120 for (int i = 0; i < 4; i++)
121 job_queue_handler.JobFetchFailed("__testjob1");
123 ON_CALL((*time_mock), GetNow())
124 .WillByDefault(Return(base::Time::UnixEpoch() +
125 base::TimeDelta::FromMinutes(4)));
127 for (int i = 0; i < 2; i++)
128 job_queue_handler.JobFetchFailed("__testjob2");
130 for (int i = 0; i < 2; i++)
131 job_queue_handler.JobFetchFailed("__testjob3");
133 std::vector<JobDetails> jobs;
134 job_queue_handler.GetJobsFromQueue(GetAsDictionary(), &jobs);
136 EXPECT_EQ(base::TimeDelta(), jobs[0].time_remaining_);
137 EXPECT_EQ(std::string("__testjob1"), jobs[0].job_id_);
140 TEST_F(PrinterJobQueueHandlerTest, PreferLowerTimeTest) {
141 TimeProviderMock* time_mock = new TimeProviderMock();
142 PrinterJobQueueHandler job_queue_handler(time_mock);
144 EXPECT_CALL((*time_mock), GetNow()).
145 Times(AtLeast(8));
147 ON_CALL((*time_mock), GetNow())
148 .WillByDefault(Return(base::Time::UnixEpoch()));
150 for (int i = 0; i < 4; i++)
151 job_queue_handler.JobFetchFailed("__testjob1");
153 ON_CALL((*time_mock), GetNow())
154 .WillByDefault(Return(base::Time::UnixEpoch() +
155 base::TimeDelta::FromSeconds(4)));
157 for (int i = 0; i < 2; i++)
158 job_queue_handler.JobFetchFailed("__testjob2");
160 for (int i = 0; i < 2; i++)
161 job_queue_handler.JobFetchFailed("__testjob3");
163 std::vector<JobDetails> jobs;
164 job_queue_handler.GetJobsFromQueue(GetAsDictionary(), &jobs);
166 base::TimeDelta time_to_wait = jobs[0].time_remaining_;
167 EXPECT_NE(base::TimeDelta(), time_to_wait);
169 jobs.clear();
170 ON_CALL((*time_mock), GetNow())
171 .WillByDefault(Return(base::Time::UnixEpoch() +
172 base::TimeDelta::FromSeconds(4) + time_to_wait));
174 job_queue_handler.GetJobsFromQueue(GetAsDictionary(), &jobs);
176 EXPECT_EQ(base::TimeDelta(), jobs[0].time_remaining_);
177 EXPECT_EQ(std::string("__testjob2"), jobs[0].job_id_);
180 } // namespace cloud_print