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