OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/browser/chromeos/policy/remote_commands/device_command_screensh
ot_job.h" | 5 #include "chrome/browser/chromeos/policy/remote_commands/device_command_screensh
ot_job.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "ash/test/ash_test_base.h" | 11 #include "ash/test/ash_test_base.h" |
12 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" |
14 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
15 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
16 #include "base/test/test_mock_time_task_runner.h" | 17 #include "base/test/test_mock_time_task_runner.h" |
17 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
18 #include "base/threading/sequenced_worker_pool.h" | 19 #include "base/threading/sequenced_worker_pool.h" |
19 #include "base/time/time.h" | 20 #include "base/time/time.h" |
20 #include "base/values.h" | 21 #include "base/values.h" |
21 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
22 #include "content/public/test/test_browser_thread_bundle.h" | 23 #include "content/public/test/test_browser_thread_bundle.h" |
23 #include "policy/proto/device_management_backend.pb.h" | 24 #include "policy/proto/device_management_backend.pb.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 return command_proto; | 59 return command_proto; |
59 } | 60 } |
60 | 61 |
61 class MockUploadJob : public policy::UploadJob { | 62 class MockUploadJob : public policy::UploadJob { |
62 public: | 63 public: |
63 // If |error_code| is a null pointer OnSuccess() will be invoked when the | 64 // If |error_code| is a null pointer OnSuccess() will be invoked when the |
64 // Start() method is called, otherwise OnFailure() will be invoked with the | 65 // Start() method is called, otherwise OnFailure() will be invoked with the |
65 // respective |error_code|. | 66 // respective |error_code|. |
66 MockUploadJob(const GURL& upload_url, | 67 MockUploadJob(const GURL& upload_url, |
67 UploadJob::Delegate* delegate, | 68 UploadJob::Delegate* delegate, |
68 scoped_ptr<UploadJob::ErrorCode> error_code); | 69 std::unique_ptr<UploadJob::ErrorCode> error_code); |
69 ~MockUploadJob() override; | 70 ~MockUploadJob() override; |
70 | 71 |
71 // policy::UploadJob: | 72 // policy::UploadJob: |
72 void AddDataSegment(const std::string& name, | 73 void AddDataSegment(const std::string& name, |
73 const std::string& filename, | 74 const std::string& filename, |
74 const std::map<std::string, std::string>& header_entries, | 75 const std::map<std::string, std::string>& header_entries, |
75 scoped_ptr<std::string> data) override; | 76 std::unique_ptr<std::string> data) override; |
76 void Start() override; | 77 void Start() override; |
77 | 78 |
78 const GURL& GetUploadUrl() const; | 79 const GURL& GetUploadUrl() const; |
79 | 80 |
80 protected: | 81 protected: |
81 const GURL upload_url_; | 82 const GURL upload_url_; |
82 UploadJob::Delegate* delegate_; | 83 UploadJob::Delegate* delegate_; |
83 scoped_ptr<UploadJob::ErrorCode> error_code_; | 84 std::unique_ptr<UploadJob::ErrorCode> error_code_; |
84 bool add_datasegment_succeeds_; | 85 bool add_datasegment_succeeds_; |
85 }; | 86 }; |
86 | 87 |
87 MockUploadJob::MockUploadJob(const GURL& upload_url, | 88 MockUploadJob::MockUploadJob(const GURL& upload_url, |
88 UploadJob::Delegate* delegate, | 89 UploadJob::Delegate* delegate, |
89 scoped_ptr<UploadJob::ErrorCode> error_code) | 90 std::unique_ptr<UploadJob::ErrorCode> error_code) |
90 : upload_url_(upload_url), | 91 : upload_url_(upload_url), |
91 delegate_(delegate), | 92 delegate_(delegate), |
92 error_code_(std::move(error_code)) {} | 93 error_code_(std::move(error_code)) {} |
93 | 94 |
94 MockUploadJob::~MockUploadJob() { | 95 MockUploadJob::~MockUploadJob() { |
95 } | 96 } |
96 | 97 |
97 void MockUploadJob::AddDataSegment( | 98 void MockUploadJob::AddDataSegment( |
98 const std::string& name, | 99 const std::string& name, |
99 const std::string& filename, | 100 const std::string& filename, |
100 const std::map<std::string, std::string>& header_entries, | 101 const std::map<std::string, std::string>& header_entries, |
101 scoped_ptr<std::string> data) { | 102 std::unique_ptr<std::string> data) {} |
102 } | |
103 | 103 |
104 void MockUploadJob::Start() { | 104 void MockUploadJob::Start() { |
105 DCHECK(delegate_); | 105 DCHECK(delegate_); |
106 EXPECT_EQ(kMockUploadUrl, upload_url_.spec()); | 106 EXPECT_EQ(kMockUploadUrl, upload_url_.spec()); |
107 if (error_code_) { | 107 if (error_code_) { |
108 base::ThreadTaskRunnerHandle::Get()->PostTask( | 108 base::ThreadTaskRunnerHandle::Get()->PostTask( |
109 FROM_HERE, base::Bind(&UploadJob::Delegate::OnFailure, | 109 FROM_HERE, base::Bind(&UploadJob::Delegate::OnFailure, |
110 base::Unretained(delegate_), *error_code_)); | 110 base::Unretained(delegate_), *error_code_)); |
111 return; | 111 return; |
112 } | 112 } |
(...skipping 19 matching lines...) Expand all Loading... |
132 gfx::Size(bmp.width(), bmp.height()), | 132 gfx::Size(bmp.width(), bmp.height()), |
133 static_cast<int>(bmp.rowBytes()), false, | 133 static_cast<int>(bmp.rowBytes()), false, |
134 std::vector<gfx::PNGCodec::Comment>(), &png_bytes->data())) { | 134 std::vector<gfx::PNGCodec::Comment>(), &png_bytes->data())) { |
135 LOG(ERROR) << "Failed to encode image"; | 135 LOG(ERROR) << "Failed to encode image"; |
136 } | 136 } |
137 return png_bytes; | 137 return png_bytes; |
138 } | 138 } |
139 | 139 |
140 class MockScreenshotDelegate : public DeviceCommandScreenshotJob::Delegate { | 140 class MockScreenshotDelegate : public DeviceCommandScreenshotJob::Delegate { |
141 public: | 141 public: |
142 MockScreenshotDelegate(scoped_ptr<UploadJob::ErrorCode> upload_job_error_code, | 142 MockScreenshotDelegate( |
143 bool screenshot_allowed); | 143 std::unique_ptr<UploadJob::ErrorCode> upload_job_error_code, |
| 144 bool screenshot_allowed); |
144 ~MockScreenshotDelegate() override; | 145 ~MockScreenshotDelegate() override; |
145 | 146 |
146 bool IsScreenshotAllowed() override; | 147 bool IsScreenshotAllowed() override; |
147 void TakeSnapshot( | 148 void TakeSnapshot( |
148 gfx::NativeWindow window, | 149 gfx::NativeWindow window, |
149 const gfx::Rect& source_rect, | 150 const gfx::Rect& source_rect, |
150 const ui::GrabWindowSnapshotAsyncPNGCallback& callback) override; | 151 const ui::GrabWindowSnapshotAsyncPNGCallback& callback) override; |
151 scoped_ptr<UploadJob> CreateUploadJob(const GURL&, | 152 std::unique_ptr<UploadJob> CreateUploadJob(const GURL&, |
152 UploadJob::Delegate*) override; | 153 UploadJob::Delegate*) override; |
153 | 154 |
154 private: | 155 private: |
155 scoped_ptr<UploadJob::ErrorCode> upload_job_error_code_; | 156 std::unique_ptr<UploadJob::ErrorCode> upload_job_error_code_; |
156 bool screenshot_allowed_; | 157 bool screenshot_allowed_; |
157 }; | 158 }; |
158 | 159 |
159 MockScreenshotDelegate::MockScreenshotDelegate( | 160 MockScreenshotDelegate::MockScreenshotDelegate( |
160 scoped_ptr<UploadJob::ErrorCode> upload_job_error_code, | 161 std::unique_ptr<UploadJob::ErrorCode> upload_job_error_code, |
161 bool screenshot_allowed) | 162 bool screenshot_allowed) |
162 : upload_job_error_code_(std::move(upload_job_error_code)), | 163 : upload_job_error_code_(std::move(upload_job_error_code)), |
163 screenshot_allowed_(screenshot_allowed) {} | 164 screenshot_allowed_(screenshot_allowed) {} |
164 | 165 |
165 MockScreenshotDelegate::~MockScreenshotDelegate() { | 166 MockScreenshotDelegate::~MockScreenshotDelegate() { |
166 } | 167 } |
167 | 168 |
168 bool MockScreenshotDelegate::IsScreenshotAllowed() { | 169 bool MockScreenshotDelegate::IsScreenshotAllowed() { |
169 return screenshot_allowed_; | 170 return screenshot_allowed_; |
170 } | 171 } |
171 | 172 |
172 void MockScreenshotDelegate::TakeSnapshot( | 173 void MockScreenshotDelegate::TakeSnapshot( |
173 gfx::NativeWindow window, | 174 gfx::NativeWindow window, |
174 const gfx::Rect& source_rect, | 175 const gfx::Rect& source_rect, |
175 const ui::GrabWindowSnapshotAsyncPNGCallback& callback) { | 176 const ui::GrabWindowSnapshotAsyncPNGCallback& callback) { |
176 const int width = source_rect.width(); | 177 const int width = source_rect.width(); |
177 const int height = source_rect.height(); | 178 const int height = source_rect.height(); |
178 scoped_refptr<base::RefCountedBytes> test_png = | 179 scoped_refptr<base::RefCountedBytes> test_png = |
179 GenerateTestPNG(width, height); | 180 GenerateTestPNG(width, height); |
180 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 181 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
181 base::Bind(callback, test_png)); | 182 base::Bind(callback, test_png)); |
182 } | 183 } |
183 | 184 |
184 scoped_ptr<UploadJob> MockScreenshotDelegate::CreateUploadJob( | 185 std::unique_ptr<UploadJob> MockScreenshotDelegate::CreateUploadJob( |
185 const GURL& upload_url, | 186 const GURL& upload_url, |
186 UploadJob::Delegate* delegate) { | 187 UploadJob::Delegate* delegate) { |
187 return make_scoped_ptr(new MockUploadJob(upload_url, delegate, | 188 return base::WrapUnique(new MockUploadJob(upload_url, delegate, |
188 std::move(upload_job_error_code_))); | 189 std::move(upload_job_error_code_))); |
189 } | 190 } |
190 | 191 |
191 } // namespace | 192 } // namespace |
192 | 193 |
193 class DeviceCommandScreenshotTest : public ash::test::AshTestBase { | 194 class DeviceCommandScreenshotTest : public ash::test::AshTestBase { |
194 public: | 195 public: |
195 void VerifyResults(RemoteCommandJob* job, | 196 void VerifyResults(RemoteCommandJob* job, |
196 RemoteCommandJob::Status expected_status, | 197 RemoteCommandJob::Status expected_status, |
197 std::string expected_payload); | 198 std::string expected_payload); |
198 | 199 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 base::JSONWriter::Write(root_dict, &payload); | 251 base::JSONWriter::Write(root_dict, &payload); |
251 return payload; | 252 return payload; |
252 } | 253 } |
253 | 254 |
254 void DeviceCommandScreenshotTest::VerifyResults( | 255 void DeviceCommandScreenshotTest::VerifyResults( |
255 RemoteCommandJob* job, | 256 RemoteCommandJob* job, |
256 RemoteCommandJob::Status expected_status, | 257 RemoteCommandJob::Status expected_status, |
257 std::string expected_payload) { | 258 std::string expected_payload) { |
258 EXPECT_EQ(expected_status, job->status()); | 259 EXPECT_EQ(expected_status, job->status()); |
259 if (job->status() == RemoteCommandJob::SUCCEEDED) { | 260 if (job->status() == RemoteCommandJob::SUCCEEDED) { |
260 scoped_ptr<std::string> payload = job->GetResultPayload(); | 261 std::unique_ptr<std::string> payload = job->GetResultPayload(); |
261 EXPECT_TRUE(payload); | 262 EXPECT_TRUE(payload); |
262 EXPECT_EQ(expected_payload, *payload); | 263 EXPECT_EQ(expected_payload, *payload); |
263 } | 264 } |
264 run_loop_.Quit(); | 265 run_loop_.Quit(); |
265 } | 266 } |
266 | 267 |
267 TEST_F(DeviceCommandScreenshotTest, Success) { | 268 TEST_F(DeviceCommandScreenshotTest, Success) { |
268 scoped_ptr<RemoteCommandJob> job(new DeviceCommandScreenshotJob( | 269 std::unique_ptr<RemoteCommandJob> job(new DeviceCommandScreenshotJob( |
269 make_scoped_ptr(new MockScreenshotDelegate(nullptr, true)))); | 270 base::WrapUnique(new MockScreenshotDelegate(nullptr, true)))); |
270 InitializeScreenshotJob(job.get(), kUniqueID, test_start_time_, | 271 InitializeScreenshotJob(job.get(), kUniqueID, test_start_time_, |
271 kMockUploadUrl); | 272 kMockUploadUrl); |
272 bool success = job->Run( | 273 bool success = job->Run( |
273 base::TimeTicks::Now(), | 274 base::TimeTicks::Now(), |
274 base::Bind( | 275 base::Bind( |
275 &DeviceCommandScreenshotTest::VerifyResults, base::Unretained(this), | 276 &DeviceCommandScreenshotTest::VerifyResults, base::Unretained(this), |
276 base::Unretained(job.get()), RemoteCommandJob::SUCCEEDED, | 277 base::Unretained(job.get()), RemoteCommandJob::SUCCEEDED, |
277 CreatePayloadFromResultCode(DeviceCommandScreenshotJob::SUCCESS))); | 278 CreatePayloadFromResultCode(DeviceCommandScreenshotJob::SUCCESS))); |
278 EXPECT_TRUE(success); | 279 EXPECT_TRUE(success); |
279 run_loop_.Run(); | 280 run_loop_.Run(); |
280 } | 281 } |
281 | 282 |
282 TEST_F(DeviceCommandScreenshotTest, FailureUserInput) { | 283 TEST_F(DeviceCommandScreenshotTest, FailureUserInput) { |
283 scoped_ptr<RemoteCommandJob> job(new DeviceCommandScreenshotJob( | 284 std::unique_ptr<RemoteCommandJob> job(new DeviceCommandScreenshotJob( |
284 make_scoped_ptr(new MockScreenshotDelegate(nullptr, false)))); | 285 base::WrapUnique(new MockScreenshotDelegate(nullptr, false)))); |
285 InitializeScreenshotJob(job.get(), kUniqueID, test_start_time_, | 286 InitializeScreenshotJob(job.get(), kUniqueID, test_start_time_, |
286 kMockUploadUrl); | 287 kMockUploadUrl); |
287 bool success = | 288 bool success = |
288 job->Run(base::TimeTicks::Now(), | 289 job->Run(base::TimeTicks::Now(), |
289 base::Bind(&DeviceCommandScreenshotTest::VerifyResults, | 290 base::Bind(&DeviceCommandScreenshotTest::VerifyResults, |
290 base::Unretained(this), base::Unretained(job.get()), | 291 base::Unretained(this), base::Unretained(job.get()), |
291 RemoteCommandJob::FAILED, | 292 RemoteCommandJob::FAILED, |
292 CreatePayloadFromResultCode( | 293 CreatePayloadFromResultCode( |
293 DeviceCommandScreenshotJob::FAILURE_USER_INPUT))); | 294 DeviceCommandScreenshotJob::FAILURE_USER_INPUT))); |
294 EXPECT_TRUE(success); | 295 EXPECT_TRUE(success); |
295 run_loop_.Run(); | 296 run_loop_.Run(); |
296 } | 297 } |
297 | 298 |
298 TEST_F(DeviceCommandScreenshotTest, Failure) { | 299 TEST_F(DeviceCommandScreenshotTest, Failure) { |
299 using ErrorCode = UploadJob::ErrorCode; | 300 using ErrorCode = UploadJob::ErrorCode; |
300 scoped_ptr<ErrorCode> error_code( | 301 std::unique_ptr<ErrorCode> error_code( |
301 new ErrorCode(UploadJob::AUTHENTICATION_ERROR)); | 302 new ErrorCode(UploadJob::AUTHENTICATION_ERROR)); |
302 scoped_ptr<RemoteCommandJob> job( | 303 std::unique_ptr<RemoteCommandJob> job( |
303 new DeviceCommandScreenshotJob(make_scoped_ptr( | 304 new DeviceCommandScreenshotJob(base::WrapUnique( |
304 new MockScreenshotDelegate(std::move(error_code), true)))); | 305 new MockScreenshotDelegate(std::move(error_code), true)))); |
305 InitializeScreenshotJob(job.get(), kUniqueID, test_start_time_, | 306 InitializeScreenshotJob(job.get(), kUniqueID, test_start_time_, |
306 kMockUploadUrl); | 307 kMockUploadUrl); |
307 bool success = job->Run( | 308 bool success = job->Run( |
308 base::TimeTicks::Now(), | 309 base::TimeTicks::Now(), |
309 base::Bind(&DeviceCommandScreenshotTest::VerifyResults, | 310 base::Bind(&DeviceCommandScreenshotTest::VerifyResults, |
310 base::Unretained(this), base::Unretained(job.get()), | 311 base::Unretained(this), base::Unretained(job.get()), |
311 RemoteCommandJob::FAILED, | 312 RemoteCommandJob::FAILED, |
312 CreatePayloadFromResultCode( | 313 CreatePayloadFromResultCode( |
313 DeviceCommandScreenshotJob::FAILURE_AUTHENTICATION))); | 314 DeviceCommandScreenshotJob::FAILURE_AUTHENTICATION))); |
314 EXPECT_TRUE(success); | 315 EXPECT_TRUE(success); |
315 run_loop_.Run(); | 316 run_loop_.Run(); |
316 } | 317 } |
317 | 318 |
318 } // namespace policy | 319 } // namespace policy |
OLD | NEW |