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/chrome_screenshot_grabber.h"
7 #include "ash/accelerators/accelerator_controller.h"
9 #include "ash/test/ash_test_base.h"
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/files/file_util.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "base/message_loop/message_loop.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/notifications/notification_ui_manager.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chrome/test/base/testing_browser_process.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "chrome/test/base/testing_profile_manager.h"
22 #include "content/public/test/test_utils.h"
23 #include "ui/aura/window_event_dispatcher.h"
24 #include "ui/snapshot/screenshot_grabber.h"
26 #if defined(OS_CHROMEOS)
27 #include "chromeos/login/login_state.h"
33 class ChromeScreenshotGrabberTest
: public AshTestBase
,
34 public ui::ScreenshotGrabberObserver
{
36 ChromeScreenshotGrabberTest()
39 screenshot_complete_(false),
40 screenshot_result_(ScreenshotGrabberObserver::SCREENSHOT_SUCCESS
) {}
42 void SetUp() override
{
44 chrome_screenshot_grabber_
.reset(new ChromeScreenshotGrabber
);
45 profile_manager_
.reset(
46 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
47 CHECK(profile_manager_
->SetUp());
48 profile_
= profile_manager_
->CreateTestingProfile("test_profile");
49 chrome_screenshot_grabber_
->SetProfileForTest(profile_
);
50 screenshot_grabber()->AddObserver(this);
53 void TearDown() override
{
54 RunAllPendingInMessageLoop();
55 screenshot_grabber()->RemoveObserver(this);
56 chrome_screenshot_grabber_
.reset();
57 profile_manager_
.reset();
58 AshTestBase::TearDown();
61 // Overridden from ui::ScreenshotGrabberObserver
62 void OnScreenshotCompleted(
63 ScreenshotGrabberObserver::Result screenshot_result
,
64 const base::FilePath
& screenshot_path
) override
{
65 screenshot_complete_
= true;
66 screenshot_result_
= screenshot_result
;
67 screenshot_path_
= screenshot_path
;
70 message_loop_runner_
->Quit();
76 if (screenshot_complete_
)
79 message_loop_runner_
= new content::MessageLoopRunner
;
80 message_loop_runner_
->Run();
81 EXPECT_TRUE(screenshot_complete_
);
84 ChromeScreenshotGrabber
* chrome_screenshot_grabber() {
85 return chrome_screenshot_grabber_
.get();
88 ui::ScreenshotGrabber
* screenshot_grabber() {
89 return chrome_screenshot_grabber_
->screenshot_grabber_
.get();
92 scoped_ptr
<TestingProfileManager
> profile_manager_
;
93 TestingProfile
* profile_
;
95 bool screenshot_complete_
;
96 ScreenshotGrabberObserver::Result screenshot_result_
;
97 scoped_ptr
<ChromeScreenshotGrabber
> chrome_screenshot_grabber_
;
98 base::FilePath screenshot_path_
;
99 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
102 DISALLOW_COPY_AND_ASSIGN(ChromeScreenshotGrabberTest
);
105 TEST_F(ChromeScreenshotGrabberTest
, TakeScreenshot
) {
106 #if defined(OS_CHROMEOS)
107 // Note that within the test framework the LoginState object will always
108 // claim that the user did log in.
109 ASSERT_FALSE(chromeos::LoginState::IsInitialized());
110 chromeos::LoginState::Initialize();
112 base::ScopedTempDir directory
;
113 ASSERT_TRUE(directory
.CreateUniqueTempDir());
114 EXPECT_TRUE(chrome_screenshot_grabber()->CanTakeScreenshot());
116 screenshot_grabber()->TakeScreenshot(
117 Shell::GetPrimaryRootWindow(), gfx::Rect(0, 0, 100, 100),
118 directory
.path().AppendASCII("Screenshot.png"));
120 EXPECT_FALSE(screenshot_grabber()->CanTakeScreenshot());
124 #if defined(OS_CHROMEOS)
125 // Screenshot notifications on Windows not yet turned on.
126 EXPECT_TRUE(g_browser_process
->notification_ui_manager()->FindById(
127 std::string("screenshot"),
128 NotificationUIManager::GetProfileID(profile_
)) != NULL
);
129 g_browser_process
->notification_ui_manager()->CancelAll();
132 EXPECT_EQ(ScreenshotGrabberObserver::SCREENSHOT_SUCCESS
, screenshot_result_
);
134 if (ScreenshotGrabberObserver::SCREENSHOT_SUCCESS
== screenshot_result_
)
135 EXPECT_TRUE(base::PathExists(screenshot_path_
));
137 #if defined(OS_CHROMEOS)
138 chromeos::LoginState::Shutdown();