NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / ash / screenshot_taker_unittest.cc
blob1d33036956bad38a621cbf2ba4861cfa900c292a
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"
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"
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/test/test_utils.h"
21 #include "ui/aura/root_window.h"
22 #include "ui/message_center/message_center_switches.h"
24 #if defined(OS_CHROMEOS)
25 #include "chromeos/login/login_state.h"
26 #endif
28 namespace ash {
29 namespace test {
31 class ScreenshotTakerTest : public AshTestBase,
32 public ScreenshotTakerObserver {
33 public:
34 ScreenshotTakerTest()
35 : running_(false),
36 screenshot_complete_(false),
37 screenshot_result_(ScreenshotTakerObserver::SCREENSHOT_SUCCESS) {
40 virtual void SetUp() {
41 AshTestBase::SetUp();
44 virtual void TearDown() {
45 RunAllPendingInMessageLoop();
46 AshTestBase::TearDown();
49 // Overridden from ScreenshotTakerObserver
50 virtual void OnScreenshotCompleted(
51 ScreenshotTakerObserver::Result screenshot_result,
52 const base::FilePath& screenshot_path) OVERRIDE {
53 screenshot_complete_ = true;
54 screenshot_result_ = screenshot_result;
55 screenshot_path_ = screenshot_path;
56 if (!running_)
57 return;
58 message_loop_runner_->Quit();
59 running_ = false;
62 protected:
63 // ScreenshotTakerTest is a friend of ScreenshotTaker and therefore
64 // allowed to set the directory, basename and profile.
65 void SetScreenshotDirectoryForTest(
66 ScreenshotTaker* screenshot_taker,
67 const base::FilePath& screenshot_directory) {
68 screenshot_taker->SetScreenshotDirectoryForTest(screenshot_directory);
70 void SetScreenshotBasenameForTest(
71 ScreenshotTaker* screenshot_taker,
72 const std::string& screenshot_basename) {
73 screenshot_taker->SetScreenshotBasenameForTest(screenshot_basename);
75 void SetScreenshotProfileForTest(
76 ScreenshotTaker* screenshot_taker,
77 Profile* profile) {
78 screenshot_taker->SetScreenshotProfileForTest(profile);
81 void Wait() {
82 if (screenshot_complete_)
83 return;
84 running_ = true;
85 message_loop_runner_ = new content::MessageLoopRunner;
86 message_loop_runner_->Run();
87 EXPECT_TRUE(screenshot_complete_);
90 bool running_;
91 bool screenshot_complete_;
92 ScreenshotTakerObserver::Result screenshot_result_;
93 base::FilePath screenshot_path_;
94 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
96 private:
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();
106 #endif
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());
127 Wait();
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();
134 #endif
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();
143 #endif
146 } // namespace test
147 } // namespace ash