| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/ash/screenshot_taker.h" | 5 #include "chrome/browser/ui/ash/chrome_screenshot_taker.h" |
| 6 | 6 |
| 7 #include "ash/accelerators/accelerator_controller.h" |
| 7 #include "ash/shell.h" | 8 #include "ash/shell.h" |
| 8 #include "ash/test/ash_test_base.h" | 9 #include "ash/test/ash_test_base.h" |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 11 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 14 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/notifications/notification_ui_manager.h" | 16 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 17 #include "chrome/browser/profiles/profile_manager.h" |
| 16 #include "chrome/test/base/in_process_browser_test.h" | 18 #include "chrome/test/base/in_process_browser_test.h" |
| 17 #include "chrome/test/base/testing_browser_process.h" | 19 #include "chrome/test/base/testing_browser_process.h" |
| 18 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
| 19 #include "chrome/test/base/testing_profile_manager.h" | 21 #include "chrome/test/base/testing_profile_manager.h" |
| 20 #include "content/public/test/test_utils.h" | 22 #include "content/public/test/test_utils.h" |
| 21 #include "ui/aura/window_event_dispatcher.h" | 23 #include "ui/aura/window_event_dispatcher.h" |
| 24 #include "ui/snapshot/screenshot_taker.h" |
| 22 | 25 |
| 23 #if defined(OS_CHROMEOS) | 26 #if defined(OS_CHROMEOS) |
| 24 #include "chromeos/login/login_state.h" | 27 #include "chromeos/login/login_state.h" |
| 25 #endif | 28 #endif |
| 26 | 29 |
| 27 namespace ash { | 30 namespace ash { |
| 28 namespace test { | 31 namespace test { |
| 29 | 32 |
| 30 class ScreenshotTakerTest : public AshTestBase, | 33 namespace { |
| 31 public ScreenshotTakerObserver { | 34 |
| 35 class TestChromeScreenshotTaker : public ChromeScreenshotTaker { |
| 32 public: | 36 public: |
| 33 ScreenshotTakerTest() | 37 TestChromeScreenshotTaker() |
| 38 : profile_manager_( |
| 39 new TestingProfileManager(TestingBrowserProcess::GetGlobal())) { |
| 40 CHECK(profile_manager_->SetUp()); |
| 41 profile_ = profile_manager_->CreateTestingProfile("test_profile"); |
| 42 } |
| 43 ~TestChromeScreenshotTaker() override {} |
| 44 |
| 45 TestingProfile* profile() { return profile_; } |
| 46 |
| 47 protected: |
| 48 Profile* GetProfile() override { return profile(); } |
| 49 |
| 50 private: |
| 51 scoped_ptr<TestingProfileManager> profile_manager_; |
| 52 TestingProfile* profile_; |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(TestChromeScreenshotTaker); |
| 55 }; |
| 56 |
| 57 } // namespace |
| 58 |
| 59 class ChromeScreenshotTakerTest : public AshTestBase, |
| 60 public ui::ScreenshotTakerObserver { |
| 61 public: |
| 62 ChromeScreenshotTakerTest() |
| 34 : running_(false), | 63 : running_(false), |
| 35 screenshot_complete_(false), | 64 screenshot_complete_(false), |
| 36 screenshot_result_(ScreenshotTakerObserver::SCREENSHOT_SUCCESS) { | 65 screenshot_result_(ScreenshotTakerObserver::SCREENSHOT_SUCCESS) {} |
| 66 |
| 67 void SetUp() override { |
| 68 AshTestBase::SetUp(); |
| 69 chrome_screenshot_taker_.reset(new TestChromeScreenshotTaker); |
| 70 screenshot_taker()->AddObserver(this); |
| 37 } | 71 } |
| 38 | 72 |
| 39 void SetUp() override { AshTestBase::SetUp(); } | |
| 40 | |
| 41 void TearDown() override { | 73 void TearDown() override { |
| 42 RunAllPendingInMessageLoop(); | 74 RunAllPendingInMessageLoop(); |
| 75 screenshot_taker()->RemoveObserver(this); |
| 76 chrome_screenshot_taker_.reset(); |
| 43 AshTestBase::TearDown(); | 77 AshTestBase::TearDown(); |
| 44 } | 78 } |
| 45 | 79 |
| 46 // Overridden from ScreenshotTakerObserver | 80 // Overridden from ui::ScreenshotTakerObserver |
| 47 void OnScreenshotCompleted(ScreenshotTakerObserver::Result screenshot_result, | 81 void OnScreenshotCompleted(ScreenshotTakerObserver::Result screenshot_result, |
| 48 const base::FilePath& screenshot_path) override { | 82 const base::FilePath& screenshot_path) override { |
| 49 screenshot_complete_ = true; | 83 screenshot_complete_ = true; |
| 50 screenshot_result_ = screenshot_result; | 84 screenshot_result_ = screenshot_result; |
| 51 screenshot_path_ = screenshot_path; | 85 screenshot_path_ = screenshot_path; |
| 52 if (!running_) | 86 if (!running_) |
| 53 return; | 87 return; |
| 54 message_loop_runner_->Quit(); | 88 message_loop_runner_->Quit(); |
| 55 running_ = false; | 89 running_ = false; |
| 56 } | 90 } |
| 57 | 91 |
| 58 protected: | 92 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() { | 93 void Wait() { |
| 78 if (screenshot_complete_) | 94 if (screenshot_complete_) |
| 79 return; | 95 return; |
| 80 running_ = true; | 96 running_ = true; |
| 81 message_loop_runner_ = new content::MessageLoopRunner; | 97 message_loop_runner_ = new content::MessageLoopRunner; |
| 82 message_loop_runner_->Run(); | 98 message_loop_runner_->Run(); |
| 83 EXPECT_TRUE(screenshot_complete_); | 99 EXPECT_TRUE(screenshot_complete_); |
| 84 } | 100 } |
| 85 | 101 |
| 102 TestChromeScreenshotTaker* chrome_screenshot_taker() { |
| 103 return chrome_screenshot_taker_.get(); |
| 104 } |
| 105 |
| 106 ui::ScreenshotTaker* screenshot_taker() { |
| 107 return chrome_screenshot_taker_->screenshot_taker_.get(); |
| 108 } |
| 109 |
| 86 bool running_; | 110 bool running_; |
| 87 bool screenshot_complete_; | 111 bool screenshot_complete_; |
| 88 ScreenshotTakerObserver::Result screenshot_result_; | 112 ScreenshotTakerObserver::Result screenshot_result_; |
| 113 scoped_ptr<TestChromeScreenshotTaker> chrome_screenshot_taker_; |
| 89 base::FilePath screenshot_path_; | 114 base::FilePath screenshot_path_; |
| 90 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | 115 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
| 91 | 116 |
| 92 private: | 117 private: |
| 93 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerTest); | 118 DISALLOW_COPY_AND_ASSIGN(ChromeScreenshotTakerTest); |
| 94 }; | 119 }; |
| 95 | 120 |
| 96 TEST_F(ScreenshotTakerTest, TakeScreenshot) { | 121 TEST_F(ChromeScreenshotTakerTest, TakeScreenshot) { |
| 97 #if defined(OS_CHROMEOS) | 122 #if defined(OS_CHROMEOS) |
| 98 // Note that within the test framework the LoginState object will always | 123 // Note that within the test framework the LoginState object will always |
| 99 // claim that the user did log in. | 124 // claim that the user did log in. |
| 100 ASSERT_FALSE(chromeos::LoginState::IsInitialized()); | 125 ASSERT_FALSE(chromeos::LoginState::IsInitialized()); |
| 101 chromeos::LoginState::Initialize(); | 126 chromeos::LoginState::Initialize(); |
| 102 #endif | 127 #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; | 128 base::ScopedTempDir directory; |
| 111 ASSERT_TRUE(directory.CreateUniqueTempDir()); | 129 ASSERT_TRUE(directory.CreateUniqueTempDir()); |
| 112 SetScreenshotDirectoryForTest(&screenshot_taker, directory.path()); | 130 EXPECT_TRUE(chrome_screenshot_taker()->CanTakeScreenshot()); |
| 113 SetScreenshotBasenameForTest(&screenshot_taker, "Screenshot"); | |
| 114 SetScreenshotProfileForTest(&screenshot_taker, profile); | |
| 115 | 131 |
| 116 EXPECT_TRUE(screenshot_taker.CanTakeScreenshot()); | 132 screenshot_taker()->TakeScreenshot( |
| 133 Shell::GetPrimaryRootWindow(), gfx::Rect(0, 0, 100, 100), |
| 134 directory.path().AppendASCII("Screenshot.png")); |
| 117 | 135 |
| 118 screenshot_taker.HandleTakePartialScreenshot( | 136 EXPECT_FALSE(screenshot_taker()->CanTakeScreenshot()); |
| 119 Shell::GetPrimaryRootWindow(), gfx::Rect(0, 0, 100, 100)); | |
| 120 | |
| 121 EXPECT_FALSE(screenshot_taker.CanTakeScreenshot()); | |
| 122 | 137 |
| 123 Wait(); | 138 Wait(); |
| 124 | 139 |
| 125 #if defined(OS_CHROMEOS) | 140 #if defined(OS_CHROMEOS) |
| 126 // Screenshot notifications on Windows not yet turned on. | 141 // Screenshot notifications on Windows not yet turned on. |
| 127 EXPECT_TRUE(g_browser_process->notification_ui_manager()->FindById( | 142 EXPECT_TRUE(g_browser_process->notification_ui_manager()->FindById( |
| 128 std::string("screenshot"), | 143 std::string("screenshot"), |
| 129 NotificationUIManager::GetProfileID(profile)) != NULL); | 144 NotificationUIManager::GetProfileID( |
| 145 chrome_screenshot_taker()->profile())) != NULL); |
| 130 g_browser_process->notification_ui_manager()->CancelAll(); | 146 g_browser_process->notification_ui_manager()->CancelAll(); |
| 131 #endif | 147 #endif |
| 132 | 148 |
| 133 EXPECT_EQ(ScreenshotTakerObserver::SCREENSHOT_SUCCESS, screenshot_result_); | 149 EXPECT_EQ(ScreenshotTakerObserver::SCREENSHOT_SUCCESS, screenshot_result_); |
| 134 | 150 |
| 135 if (ScreenshotTakerObserver::SCREENSHOT_SUCCESS == screenshot_result_) | 151 if (ScreenshotTakerObserver::SCREENSHOT_SUCCESS == screenshot_result_) |
| 136 EXPECT_TRUE(base::PathExists(screenshot_path_)); | 152 EXPECT_TRUE(base::PathExists(screenshot_path_)); |
| 137 | 153 |
| 138 #if defined(OS_CHROMEOS) | 154 #if defined(OS_CHROMEOS) |
| 139 chromeos::LoginState::Shutdown(); | 155 chromeos::LoginState::Shutdown(); |
| 140 #endif | 156 #endif |
| 141 } | 157 } |
| 142 | 158 |
| 143 } // namespace test | 159 } // namespace test |
| 144 } // namespace ash | 160 } // namespace ash |
| OLD | NEW |