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_testing_mixin.h"
6
7#include "chrome/browser/browser_process.h"
8#include "chrome/browser/chrome_notification_types.h"
9#include "content/public/browser/browser_thread.h"
10#include "content/public/browser/notification_service.h"
11#include "content/public/browser/notification_types.h"
12#include "ui/compositor/compositor_switches.h"
13
14namespace chromeos {
15
16ScreenshotTestingMixin::ScreenshotTestingMixin() {
17}
18
19ScreenshotTestingMixin::~ScreenshotTestingMixin() {
20}
21
22void ScreenshotTestingMixin::SetUpInProcessBrowserTestFixture() {
23  enable_test_screenshots_ = screenshot_tester_.TryInitialize();
24}
25
26void ScreenshotTestingMixin::SetUpCommandLine(base::CommandLine* command_line) {
27  if (enable_test_screenshots_) {
28    command_line->AppendSwitch(switches::kEnablePixelOutputInTests);
29    command_line->AppendSwitch(switches::kUIEnableImplSidePainting);
30  }
31}
32
33void ScreenshotTestingMixin::RunScreenshotTesting(
34    const std::string& test_name) {
35  if (enable_test_screenshots_) {
36    SynchronizeAnimationLoadWithCompositor();
37    screenshot_tester_.Run(test_name);
38  }
39}
40
41// Current implementation is a mockup.
42// It simply waits for 5 seconds, assuming that this time is enough for
43// animation to load completely.
44// TODO(elizavetai): Replace this temporary hack with getting a
45// valid notification from compositor.
46void ScreenshotTestingMixin::SynchronizeAnimationLoadWithCompositor() {
47  base::RunLoop waiter;
48  animation_waiter_quitter_ = waiter.QuitClosure();
49  timer_.Start(FROM_HERE,
50               base::TimeDelta::FromSeconds(5),
51               this,
52               &ScreenshotTestingMixin::HandleAnimationLoad);
53  waiter.Run();
54}
55
56void ScreenshotTestingMixin::HandleAnimationLoad() {
57  timer_.Stop();
58  content::BrowserThread::PostTask(
59      content::BrowserThread::UI, FROM_HERE, animation_waiter_quitter_);
60}
61
62}  // namespace chromeos
63