| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 ScreenshotTester::Result::~Result() { | 52 ScreenshotTester::Result::~Result() { |
| 53 } | 53 } |
| 54 | 54 |
| 55 bool ScreenshotTester::TryInitialize() { | 55 bool ScreenshotTester::TryInitialize() { |
| 56 base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); | 56 base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); |
| 57 if (!command_line.HasSwitch(switches::kEnableScreenshotTestingWithMode)) | 57 if (!command_line.HasSwitch(switches::kEnableScreenshotTestingWithMode)) |
| 58 return false; | 58 return false; |
| 59 | 59 |
| 60 std::string mode = command_line.GetSwitchValueASCII( | 60 std::string mode = command_line.GetSwitchValueASCII( |
| 61 switches::kEnableScreenshotTestingWithMode); | 61 switches::kEnableScreenshotTestingWithMode); |
| 62 CHECK(mode == kUpdateMode || mode == kTestMode || mode == kPdiffTestMode) | 62 // Invalid mode for screenshot testing. |
| 63 << "Invalid mode for screenshot testing: " << mode; | 63 CHECK(mode == kUpdateMode || mode == kTestMode || mode == kPdiffTestMode); |
| 64 | 64 |
| 65 CHECK(command_line.HasSwitch(chromeos::switches::kGoldenScreenshotsDir)) | 65 // No directory with golden screenshots specified, use |
| 66 << "No directory with golden screenshots specified, use " | 66 // --golden-screenshots-dir |
| 67 "--golden-screenshots-dir"; | 67 CHECK(command_line.HasSwitch(chromeos::switches::kGoldenScreenshotsDir)); |
| 68 | 68 |
| 69 golden_screenshots_dir_ = | 69 golden_screenshots_dir_ = |
| 70 command_line.GetSwitchValuePath(switches::kGoldenScreenshotsDir); | 70 command_line.GetSwitchValuePath(switches::kGoldenScreenshotsDir); |
| 71 | 71 |
| 72 if (mode == kTestMode || mode == kPdiffTestMode) { | 72 if (mode == kTestMode || mode == kPdiffTestMode) { |
| 73 test_mode_ = true; | 73 test_mode_ = true; |
| 74 generate_artifacts_ = command_line.HasSwitch(switches::kArtifactsDir); | 74 generate_artifacts_ = command_line.HasSwitch(switches::kArtifactsDir); |
| 75 if (generate_artifacts_) { | 75 if (generate_artifacts_) { |
| 76 artifacts_dir_ = command_line.GetSwitchValuePath(switches::kArtifactsDir); | 76 artifacts_dir_ = command_line.GetSwitchValuePath(switches::kArtifactsDir); |
| 77 } | 77 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 } | 115 } |
| 116 return file_path; | 116 return file_path; |
| 117 } | 117 } |
| 118 | 118 |
| 119 void ScreenshotTester::Run(const std::string& test_name) { | 119 void ScreenshotTester::Run(const std::string& test_name) { |
| 120 PNGFile current_screenshot = TakeScreenshot(); | 120 PNGFile current_screenshot = TakeScreenshot(); |
| 121 base::FilePath golden_screenshot_path = | 121 base::FilePath golden_screenshot_path = |
| 122 GetImageFilePath(test_name, kGoldenScreenshot); | 122 GetImageFilePath(test_name, kGoldenScreenshot); |
| 123 PNGFile golden_screenshot = LoadGoldenScreenshot(golden_screenshot_path); | 123 PNGFile golden_screenshot = LoadGoldenScreenshot(golden_screenshot_path); |
| 124 if (test_mode_) { | 124 if (test_mode_) { |
| 125 CHECK(golden_screenshot.get()) | 125 // A golden screenshot is required for screenshot testing |
| 126 << "A golden screenshot is required for screenshot testing"; | 126 CHECK(golden_screenshot.get()); |
| 127 VLOG(0) << "Loaded golden screenshot"; | 127 VLOG(0) << "Loaded golden screenshot"; |
| 128 Result result = CompareScreenshots(golden_screenshot, current_screenshot); | 128 Result result = CompareScreenshots(golden_screenshot, current_screenshot); |
| 129 VLOG(0) << "Compared"; | 129 VLOG(0) << "Compared"; |
| 130 LogComparisonResults(result); | 130 LogComparisonResults(result); |
| 131 if (!result.screenshots_match && generate_artifacts_) { | 131 if (!result.screenshots_match && generate_artifacts_) { |
| 132 // Saving diff imag | 132 // Saving diff imag |
| 133 if (!pdiff_enabled_) { | 133 if (!pdiff_enabled_) { |
| 134 base::FilePath difference_image_path = | 134 base::FilePath difference_image_path = |
| 135 GetImageFilePath(test_name, kDifferenceImage); | 135 GetImageFilePath(test_name, kDifferenceImage); |
| 136 CHECK(SaveImage(difference_image_path, result.diff_image)); | 136 CHECK(SaveImage(difference_image_path, result.diff_image)); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 | 230 |
| 231 if (!base::PathExists(image_path)) { | 231 if (!base::PathExists(image_path)) { |
| 232 LOG(WARNING) << "Can't find a golden screenshot for this test"; | 232 LOG(WARNING) << "Can't find a golden screenshot for this test"; |
| 233 return 0; | 233 return 0; |
| 234 } | 234 } |
| 235 | 235 |
| 236 int64_t golden_screenshot_size; | 236 int64_t golden_screenshot_size; |
| 237 base::GetFileSize(image_path, &golden_screenshot_size); | 237 base::GetFileSize(image_path, &golden_screenshot_size); |
| 238 | 238 |
| 239 if (golden_screenshot_size == -1) { | 239 if (golden_screenshot_size == -1) { |
| 240 CHECK(false) << "Can't get golden screenshot size"; | 240 // Can't get golden screenshot size |
| 241 CHECK(false); |
| 241 } | 242 } |
| 242 PNGFile png_data = new base::RefCountedBytes; | 243 PNGFile png_data = new base::RefCountedBytes; |
| 243 png_data->data().resize(golden_screenshot_size); | 244 png_data->data().resize(golden_screenshot_size); |
| 244 base::ReadFile(image_path, | 245 base::ReadFile(image_path, |
| 245 reinterpret_cast<char*>(&(png_data->data()[0])), | 246 reinterpret_cast<char*>(&(png_data->data()[0])), |
| 246 golden_screenshot_size); | 247 golden_screenshot_size); |
| 247 | 248 |
| 248 return png_data; | 249 return png_data; |
| 249 } | 250 } |
| 250 | 251 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 testing_result.screenshots_match = | 324 testing_result.screenshots_match = |
| 324 (result.result >= kPrecision && | 325 (result.result >= kPrecision && |
| 325 result.maxRedDiff <= kMaxAllowedColorDifference && | 326 result.maxRedDiff <= kMaxAllowedColorDifference && |
| 326 result.maxGreenDiff <= kMaxAllowedColorDifference && | 327 result.maxGreenDiff <= kMaxAllowedColorDifference && |
| 327 result.maxBlueDiff <= kMaxAllowedColorDifference); | 328 result.maxBlueDiff <= kMaxAllowedColorDifference); |
| 328 | 329 |
| 329 testing_result.similarity = result.result; | 330 testing_result.similarity = result.result; |
| 330 | 331 |
| 331 testing_result.diff_image = new base::RefCountedBytes; | 332 testing_result.diff_image = new base::RefCountedBytes; |
| 332 testing_result.diff_image->data().resize(result.rgbDiffBitmap.getSize()); | 333 testing_result.diff_image->data().resize(result.rgbDiffBitmap.getSize()); |
| 333 CHECK(gfx::PNGCodec::EncodeBGRASkBitmap( | 334 // Could not encode difference to PNG |
| 334 result.rgbDiffBitmap, false, &testing_result.diff_image->data())) | 335 CHECK(gfx::PNGCodec::EncodeBGRASkBitmap(result.rgbDiffBitmap, false, |
| 335 << "Could not encode difference to PNG"; | 336 &testing_result.diff_image->data())); |
| 336 | 337 |
| 337 return testing_result; | 338 return testing_result; |
| 338 } | 339 } |
| 339 | 340 |
| 340 ScreenshotTester::Result ScreenshotTester::CompareScreenshotsPerceptually( | 341 ScreenshotTester::Result ScreenshotTester::CompareScreenshotsPerceptually( |
| 341 SkBitmap model_bitmap, | 342 SkBitmap model_bitmap, |
| 342 SkBitmap sample_bitmap) { | 343 SkBitmap sample_bitmap) { |
| 343 SkPMetric differ; | 344 SkPMetric differ; |
| 344 SkImageDiffer::BitmapsToCreate diff_parameters; | 345 SkImageDiffer::BitmapsToCreate diff_parameters; |
| 345 SkImageDiffer::Result result; | 346 SkImageDiffer::Result result; |
| 346 | 347 |
| 347 differ.diff(&model_bitmap, &sample_bitmap, diff_parameters, &result); | 348 differ.diff(&model_bitmap, &sample_bitmap, diff_parameters, &result); |
| 348 | 349 |
| 349 ScreenshotTester::Result testing_result; | 350 ScreenshotTester::Result testing_result; |
| 350 testing_result.similarity = result.result; | 351 testing_result.similarity = result.result; |
| 351 testing_result.screenshots_match = | 352 testing_result.screenshots_match = |
| 352 (result.result == SkImageDiffer::RESULT_CORRECT); | 353 (result.result == SkImageDiffer::RESULT_CORRECT); |
| 353 | 354 |
| 354 return testing_result; | 355 return testing_result; |
| 355 } | 356 } |
| 356 | 357 |
| 357 } // namespace chromeos | 358 } // namespace chromeos |
| OLD | NEW |