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

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: 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
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/memory/weak_ptr.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/run_loop.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/chromeos/login/login_manager_test.h"
15 #include "chrome/browser/chromeos/login/startup_utils.h"
16 #include "chrome/browser/chromeos/login/test/oobe_screen_waiter.h"
17 #include "chrome/browser/ui/ash/screenshot_taker.h"
18 #include "chrome/common/pref_names.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
22 #include "ui/compositor/compositor_switches.h"
23 #include "ui/gfx/image/image.h"
24 #include "ui/snapshot/snapshot.h"
25
26 ScreenshotTester::ScreenshotTester() : weak_factory_(this) {
27 }
28
29 ScreenshotTester::~ScreenshotTester() {
30 }
31
32 bool ScreenshotTester::TryInitialize() {
33 CommandLine& command_line = *CommandLine::ForCurrentProcess();
34 if (!command_line.HasSwitch(kEnableTestScreenshots)) {
ygorshenin1 2014/07/21 09:48:22 nit: curly braces are not needed here.
Lisa Ignatyeva 2014/07/21 13:41:03 Done.
35 return false;
36 }
37 bool screenshot_dest_enabled_ = command_line.HasSwitch(kScreenshotDest);
ygorshenin1 2014/07/21 09:48:22 Local variables must be declared without underscor
Lisa Ignatyeva 2014/07/21 13:41:03 Done.
38 if (!command_line.HasSwitch(switches::kEnablePixelOutputInTests) ||
39 !command_line.HasSwitch(switches::kUIEnableImplSidePainting)) {
40 // TODO(elizavetai): make turning on --enable-pixel-output-in-tests
41 // and --ui-enalbe-impl-side-painting automatical.
42 LOG(ERROR) << "--enable-pixel-output-in-tests and "
43 << "--ui-enable-impl-side-painting are required to take "
44 << "screenshots. Screenshots will not be taken";
45 return false;
46 }
47 if (screenshot_dest_enabled_) {
ygorshenin1 2014/07/21 09:48:22 As you're always use both flags: "--enable-test-sc
Denis Kuznetsov (DE-MUC) 2014/07/21 12:13:16 Later there will be a stage when we compare screen
Lisa Ignatyeva 2014/07/21 13:41:03 Done.
Lisa Ignatyeva 2014/07/21 13:41:03 Done.
48 screenshot_dest_ = command_line.GetSwitchValuePath(kScreenshotDest);
49 } else {
50 LOG(ERROR)
51 << "Using --enable-test-screenshots without setting "
52 "screenshot destination"
53 << " via --screenshot-dest=YOUR_PATH is not supported. Screenshots"
54 << " will not be taken";
55 return false;
56 }
57 update_golden_screenshot_ = command_line.HasSwitch(kUpdateGoldenScreenshots);
58 return true;
59 }
60
61 void ScreenshotTester::Run(const std::string& file_name) {
62 TakeScreenshot();
63 PNGFile current_screenshot = screenshot;
64 golden_screenshot_path = screenshot_dest_.AppendASCII(file_name + ".png");
65 if (update_golden_screenshot_) {
66 UpdateGoldenScreenshot(current_screenshot);
67 } else {
68 // TODO(elizavetai): implement loading screenshots and comparing them
69 LOG(ERROR)
70 << "Comparing screenshots is not supported yet. Nothing will be done";
71 }
72 }
73
74 void ScreenshotTester::UpdateGoldenScreenshot(PNGFile png_data) {
75 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
76 if (!png_data) {
77 LOG(ERROR) << "Can't take a screenshot";
78 return;
79 }
80 if (!base::CreateDirectory(golden_screenshot_path.DirName())) {
81 LOG(ERROR) << "Can't create a directory ";
82 return;
83 }
84 if (static_cast<size_t>(
85 base::WriteFile(golden_screenshot_path,
86 reinterpret_cast<char*>(&(png_data->data()[0])),
87 png_data->size())) != png_data->size()) {
88 LOG(ERROR) << "Can't save screenshot";
89 }
90 VLOG(0) << "Golden screenshot updated successfully";
91 }
92
93 void ScreenshotTester::ReturnScreenshot(PNGFile png_data) {
94 // TODO(elizavetai): rewrite this function so that TakeScreenshot
95 // could return a |PNGFile| -- current screenshot.
96 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
97 screenshot = png_data;
98 content::BrowserThread::PostTask(
99 content::BrowserThread::UI, FROM_HERE, run_loop_quitter_);
100 }
101
102 void ScreenshotTester::TakeScreenshot() {
103 aura::Window* primary_window = ash::Shell::GetPrimaryRootWindow();
104 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
ygorshenin1 2014/07/21 09:48:23 nit: move DCHECK to the beginning of the method.
Denis Kuznetsov (DE-MUC) 2014/07/21 12:13:16 Acknowledged.
Lisa Ignatyeva 2014/07/21 13:41:03 Done.
Lisa Ignatyeva 2014/07/21 13:41:03 Done.
105 gfx::Rect rect = primary_window->bounds();
106 PNGFile screenshot;
ygorshenin1 2014/07/21 09:48:22 This variable is unused.
Lisa Ignatyeva 2014/07/21 13:41:03 Done.
107 ui::GrabWindowSnapshotAsync(primary_window,
108 rect,
109 content::BrowserThread::GetBlockingPool(),
110 base::Bind(&ScreenshotTester::ReturnScreenshot,
111 weak_factory_.GetWeakPtr()));
112 base::RunLoop run_loop;
113 run_loop_quitter_ = run_loop.QuitClosure();
114 run_loop.Run();
115 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698