ash_test_base.h revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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/aura/client/window_types.h" 17#include "ui/views/test/test_views_delegate.h" 18 19#if defined(OS_WIN) 20#include "ui/base/win/scoped_ole_initializer.h" 21#endif 22 23namespace aura { 24class RootWindow; 25class Window; 26class WindowDelegate; 27 28namespace test { 29class EventGenerator; 30} // namespace test 31} // namespace aura 32 33namespace ash { 34namespace internal { 35class DisplayManager; 36} // namespace internal 37 38namespace test { 39 40class AshTestHelper; 41class TestScreenshotDelegate; 42#if defined(OS_WIN) 43class TestMetroViewerProcessHost; 44#endif 45 46class AshTestViewsDelegate : public views::TestViewsDelegate { 47 public: 48 // Overriden from TestViewsDelegate. 49 virtual content::WebContents* CreateWebContents( 50 content::BrowserContext* browser_context, 51 content::SiteInstance* site_instance) OVERRIDE; 52}; 53 54class AshTestBase : public testing::Test { 55 public: 56 AshTestBase(); 57 virtual ~AshTestBase(); 58 59 // testing::Test: 60 virtual void SetUp() OVERRIDE; 61 virtual void TearDown() OVERRIDE; 62 63 // Update the display configuration as given in |display_specs|. 64 // See ash::test::DisplayManagerTestApi::UpdateDisplay for more details. 65 void UpdateDisplay(const std::string& display_specs); 66 67 // Returns a root Window. Usually this is the active root Window, but that 68 // method can return NULL sometimes, and in those cases, we fall back on the 69 // primary root Window. 70 aura::Window* CurrentContext(); 71 72 // Versions of the functions in aura::test:: that go through our shell 73 // StackingController instead of taking a parent. 74 aura::Window* CreateTestWindowInShellWithId(int id); 75 aura::Window* CreateTestWindowInShellWithBounds(const gfx::Rect& bounds); 76 aura::Window* CreateTestWindowInShell(SkColor color, 77 int id, 78 const gfx::Rect& bounds); 79 aura::Window* CreateTestWindowInShellWithDelegate( 80 aura::WindowDelegate* delegate, 81 int id, 82 const gfx::Rect& bounds); 83 aura::Window* CreateTestWindowInShellWithDelegateAndType( 84 aura::WindowDelegate* delegate, 85 aura::client::WindowType type, 86 int id, 87 const gfx::Rect& bounds); 88 89 // Attach |window| to the current shell's root window. 90 void ParentWindowInPrimaryRootWindow(aura::Window* window); 91 92 // Returns the EventGenerator that uses screen coordinates and works 93 // across multiple displays. It createse a new generator if it 94 // hasn't been created yet. 95 aura::test::EventGenerator& GetEventGenerator(); 96 97 protected: 98 enum UserSessionBlockReason { 99 FIRST_BLOCK_REASON, 100 BLOCKED_BY_LOCK_SCREEN = FIRST_BLOCK_REASON, 101 BLOCKED_BY_LOGIN_SCREEN, 102 BLOCKED_BY_USER_ADDING_SCREEN, 103 NUMBER_OF_BLOCK_REASONS 104 }; 105 106 // True if the running environment supports multiple displays, 107 // or false otherwise (e.g. win8 bot). 108 static bool SupportsMultipleDisplays(); 109 110 // True if the running environment supports host window resize, 111 // or false otherwise (e.g. win8 bot). 112 static bool SupportsHostWindowResize(); 113 114 void set_start_session(bool start_session) { start_session_ = start_session; } 115 116 void RunAllPendingInMessageLoop(); 117 118 TestScreenshotDelegate* GetScreenshotDelegate(); 119 120 // Utility methods to emulate user logged in or not, session started or not 121 // and user able to lock screen or not cases. 122 void SetSessionStarted(bool session_started); 123 void SetUserLoggedIn(bool user_logged_in); 124 void SetCanLockScreen(bool can_lock_screen); 125 void SetShouldLockScreenBeforeSuspending(bool should_lock); 126 void SetUserAddingScreenRunning(bool user_adding_screen_running); 127 128 // Methods to emulate blocking and unblocking user session with given 129 // |block_reason|. 130 void BlockUserSession(UserSessionBlockReason block_reason); 131 void UnblockUserSession(); 132 133 private: 134 bool setup_called_; 135 bool teardown_called_; 136 // |SetUp()| doesn't activate session if this is set to false. 137 bool start_session_; 138 content::TestBrowserThreadBundle thread_bundle_; 139 scoped_ptr<AshTestHelper> ash_test_helper_; 140 scoped_ptr<aura::test::EventGenerator> event_generator_; 141#if defined(OS_WIN) 142 // Note that the order is important here as ipc_thread_ should be destroyed 143 // after metro_viewer_host_->channel_. 144 scoped_ptr<base::Thread> ipc_thread_; 145 scoped_ptr<TestMetroViewerProcessHost> metro_viewer_host_; 146 ui::ScopedOleInitializer ole_initializer_; 147#endif 148 149 DISALLOW_COPY_AND_ASSIGN(AshTestBase); 150}; 151 152class NoSessionAshTestBase : public AshTestBase { 153 public: 154 NoSessionAshTestBase() { 155 set_start_session(false); 156 } 157 virtual ~NoSessionAshTestBase() {} 158 159 private: 160 DISALLOW_COPY_AND_ASSIGN(NoSessionAshTestBase); 161}; 162 163} // namespace test 164} // namespace ash 165 166#endif // ASH_TEST_ASH_TEST_BASE_H_ 167