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