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