OLD | NEW |
(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 ScreenshotTester::ScreenshotTester() : weak_factory_(this) { |
| 23 } |
| 24 |
| 25 ScreenshotTester::~ScreenshotTester() { |
| 26 } |
| 27 |
| 28 bool ScreenshotTester::TryInitialize() { |
| 29 CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 30 if (!command_line.HasSwitch(chromeos::switches::kEnableScreenshotTesting)) |
| 31 return false; |
| 32 if (!command_line.HasSwitch(switches::kEnablePixelOutputInTests) || |
| 33 !command_line.HasSwitch(switches::kUIEnableImplSidePainting)) { |
| 34 // TODO(elizavetai): make turning on --enable-pixel-output-in-tests |
| 35 // and --ui-enable-impl-side-painting automatical. |
| 36 LOG(ERROR) << "--enable-pixel-output-in-tests and " |
| 37 << "--ui-enable-impl-side-painting are required to take " |
| 38 << "screenshots"; |
| 39 return false; |
| 40 } |
| 41 if (!command_line.HasSwitch(chromeos::switches::kScreenshotDestinationDir)) { |
| 42 LOG(ERROR) << "No destination for screenshots specified"; |
| 43 return false; |
| 44 } |
| 45 screenshot_dest_ = command_line.GetSwitchValuePath( |
| 46 chromeos::switches::kScreenshotDestinationDir); |
| 47 return true; |
| 48 } |
| 49 |
| 50 void ScreenshotTester::Run(const std::string& file_name) { |
| 51 TakeScreenshot(); |
| 52 PNGFile current_screenshot = screenshot_; |
| 53 golden_screenshot_path_ = screenshot_dest_.AppendASCII(file_name + ".png"); |
| 54 UpdateGoldenScreenshot(current_screenshot); |
| 55 } |
| 56 |
| 57 void ScreenshotTester::UpdateGoldenScreenshot(PNGFile png_data) { |
| 58 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 59 if (!png_data) { |
| 60 LOG(ERROR) << "Can't take a screenshot"; |
| 61 return; |
| 62 } |
| 63 if (!base::CreateDirectory(golden_screenshot_path_.DirName())) { |
| 64 LOG(ERROR) << "Can't create a directory "; |
| 65 return; |
| 66 } |
| 67 if (static_cast<size_t>( |
| 68 base::WriteFile(golden_screenshot_path_, |
| 69 reinterpret_cast<char*>(&(png_data->data()[0])), |
| 70 png_data->size())) != png_data->size()) { |
| 71 LOG(ERROR) << "Can't save screenshot"; |
| 72 } |
| 73 VLOG(0) << "Golden screenshot updated successfully"; |
| 74 } |
| 75 |
| 76 void ScreenshotTester::ReturnScreenshot(PNGFile png_data) { |
| 77 // TODO(elizavetai): rewrite this function so that TakeScreenshot |
| 78 // could return a |PNGFile| -- current screenshot. |
| 79 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 80 screenshot_ = png_data; |
| 81 content::BrowserThread::PostTask( |
| 82 content::BrowserThread::UI, FROM_HERE, run_loop_quitter_); |
| 83 } |
| 84 |
| 85 void ScreenshotTester::TakeScreenshot() { |
| 86 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 87 aura::Window* primary_window = ash::Shell::GetPrimaryRootWindow(); |
| 88 gfx::Rect rect = primary_window->bounds(); |
| 89 ui::GrabWindowSnapshotAsync(primary_window, |
| 90 rect, |
| 91 content::BrowserThread::GetBlockingPool(), |
| 92 base::Bind(&ScreenshotTester::ReturnScreenshot, |
| 93 weak_factory_.GetWeakPtr())); |
| 94 base::RunLoop run_loop; |
| 95 run_loop_quitter_ = run_loop.QuitClosure(); |
| 96 run_loop.Run(); |
| 97 } |
OLD | NEW |