OLD | NEW |
(Empty) | |
| 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 "components/screenshot_taker/screenshot_taker.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "content/public/test/test_utils.h" |
| 13 #include "ui/aura/client/window_tree_client.h" |
| 14 #include "ui/aura/test/aura_test_helper.h" |
| 15 #include "ui/aura/test/test_window_delegate.h" |
| 16 #include "ui/aura/window_event_dispatcher.h" |
| 17 #include "ui/compositor/test/context_factories_for_test.h" |
| 18 |
| 19 class ScreenshotTakerTest : public testing::Test, |
| 20 public ScreenshotTakerClient, |
| 21 public ScreenshotTakerObserver { |
| 22 public: |
| 23 ScreenshotTakerTest() |
| 24 : running_(false), |
| 25 screenshot_complete_(false), |
| 26 screenshot_taker_(this), |
| 27 screenshot_result_(ScreenshotTakerObserver::SCREENSHOT_SUCCESS) { |
| 28 screenshot_taker_.AddObserver(this); |
| 29 } |
| 30 |
| 31 ~ScreenshotTakerTest() override { |
| 32 screenshot_taker_.RemoveObserver(this); |
| 33 } |
| 34 |
| 35 void SetUp() override { |
| 36 testing::Test::SetUp(); |
| 37 ui::ContextFactory* context_factory = |
| 38 ui::InitializeContextFactoryForTests(true); |
| 39 helper_.reset(new aura::test::AuraTestHelper( |
| 40 base::MessageLoopForUI::current())); |
| 41 helper_->SetUp(context_factory); |
| 42 } |
| 43 |
| 44 void TearDown() override { |
| 45 helper_->TearDown(); |
| 46 ui::TerminateContextFactoryForTests(); |
| 47 testing::Test::TearDown(); |
| 48 } |
| 49 |
| 50 // Taking a screenshot requires the window to have a drawn layer. |
| 51 aura::Window* CreateTexturedWindow() { |
| 52 aura::Window* window = new aura::Window(NULL); |
| 53 window->SetType(ui::wm::WINDOW_TYPE_NORMAL); |
| 54 window->Init(aura::WINDOW_LAYER_TEXTURED); |
| 55 aura::client::ParentWindowWithContext(window, root_window(), gfx::Rect()); |
| 56 window->Show(); |
| 57 window->SetBounds(root_window()->bounds()); |
| 58 return window; |
| 59 } |
| 60 |
| 61 // Overridden from ScreenshotTakerObserver |
| 62 void OnScreenshotCompleted(ScreenshotTakerObserver::Result screenshot_result, |
| 63 const base::FilePath& screenshot_path) override { |
| 64 screenshot_complete_ = true; |
| 65 screenshot_result_ = screenshot_result; |
| 66 screenshot_path_ = screenshot_path; |
| 67 if (!running_) |
| 68 return; |
| 69 message_loop_runner_->Quit(); |
| 70 running_ = false; |
| 71 } |
| 72 |
| 73 protected: |
| 74 void Wait() { |
| 75 if (screenshot_complete_) |
| 76 return; |
| 77 running_ = true; |
| 78 message_loop_runner_ = new content::MessageLoopRunner; |
| 79 message_loop_runner_->Run(); |
| 80 EXPECT_TRUE(screenshot_complete_); |
| 81 } |
| 82 |
| 83 aura::Window* root_window() { |
| 84 return helper_->root_window(); |
| 85 } |
| 86 |
| 87 bool running_; |
| 88 bool screenshot_complete_; |
| 89 ScreenshotTaker screenshot_taker_; |
| 90 ScreenshotTakerObserver::Result screenshot_result_; |
| 91 base::FilePath screenshot_path_; |
| 92 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
| 93 |
| 94 private: |
| 95 content::TestBrowserThreadBundle thread_bundle_; |
| 96 scoped_ptr<aura::test::AuraTestHelper> helper_; |
| 97 |
| 98 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerTest); |
| 99 }; |
| 100 |
| 101 TEST_F(ScreenshotTakerTest, TakeScreenshot) { |
| 102 scoped_ptr<aura::Window> window(CreateTexturedWindow()); |
| 103 aura::test::TestWindowDelegate delegate; |
| 104 base::ScopedTempDir directory; |
| 105 ASSERT_TRUE(directory.CreateUniqueTempDir()); |
| 106 |
| 107 EXPECT_TRUE(screenshot_taker_.CanTakeScreenshot()); |
| 108 |
| 109 screenshot_taker_.TakeScreenshot( |
| 110 window.get(), gfx::Rect(0, 0, 100, 100), |
| 111 directory.path().AppendASCII("Screenshot.png")); |
| 112 |
| 113 EXPECT_FALSE(screenshot_taker_.CanTakeScreenshot()); |
| 114 |
| 115 Wait(); |
| 116 EXPECT_EQ(ScreenshotTakerObserver::SCREENSHOT_SUCCESS, screenshot_result_); |
| 117 |
| 118 if (ScreenshotTakerObserver::SCREENSHOT_SUCCESS == screenshot_result_) |
| 119 EXPECT_TRUE(base::PathExists(screenshot_path_)); |
| 120 } |
OLD | NEW |