OLD | NEW |
| (Empty) |
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. | |
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/files/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/window_event_dispatcher.h" | |
22 | |
23 #if defined(OS_CHROMEOS) | |
24 #include "chromeos/login/login_state.h" | |
25 #endif | |
26 | |
27 namespace ash { | |
28 namespace test { | |
29 | |
30 class ScreenshotTakerTest : public AshTestBase, | |
31 public ScreenshotTakerObserver { | |
32 public: | |
33 ScreenshotTakerTest() | |
34 : running_(false), | |
35 screenshot_complete_(false), | |
36 screenshot_result_(ScreenshotTakerObserver::SCREENSHOT_SUCCESS) { | |
37 } | |
38 | |
39 void SetUp() override { AshTestBase::SetUp(); } | |
40 | |
41 void TearDown() override { | |
42 RunAllPendingInMessageLoop(); | |
43 AshTestBase::TearDown(); | |
44 } | |
45 | |
46 // Overridden from ScreenshotTakerObserver | |
47 void OnScreenshotCompleted(ScreenshotTakerObserver::Result screenshot_result, | |
48 const base::FilePath& screenshot_path) override { | |
49 screenshot_complete_ = true; | |
50 screenshot_result_ = screenshot_result; | |
51 screenshot_path_ = screenshot_path; | |
52 if (!running_) | |
53 return; | |
54 message_loop_runner_->Quit(); | |
55 running_ = false; | |
56 } | |
57 | |
58 protected: | |
59 // ScreenshotTakerTest is a friend of ScreenshotTaker and therefore | |
60 // allowed to set the directory, basename and profile. | |
61 void SetScreenshotDirectoryForTest( | |
62 ScreenshotTaker* screenshot_taker, | |
63 const base::FilePath& screenshot_directory) { | |
64 screenshot_taker->SetScreenshotDirectoryForTest(screenshot_directory); | |
65 } | |
66 void SetScreenshotBasenameForTest( | |
67 ScreenshotTaker* screenshot_taker, | |
68 const std::string& screenshot_basename) { | |
69 screenshot_taker->SetScreenshotBasenameForTest(screenshot_basename); | |
70 } | |
71 void SetScreenshotProfileForTest( | |
72 ScreenshotTaker* screenshot_taker, | |
73 Profile* profile) { | |
74 screenshot_taker->SetScreenshotProfileForTest(profile); | |
75 } | |
76 | |
77 void Wait() { | |
78 if (screenshot_complete_) | |
79 return; | |
80 running_ = true; | |
81 message_loop_runner_ = new content::MessageLoopRunner; | |
82 message_loop_runner_->Run(); | |
83 EXPECT_TRUE(screenshot_complete_); | |
84 } | |
85 | |
86 bool running_; | |
87 bool screenshot_complete_; | |
88 ScreenshotTakerObserver::Result screenshot_result_; | |
89 base::FilePath screenshot_path_; | |
90 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | |
91 | |
92 private: | |
93 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerTest); | |
94 }; | |
95 | |
96 TEST_F(ScreenshotTakerTest, TakeScreenshot) { | |
97 #if defined(OS_CHROMEOS) | |
98 // Note that within the test framework the LoginState object will always | |
99 // claim that the user did log in. | |
100 ASSERT_FALSE(chromeos::LoginState::IsInitialized()); | |
101 chromeos::LoginState::Initialize(); | |
102 #endif | |
103 scoped_ptr<TestingProfileManager> profile_manager( | |
104 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); | |
105 ASSERT_TRUE(profile_manager->SetUp()); | |
106 TestingProfile* profile = | |
107 profile_manager->CreateTestingProfile("test_profile"); | |
108 ScreenshotTaker screenshot_taker; | |
109 screenshot_taker.AddObserver(this); | |
110 base::ScopedTempDir directory; | |
111 ASSERT_TRUE(directory.CreateUniqueTempDir()); | |
112 SetScreenshotDirectoryForTest(&screenshot_taker, directory.path()); | |
113 SetScreenshotBasenameForTest(&screenshot_taker, "Screenshot"); | |
114 SetScreenshotProfileForTest(&screenshot_taker, profile); | |
115 | |
116 EXPECT_TRUE(screenshot_taker.CanTakeScreenshot()); | |
117 | |
118 screenshot_taker.HandleTakePartialScreenshot( | |
119 Shell::GetPrimaryRootWindow(), gfx::Rect(0, 0, 100, 100)); | |
120 | |
121 EXPECT_FALSE(screenshot_taker.CanTakeScreenshot()); | |
122 | |
123 Wait(); | |
124 | |
125 #if defined(OS_CHROMEOS) | |
126 // Screenshot notifications on Windows not yet turned on. | |
127 EXPECT_TRUE(g_browser_process->notification_ui_manager()->FindById( | |
128 std::string("screenshot"), | |
129 NotificationUIManager::GetProfileID(profile)) != NULL); | |
130 g_browser_process->notification_ui_manager()->CancelAll(); | |
131 #endif | |
132 | |
133 EXPECT_EQ(ScreenshotTakerObserver::SCREENSHOT_SUCCESS, screenshot_result_); | |
134 | |
135 if (ScreenshotTakerObserver::SCREENSHOT_SUCCESS == screenshot_result_) | |
136 EXPECT_TRUE(base::PathExists(screenshot_path_)); | |
137 | |
138 #if defined(OS_CHROMEOS) | |
139 chromeos::LoginState::Shutdown(); | |
140 #endif | |
141 } | |
142 | |
143 } // namespace test | |
144 } // namespace ash | |
OLD | NEW |