Index: chrome/browser/ui/ash/screenshot_taker_unittest.cc |
diff --git a/chrome/browser/ui/ash/screenshot_taker_unittest.cc b/chrome/browser/ui/ash/screenshot_taker_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..01556fbf47a8304957be918256d147cb34cb9f86 |
--- /dev/null |
+++ b/chrome/browser/ui/ash/screenshot_taker_unittest.cc |
@@ -0,0 +1,119 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ash/shell.h" |
+#include "ash/test/ash_test_base.h" |
+#include "base/bind.h" |
+#include "base/command_line.h" |
+#include "base/file_util.h" |
+#include "base/files/scoped_temp_dir.h" |
+#include "base/message_loop.h" |
+#include "chrome/browser/browser_process.h" |
+#include "chrome/browser/notifications/notification_ui_manager.h" |
+#include "chrome/browser/ui/ash/screenshot_taker.h" |
+#include "chrome/test/base/in_process_browser_test.h" |
+#include "chrome/test/base/scoped_testing_local_state.h" |
+#include "chrome/test/base/testing_browser_process.h" |
+#include "chrome/test/base/testing_profile.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/test/test_browser_thread.h" |
+#include "content/public/test/test_utils.h" |
+#include "ui/aura/root_window.h" |
+#include "ui/message_center/message_center_switches.h" |
+ |
+namespace ash { |
+namespace test { |
+ |
+class ScreenshotTakerTest : public AshTestBase, |
+ public ScreenshotTakerObserver { |
+ public: |
+ ScreenshotTakerTest() |
+ : local_state_(TestingBrowserProcess::GetGlobal()), |
+ running_(false), |
+ screenshot_complete_(false), |
+ screenshot_result_(ScreenshotTakerObserver::SCREENSHOT_SUCCESS) { |
+ MessageLoopForUI* ui_loop = message_loop(); |
+ ui_thread_.reset( |
+ new content::TestBrowserThread(content::BrowserThread::UI, ui_loop)); |
+ } |
+ |
+ virtual ~ScreenshotTakerTest() { |
+ screenshot_taker_->RemoveObserver(this); |
James Cook
2013/04/02 22:47:18
Things you built in SetUp() should be cleaned up i
sschmitz
2013/04/03 17:57:40
Reorganized so overridden SetUp and TearDown not n
|
+ screenshot_taker_.reset(); |
+ profile_.reset(); |
+ ui_thread_.reset(); |
+ } |
+ |
+ // Overridden from AshTestBase: |
+ virtual void SetUp() { |
+ CommandLine::ForCurrentProcess()->AppendSwitch( |
+ message_center::switches::kEnableRichNotifications); |
+ AshTestBase::SetUp(); |
+ profile_.reset(new TestingProfile()); |
James Cook
2013/04/02 22:47:18
Can profile_, screenshot_taker_ and directory be m
sschmitz
2013/04/03 17:57:40
Good Point. done.
|
+ base::ScopedTempDir directory; |
James Cook
2013/04/02 22:47:18
I think this directory will be deleted when SetUp(
sschmitz
2013/04/03 17:57:40
Of course, I can't believe I did this. Reorganized
|
+ ASSERT_TRUE(directory.CreateUniqueTempDir()); |
+ screenshot_taker_.reset(new ScreenshotTaker(profile_.get())); |
+ screenshot_taker_->AddObserver(this); |
+ screenshot_taker_->OverrideScreenshotDirectory(directory.path()); |
+ screenshot_taker_->OverrideScreenshotBasename("Screenshot"); |
+ } |
+ |
+ // Overridden from ScreenshotTakerObserver |
+ virtual void OnScreenshotCompleted( |
+ ScreenshotTakerObserver::Result screenshot_result, |
+ const base::FilePath& screenshot_path) OVERRIDE { |
+ screenshot_complete_ = true; |
+ screenshot_result_ = screenshot_result; |
+ screenshot_path_ = screenshot_path; |
+ if (!running_) |
+ return; |
+ message_loop_runner_->Quit(); |
+ running_ = false; |
+ } |
+ |
+ protected: |
+ void Wait() { |
+ if (screenshot_complete_) |
+ return; |
+ running_ = true; |
+ message_loop_runner_ = new content::MessageLoopRunner; |
+ message_loop_runner_->Run(); |
+ EXPECT_TRUE(screenshot_complete_); |
+ } |
+ |
+ ScopedTestingLocalState local_state_; |
+ scoped_ptr<content::TestBrowserThread> ui_thread_; |
+ scoped_ptr<TestingProfile> profile_; |
+ scoped_ptr<ScreenshotTaker> screenshot_taker_; |
+ base::ScopedTempDir temp_dir_; |
James Cook
2013/04/02 22:47:18
Is this used?
sschmitz
2013/04/03 17:57:40
Nope, removed.
|
+ bool running_; |
+ bool screenshot_complete_; |
+ ScreenshotTakerObserver::Result screenshot_result_; |
+ base::FilePath screenshot_path_; |
+ scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerTest); |
+}; |
+ |
+TEST_F(ScreenshotTakerTest, TakeScreenshot) { |
+ EXPECT_TRUE(screenshot_taker_->CanTakeScreenshot()); |
+ |
+ screenshot_taker_->HandleTakePartialScreenshot( |
+ ash::Shell::GetPrimaryRootWindow(), gfx::Rect(0,0,100,100)); |
+ |
+ EXPECT_FALSE(screenshot_taker_->CanTakeScreenshot()); |
+ |
+ Wait(); |
+ |
+ EXPECT_TRUE(g_browser_process->notification_ui_manager()->DoesIdExist( |
+ std::string("screenshot_001"))); |
+ |
+ EXPECT_EQ(ScreenshotTakerObserver::SCREENSHOT_SUCCESS, screenshot_result_); |
+ |
+ if (ScreenshotTakerObserver::SCREENSHOT_SUCCESS == screenshot_result_) |
+ EXPECT_TRUE(file_util::PathExists(screenshot_path_)); |
+} |
+ |
+} // namespace test |
+} // namespace ash |