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_tester.h" | 5 #include "chrome/browser/chromeos/login/screenshot_tester.h" |
6 | 6 |
7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
9 #include "base/bind_internal.h" | 9 #include "base/bind_internal.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/logging.h" | |
12 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
13 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
14 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
15 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
16 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
17 #include "chromeos/chromeos_switches.h" | 16 #include "chromeos/chromeos_switches.h" |
18 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 #include "third_party/skia/include/core/SkBitmap.h" | |
21 #include "third_party/skia/include/core/SkCanvas.h" | |
22 #include "ui/compositor/compositor_switches.h" | 18 #include "ui/compositor/compositor_switches.h" |
23 #include "ui/gfx/codec/png_codec.h" | |
24 #include "ui/gfx/image/image.h" | 19 #include "ui/gfx/image/image.h" |
25 #include "ui/snapshot/snapshot.h" | 20 #include "ui/snapshot/snapshot.h" |
26 | 21 |
27 namespace { | |
28 | |
29 // Sets test mode for screenshot testing. | |
30 const char kTestMode[] = "test"; | |
31 | |
32 // Sets update mode for screenshot testing. | |
33 const char kUpdateMode[] = "update;" | |
34 | |
35 } // namespace | |
36 | |
37 namespace chromeos { | 22 namespace chromeos { |
38 | 23 |
39 ScreenshotTester::ScreenshotTester() : test_mode_(false), weak_factory_(this) { | 24 ScreenshotTester::ScreenshotTester() : weak_factory_(this) { |
40 } | 25 } |
41 | 26 |
42 ScreenshotTester::~ScreenshotTester() { | 27 ScreenshotTester::~ScreenshotTester() { |
43 } | 28 } |
44 | 29 |
45 bool ScreenshotTester::TryInitialize() { | 30 bool ScreenshotTester::TryInitialize() { |
46 CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 31 CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
47 if (!command_line.HasSwitch(switches::kEnableScreenshotTestingWithMode)) | 32 if (!command_line.HasSwitch(switches::kEnableScreenshotTesting)) |
48 return false; | 33 return false; |
49 if (!command_line.HasSwitch(::switches::kEnablePixelOutputInTests) || | 34 if (!command_line.HasSwitch(::switches::kEnablePixelOutputInTests) || |
50 !command_line.HasSwitch(::switches::kUIEnableImplSidePainting)) { | 35 !command_line.HasSwitch(::switches::kUIEnableImplSidePainting)) { |
51 // TODO(elizavetai): make turning on --enable-pixel-output-in-tests | 36 // TODO(elizavetai): make turning on --enable-pixel-output-in-tests |
52 // and --ui-enable-impl-side-painting automatical. | 37 // and --ui-enable-impl-side-painting automatical. |
53 LOG(ERROR) << "--enable-pixel-output-in-tests and " | 38 LOG(ERROR) << "--enable-pixel-output-in-tests and " |
54 << "--ui-enable-impl-side-painting are required to take " | 39 << "--ui-enable-impl-side-painting are required to take " |
55 << "screenshots"; | 40 << "screenshots"; |
56 return false; | 41 return false; |
57 } | 42 } |
58 | 43 if (!command_line.HasSwitch(switches::kScreenshotDestinationDir)) { |
59 std::string mode = command_line.GetSwitchValueASCII( | 44 LOG(ERROR) << "No destination for screenshots specified"; |
60 switches::kEnableScreenshotTestingWithMode); | 45 return false; |
61 if (mode != kUpdateMode && mode != kTestMode) { | |
62 CHECK(false) << "Invalid mode for screenshot testing: " << mode; | |
63 } | 46 } |
64 | 47 screenshot_dest_ = command_line.GetSwitchValuePath( |
65 if (!command_line.HasSwitch(chromeos::switches::kGoldenScreenshotsDir)) { | 48 chromeos::switches::kScreenshotDestinationDir); |
66 CHECK(false) << "No directory for golden screenshots specified"; | |
67 } | |
68 | |
69 golden_screenshots_dir_ = | |
70 command_line.GetSwitchValuePath(switches::kGoldenScreenshotsDir); | |
71 | |
72 if (mode == kTestMode) { | |
73 test_mode_ = true; | |
74 if (!command_line.HasSwitch(switches::kArtifactsDir)) { | |
75 artifacts_dir_ = golden_screenshots_dir_; | |
76 LOG(WARNING) | |
77 << "No directory for artifact storing specified. Artifacts will be" | |
78 << "saved at golden screenshots directory."; | |
79 } else { | |
80 artifacts_dir_ = command_line.GetSwitchValuePath(switches::kArtifactsDir); | |
81 } | |
82 } | |
83 return true; | 49 return true; |
84 } | 50 } |
85 | 51 |
86 void ScreenshotTester::Run(const std::string& test_name) { | 52 void ScreenshotTester::Run(const std::string& file_name) { |
87 test_name_ = test_name; | 53 TakeScreenshot(); |
88 PNGFile current_screenshot = TakeScreenshot(); | 54 PNGFile current_screenshot = screenshot_; |
89 if (test_mode_) { | 55 UpdateGoldenScreenshot(file_name, current_screenshot); |
90 PNGFile golden_screenshot = LoadGoldenScreenshot(); | |
91 VLOG(0) << "Loaded golden screenshot"; | |
92 CompareScreenshots(golden_screenshot, current_screenshot); | |
93 } else { | |
94 UpdateGoldenScreenshot(current_screenshot); | |
95 } | |
96 } | 56 } |
97 | 57 |
98 void ScreenshotTester::UpdateGoldenScreenshot(PNGFile png_data) { | 58 void ScreenshotTester::UpdateGoldenScreenshot(const std::string& file_name, |
99 CHECK(SaveImage("golden_screenshot", golden_screenshots_dir_, png_data)); | 59 PNGFile png_data) { |
| 60 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 61 if (!png_data) { |
| 62 LOG(ERROR) << "Can't take a screenshot"; |
| 63 return; |
| 64 } |
| 65 base::FilePath golden_screenshot_path = |
| 66 screenshot_dest_.AppendASCII(file_name + ".png"); |
| 67 if (!base::CreateDirectory(golden_screenshot_path.DirName())) { |
| 68 LOG(ERROR) << "Can't create a directory "; |
| 69 return; |
| 70 } |
| 71 if (static_cast<size_t>( |
| 72 base::WriteFile(golden_screenshot_path, |
| 73 reinterpret_cast<char*>(&(png_data->data()[0])), |
| 74 png_data->size())) != png_data->size()) { |
| 75 LOG(ERROR) << "Can't save screenshot"; |
| 76 } |
| 77 VLOG(0) << "Golden screenshot updated successfully"; |
100 } | 78 } |
101 | 79 |
102 bool ScreenshotTester::SaveImage(const std::string& file_name, | 80 void ScreenshotTester::ReturnScreenshot(PNGFile png_data) { |
103 const base::FilePath& screenshot_dir, | 81 // TODO(elizavetai): rewrite this function so that TakeScreenshot |
104 PNGFile png_data) { | 82 // could return a |PNGFile| -- current screenshot. |
105 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
106 base::FilePath screenshot_path = | 84 screenshot_ = png_data; |
107 screenshot_dir.AppendASCII(test_name_ + "_" + file_name + ".png"); | |
108 if (!png_data) { | |
109 LOG(ERROR) << "Can't take a screenshot"; | |
110 return false; | |
111 } | |
112 if (!base::CreateDirectory(screenshot_path.DirName())) { | |
113 LOG(ERROR) << "Can't create a directory " | |
114 << screenshot_path.DirName().value(); | |
115 return false; | |
116 } | |
117 if (static_cast<size_t>( | |
118 base::WriteFile(screenshot_path, | |
119 reinterpret_cast<char*>(&(png_data->data()[0])), | |
120 png_data->size())) != png_data->size()) { | |
121 LOG(ERROR) << "Can't save screenshot " << file_name; | |
122 return false; | |
123 } | |
124 VLOG(0) << "Screenshot " << file_name << ".png saved successfully to " | |
125 << screenshot_dir.value(); | |
126 return true; | |
127 } | |
128 | |
129 void ScreenshotTester::ReturnScreenshot(const PNGFile& screenshot, | |
130 PNGFile png_data) { | |
131 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
132 screenshot->data() = png_data->data(); | |
133 content::BrowserThread::PostTask( | 85 content::BrowserThread::PostTask( |
134 content::BrowserThread::UI, FROM_HERE, run_loop_quitter_); | 86 content::BrowserThread::UI, FROM_HERE, run_loop_quitter_); |
135 } | 87 } |
136 | 88 |
137 ScreenshotTester::PNGFile ScreenshotTester::TakeScreenshot() { | 89 void ScreenshotTester::TakeScreenshot() { |
138 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
139 aura::Window* primary_window = ash::Shell::GetPrimaryRootWindow(); | 91 aura::Window* primary_window = ash::Shell::GetPrimaryRootWindow(); |
140 gfx::Rect rect = primary_window->bounds(); | 92 gfx::Rect rect = primary_window->bounds(); |
141 PNGFile screenshot = new base::RefCountedBytes; | |
142 ui::GrabWindowSnapshotAsync(primary_window, | 93 ui::GrabWindowSnapshotAsync(primary_window, |
143 rect, | 94 rect, |
144 content::BrowserThread::GetBlockingPool(), | 95 content::BrowserThread::GetBlockingPool(), |
145 base::Bind(&ScreenshotTester::ReturnScreenshot, | 96 base::Bind(&ScreenshotTester::ReturnScreenshot, |
146 weak_factory_.GetWeakPtr(), | 97 weak_factory_.GetWeakPtr())); |
147 screenshot)); | |
148 base::RunLoop run_loop; | 98 base::RunLoop run_loop; |
149 run_loop_quitter_ = run_loop.QuitClosure(); | 99 run_loop_quitter_ = run_loop.QuitClosure(); |
150 run_loop.Run(); | 100 run_loop.Run(); |
151 return screenshot; | |
152 } | |
153 | |
154 ScreenshotTester::PNGFile ScreenshotTester::LoadGoldenScreenshot() { | |
155 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
156 | |
157 base::FilePath screenshot_path = golden_screenshots_dir_.AppendASCII( | |
158 test_name_ + "_golden_screenshot.png"); | |
159 if (!base::PathExists(screenshot_path)) { | |
160 CHECK(false) << "Can't find a golden screenshot for this test"; | |
161 } | |
162 | |
163 size_t golden_screenshot_size; | |
164 base::GetFileSize(screenshot_path, | |
165 reinterpret_cast<int64*>(&golden_screenshot_size)); | |
166 | |
167 if (golden_screenshot_size == -1) { | |
168 CHECK(false) << "Can't get golden screenshot size"; | |
169 } | |
170 PNGFile png_data = new base::RefCountedBytes; | |
171 png_data->data().resize(golden_screenshot_size); | |
172 base::ReadFile(screenshot_path, | |
173 reinterpret_cast<char*>(&(png_data->data()[0])), | |
174 golden_screenshot_size); | |
175 | |
176 return png_data; | |
177 } | |
178 | |
179 void ScreenshotTester::CompareScreenshots(PNGFile model, PNGFile sample) { | |
180 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
181 | |
182 ASSERT_TRUE(model.get()); | |
183 ASSERT_TRUE(sample.get()); | |
184 | |
185 SkBitmap model_bitmap; | |
186 SkBitmap sample_bitmap; | |
187 gfx::PNGCodec::Decode(reinterpret_cast<unsigned char*>(&(model->data()[0])), | |
188 model->data().size(), | |
189 &model_bitmap); | |
190 gfx::PNGCodec::Decode(reinterpret_cast<unsigned char*>(&(sample->data()[0])), | |
191 sample->data().size(), | |
192 &sample_bitmap); | |
193 | |
194 ASSERT_EQ(model_bitmap.config(), sample_bitmap.config()); | |
195 ASSERT_EQ(model_bitmap.width(), sample_bitmap.width()); | |
196 ASSERT_EQ(model_bitmap.height(), sample_bitmap.height()); | |
197 | |
198 bool screenshots_match = true; | |
199 | |
200 SkCanvas diff_canvas(sample_bitmap); | |
201 for (int i = 0; i < model_bitmap.width(); i++) | |
202 for (int j = 0; j < model_bitmap.height(); j++) { | |
203 if (model_bitmap.getColor(i, j) == sample_bitmap.getColor(i, j)) { | |
204 diff_canvas.drawPoint(i, j, SK_ColorWHITE); | |
205 } else { | |
206 screenshots_match = false; | |
207 diff_canvas.drawPoint(i, j, SK_ColorRED); | |
208 } | |
209 } | |
210 | |
211 if (!screenshots_match) { | |
212 CHECK(SaveImage("failed_screenshot", artifacts_dir_, sample)); | |
213 gfx::PNGCodec::EncodeBGRASkBitmap(sample_bitmap, false, &sample->data()); | |
214 CHECK(SaveImage("difference", artifacts_dir_, sample)); | |
215 LOG(ERROR) | |
216 << "Screenshots testing failed. Screenshots differ in some pixels"; | |
217 VLOG(0) << "Current screenshot and diff picture saved to " | |
218 << artifacts_dir_.value(); | |
219 } else { | |
220 VLOG(0) << "Current screenshot matches the golden screenshot. Screenshot " | |
221 "testing passed."; | |
222 } | |
223 ASSERT_TRUE(screenshots_match); | |
224 } | 101 } |
225 | 102 |
226 } // namespace chromeos | 103 } // namespace chromeos |
OLD | NEW |