1 // 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.
5 #include "chrome/browser/ui/ash/screenshot_taker.h"
8 #include "ash/test/ash_test_base.h"
10 #include "base/command_line.h"
11 #include "base/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/message_loop/message_loop.h"
14 #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"
17 #include "chrome/test/base/testing_browser_process.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "chrome/test/base/testing_profile_manager.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/test/test_utils.h"
22 #include "ui/aura/root_window.h"
23 #include "ui/message_center/message_center_switches.h"
25 #if defined(OS_CHROMEOS)
26 #include "chromeos/login/login_state.h"
32 class ScreenshotTakerTest
: public AshTestBase
,
33 public ScreenshotTakerObserver
{
37 screenshot_complete_(false),
38 screenshot_result_(ScreenshotTakerObserver::SCREENSHOT_SUCCESS
) {
41 virtual void SetUp() {
45 virtual void TearDown() {
46 RunAllPendingInMessageLoop();
47 AshTestBase::TearDown();
50 // Overridden from ScreenshotTakerObserver
51 virtual void OnScreenshotCompleted(
52 ScreenshotTakerObserver::Result screenshot_result
,
53 const base::FilePath
& screenshot_path
) OVERRIDE
{
54 screenshot_complete_
= true;
55 screenshot_result_
= screenshot_result
;
56 screenshot_path_
= screenshot_path
;
59 message_loop_runner_
->Quit();
64 // ScreenshotTakerTest is a friend of ScreenshotTaker and therefore
65 // allowed to set the directory, basename and profile.
66 void SetScreenshotDirectoryForTest(
67 ScreenshotTaker
* screenshot_taker
,
68 const base::FilePath
& screenshot_directory
) {
69 screenshot_taker
->SetScreenshotDirectoryForTest(screenshot_directory
);
71 void SetScreenshotBasenameForTest(
72 ScreenshotTaker
* screenshot_taker
,
73 const std::string
& screenshot_basename
) {
74 screenshot_taker
->SetScreenshotBasenameForTest(screenshot_basename
);
76 void SetScreenshotProfileForTest(
77 ScreenshotTaker
* screenshot_taker
,
79 screenshot_taker
->SetScreenshotProfileForTest(profile
);
83 if (screenshot_complete_
)
86 message_loop_runner_
= new content::MessageLoopRunner
;
87 message_loop_runner_
->Run();
88 EXPECT_TRUE(screenshot_complete_
);
92 bool screenshot_complete_
;
93 ScreenshotTakerObserver::Result screenshot_result_
;
94 base::FilePath screenshot_path_
;
95 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
97 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerTest
);
100 TEST_F(ScreenshotTakerTest
, TakeScreenshot
) {
101 #if defined(OS_CHROMEOS)
102 // Note that within the test framework the LoginState object will always
103 // claim that the user did log in.
104 ASSERT_FALSE(chromeos::LoginState::IsInitialized());
105 chromeos::LoginState::Initialize();
107 scoped_ptr
<TestingProfileManager
> profile_manager(
108 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
109 ASSERT_TRUE(profile_manager
->SetUp());
110 TestingProfile
* profile
=
111 profile_manager
->CreateTestingProfile("test_profile");
112 ScreenshotTaker screenshot_taker
;
113 screenshot_taker
.AddObserver(this);
114 base::ScopedTempDir directory
;
115 ASSERT_TRUE(directory
.CreateUniqueTempDir());
116 SetScreenshotDirectoryForTest(&screenshot_taker
, directory
.path());
117 SetScreenshotBasenameForTest(&screenshot_taker
, "Screenshot");
118 SetScreenshotProfileForTest(&screenshot_taker
, profile
);
120 EXPECT_TRUE(screenshot_taker
.CanTakeScreenshot());
122 screenshot_taker
.HandleTakePartialScreenshot(
123 Shell::GetPrimaryRootWindow(), gfx::Rect(0, 0, 100, 100));
125 EXPECT_FALSE(screenshot_taker
.CanTakeScreenshot());
129 #if defined(OS_CHROMEOS)
130 // Screenshot notifications on Windows not yet turned on.
131 EXPECT_TRUE(g_browser_process
->notification_ui_manager()->FindById(
132 std::string("screenshot")) != NULL
);
133 g_browser_process
->notification_ui_manager()->CancelAll();
136 EXPECT_EQ(ScreenshotTakerObserver::SCREENSHOT_SUCCESS
, screenshot_result_
);
138 if (ScreenshotTakerObserver::SCREENSHOT_SUCCESS
== screenshot_result_
)
139 EXPECT_TRUE(base::PathExists(screenshot_path_
));
141 #if defined(OS_CHROMEOS)
142 chromeos::LoginState::Shutdown();