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