ash_test_base.h revision 116680a4aac90f2aa7413d9095a592090648e557
11320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Copyright (c) 2012 The Chromium Authors. All rights reserved.
21320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Use of this source code is governed by a BSD-style license that can be
31320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// found in the LICENSE file.
41320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#ifndef ASH_TEST_ASH_TEST_BASE_H_
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define ASH_TEST_ASH_TEST_BASE_H_
71320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include <string>
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/compiler_specific.h"
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/message_loop/message_loop.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/threading/thread.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/public/test/test_browser_thread_bundle.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "testing/gtest/include/gtest/gtest.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "third_party/skia/include/core/SkColor.h"
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "ui/wm/public/window_types.h"
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(OS_WIN)
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "ui/base/win/scoped_ole_initializer.h"
201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace gfx {
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass Rect;
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace ui {
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace test {
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass EventGenerator;
291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace aura {
331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass RootWindow;
341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass Window;
351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass WindowDelegate;
36}  // namespace aura
37
38namespace ash {
39class DisplayManager;
40
41namespace test {
42
43class AshTestHelper;
44class TestScreenshotDelegate;
45class TestSystemTrayDelegate;
46#if defined(OS_WIN)
47class TestMetroViewerProcessHost;
48#endif
49
50class AshTestBase : public testing::Test {
51 public:
52  AshTestBase();
53  virtual ~AshTestBase();
54
55  // testing::Test:
56  virtual void SetUp() OVERRIDE;
57  virtual void TearDown() OVERRIDE;
58
59  // Update the display configuration as given in |display_specs|.
60  // See ash::test::DisplayManagerTestApi::UpdateDisplay for more details.
61  void UpdateDisplay(const std::string& display_specs);
62
63  // Returns a root Window. Usually this is the active root Window, but that
64  // method can return NULL sometimes, and in those cases, we fall back on the
65  // primary root Window.
66  aura::Window* CurrentContext();
67
68  // Versions of the functions in aura::test:: that go through our shell
69  // StackingController instead of taking a parent.
70  aura::Window* CreateTestWindowInShellWithId(int id);
71  aura::Window* CreateTestWindowInShellWithBounds(const gfx::Rect& bounds);
72  aura::Window* CreateTestWindowInShell(SkColor color,
73                                        int id,
74                                        const gfx::Rect& bounds);
75  aura::Window* CreateTestWindowInShellWithDelegate(
76      aura::WindowDelegate* delegate,
77      int id,
78      const gfx::Rect& bounds);
79  aura::Window* CreateTestWindowInShellWithDelegateAndType(
80      aura::WindowDelegate* delegate,
81      ui::wm::WindowType type,
82      int id,
83      const gfx::Rect& bounds);
84
85  // Attach |window| to the current shell's root window.
86  void ParentWindowInPrimaryRootWindow(aura::Window* window);
87
88  // Returns the EventGenerator that uses screen coordinates and works
89  // across multiple displays. It createse a new generator if it
90  // hasn't been created yet.
91  ui::test::EventGenerator& GetEventGenerator();
92
93 protected:
94  enum UserSessionBlockReason {
95    FIRST_BLOCK_REASON,
96    BLOCKED_BY_LOCK_SCREEN = FIRST_BLOCK_REASON,
97    BLOCKED_BY_LOGIN_SCREEN,
98    BLOCKED_BY_USER_ADDING_SCREEN,
99    NUMBER_OF_BLOCK_REASONS
100  };
101
102  // Proxy to AshTestHelper::SupportsMultipleDisplays().
103  static bool SupportsMultipleDisplays();
104
105  // Proxy to AshTestHelper::SupportsHostWindowResize().
106  static bool SupportsHostWindowResize();
107
108  void set_start_session(bool start_session) { start_session_ = start_session; }
109
110  AshTestHelper* ash_test_helper() { return ash_test_helper_.get(); }
111
112  void RunAllPendingInMessageLoop();
113
114  TestScreenshotDelegate* GetScreenshotDelegate();
115  TestSystemTrayDelegate* GetSystemTrayDelegate();
116
117  // Utility methods to emulate user logged in or not, session started or not
118  // and user able to lock screen or not cases.
119  void SetSessionStarted(bool session_started);
120  void SetUserLoggedIn(bool user_logged_in);
121  void SetCanLockScreen(bool can_lock_screen);
122  void SetShouldLockScreenBeforeSuspending(bool should_lock);
123  void SetUserAddingScreenRunning(bool user_adding_screen_running);
124
125  // Methods to emulate blocking and unblocking user session with given
126  // |block_reason|.
127  void BlockUserSession(UserSessionBlockReason block_reason);
128  void UnblockUserSession();
129
130 private:
131  bool setup_called_;
132  bool teardown_called_;
133  // |SetUp()| doesn't activate session if this is set to false.
134  bool start_session_;
135  scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_;
136  scoped_ptr<AshTestHelper> ash_test_helper_;
137  scoped_ptr<ui::test::EventGenerator> event_generator_;
138#if defined(OS_WIN)
139  // Note that the order is important here as ipc_thread_ should be destroyed
140  // after metro_viewer_host_->channel_.
141  scoped_ptr<base::Thread> ipc_thread_;
142  scoped_ptr<TestMetroViewerProcessHost> metro_viewer_host_;
143  ui::ScopedOleInitializer ole_initializer_;
144#endif
145
146  DISALLOW_COPY_AND_ASSIGN(AshTestBase);
147};
148
149class NoSessionAshTestBase : public AshTestBase {
150 public:
151  NoSessionAshTestBase() {
152    set_start_session(false);
153  }
154  virtual ~NoSessionAshTestBase() {}
155
156 private:
157  DISALLOW_COPY_AND_ASSIGN(NoSessionAshTestBase);
158};
159
160}  // namespace test
161}  // namespace ash
162
163#endif  // ASH_TEST_ASH_TEST_BASE_H_
164