blob: a35339af4ccfcc8faca9782b3691c5233a461288 [file] [log] [blame]
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001// Copyright 2013 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/ui/ash/screenshot_taker.h"
6
7#include "ash/shell.h"
8#include "ash/test/ash_test_base.h"
9#include "base/bind.h"
10#include "base/command_line.h"
11#include "base/file_util.h"
12#include "base/files/scoped_temp_dir.h"
Ben Murdoch9ab55632013-07-18 11:57:30 +010013#include "base/message_loop/message_loop.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010014#include "chrome/browser/browser_process.h"
15#include "chrome/browser/notifications/notification_ui_manager.h"
16#include "chrome/test/base/in_process_browser_test.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010017#include "chrome/test/base/testing_browser_process.h"
18#include "chrome/test/base/testing_profile.h"
Ben Murdochbb1529c2013-08-08 10:24:53 +010019#include "chrome/test/base/testing_profile_manager.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010020#include "content/public/browser/browser_thread.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010021#include "content/public/test/test_utils.h"
22#include "ui/aura/root_window.h"
23#include "ui/message_center/message_center_switches.h"
24
25namespace ash {
26namespace test {
27
28class ScreenshotTakerTest : public AshTestBase,
29 public ScreenshotTakerObserver {
30 public:
31 ScreenshotTakerTest()
Ben Murdocha3f7b4e2013-07-24 10:36:34 +010032 : running_(false),
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010033 screenshot_complete_(false),
34 screenshot_result_(ScreenshotTakerObserver::SCREENSHOT_SUCCESS) {
35 }
36
37 virtual void SetUp() {
38 AshTestBase::SetUp();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010039 }
40
41 virtual void TearDown() {
42 RunAllPendingInMessageLoop();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010043 AshTestBase::TearDown();
44 }
45
46 // Overridden from ScreenshotTakerObserver
47 virtual void OnScreenshotCompleted(
48 ScreenshotTakerObserver::Result screenshot_result,
49 const base::FilePath& screenshot_path) OVERRIDE {
50 screenshot_complete_ = true;
51 screenshot_result_ = screenshot_result;
52 screenshot_path_ = screenshot_path;
53 if (!running_)
54 return;
55 message_loop_runner_->Quit();
56 running_ = false;
57 }
58
59 protected:
60 // ScreenshotTakerTest is a friend of ScreenshotTaker and therefore
61 // allowed to set the directory, basename and profile.
62 void SetScreenshotDirectoryForTest(
63 ScreenshotTaker* screenshot_taker,
64 const base::FilePath& screenshot_directory) {
65 screenshot_taker->SetScreenshotDirectoryForTest(screenshot_directory);
66 }
67 void SetScreenshotBasenameForTest(
68 ScreenshotTaker* screenshot_taker,
69 const std::string& screenshot_basename) {
70 screenshot_taker->SetScreenshotBasenameForTest(screenshot_basename);
71 }
72 void SetScreenshotProfileForTest(
73 ScreenshotTaker* screenshot_taker,
74 Profile* profile) {
75 screenshot_taker->SetScreenshotProfileForTest(profile);
76 }
77
78 void Wait() {
79 if (screenshot_complete_)
80 return;
81 running_ = true;
82 message_loop_runner_ = new content::MessageLoopRunner;
83 message_loop_runner_->Run();
84 EXPECT_TRUE(screenshot_complete_);
85 }
86
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010087 bool running_;
88 bool screenshot_complete_;
89 ScreenshotTakerObserver::Result screenshot_result_;
90 base::FilePath screenshot_path_;
91 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
92
93 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerTest);
94};
95
96TEST_F(ScreenshotTakerTest, TakeScreenshot) {
Ben Murdochbb1529c2013-08-08 10:24:53 +010097 scoped_ptr<TestingProfileManager> profile_manager(
98 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
99 ASSERT_TRUE(profile_manager->SetUp());
100 TestingProfile* profile =
101 profile_manager->CreateTestingProfile("test_profile");
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100102 ScreenshotTaker screenshot_taker;
103 screenshot_taker.AddObserver(this);
104 base::ScopedTempDir directory;
105 ASSERT_TRUE(directory.CreateUniqueTempDir());
106 SetScreenshotDirectoryForTest(&screenshot_taker, directory.path());
107 SetScreenshotBasenameForTest(&screenshot_taker, "Screenshot");
Ben Murdochbb1529c2013-08-08 10:24:53 +0100108 SetScreenshotProfileForTest(&screenshot_taker, profile);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100109
110 EXPECT_TRUE(screenshot_taker.CanTakeScreenshot());
111
112 screenshot_taker.HandleTakePartialScreenshot(
113 Shell::GetPrimaryRootWindow(), gfx::Rect(0, 0, 100, 100));
114
115 EXPECT_FALSE(screenshot_taker.CanTakeScreenshot());
116
117 Wait();
118
119#if defined(OS_CHROMEOS)
120 // Screenshot notifications on Windows not yet turned on.
Ben Murdoch558790d2013-07-30 15:19:42 +0100121 EXPECT_TRUE(g_browser_process->notification_ui_manager()->FindById(
122 std::string("screenshot")) != NULL);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100123 g_browser_process->notification_ui_manager()->CancelAll();
124#endif
125
126 EXPECT_EQ(ScreenshotTakerObserver::SCREENSHOT_SUCCESS, screenshot_result_);
127
128 if (ScreenshotTakerObserver::SCREENSHOT_SUCCESS == screenshot_result_)
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100129 EXPECT_TRUE(base::PathExists(screenshot_path_));
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100130}
131
132} // namespace test
133} // namespace ash