Chromium Code Reviews| 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..10d23da817c61e477eeea3d58bff3ba935ecda37 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/login/screenshot_tester.cc |
| @@ -0,0 +1,115 @@ |
| +// 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/memory/weak_ptr.h" |
| +#include "base/prefs/pref_service.h" |
| +#include "base/run_loop.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/chromeos/login/login_manager_test.h" |
| +#include "chrome/browser/chromeos/login/startup_utils.h" |
| +#include "chrome/browser/chromeos/login/test/oobe_screen_waiter.h" |
| +#include "chrome/browser/ui/ash/screenshot_taker.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/notification_observer.h" |
| +#include "content/public/browser/notification_registrar.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(kEnableTestScreenshots)) { |
|
ygorshenin1
2014/07/21 09:48:22
nit: curly braces are not needed here.
Lisa Ignatyeva
2014/07/21 13:41:03
Done.
|
| + return false; |
| + } |
| + 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.
|
| + if (!command_line.HasSwitch(switches::kEnablePixelOutputInTests) || |
| + !command_line.HasSwitch(switches::kUIEnableImplSidePainting)) { |
| + // TODO(elizavetai): make turning on --enable-pixel-output-in-tests |
| + // and --ui-enalbe-impl-side-painting automatical. |
| + LOG(ERROR) << "--enable-pixel-output-in-tests and " |
| + << "--ui-enable-impl-side-painting are required to take " |
| + << "screenshots. Screenshots will not be taken"; |
| + return false; |
| + } |
| + 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.
|
| + screenshot_dest_ = command_line.GetSwitchValuePath(kScreenshotDest); |
| + } else { |
| + LOG(ERROR) |
| + << "Using --enable-test-screenshots without setting " |
| + "screenshot destination" |
| + << " via --screenshot-dest=YOUR_PATH is not supported. Screenshots" |
| + << " will not be taken"; |
| + return false; |
| + } |
| + update_golden_screenshot_ = command_line.HasSwitch(kUpdateGoldenScreenshots); |
| + return true; |
| +} |
| + |
| +void ScreenshotTester::Run(const std::string& file_name) { |
| + TakeScreenshot(); |
| + PNGFile current_screenshot = screenshot; |
| + golden_screenshot_path = screenshot_dest_.AppendASCII(file_name + ".png"); |
| + if (update_golden_screenshot_) { |
| + UpdateGoldenScreenshot(current_screenshot); |
| + } else { |
| + // TODO(elizavetai): implement loading screenshots and comparing them |
| + LOG(ERROR) |
| + << "Comparing screenshots is not supported yet. Nothing will be done"; |
| + } |
| +} |
| + |
| +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() { |
| + aura::Window* primary_window = ash::Shell::GetPrimaryRootWindow(); |
| + 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.
|
| + gfx::Rect rect = primary_window->bounds(); |
| + PNGFile screenshot; |
|
ygorshenin1
2014/07/21 09:48:22
This variable is unused.
Lisa Ignatyeva
2014/07/21 13:41:03
Done.
|
| + 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(); |
| +} |