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 <map> | 5 #include <map> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "ash/test/ash_test_base.h" | 8 #include "ash/test/ash_test_base.h" |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 gfx::Size(bmp.width(), bmp.height()), | 130 gfx::Size(bmp.width(), bmp.height()), |
131 static_cast<int>(bmp.rowBytes()), false, | 131 static_cast<int>(bmp.rowBytes()), false, |
132 std::vector<gfx::PNGCodec::Comment>(), &png_bytes->data())) { | 132 std::vector<gfx::PNGCodec::Comment>(), &png_bytes->data())) { |
133 LOG(ERROR) << "Failed to encode image"; | 133 LOG(ERROR) << "Failed to encode image"; |
134 } | 134 } |
135 return png_bytes; | 135 return png_bytes; |
136 } | 136 } |
137 | 137 |
138 class MockScreenshotDelegate : public DeviceCommandScreenshotJob::Delegate { | 138 class MockScreenshotDelegate : public DeviceCommandScreenshotJob::Delegate { |
139 public: | 139 public: |
140 MockScreenshotDelegate( | 140 MockScreenshotDelegate(scoped_ptr<UploadJob::ErrorCode> upload_job_error_code, |
141 scoped_ptr<UploadJob::ErrorCode> upload_job_error_code); | 141 bool screenshot_allowed); |
142 ~MockScreenshotDelegate() override; | 142 ~MockScreenshotDelegate() override; |
143 | 143 |
| 144 bool IsScreenshotAllowed() override; |
144 void TakeSnapshot( | 145 void TakeSnapshot( |
145 gfx::NativeWindow window, | 146 gfx::NativeWindow window, |
146 const gfx::Rect& source_rect, | 147 const gfx::Rect& source_rect, |
147 const ui::GrabWindowSnapshotAsyncPNGCallback& callback) override; | 148 const ui::GrabWindowSnapshotAsyncPNGCallback& callback) override; |
148 scoped_ptr<UploadJob> CreateUploadJob(const GURL&, | 149 scoped_ptr<UploadJob> CreateUploadJob(const GURL&, |
149 UploadJob::Delegate*) override; | 150 UploadJob::Delegate*) override; |
150 | 151 |
151 private: | 152 private: |
152 scoped_ptr<UploadJob::ErrorCode> upload_job_error_code_; | 153 scoped_ptr<UploadJob::ErrorCode> upload_job_error_code_; |
| 154 bool screenshot_allowed_; |
153 }; | 155 }; |
154 | 156 |
155 MockScreenshotDelegate::MockScreenshotDelegate( | 157 MockScreenshotDelegate::MockScreenshotDelegate( |
156 scoped_ptr<UploadJob::ErrorCode> upload_job_error_code) | 158 scoped_ptr<UploadJob::ErrorCode> upload_job_error_code, |
157 : upload_job_error_code_(upload_job_error_code.Pass()) { | 159 bool screenshot_allowed) |
| 160 : upload_job_error_code_(upload_job_error_code.Pass()), |
| 161 screenshot_allowed_(screenshot_allowed) { |
158 } | 162 } |
159 | 163 |
160 MockScreenshotDelegate::~MockScreenshotDelegate() { | 164 MockScreenshotDelegate::~MockScreenshotDelegate() { |
161 } | 165 } |
162 | 166 |
| 167 bool MockScreenshotDelegate::IsScreenshotAllowed() { |
| 168 return screenshot_allowed_; |
| 169 } |
| 170 |
163 void MockScreenshotDelegate::TakeSnapshot( | 171 void MockScreenshotDelegate::TakeSnapshot( |
164 gfx::NativeWindow window, | 172 gfx::NativeWindow window, |
165 const gfx::Rect& source_rect, | 173 const gfx::Rect& source_rect, |
166 const ui::GrabWindowSnapshotAsyncPNGCallback& callback) { | 174 const ui::GrabWindowSnapshotAsyncPNGCallback& callback) { |
167 const int width = source_rect.width(); | 175 const int width = source_rect.width(); |
168 const int height = source_rect.height(); | 176 const int height = source_rect.height(); |
169 scoped_refptr<base::RefCountedBytes> test_png = | 177 scoped_refptr<base::RefCountedBytes> test_png = |
170 GenerateTestPNG(width, height); | 178 GenerateTestPNG(width, height); |
171 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 179 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
172 base::Bind(callback, test_png)); | 180 base::Bind(callback, test_png)); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 if (job->status() == RemoteCommandJob::SUCCEEDED) { | 258 if (job->status() == RemoteCommandJob::SUCCEEDED) { |
251 scoped_ptr<std::string> payload = job->GetResultPayload(); | 259 scoped_ptr<std::string> payload = job->GetResultPayload(); |
252 EXPECT_TRUE(payload); | 260 EXPECT_TRUE(payload); |
253 EXPECT_EQ(expected_payload, *payload); | 261 EXPECT_EQ(expected_payload, *payload); |
254 } | 262 } |
255 run_loop_.Quit(); | 263 run_loop_.Quit(); |
256 } | 264 } |
257 | 265 |
258 TEST_F(DeviceCommandScreenshotTest, Success) { | 266 TEST_F(DeviceCommandScreenshotTest, Success) { |
259 scoped_ptr<RemoteCommandJob> job(new DeviceCommandScreenshotJob( | 267 scoped_ptr<RemoteCommandJob> job(new DeviceCommandScreenshotJob( |
260 make_scoped_ptr(new MockScreenshotDelegate(nullptr)))); | 268 make_scoped_ptr(new MockScreenshotDelegate(nullptr, true)))); |
261 InitializeScreenshotJob(job.get(), kUniqueID, test_start_time_, | 269 InitializeScreenshotJob(job.get(), kUniqueID, test_start_time_, |
262 kMockUploadUrl); | 270 kMockUploadUrl); |
263 bool success = job->Run( | 271 bool success = job->Run( |
264 base::TimeTicks::Now(), | 272 base::TimeTicks::Now(), |
265 base::Bind( | 273 base::Bind( |
266 &DeviceCommandScreenshotTest::VerifyResults, base::Unretained(this), | 274 &DeviceCommandScreenshotTest::VerifyResults, base::Unretained(this), |
267 base::Unretained(job.get()), RemoteCommandJob::SUCCEEDED, | 275 base::Unretained(job.get()), RemoteCommandJob::SUCCEEDED, |
268 CreatePayloadFromResultCode(DeviceCommandScreenshotJob::SUCCESS))); | 276 CreatePayloadFromResultCode(DeviceCommandScreenshotJob::SUCCESS))); |
269 EXPECT_TRUE(success); | 277 EXPECT_TRUE(success); |
270 run_loop_.Run(); | 278 run_loop_.Run(); |
271 } | 279 } |
272 | 280 |
| 281 TEST_F(DeviceCommandScreenshotTest, FailureUserInput) { |
| 282 scoped_ptr<RemoteCommandJob> job(new DeviceCommandScreenshotJob( |
| 283 make_scoped_ptr(new MockScreenshotDelegate(nullptr, false)))); |
| 284 InitializeScreenshotJob(job.get(), kUniqueID, test_start_time_, |
| 285 kMockUploadUrl); |
| 286 bool success = |
| 287 job->Run(base::TimeTicks::Now(), |
| 288 base::Bind(&DeviceCommandScreenshotTest::VerifyResults, |
| 289 base::Unretained(this), base::Unretained(job.get()), |
| 290 RemoteCommandJob::FAILED, |
| 291 CreatePayloadFromResultCode( |
| 292 DeviceCommandScreenshotJob::FAILURE_USER_INPUT))); |
| 293 EXPECT_TRUE(success); |
| 294 run_loop_.Run(); |
| 295 } |
| 296 |
273 TEST_F(DeviceCommandScreenshotTest, Failure) { | 297 TEST_F(DeviceCommandScreenshotTest, Failure) { |
274 using ErrorCode = UploadJob::ErrorCode; | 298 using ErrorCode = UploadJob::ErrorCode; |
275 scoped_ptr<ErrorCode> error_code( | 299 scoped_ptr<ErrorCode> error_code( |
276 new ErrorCode(UploadJob::AUTHENTICATION_ERROR)); | 300 new ErrorCode(UploadJob::AUTHENTICATION_ERROR)); |
277 scoped_ptr<RemoteCommandJob> job(new DeviceCommandScreenshotJob( | 301 scoped_ptr<RemoteCommandJob> job(new DeviceCommandScreenshotJob( |
278 make_scoped_ptr(new MockScreenshotDelegate(error_code.Pass())))); | 302 make_scoped_ptr(new MockScreenshotDelegate(error_code.Pass(), true)))); |
279 InitializeScreenshotJob(job.get(), kUniqueID, test_start_time_, | 303 InitializeScreenshotJob(job.get(), kUniqueID, test_start_time_, |
280 kMockUploadUrl); | 304 kMockUploadUrl); |
281 bool success = job->Run( | 305 bool success = job->Run( |
282 base::TimeTicks::Now(), | 306 base::TimeTicks::Now(), |
283 base::Bind(&DeviceCommandScreenshotTest::VerifyResults, | 307 base::Bind(&DeviceCommandScreenshotTest::VerifyResults, |
284 base::Unretained(this), base::Unretained(job.get()), | 308 base::Unretained(this), base::Unretained(job.get()), |
285 RemoteCommandJob::FAILED, | 309 RemoteCommandJob::FAILED, |
286 CreatePayloadFromResultCode( | 310 CreatePayloadFromResultCode( |
287 DeviceCommandScreenshotJob::FAILURE_AUTHENTICATION))); | 311 DeviceCommandScreenshotJob::FAILURE_AUTHENTICATION))); |
288 EXPECT_TRUE(success); | 312 EXPECT_TRUE(success); |
289 run_loop_.Run(); | 313 run_loop_.Run(); |
290 } | 314 } |
291 | 315 |
292 } // namespace policy | 316 } // namespace policy |
OLD | NEW |