Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(478)

Unified Diff: chrome/browser/chromeos/login/screenshot_tester.cc

Issue 405093003: Taking screenshots while running a test (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Switches restructured again Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/login/screenshot_tester.cc
diff --git a/chrome/browser/chromeos/login/screenshot_tester.cc b/chrome/browser/chromeos/login/screenshot_tester.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8b7b0cfe5994b117038903dab7b9bcca19fa854d
--- /dev/null
+++ b/chrome/browser/chromeos/login/screenshot_tester.cc
@@ -0,0 +1,97 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/chromeos/login/screenshot_tester.h"
+
+#include "ash/shell.h"
+#include "base/base_export.h"
+#include "base/bind_internal.h"
+#include "base/command_line.h"
+#include "base/memory/weak_ptr.h"
+#include "base/prefs/pref_service.h"
+#include "base/run_loop.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/common/pref_names.h"
+#include "chromeos/chromeos_switches.h"
+#include "content/public/browser/browser_thread.h"
+#include "ui/compositor/compositor_switches.h"
+#include "ui/gfx/image/image.h"
+#include "ui/snapshot/snapshot.h"
+
+ScreenshotTester::ScreenshotTester() : weak_factory_(this) {
+}
+
+ScreenshotTester::~ScreenshotTester() {
+}
+
+bool ScreenshotTester::TryInitialize() {
+ CommandLine& command_line = *CommandLine::ForCurrentProcess();
+ if (!command_line.HasSwitch(chromeos::switches::kEnableScreenshotTesting))
+ return false;
+ if (!command_line.HasSwitch(switches::kEnablePixelOutputInTests) ||
+ !command_line.HasSwitch(switches::kUIEnableImplSidePainting)) {
+ // TODO(elizavetai): make turning on --enable-pixel-output-in-tests
+ // and --ui-enable-impl-side-painting automatical.
+ LOG(ERROR) << "--enable-pixel-output-in-tests and "
+ << "--ui-enable-impl-side-painting are required to take "
+ << "screenshots";
+ return false;
+ }
+ if (!command_line.HasSwitch(chromeos::switches::kScreenshotDestinationDir)) {
+ LOG(ERROR) << "No destination for screenshots specified";
+ return false;
+ }
+ screenshot_dest_ = command_line.GetSwitchValuePath(
+ chromeos::switches::kScreenshotDestinationDir);
+ return true;
+}
+
+void ScreenshotTester::Run(const std::string& file_name) {
+ TakeScreenshot();
+ PNGFile current_screenshot = screenshot_;
+ golden_screenshot_path_ = screenshot_dest_.AppendASCII(file_name + ".png");
+ UpdateGoldenScreenshot(current_screenshot);
+}
+
+void ScreenshotTester::UpdateGoldenScreenshot(PNGFile png_data) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+ if (!png_data) {
+ LOG(ERROR) << "Can't take a screenshot";
+ return;
+ }
+ if (!base::CreateDirectory(golden_screenshot_path_.DirName())) {
+ LOG(ERROR) << "Can't create a directory ";
+ return;
+ }
+ if (static_cast<size_t>(
+ base::WriteFile(golden_screenshot_path_,
+ reinterpret_cast<char*>(&(png_data->data()[0])),
+ png_data->size())) != png_data->size()) {
+ LOG(ERROR) << "Can't save screenshot";
+ }
+ VLOG(0) << "Golden screenshot updated successfully";
+}
+
+void ScreenshotTester::ReturnScreenshot(PNGFile png_data) {
+ // TODO(elizavetai): rewrite this function so that TakeScreenshot
+ // could return a |PNGFile| -- current screenshot.
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+ screenshot_ = png_data;
+ content::BrowserThread::PostTask(
+ content::BrowserThread::UI, FROM_HERE, run_loop_quitter_);
+}
+
+void ScreenshotTester::TakeScreenshot() {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+ aura::Window* primary_window = ash::Shell::GetPrimaryRootWindow();
+ gfx::Rect rect = primary_window->bounds();
+ ui::GrabWindowSnapshotAsync(primary_window,
+ rect,
+ content::BrowserThread::GetBlockingPool(),
+ base::Bind(&ScreenshotTester::ReturnScreenshot,
+ weak_factory_.GetWeakPtr()));
+ base::RunLoop run_loop;
+ run_loop_quitter_ = run_loop.QuitClosure();
+ run_loop.Run();
+}

Powered by Google App Engine
This is Rietveld 408576698