File: chrome_screenshot_grabber_unittest.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (143 lines) | stat: -rw-r--r-- 4,734 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// 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 "chrome/browser/ui/ash/chrome_screenshot_grabber.h"

#include "ash/accelerators/accelerator_controller.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/message_loop/message_loop.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/notifications/notification_ui_manager.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "content/public/test/test_utils.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/snapshot/screenshot_grabber.h"

#if defined(OS_CHROMEOS)
#include "chromeos/login/login_state.h"
#endif

namespace ash {
namespace test {

class ChromeScreenshotGrabberTest : public AshTestBase,
                                    public ui::ScreenshotGrabberObserver {
 public:
  ChromeScreenshotGrabberTest()
      : profile_(NULL),
        running_(false),
        screenshot_complete_(false),
        screenshot_result_(ScreenshotGrabberObserver::SCREENSHOT_SUCCESS) {}

  void SetUp() override {
    AshTestBase::SetUp();
    chrome_screenshot_grabber_.reset(new ChromeScreenshotGrabber);
    profile_manager_.reset(
        new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
    CHECK(profile_manager_->SetUp());
    profile_ = profile_manager_->CreateTestingProfile("test_profile");
    chrome_screenshot_grabber_->SetProfileForTest(profile_);
    screenshot_grabber()->AddObserver(this);
  }

  void TearDown() override {
    RunAllPendingInMessageLoop();
    screenshot_grabber()->RemoveObserver(this);
    chrome_screenshot_grabber_.reset();
    profile_manager_.reset();
    AshTestBase::TearDown();
  }

  // Overridden from ui::ScreenshotGrabberObserver
  void OnScreenshotCompleted(
      ScreenshotGrabberObserver::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_);
  }

  ChromeScreenshotGrabber* chrome_screenshot_grabber() {
    return chrome_screenshot_grabber_.get();
  }

  ui::ScreenshotGrabber* screenshot_grabber() {
    return chrome_screenshot_grabber_->screenshot_grabber_.get();
  }

  scoped_ptr<TestingProfileManager> profile_manager_;
  TestingProfile* profile_;
  bool running_;
  bool screenshot_complete_;
  ScreenshotGrabberObserver::Result screenshot_result_;
  scoped_ptr<ChromeScreenshotGrabber> chrome_screenshot_grabber_;
  base::FilePath screenshot_path_;
  scoped_refptr<content::MessageLoopRunner> message_loop_runner_;

 private:
  DISALLOW_COPY_AND_ASSIGN(ChromeScreenshotGrabberTest);
};

TEST_F(ChromeScreenshotGrabberTest, TakeScreenshot) {
#if defined(OS_CHROMEOS)
  // Note that within the test framework the LoginState object will always
  // claim that the user did log in.
  ASSERT_FALSE(chromeos::LoginState::IsInitialized());
  chromeos::LoginState::Initialize();
#endif
  base::ScopedTempDir directory;
  ASSERT_TRUE(directory.CreateUniqueTempDir());
  EXPECT_TRUE(chrome_screenshot_grabber()->CanTakeScreenshot());

  screenshot_grabber()->TakeScreenshot(
      Shell::GetPrimaryRootWindow(), gfx::Rect(0, 0, 100, 100),
      directory.path().AppendASCII("Screenshot.png"));

  EXPECT_FALSE(screenshot_grabber()->CanTakeScreenshot());

  Wait();

#if defined(OS_CHROMEOS)
  // Screenshot notifications on Windows not yet turned on.
  EXPECT_TRUE(g_browser_process->notification_ui_manager()->FindById(
                  std::string("screenshot"),
                  NotificationUIManager::GetProfileID(profile_)) != NULL);
  g_browser_process->notification_ui_manager()->CancelAll();
#endif

  EXPECT_EQ(ScreenshotGrabberObserver::SCREENSHOT_SUCCESS, screenshot_result_);

  if (ScreenshotGrabberObserver::SCREENSHOT_SUCCESS == screenshot_result_)
    EXPECT_TRUE(base::PathExists(screenshot_path_));

#if defined(OS_CHROMEOS)
  chromeos::LoginState::Shutdown();
#endif
}

}  // namespace test
}  // namespace ash