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