Chromium Code Reviews| 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 "ash/shell.h" | |
| 6 #include "ash/test/ash_test_base.h" | |
| 7 #include "base/bind.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/files/scoped_temp_dir.h" | |
| 11 #include "base/message_loop.h" | |
| 12 #include "chrome/browser/browser_process.h" | |
| 13 #include "chrome/browser/notifications/notification_ui_manager.h" | |
| 14 #include "chrome/browser/ui/ash/screenshot_taker.h" | |
| 15 #include "chrome/test/base/in_process_browser_test.h" | |
| 16 #include "chrome/test/base/scoped_testing_local_state.h" | |
| 17 #include "chrome/test/base/testing_browser_process.h" | |
| 18 #include "chrome/test/base/testing_profile.h" | |
| 19 #include "content/public/browser/browser_thread.h" | |
| 20 #include "content/public/test/test_browser_thread.h" | |
| 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() | |
| 32 : local_state_(TestingBrowserProcess::GetGlobal()), | |
| 33 running_(false), | |
| 34 screenshot_complete_(false), | |
| 35 screenshot_result_(ScreenshotTakerObserver::SCREENSHOT_SUCCESS) { | |
| 36 CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 37 message_center::switches::kEnableRichNotifications); | |
| 38 ui_thread_.reset( | |
| 39 new content::TestBrowserThread(content::BrowserThread::UI, | |
| 40 message_loop())); | |
| 41 } | |
| 42 | |
| 43 // Overridden from ScreenshotTakerObserver | |
| 44 virtual void OnScreenshotCompleted( | |
| 45 ScreenshotTakerObserver::Result screenshot_result, | |
| 46 const base::FilePath& screenshot_path) OVERRIDE { | |
| 47 screenshot_complete_ = true; | |
| 48 screenshot_result_ = screenshot_result; | |
| 49 screenshot_path_ = screenshot_path; | |
| 50 if (!running_) | |
| 51 return; | |
| 52 message_loop_runner_->Quit(); | |
| 53 running_ = false; | |
| 54 } | |
| 55 | |
| 56 protected: | |
| 57 // ScreenshotTakerTest is a friend of ScreenshotTaker and therefore | |
| 58 // allowed to set the directory and basename. | |
| 59 void SetScreenshotDirectoryForTest( | |
| 60 ScreenshotTaker& screenshot_taker, | |
|
James Cook
2013/04/03 19:50:19
We usually pass pointers instead of non-const refe
sschmitz
2013/04/03 21:13:10
Done.
| |
| 61 const base::FilePath& screenshot_directory) { | |
| 62 screenshot_taker.SetScreenshotDirectoryForTest(screenshot_directory); | |
| 63 } | |
| 64 void SetScreenshotBasenameForTest( | |
| 65 ScreenshotTaker& screenshot_taker, | |
|
James Cook
2013/04/03 19:50:19
ditto
sschmitz
2013/04/03 21:13:10
Done.
| |
| 66 const std::string& screenshot_basename) { | |
| 67 screenshot_taker.SetScreenshotBasenameForTest(screenshot_basename); | |
| 68 } | |
| 69 | |
| 70 void Wait() { | |
| 71 if (screenshot_complete_) | |
| 72 return; | |
| 73 running_ = true; | |
| 74 message_loop_runner_ = new content::MessageLoopRunner; | |
| 75 message_loop_runner_->Run(); | |
| 76 EXPECT_TRUE(screenshot_complete_); | |
| 77 } | |
| 78 | |
| 79 ScopedTestingLocalState local_state_; | |
| 80 scoped_ptr<content::TestBrowserThread> ui_thread_; | |
| 81 bool running_; | |
| 82 bool screenshot_complete_; | |
| 83 ScreenshotTakerObserver::Result screenshot_result_; | |
| 84 base::FilePath screenshot_path_; | |
| 85 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | |
| 86 | |
| 87 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerTest); | |
| 88 }; | |
| 89 | |
| 90 TEST_F(ScreenshotTakerTest, TakeScreenshot) { | |
| 91 TestingProfile profile; | |
| 92 ScreenshotTaker screenshot_taker(&profile); | |
| 93 screenshot_taker.AddObserver(this); | |
| 94 base::ScopedTempDir directory; | |
| 95 ASSERT_TRUE(directory.CreateUniqueTempDir()); | |
| 96 SetScreenshotDirectoryForTest(screenshot_taker, directory.path()); | |
| 97 SetScreenshotBasenameForTest(screenshot_taker, "Screenshot"); | |
| 98 | |
| 99 EXPECT_TRUE(screenshot_taker.CanTakeScreenshot()); | |
| 100 | |
| 101 screenshot_taker.HandleTakePartialScreenshot( | |
| 102 ash::Shell::GetPrimaryRootWindow(), gfx::Rect(0,0,100,100)); | |
| 103 | |
| 104 EXPECT_FALSE(screenshot_taker.CanTakeScreenshot()); | |
| 105 | |
| 106 Wait(); | |
| 107 | |
| 108 EXPECT_TRUE(g_browser_process->notification_ui_manager()->DoesIdExist( | |
| 109 std::string("screenshot_001"))); | |
| 110 | |
| 111 EXPECT_EQ(ScreenshotTakerObserver::SCREENSHOT_SUCCESS, screenshot_result_); | |
| 112 | |
| 113 if (ScreenshotTakerObserver::SCREENSHOT_SUCCESS == screenshot_result_) | |
| 114 EXPECT_TRUE(file_util::PathExists(screenshot_path_)); | |
| 115 } | |
| 116 | |
| 117 } // namespace test | |
| 118 } // namespace ash | |
| OLD | NEW |