Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / ash / chrome_screenshot_grabber_unittest.cc
blobb9179120ccd2ebba4c7707d1f196993f837af910
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.
5 #include "chrome/browser/ui/ash/chrome_screenshot_grabber.h"
7 #include "ash/accelerators/accelerator_controller.h"
8 #include "ash/shell.h"
9 #include "ash/test/ash_test_base.h"
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/files/file_util.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "base/message_loop/message_loop.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/notifications/notification_ui_manager.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chrome/test/base/testing_browser_process.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "chrome/test/base/testing_profile_manager.h"
22 #include "content/public/test/test_utils.h"
23 #include "ui/aura/window_event_dispatcher.h"
24 #include "ui/snapshot/screenshot_grabber.h"
26 #if defined(OS_CHROMEOS)
27 #include "chromeos/login/login_state.h"
28 #endif
30 namespace ash {
31 namespace test {
33 class ChromeScreenshotGrabberTest : public AshTestBase,
34 public ui::ScreenshotGrabberObserver {
35 public:
36 ChromeScreenshotGrabberTest()
37 : profile_(NULL),
38 running_(false),
39 screenshot_complete_(false),
40 screenshot_result_(ScreenshotGrabberObserver::SCREENSHOT_SUCCESS) {}
42 void SetUp() override {
43 AshTestBase::SetUp();
44 chrome_screenshot_grabber_.reset(new ChromeScreenshotGrabber);
45 profile_manager_.reset(
46 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
47 CHECK(profile_manager_->SetUp());
48 profile_ = profile_manager_->CreateTestingProfile("test_profile");
49 chrome_screenshot_grabber_->SetProfileForTest(profile_);
50 screenshot_grabber()->AddObserver(this);
53 void TearDown() override {
54 RunAllPendingInMessageLoop();
55 screenshot_grabber()->RemoveObserver(this);
56 chrome_screenshot_grabber_.reset();
57 profile_manager_.reset();
58 AshTestBase::TearDown();
61 // Overridden from ui::ScreenshotGrabberObserver
62 void OnScreenshotCompleted(
63 ScreenshotGrabberObserver::Result screenshot_result,
64 const base::FilePath& screenshot_path) override {
65 screenshot_complete_ = true;
66 screenshot_result_ = screenshot_result;
67 screenshot_path_ = screenshot_path;
68 if (!running_)
69 return;
70 message_loop_runner_->Quit();
71 running_ = false;
74 protected:
75 void Wait() {
76 if (screenshot_complete_)
77 return;
78 running_ = true;
79 message_loop_runner_ = new content::MessageLoopRunner;
80 message_loop_runner_->Run();
81 EXPECT_TRUE(screenshot_complete_);
84 ChromeScreenshotGrabber* chrome_screenshot_grabber() {
85 return chrome_screenshot_grabber_.get();
88 ui::ScreenshotGrabber* screenshot_grabber() {
89 return chrome_screenshot_grabber_->screenshot_grabber_.get();
92 scoped_ptr<TestingProfileManager> profile_manager_;
93 TestingProfile* profile_;
94 bool running_;
95 bool screenshot_complete_;
96 ScreenshotGrabberObserver::Result screenshot_result_;
97 scoped_ptr<ChromeScreenshotGrabber> chrome_screenshot_grabber_;
98 base::FilePath screenshot_path_;
99 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
101 private:
102 DISALLOW_COPY_AND_ASSIGN(ChromeScreenshotGrabberTest);
105 TEST_F(ChromeScreenshotGrabberTest, TakeScreenshot) {
106 #if defined(OS_CHROMEOS)
107 // Note that within the test framework the LoginState object will always
108 // claim that the user did log in.
109 ASSERT_FALSE(chromeos::LoginState::IsInitialized());
110 chromeos::LoginState::Initialize();
111 #endif
112 base::ScopedTempDir directory;
113 ASSERT_TRUE(directory.CreateUniqueTempDir());
114 EXPECT_TRUE(chrome_screenshot_grabber()->CanTakeScreenshot());
116 screenshot_grabber()->TakeScreenshot(
117 Shell::GetPrimaryRootWindow(), gfx::Rect(0, 0, 100, 100),
118 directory.path().AppendASCII("Screenshot.png"));
120 EXPECT_FALSE(screenshot_grabber()->CanTakeScreenshot());
122 Wait();
124 #if defined(OS_CHROMEOS)
125 // Screenshot notifications on Windows not yet turned on.
126 EXPECT_TRUE(g_browser_process->notification_ui_manager()->FindById(
127 std::string("screenshot"),
128 NotificationUIManager::GetProfileID(profile_)) != NULL);
129 g_browser_process->notification_ui_manager()->CancelAll();
130 #endif
132 EXPECT_EQ(ScreenshotGrabberObserver::SCREENSHOT_SUCCESS, screenshot_result_);
134 if (ScreenshotGrabberObserver::SCREENSHOT_SUCCESS == screenshot_result_)
135 EXPECT_TRUE(base::PathExists(screenshot_path_));
137 #if defined(OS_CHROMEOS)
138 chromeos::LoginState::Shutdown();
139 #endif
142 } // namespace test
143 } // namespace ash