| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/login/screenshot_testing/screenshot_tester.h" | 5 #include "chrome/browser/chromeos/login/screenshot_testing/screenshot_tester.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "ash/shell.h" | 10 #include "ash/shell.h" |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 PNGFile png_data) { | 180 PNGFile png_data) { |
| 181 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 181 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 182 if (!png_data.get()) { | 182 if (!png_data.get()) { |
| 183 LOG(ERROR) << "There is no png data"; | 183 LOG(ERROR) << "There is no png data"; |
| 184 return false; | 184 return false; |
| 185 } | 185 } |
| 186 if (!base::CreateDirectory(image_path.DirName())) { | 186 if (!base::CreateDirectory(image_path.DirName())) { |
| 187 LOG(ERROR) << "Can't create directory" << image_path.DirName().value(); | 187 LOG(ERROR) << "Can't create directory" << image_path.DirName().value(); |
| 188 return false; | 188 return false; |
| 189 } | 189 } |
| 190 if (static_cast<size_t>( | 190 if (static_cast<size_t>(base::WriteFile( |
| 191 base::WriteFile(image_path, | 191 image_path, reinterpret_cast<const char*>(png_data->front()), |
| 192 reinterpret_cast<char*>(&(png_data->data()[0])), | 192 png_data->size())) != png_data->size()) { |
| 193 png_data->size())) != png_data->size()) { | |
| 194 LOG(ERROR) << "Can't save screenshot " << image_path.BaseName().value() | 193 LOG(ERROR) << "Can't save screenshot " << image_path.BaseName().value() |
| 195 << "."; | 194 << "."; |
| 196 return false; | 195 return false; |
| 197 } | 196 } |
| 198 VLOG(0) << "Screenshot " << image_path.BaseName().value() << " saved to " | 197 VLOG(0) << "Screenshot " << image_path.BaseName().value() << " saved to " |
| 199 << image_path.DirName().value() << "."; | 198 << image_path.DirName().value() << "."; |
| 200 return true; | 199 return true; |
| 201 } | 200 } |
| 202 | 201 |
| 203 void ScreenshotTester::ReturnScreenshot(const PNGFile& screenshot, | 202 void ScreenshotTester::ReturnScreenshot(PNGFile* screenshot, PNGFile png_data) { |
| 204 PNGFile png_data) { | |
| 205 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 203 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 206 screenshot->data() = png_data->data(); | 204 *screenshot = png_data; |
| 207 content::BrowserThread::PostTask( | 205 content::BrowserThread::PostTask( |
| 208 content::BrowserThread::UI, FROM_HERE, run_loop_quitter_); | 206 content::BrowserThread::UI, FROM_HERE, run_loop_quitter_); |
| 209 } | 207 } |
| 210 | 208 |
| 211 ScreenshotTester::PNGFile ScreenshotTester::TakeScreenshot() { | 209 ScreenshotTester::PNGFile ScreenshotTester::TakeScreenshot() { |
| 212 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 210 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 213 aura::Window* primary_window = ash::Shell::GetPrimaryRootWindow(); | 211 aura::Window* primary_window = ash::Shell::GetPrimaryRootWindow(); |
| 214 gfx::Rect rect = primary_window->bounds(); | 212 gfx::Rect rect = primary_window->bounds(); |
| 215 PNGFile screenshot = new base::RefCountedBytes; | 213 PNGFile screenshot; |
| 216 ui::GrabWindowSnapshotAsync(primary_window, | 214 ui::GrabWindowSnapshotAsyncPNG( |
| 217 rect, | 215 primary_window, rect, content::BrowserThread::GetBlockingPool(), |
| 218 content::BrowserThread::GetBlockingPool(), | 216 base::Bind(&ScreenshotTester::ReturnScreenshot, |
| 219 base::Bind(&ScreenshotTester::ReturnScreenshot, | 217 weak_factory_.GetWeakPtr(), &screenshot)); |
| 220 weak_factory_.GetWeakPtr(), | |
| 221 screenshot)); | |
| 222 base::RunLoop run_loop; | 218 base::RunLoop run_loop; |
| 223 run_loop_quitter_ = run_loop.QuitClosure(); | 219 run_loop_quitter_ = run_loop.QuitClosure(); |
| 224 run_loop.Run(); | 220 run_loop.Run(); |
| 225 return screenshot; | 221 return screenshot; |
| 226 } | 222 } |
| 227 | 223 |
| 228 ScreenshotTester::PNGFile ScreenshotTester::LoadGoldenScreenshot( | 224 ScreenshotTester::PNGFile ScreenshotTester::LoadGoldenScreenshot( |
| 229 base::FilePath image_path) { | 225 base::FilePath image_path) { |
| 230 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 226 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 231 | 227 |
| 232 if (!base::PathExists(image_path)) { | 228 if (!base::PathExists(image_path)) { |
| 233 LOG(WARNING) << "Can't find a golden screenshot for this test"; | 229 LOG(WARNING) << "Can't find a golden screenshot for this test"; |
| 234 return 0; | 230 return 0; |
| 235 } | 231 } |
| 236 | 232 |
| 237 int64_t golden_screenshot_size; | 233 int64_t golden_screenshot_size; |
| 238 base::GetFileSize(image_path, &golden_screenshot_size); | 234 base::GetFileSize(image_path, &golden_screenshot_size); |
| 239 | 235 |
| 240 if (golden_screenshot_size == -1) { | 236 if (golden_screenshot_size == -1) { |
| 241 CHECK(false) << "Can't get golden screenshot size"; | 237 CHECK(false) << "Can't get golden screenshot size"; |
| 242 } | 238 } |
| 243 PNGFile png_data = new base::RefCountedBytes; | 239 scoped_refptr<base::RefCountedBytes> png_data = new base::RefCountedBytes; |
| 244 png_data->data().resize(golden_screenshot_size); | 240 png_data->data().resize(golden_screenshot_size); |
| 245 base::ReadFile(image_path, | 241 base::ReadFile(image_path, |
| 246 reinterpret_cast<char*>(&(png_data->data()[0])), | 242 reinterpret_cast<char*>(&(png_data->data()[0])), |
| 247 golden_screenshot_size); | 243 golden_screenshot_size); |
| 248 | 244 |
| 249 return png_data; | 245 return png_data; |
| 250 } | 246 } |
| 251 | 247 |
| 252 SkBitmap ScreenshotTester::ProcessImageForComparison(const PNGFile& image) { | 248 SkBitmap ScreenshotTester::ProcessImageForComparison(const PNGFile& image) { |
| 253 CHECK(image.get()); | 249 CHECK(image.get()); |
| 254 SkBitmap current_bitmap; | 250 SkBitmap current_bitmap; |
| 255 gfx::PNGCodec::Decode(reinterpret_cast<unsigned char*>(&(image->data()[0])), | 251 gfx::PNGCodec::Decode(image->front(), image->size(), ¤t_bitmap); |
| 256 image->data().size(), | |
| 257 ¤t_bitmap); | |
| 258 EraseIgnoredAreas(current_bitmap); | 252 EraseIgnoredAreas(current_bitmap); |
| 259 return current_bitmap; | 253 return current_bitmap; |
| 260 } | 254 } |
| 261 | 255 |
| 262 ScreenshotTester::Result ScreenshotTester::CompareScreenshots( | 256 ScreenshotTester::Result ScreenshotTester::CompareScreenshots( |
| 263 const PNGFile& model, | 257 const PNGFile& model, |
| 264 const PNGFile& sample) { | 258 const PNGFile& sample) { |
| 265 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 259 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 266 | 260 |
| 267 SkBitmap model_bitmap; | 261 SkBitmap model_bitmap; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 Result testing_result; | 316 Result testing_result; |
| 323 | 317 |
| 324 testing_result.screenshots_match = | 318 testing_result.screenshots_match = |
| 325 (result.result >= kPrecision && | 319 (result.result >= kPrecision && |
| 326 result.maxRedDiff <= kMaxAllowedColorDifference && | 320 result.maxRedDiff <= kMaxAllowedColorDifference && |
| 327 result.maxGreenDiff <= kMaxAllowedColorDifference && | 321 result.maxGreenDiff <= kMaxAllowedColorDifference && |
| 328 result.maxBlueDiff <= kMaxAllowedColorDifference); | 322 result.maxBlueDiff <= kMaxAllowedColorDifference); |
| 329 | 323 |
| 330 testing_result.similarity = result.result; | 324 testing_result.similarity = result.result; |
| 331 | 325 |
| 332 testing_result.diff_image = new base::RefCountedBytes; | 326 scoped_refptr<base::RefCountedBytes> diff_image(new base::RefCountedBytes); |
| 333 testing_result.diff_image->data().resize(result.rgbDiffBitmap.getSize()); | 327 diff_image->data().resize(result.rgbDiffBitmap.getSize()); |
| 334 CHECK(gfx::PNGCodec::EncodeBGRASkBitmap( | 328 CHECK(gfx::PNGCodec::EncodeBGRASkBitmap(result.rgbDiffBitmap, false, |
| 335 result.rgbDiffBitmap, false, &testing_result.diff_image->data())) | 329 &diff_image->data())) |
| 336 << "Could not encode difference to PNG"; | 330 << "Could not encode difference to PNG"; |
| 331 testing_result.diff_image = diff_image; |
| 337 | 332 |
| 338 return testing_result; | 333 return testing_result; |
| 339 } | 334 } |
| 340 | 335 |
| 341 ScreenshotTester::Result ScreenshotTester::CompareScreenshotsPerceptually( | 336 ScreenshotTester::Result ScreenshotTester::CompareScreenshotsPerceptually( |
| 342 SkBitmap model_bitmap, | 337 SkBitmap model_bitmap, |
| 343 SkBitmap sample_bitmap) { | 338 SkBitmap sample_bitmap) { |
| 344 SkPMetric differ; | 339 SkPMetric differ; |
| 345 SkImageDiffer::BitmapsToCreate diff_parameters; | 340 SkImageDiffer::BitmapsToCreate diff_parameters; |
| 346 SkImageDiffer::Result result; | 341 SkImageDiffer::Result result; |
| 347 | 342 |
| 348 differ.diff(&model_bitmap, &sample_bitmap, diff_parameters, &result); | 343 differ.diff(&model_bitmap, &sample_bitmap, diff_parameters, &result); |
| 349 | 344 |
| 350 ScreenshotTester::Result testing_result; | 345 ScreenshotTester::Result testing_result; |
| 351 testing_result.similarity = result.result; | 346 testing_result.similarity = result.result; |
| 352 testing_result.screenshots_match = | 347 testing_result.screenshots_match = |
| 353 (result.result == SkImageDiffer::RESULT_CORRECT); | 348 (result.result == SkImageDiffer::RESULT_CORRECT); |
| 354 | 349 |
| 355 return testing_result; | 350 return testing_result; |
| 356 } | 351 } |
| 357 | 352 |
| 358 } // namespace chromeos | 353 } // namespace chromeos |
| OLD | NEW |