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