Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 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/file_util.h" |
| 12 | #include "base/files/scoped_temp_dir.h" |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 13 | #include "base/message_loop/message_loop.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 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" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 17 | #include "chrome/test/base/testing_browser_process.h" |
| 18 | #include "chrome/test/base/testing_profile.h" |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 19 | #include "chrome/test/base/testing_profile_manager.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 20 | #include "content/public/browser/browser_thread.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 21 | #include "content/public/test/test_utils.h" |
| 22 | #include "ui/aura/root_window.h" |
| 23 | #include "ui/message_center/message_center_switches.h" |
| 24 | |
| 25 | namespace ash { |
| 26 | namespace test { |
| 27 | |
| 28 | class ScreenshotTakerTest : public AshTestBase, |
| 29 | public ScreenshotTakerObserver { |
| 30 | public: |
| 31 | ScreenshotTakerTest() |
Ben Murdoch | a3f7b4e | 2013-07-24 10:36:34 +0100 | [diff] [blame] | 32 | : running_(false), |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 33 | screenshot_complete_(false), |
| 34 | screenshot_result_(ScreenshotTakerObserver::SCREENSHOT_SUCCESS) { |
| 35 | } |
| 36 | |
| 37 | virtual void SetUp() { |
| 38 | AshTestBase::SetUp(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | virtual void TearDown() { |
| 42 | RunAllPendingInMessageLoop(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 43 | AshTestBase::TearDown(); |
| 44 | } |
| 45 | |
| 46 | // Overridden from ScreenshotTakerObserver |
| 47 | virtual void OnScreenshotCompleted( |
| 48 | ScreenshotTakerObserver::Result screenshot_result, |
| 49 | const base::FilePath& screenshot_path) OVERRIDE { |
| 50 | screenshot_complete_ = true; |
| 51 | screenshot_result_ = screenshot_result; |
| 52 | screenshot_path_ = screenshot_path; |
| 53 | if (!running_) |
| 54 | return; |
| 55 | message_loop_runner_->Quit(); |
| 56 | running_ = false; |
| 57 | } |
| 58 | |
| 59 | protected: |
| 60 | // ScreenshotTakerTest is a friend of ScreenshotTaker and therefore |
| 61 | // allowed to set the directory, basename and profile. |
| 62 | void SetScreenshotDirectoryForTest( |
| 63 | ScreenshotTaker* screenshot_taker, |
| 64 | const base::FilePath& screenshot_directory) { |
| 65 | screenshot_taker->SetScreenshotDirectoryForTest(screenshot_directory); |
| 66 | } |
| 67 | void SetScreenshotBasenameForTest( |
| 68 | ScreenshotTaker* screenshot_taker, |
| 69 | const std::string& screenshot_basename) { |
| 70 | screenshot_taker->SetScreenshotBasenameForTest(screenshot_basename); |
| 71 | } |
| 72 | void SetScreenshotProfileForTest( |
| 73 | ScreenshotTaker* screenshot_taker, |
| 74 | Profile* profile) { |
| 75 | screenshot_taker->SetScreenshotProfileForTest(profile); |
| 76 | } |
| 77 | |
| 78 | void Wait() { |
| 79 | if (screenshot_complete_) |
| 80 | return; |
| 81 | running_ = true; |
| 82 | message_loop_runner_ = new content::MessageLoopRunner; |
| 83 | message_loop_runner_->Run(); |
| 84 | EXPECT_TRUE(screenshot_complete_); |
| 85 | } |
| 86 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 87 | bool running_; |
| 88 | bool screenshot_complete_; |
| 89 | ScreenshotTakerObserver::Result screenshot_result_; |
| 90 | base::FilePath screenshot_path_; |
| 91 | scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
| 92 | |
| 93 | DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerTest); |
| 94 | }; |
| 95 | |
| 96 | TEST_F(ScreenshotTakerTest, TakeScreenshot) { |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 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"); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 102 | ScreenshotTaker screenshot_taker; |
| 103 | screenshot_taker.AddObserver(this); |
| 104 | base::ScopedTempDir directory; |
| 105 | ASSERT_TRUE(directory.CreateUniqueTempDir()); |
| 106 | SetScreenshotDirectoryForTest(&screenshot_taker, directory.path()); |
| 107 | SetScreenshotBasenameForTest(&screenshot_taker, "Screenshot"); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 108 | SetScreenshotProfileForTest(&screenshot_taker, profile); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 109 | |
| 110 | EXPECT_TRUE(screenshot_taker.CanTakeScreenshot()); |
| 111 | |
| 112 | screenshot_taker.HandleTakePartialScreenshot( |
| 113 | Shell::GetPrimaryRootWindow(), gfx::Rect(0, 0, 100, 100)); |
| 114 | |
| 115 | EXPECT_FALSE(screenshot_taker.CanTakeScreenshot()); |
| 116 | |
| 117 | Wait(); |
| 118 | |
| 119 | #if defined(OS_CHROMEOS) |
| 120 | // Screenshot notifications on Windows not yet turned on. |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 121 | EXPECT_TRUE(g_browser_process->notification_ui_manager()->FindById( |
| 122 | std::string("screenshot")) != NULL); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 123 | g_browser_process->notification_ui_manager()->CancelAll(); |
| 124 | #endif |
| 125 | |
| 126 | EXPECT_EQ(ScreenshotTakerObserver::SCREENSHOT_SUCCESS, screenshot_result_); |
| 127 | |
| 128 | if (ScreenshotTakerObserver::SCREENSHOT_SUCCESS == screenshot_result_) |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 129 | EXPECT_TRUE(base::PathExists(screenshot_path_)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | } // namespace test |
| 133 | } // namespace ash |