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

Side by Side 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: last DCHECK replaced with DCHECK_CURRENTLY_ON 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 unified diff | Download patch
« no previous file with comments | « chrome/browser/chromeos/login/screenshot_tester.h ('k') | chrome/chrome_tests.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/chromeos/login/screenshot_tester.h"
6
7 #include "ash/shell.h"
8 #include "base/base_export.h"
9 #include "base/bind_internal.h"
10 #include "base/command_line.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/run_loop.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/common/pref_names.h"
16 #include "chromeos/chromeos_switches.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "ui/compositor/compositor_switches.h"
19 #include "ui/gfx/image/image.h"
20 #include "ui/snapshot/snapshot.h"
21
22 namespace chromeos {
23
24 ScreenshotTester::ScreenshotTester() : weak_factory_(this) {
25 }
26
27 ScreenshotTester::~ScreenshotTester() {
28 }
29
30 bool ScreenshotTester::TryInitialize() {
31 CommandLine& command_line = *CommandLine::ForCurrentProcess();
32 if (!command_line.HasSwitch(switches::kEnableScreenshotTesting))
33 return false;
34 if (!command_line.HasSwitch(::switches::kEnablePixelOutputInTests) ||
35 !command_line.HasSwitch(::switches::kUIEnableImplSidePainting)) {
36 // TODO(elizavetai): make turning on --enable-pixel-output-in-tests
37 // and --ui-enable-impl-side-painting automatical.
38 LOG(ERROR) << "--enable-pixel-output-in-tests and "
39 << "--ui-enable-impl-side-painting are required to take "
40 << "screenshots";
41 return false;
42 }
43 if (!command_line.HasSwitch(switches::kScreenshotDestinationDir)) {
44 LOG(ERROR) << "No destination for screenshots specified";
45 return false;
46 }
47 screenshot_dest_ = command_line.GetSwitchValuePath(
48 chromeos::switches::kScreenshotDestinationDir);
49 return true;
50 }
51
52 void ScreenshotTester::Run(const std::string& file_name) {
53 TakeScreenshot();
54 PNGFile current_screenshot = screenshot_;
55 UpdateGoldenScreenshot(file_name, current_screenshot);
56 }
57
58 void ScreenshotTester::UpdateGoldenScreenshot(const std::string& file_name,
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";
78 }
79
80 void ScreenshotTester::ReturnScreenshot(PNGFile png_data) {
81 // TODO(elizavetai): rewrite this function so that TakeScreenshot
82 // could return a |PNGFile| -- current screenshot.
83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
84 screenshot_ = png_data;
85 content::BrowserThread::PostTask(
86 content::BrowserThread::UI, FROM_HERE, run_loop_quitter_);
87 }
88
89 void ScreenshotTester::TakeScreenshot() {
90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
91 aura::Window* primary_window = ash::Shell::GetPrimaryRootWindow();
92 gfx::Rect rect = primary_window->bounds();
93 ui::GrabWindowSnapshotAsync(primary_window,
94 rect,
95 content::BrowserThread::GetBlockingPool(),
96 base::Bind(&ScreenshotTester::ReturnScreenshot,
97 weak_factory_.GetWeakPtr()));
98 base::RunLoop run_loop;
99 run_loop_quitter_ = run_loop.QuitClosure();
100 run_loop.Run();
101 }
102
103 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/login/screenshot_tester.h ('k') | chrome/chrome_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698