Index: components/screenshot_taker/screenshot_taker_unittest.cc |
diff --git a/components/screenshot_taker/screenshot_taker_unittest.cc b/components/screenshot_taker/screenshot_taker_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e0ad7ec89897f77db25d91820cd1872ba952cd16 |
--- /dev/null |
+++ b/components/screenshot_taker/screenshot_taker_unittest.cc |
@@ -0,0 +1,120 @@ |
+// 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 "components/screenshot_taker/screenshot_taker.h" |
+ |
+#include "base/bind.h" |
+#include "base/files/file_util.h" |
+#include "base/files/scoped_temp_dir.h" |
+#include "base/message_loop/message_loop.h" |
+#include "content/public/test/test_browser_thread_bundle.h" |
+#include "content/public/test/test_utils.h" |
+#include "ui/aura/client/window_tree_client.h" |
+#include "ui/aura/test/aura_test_helper.h" |
+#include "ui/aura/test/test_window_delegate.h" |
+#include "ui/aura/window_event_dispatcher.h" |
+#include "ui/compositor/test/context_factories_for_test.h" |
+ |
+class ScreenshotTakerTest : public testing::Test, |
+ public ScreenshotTakerClient, |
+ public ScreenshotTakerObserver { |
+ public: |
+ ScreenshotTakerTest() |
+ : running_(false), |
+ screenshot_complete_(false), |
+ screenshot_taker_(this), |
+ screenshot_result_(ScreenshotTakerObserver::SCREENSHOT_SUCCESS) { |
+ screenshot_taker_.AddObserver(this); |
+ } |
+ |
+ ~ScreenshotTakerTest() override { |
+ screenshot_taker_.RemoveObserver(this); |
+ } |
+ |
+ void SetUp() override { |
+ testing::Test::SetUp(); |
+ ui::ContextFactory* context_factory = |
+ ui::InitializeContextFactoryForTests(true); |
+ helper_.reset(new aura::test::AuraTestHelper( |
+ base::MessageLoopForUI::current())); |
+ helper_->SetUp(context_factory); |
+ } |
+ |
+ void TearDown() override { |
+ helper_->TearDown(); |
+ ui::TerminateContextFactoryForTests(); |
+ testing::Test::TearDown(); |
+ } |
+ |
+ // Taking a screenshot requires the window to have a drawn layer. |
+ aura::Window* CreateTexturedWindow() { |
+ aura::Window* window = new aura::Window(NULL); |
+ window->SetType(ui::wm::WINDOW_TYPE_NORMAL); |
+ window->Init(aura::WINDOW_LAYER_TEXTURED); |
+ aura::client::ParentWindowWithContext(window, root_window(), gfx::Rect()); |
+ window->Show(); |
+ window->SetBounds(root_window()->bounds()); |
+ return window; |
+ } |
+ |
+ // Overridden from ScreenshotTakerObserver |
+ 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_); |
+ } |
+ |
+ aura::Window* root_window() { |
+ return helper_->root_window(); |
+ } |
+ |
+ bool running_; |
+ bool screenshot_complete_; |
+ ScreenshotTaker screenshot_taker_; |
+ ScreenshotTakerObserver::Result screenshot_result_; |
+ base::FilePath screenshot_path_; |
+ scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
+ |
+ private: |
+ content::TestBrowserThreadBundle thread_bundle_; |
+ scoped_ptr<aura::test::AuraTestHelper> helper_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerTest); |
+}; |
+ |
+TEST_F(ScreenshotTakerTest, TakeScreenshot) { |
+ scoped_ptr<aura::Window> window(CreateTexturedWindow()); |
+ aura::test::TestWindowDelegate delegate; |
+ base::ScopedTempDir directory; |
+ ASSERT_TRUE(directory.CreateUniqueTempDir()); |
+ |
+ EXPECT_TRUE(screenshot_taker_.CanTakeScreenshot()); |
+ |
+ screenshot_taker_.TakeScreenshot( |
+ window.get(), gfx::Rect(0, 0, 100, 100), |
+ directory.path().AppendASCII("Screenshot.png")); |
+ |
+ EXPECT_FALSE(screenshot_taker_.CanTakeScreenshot()); |
+ |
+ Wait(); |
+ EXPECT_EQ(ScreenshotTakerObserver::SCREENSHOT_SUCCESS, screenshot_result_); |
+ |
+ if (ScreenshotTakerObserver::SCREENSHOT_SUCCESS == screenshot_result_) |
+ EXPECT_TRUE(base::PathExists(screenshot_path_)); |
+} |