| 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/screenshot_taker.h" |
| 6 | 6 |
| 7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
| 8 #include "ash/test/ash_test_base.h" | 8 #include "ash/test/ash_test_base.h" |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/notifications/notification_ui_manager.h" | 15 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 16 #include "chrome/test/base/in_process_browser_test.h" | 16 #include "chrome/test/base/in_process_browser_test.h" |
| 17 #include "chrome/test/base/scoped_testing_local_state.h" | |
| 18 #include "chrome/test/base/testing_browser_process.h" | 17 #include "chrome/test/base/testing_browser_process.h" |
| 19 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
| 19 #include "chrome/test/base/testing_profile_manager.h" |
| 20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/test/test_utils.h" | 21 #include "content/public/test/test_utils.h" |
| 22 #include "ui/aura/root_window.h" | 22 #include "ui/aura/root_window.h" |
| 23 #include "ui/message_center/message_center_switches.h" | 23 #include "ui/message_center/message_center_switches.h" |
| 24 | 24 |
| 25 namespace ash { | 25 namespace ash { |
| 26 namespace test { | 26 namespace test { |
| 27 | 27 |
| 28 class ScreenshotTakerTest : public AshTestBase, | 28 class ScreenshotTakerTest : public AshTestBase, |
| 29 public ScreenshotTakerObserver { | 29 public ScreenshotTakerObserver { |
| 30 public: | 30 public: |
| 31 ScreenshotTakerTest() | 31 ScreenshotTakerTest() |
| 32 : running_(false), | 32 : running_(false), |
| 33 screenshot_complete_(false), | 33 screenshot_complete_(false), |
| 34 screenshot_result_(ScreenshotTakerObserver::SCREENSHOT_SUCCESS) { | 34 screenshot_result_(ScreenshotTakerObserver::SCREENSHOT_SUCCESS) { |
| 35 } | 35 } |
| 36 | 36 |
| 37 virtual void SetUp() { | 37 virtual void SetUp() { |
| 38 AshTestBase::SetUp(); | 38 AshTestBase::SetUp(); |
| 39 local_state_.reset( | |
| 40 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal())); | |
| 41 } | 39 } |
| 42 | 40 |
| 43 virtual void TearDown() { | 41 virtual void TearDown() { |
| 44 RunAllPendingInMessageLoop(); | 42 RunAllPendingInMessageLoop(); |
| 45 local_state_.reset(); | |
| 46 AshTestBase::TearDown(); | 43 AshTestBase::TearDown(); |
| 47 } | 44 } |
| 48 | 45 |
| 49 // Overridden from ScreenshotTakerObserver | 46 // Overridden from ScreenshotTakerObserver |
| 50 virtual void OnScreenshotCompleted( | 47 virtual void OnScreenshotCompleted( |
| 51 ScreenshotTakerObserver::Result screenshot_result, | 48 ScreenshotTakerObserver::Result screenshot_result, |
| 52 const base::FilePath& screenshot_path) OVERRIDE { | 49 const base::FilePath& screenshot_path) OVERRIDE { |
| 53 screenshot_complete_ = true; | 50 screenshot_complete_ = true; |
| 54 screenshot_result_ = screenshot_result; | 51 screenshot_result_ = screenshot_result; |
| 55 screenshot_path_ = screenshot_path; | 52 screenshot_path_ = screenshot_path; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 80 | 77 |
| 81 void Wait() { | 78 void Wait() { |
| 82 if (screenshot_complete_) | 79 if (screenshot_complete_) |
| 83 return; | 80 return; |
| 84 running_ = true; | 81 running_ = true; |
| 85 message_loop_runner_ = new content::MessageLoopRunner; | 82 message_loop_runner_ = new content::MessageLoopRunner; |
| 86 message_loop_runner_->Run(); | 83 message_loop_runner_->Run(); |
| 87 EXPECT_TRUE(screenshot_complete_); | 84 EXPECT_TRUE(screenshot_complete_); |
| 88 } | 85 } |
| 89 | 86 |
| 90 scoped_ptr<ScopedTestingLocalState> local_state_; | |
| 91 bool running_; | 87 bool running_; |
| 92 bool screenshot_complete_; | 88 bool screenshot_complete_; |
| 93 ScreenshotTakerObserver::Result screenshot_result_; | 89 ScreenshotTakerObserver::Result screenshot_result_; |
| 94 base::FilePath screenshot_path_; | 90 base::FilePath screenshot_path_; |
| 95 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | 91 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
| 96 | 92 |
| 97 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerTest); | 93 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerTest); |
| 98 }; | 94 }; |
| 99 | 95 |
| 100 TEST_F(ScreenshotTakerTest, TakeScreenshot) { | 96 TEST_F(ScreenshotTakerTest, TakeScreenshot) { |
| 101 TestingProfile profile; | 97 scoped_ptr<TestingProfileManager> profile_manager( |
| 98 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); |
| 99 ASSERT_TRUE(profile_manager->SetUp()); |
| 100 TestingProfile* profile = |
| 101 profile_manager->CreateTestingProfile("test_profile"); |
| 102 ScreenshotTaker screenshot_taker; | 102 ScreenshotTaker screenshot_taker; |
| 103 screenshot_taker.AddObserver(this); | 103 screenshot_taker.AddObserver(this); |
| 104 base::ScopedTempDir directory; | 104 base::ScopedTempDir directory; |
| 105 ASSERT_TRUE(directory.CreateUniqueTempDir()); | 105 ASSERT_TRUE(directory.CreateUniqueTempDir()); |
| 106 SetScreenshotDirectoryForTest(&screenshot_taker, directory.path()); | 106 SetScreenshotDirectoryForTest(&screenshot_taker, directory.path()); |
| 107 SetScreenshotBasenameForTest(&screenshot_taker, "Screenshot"); | 107 SetScreenshotBasenameForTest(&screenshot_taker, "Screenshot"); |
| 108 SetScreenshotProfileForTest(&screenshot_taker, &profile); | 108 SetScreenshotProfileForTest(&screenshot_taker, profile); |
| 109 | 109 |
| 110 EXPECT_TRUE(screenshot_taker.CanTakeScreenshot()); | 110 EXPECT_TRUE(screenshot_taker.CanTakeScreenshot()); |
| 111 | 111 |
| 112 screenshot_taker.HandleTakePartialScreenshot( | 112 screenshot_taker.HandleTakePartialScreenshot( |
| 113 Shell::GetPrimaryRootWindow(), gfx::Rect(0, 0, 100, 100)); | 113 Shell::GetPrimaryRootWindow(), gfx::Rect(0, 0, 100, 100)); |
| 114 | 114 |
| 115 EXPECT_FALSE(screenshot_taker.CanTakeScreenshot()); | 115 EXPECT_FALSE(screenshot_taker.CanTakeScreenshot()); |
| 116 | 116 |
| 117 Wait(); | 117 Wait(); |
| 118 | 118 |
| 119 #if defined(OS_CHROMEOS) | 119 #if defined(OS_CHROMEOS) |
| 120 // Screenshot notifications on Windows not yet turned on. | 120 // Screenshot notifications on Windows not yet turned on. |
| 121 EXPECT_TRUE(g_browser_process->notification_ui_manager()->FindById( | 121 EXPECT_TRUE(g_browser_process->notification_ui_manager()->FindById( |
| 122 std::string("screenshot")) != NULL); | 122 std::string("screenshot")) != NULL); |
| 123 g_browser_process->notification_ui_manager()->CancelAll(); | 123 g_browser_process->notification_ui_manager()->CancelAll(); |
| 124 #endif | 124 #endif |
| 125 | 125 |
| 126 EXPECT_EQ(ScreenshotTakerObserver::SCREENSHOT_SUCCESS, screenshot_result_); | 126 EXPECT_EQ(ScreenshotTakerObserver::SCREENSHOT_SUCCESS, screenshot_result_); |
| 127 | 127 |
| 128 if (ScreenshotTakerObserver::SCREENSHOT_SUCCESS == screenshot_result_) | 128 if (ScreenshotTakerObserver::SCREENSHOT_SUCCESS == screenshot_result_) |
| 129 EXPECT_TRUE(base::PathExists(screenshot_path_)); | 129 EXPECT_TRUE(base::PathExists(screenshot_path_)); |
| 130 } | 130 } |
| 131 | 131 |
| 132 } // namespace test | 132 } // namespace test |
| 133 } // namespace ash | 133 } // namespace ash |
| OLD | NEW |