ash_test_base.h revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright (c) 2012 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#ifndef ASH_TEST_ASH_TEST_BASE_H_ 6#define ASH_TEST_ASH_TEST_BASE_H_ 7 8#include <string> 9 10#include "base/compiler_specific.h" 11#include "base/message_loop/message_loop.h" 12#include "base/threading/thread.h" 13#include "content/public/test/test_browser_thread_bundle.h" 14#include "testing/gtest/include/gtest/gtest.h" 15#include "third_party/skia/include/core/SkColor.h" 16#include "ui/wm/public/window_types.h" 17 18#if defined(OS_WIN) 19#include "ui/base/win/scoped_ole_initializer.h" 20#endif 21 22namespace gfx { 23class Rect; 24} 25 26namespace ui { 27namespace test { 28class EventGenerator; 29} 30} 31 32namespace aura { 33class RootWindow; 34class Window; 35class 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