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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_SCREENSHOT_TESTING_SCREENSHOT_TESTER_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_SCREENSHOT_TESTING_SCREENSHOT_TESTER_H_
11 #include "base/files/file_path.h"
12 #include "base/memory/ref_counted_memory.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/run_loop.h"
15 #include "third_party/skia/include/core/SkBitmap.h"
19 // A class that allows taking, saving and comparing screnshots while
21 class ScreenshotTester
{
24 virtual ~ScreenshotTester();
26 typedef scoped_refptr
<base::RefCountedBytes
> PNGFile
;
28 // Contains the results of comparison
33 // Is true if screenshots are considered to be equal, false otherwise.
34 bool screenshots_match
;
36 // How similar are images from 0 to 1.
39 // Image representing difference. Might not be filled during to specifics of
40 // some comparison methods.
44 // Returns true if the screenshots should be taken and will be taken,
45 // false otherwise. Also gets all the information from the command line
49 // Does all the work that has been stated through switches:
50 // updates golden screenshot or takes a new screenshot and compares it
51 // with the golden one. |test_name| is the name of the test from which
52 // we run this method.
53 void Run(const std::string
& test_name
);
55 // Remembers that area |area| should be ignored during comparison.
56 void IgnoreArea(const SkIRect
& area
);
58 // Prints out how similar two images are.
59 void LogSimilarity(double similarity
, bool screenshots_match
);
62 // Categories of images that can be possible generated and used during this
64 enum ImageCategories
{
70 // When images are same less that |kPrecision| pixels, they are considered to
72 const double kPrecision
= 0.99;
74 // When difference in any of the RGB colors in any of the pixels between two
76 // exceeds |kMaxAllowedColorDifference|, images are considered to be
78 const int kMaxAllowedColorDifference
= 32;
80 // Returns full name of a .png file of a |category| type starting with
81 // |file_name_prefix| prefix,
82 // e.g. MyTest_golden_screenshot.png
83 std::string
GetImageFileName(const std::string
& file_name_prefix
,
84 ImageCategories category
);
86 // Returns full path for an image of this category.
87 base::FilePath
GetImageFilePath(const std::string
& file_name_prefix
,
88 ImageCategories category
);
90 // Erases areas that should be ignored during comparison from the screenshot.
91 void EraseIgnoredAreas(SkBitmap
& bitmap
);
93 // Takes a screenshot and returns it.
94 PNGFile
TakeScreenshot();
96 // Saves |png_data| as a new golden screenshot for test |test_name_|.
97 void UpdateGoldenScreenshot(PNGFile png_data
);
99 // Saves an image |png_data|, assuming it is a .png file.
100 // Returns true if image was saved successfully.
101 bool SaveImage(const base::FilePath
& image_path
, PNGFile png_data
);
103 // Logs results of comparison accordint to information in |result|.
104 void LogComparisonResults(const ScreenshotTester::Result
& result
);
106 // Saves |png_data| as a current screenshot.
107 void ReturnScreenshot(const PNGFile
& screenshot
, PNGFile png_data
);
109 // Loads golden screenshot from the disk, assuming it lies at |image_path|.
110 // Fails if there is no such a file.
111 PNGFile
LoadGoldenScreenshot(base::FilePath image_path
);
113 // Converts .png file to a bitmap which is ready for comparison (erasing
114 // ignored areas included).
115 SkBitmap
ProcessImageForComparison(const PNGFile
& image
);
117 // Calls comparing two given screenshots with one of the comparators.
118 // Returns a Result structure containing comparison results.
119 Result
CompareScreenshots(const PNGFile
& model
, const PNGFile
& sample
);
121 // Compares images using PerceptualDiff, and returns a Result structure
122 // without a diff image containing comparison results.
123 Result
CompareScreenshotsPerceptually(SkBitmap model_bitmap
,
124 SkBitmap sample_bitmap
);
126 // Compares images pixel-by-pixel with some features and returns a Result
127 // structure containing comparison results.
128 Result
CompareScreenshotsRegularly(SkBitmap model_bitmap
,
129 SkBitmap sample_bitmap
);
131 // Path to the directory for golden screenshots.
132 base::FilePath golden_screenshots_dir_
;
134 // Path to the directory where screenshots that failed comparing
135 // and difference between them and golden ones will be stored.
136 base::FilePath artifacts_dir_
;
138 // |run_loop_| and |run_loop_quitter_| are used to synchronize
139 // with ui::GrabWindowSnapshotAsync.
140 base::RunLoop run_loop_
;
141 base::Closure run_loop_quitter_
;
143 // Is true when we're in test mode:
144 // comparing golden screenshots and current ones.
147 // Is true when switches specify that PerceptualDiff should
148 // be used to compare images.
151 // Is true when switches specify that artifacts should be saved somewhere.
152 bool generate_artifacts_
;
154 // Vector which holds areas which the comparison ignores because
155 // them being different is not a bug (e.g. time on the clock).
156 std::vector
<SkIRect
> ignored_areas_
;
158 base::WeakPtrFactory
<ScreenshotTester
> weak_factory_
;
160 DISALLOW_COPY_AND_ASSIGN(ScreenshotTester
);
163 } // namespace chromeos
165 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_SCREENSHOT_TESTING_SCREENSHOT_TESTER_H_