| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/aura/test/aura_test_helper.h" | 5 #include "ui/aura/test/aura_test_helper.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "ui/aura/client/default_capture_client.h" | 10 #include "ui/aura/client/default_capture_client.h" |
| 11 #include "ui/aura/client/focus_client.h" | 11 #include "ui/aura/client/focus_client.h" |
| 12 #include "ui/aura/env.h" | 12 #include "ui/aura/env.h" |
| 13 #include "ui/aura/input_state_lookup.h" | 13 #include "ui/aura/input_state_lookup.h" |
| 14 #include "ui/aura/mus/window_port_mus.h" | 14 #include "ui/aura/mus/window_port_mus.h" |
| 15 #include "ui/aura/mus/window_tree_client.h" | 15 #include "ui/aura/mus/window_tree_client.h" |
| 16 #include "ui/aura/test/env_test_helper.h" | 16 #include "ui/aura/test/env_test_helper.h" |
| 17 #include "ui/aura/test/event_generator_delegate_aura.h" | 17 #include "ui/aura/test/event_generator_delegate_aura.h" |
| 18 #include "ui/aura/test/mus/test_window_tree.h" | 18 #include "ui/aura/test/mus/test_window_tree.h" |
| 19 #include "ui/aura/test/mus/test_window_tree_client_setup.h" | 19 #include "ui/aura/test/mus/test_window_tree_client_setup.h" |
| 20 #include "ui/aura/test/test_focus_client.h" | 20 #include "ui/aura/test/test_focus_client.h" |
| 21 #include "ui/aura/test/test_screen.h" | 21 #include "ui/aura/test/test_screen.h" |
| 22 #include "ui/aura/test/test_window_parenting_client.h" | 22 #include "ui/aura/test/test_window_parenting_client.h" |
| 23 #include "ui/aura/window.h" | 23 #include "ui/aura/window.h" |
| 24 #include "ui/aura/window_event_dispatcher.h" | 24 #include "ui/aura/window_event_dispatcher.h" |
| 25 #include "ui/aura/window_port_local.h" |
| 25 #include "ui/base/ime/input_method_factory.h" | 26 #include "ui/base/ime/input_method_factory.h" |
| 26 #include "ui/base/ime/input_method_initializer.h" | 27 #include "ui/base/ime/input_method_initializer.h" |
| 27 #include "ui/compositor/compositor.h" | 28 #include "ui/compositor/compositor.h" |
| 28 #include "ui/compositor/layer_animator.h" | 29 #include "ui/compositor/layer_animator.h" |
| 29 #include "ui/compositor/scoped_animation_duration_scale_mode.h" | 30 #include "ui/compositor/scoped_animation_duration_scale_mode.h" |
| 30 #include "ui/display/screen.h" | 31 #include "ui/display/screen.h" |
| 31 #include "ui/wm/core/wm_state.h" | 32 #include "ui/wm/core/wm_state.h" |
| 32 | 33 |
| 33 #if defined(USE_X11) | 34 #if defined(USE_X11) |
| 34 #include "ui/aura/window_tree_host_x11.h" | 35 #include "ui/aura/window_tree_host_x11.h" |
| 35 #include "ui/base/x/x11_util.h" // nogncheck | 36 #include "ui/base/x/x11_util.h" // nogncheck |
| 36 #endif | 37 #endif |
| 37 | 38 |
| 38 namespace aura { | 39 namespace aura { |
| 39 namespace test { | 40 namespace test { |
| 40 | 41 |
| 41 AuraTestHelper::AuraTestHelper(base::MessageLoopForUI* message_loop) | 42 AuraTestHelper::AuraTestHelper(base::MessageLoopForUI* message_loop) |
| 42 : setup_called_(false), | 43 : setup_called_(false), teardown_called_(false) { |
| 43 teardown_called_(false) { | |
| 44 DCHECK(message_loop); | 44 DCHECK(message_loop); |
| 45 message_loop_ = message_loop; | |
| 46 // Disable animations during tests. | 45 // Disable animations during tests. |
| 47 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode( | 46 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode( |
| 48 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION)); | 47 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION)); |
| 49 #if defined(USE_X11) | 48 #if defined(USE_X11) |
| 50 test::SetUseOverrideRedirectWindowByDefault(true); | 49 test::SetUseOverrideRedirectWindowByDefault(true); |
| 51 #endif | 50 #endif |
| 52 InitializeAuraEventGeneratorDelegate(); | 51 InitializeAuraEventGeneratorDelegate(); |
| 53 } | 52 } |
| 54 | 53 |
| 55 AuraTestHelper::~AuraTestHelper() { | 54 AuraTestHelper::~AuraTestHelper() { |
| 56 CHECK(setup_called_) | 55 CHECK(setup_called_) |
| 57 << "AuraTestHelper::SetUp() never called."; | 56 << "AuraTestHelper::SetUp() never called."; |
| 58 CHECK(teardown_called_) | 57 CHECK(teardown_called_) |
| 59 << "AuraTestHelper::TearDown() never called."; | 58 << "AuraTestHelper::TearDown() never called."; |
| 60 } | 59 } |
| 61 | 60 |
| 62 void AuraTestHelper::EnableMus(WindowTreeClientDelegate* window_tree_delegate, | 61 void AuraTestHelper::EnableMusWithTestWindowTree( |
| 63 WindowManagerDelegate* window_manager_delegate) { | 62 WindowTreeClientDelegate* window_tree_delegate, |
| 63 WindowManagerDelegate* window_manager_delegate) { |
| 64 DCHECK(!setup_called_); | 64 DCHECK(!setup_called_); |
| 65 use_mus_ = true; | 65 DCHECK_EQ(Mode::LOCAL, mode_); |
| 66 mode_ = Mode::MUS_CREATE_WINDOW_TREE_CLIENT; |
| 66 window_tree_delegate_ = window_tree_delegate; | 67 window_tree_delegate_ = window_tree_delegate; |
| 67 window_manager_delegate_ = window_manager_delegate; | 68 window_manager_delegate_ = window_manager_delegate; |
| 68 } | 69 } |
| 69 | 70 |
| 71 void AuraTestHelper::EnableMusWithWindowTreeClient( |
| 72 WindowTreeClient* window_tree_client) { |
| 73 DCHECK(!setup_called_); |
| 74 DCHECK_EQ(Mode::LOCAL, mode_); |
| 75 mode_ = Mode::MUS; |
| 76 window_tree_client_ = window_tree_client; |
| 77 } |
| 78 |
| 70 void AuraTestHelper::SetUp(ui::ContextFactory* context_factory) { | 79 void AuraTestHelper::SetUp(ui::ContextFactory* context_factory) { |
| 71 setup_called_ = true; | 80 setup_called_ = true; |
| 72 | 81 |
| 73 wm_state_ = base::MakeUnique<wm::WMState>(); | 82 if (mode_ != Mode::MUS) { |
| 83 // Assume if an explicit WindowTreeClient was created then a WmState was |
| 84 // already created. |
| 85 wm_state_ = base::MakeUnique<wm::WMState>(); |
| 86 } |
| 74 // Needs to be before creating WindowTreeClient. | 87 // Needs to be before creating WindowTreeClient. |
| 75 focus_client_ = base::MakeUnique<TestFocusClient>(); | 88 focus_client_ = base::MakeUnique<TestFocusClient>(); |
| 76 capture_client_ = base::MakeUnique<client::DefaultCaptureClient>(); | 89 capture_client_ = base::MakeUnique<client::DefaultCaptureClient>(); |
| 77 Env::WindowPortFactory window_impl_factory; | 90 Env::WindowPortFactory window_impl_factory = |
| 78 if (use_mus_) | 91 base::Bind(&AuraTestHelper::CreateWindowPort, base::Unretained(this)); |
| 79 window_impl_factory = InitMus(); | 92 if (mode_ == Mode::MUS_CREATE_WINDOW_TREE_CLIENT) |
| 93 InitWindowTreeClient(); |
| 80 if (!Env::GetInstanceDontCreate()) | 94 if (!Env::GetInstanceDontCreate()) |
| 81 env_ = aura::Env::CreateInstance(window_impl_factory); | 95 env_ = Env::CreateInstance(window_impl_factory); |
| 82 else if (use_mus_) | 96 else |
| 83 EnvTestHelper(Env::GetInstance()).SetWindowPortFactory(window_impl_factory); | 97 EnvTestHelper(Env::GetInstance()).SetWindowPortFactory(window_impl_factory); |
| 84 Env::GetInstance()->set_context_factory(context_factory); | 98 Env::GetInstance()->set_context_factory(context_factory); |
| 85 // Unit tests generally don't want to query the system, rather use the state | 99 // Unit tests generally don't want to query the system, rather use the state |
| 86 // from RootWindow. | 100 // from RootWindow. |
| 87 EnvTestHelper env_helper(Env::GetInstance()); | 101 EnvTestHelper env_helper(Env::GetInstance()); |
| 88 env_helper.SetInputStateLookup(nullptr); | 102 env_helper.SetInputStateLookup(nullptr); |
| 89 env_helper.ResetEventState(); | 103 env_helper.ResetEventState(); |
| 90 | 104 |
| 91 ui::InitializeInputMethodForTesting(); | 105 ui::InitializeInputMethodForTesting(); |
| 92 | 106 |
| 93 display::Screen* screen = display::Screen::GetScreen(); | 107 display::Screen* screen = display::Screen::GetScreen(); |
| 94 gfx::Size host_size(screen ? screen->GetPrimaryDisplay().GetSizeInPixel() | 108 gfx::Size host_size(screen ? screen->GetPrimaryDisplay().GetSizeInPixel() |
| 95 : gfx::Size(800, 600)); | 109 : gfx::Size(800, 600)); |
| 96 // TODO(sky): creating the screen and host should not happen for mus. | 110 // TODO(sky): creating the screen and host should not happen for mus. |
| 97 test_screen_.reset(TestScreen::Create(host_size)); | 111 test_screen_.reset(TestScreen::Create(host_size)); |
| 98 if (!screen) | 112 if (!screen) |
| 99 display::Screen::SetScreenInstance(test_screen_.get()); | 113 display::Screen::SetScreenInstance(test_screen_.get()); |
| 100 host_.reset(test_screen_->CreateHostForPrimaryDisplay()); | 114 host_.reset(test_screen_->CreateHostForPrimaryDisplay()); |
| 101 | 115 |
| 102 client::SetFocusClient(root_window(), focus_client_.get()); | 116 client::SetFocusClient(root_window(), focus_client_.get()); |
| 103 client::SetCaptureClient(root_window(), capture_client()); | 117 client::SetCaptureClient(root_window(), capture_client()); |
| 104 parenting_client_.reset(new TestWindowParentingClient(root_window())); | 118 parenting_client_.reset(new TestWindowParentingClient(root_window())); |
| 105 | 119 |
| 106 root_window()->Show(); | 120 root_window()->Show(); |
| 107 // Ensure width != height so tests won't confuse them. | 121 // Ensure width != height so tests won't confuse them. |
| 108 host()->SetBounds(gfx::Rect(host_size)); | 122 host()->SetBounds(gfx::Rect(host_size)); |
| 109 | 123 |
| 110 if (use_mus_) | 124 if (mode_ == Mode::MUS_CREATE_WINDOW_TREE_CLIENT) |
| 111 window_tree()->AckAllChanges(); | 125 window_tree()->AckAllChanges(); |
| 112 } | 126 } |
| 113 | 127 |
| 114 void AuraTestHelper::TearDown() { | 128 void AuraTestHelper::TearDown() { |
| 115 teardown_called_ = true; | 129 teardown_called_ = true; |
| 116 parenting_client_.reset(); | 130 parenting_client_.reset(); |
| 117 client::SetFocusClient(root_window(), nullptr); | 131 client::SetFocusClient(root_window(), nullptr); |
| 118 client::SetCaptureClient(root_window(), nullptr); | 132 client::SetCaptureClient(root_window(), nullptr); |
| 119 host_.reset(); | 133 host_.reset(); |
| 120 ui::GestureRecognizer::Reset(); | 134 ui::GestureRecognizer::Reset(); |
| 121 if (display::Screen::GetScreen() == test_screen_.get()) | 135 if (display::Screen::GetScreen() == test_screen_.get()) |
| 122 display::Screen::SetScreenInstance(nullptr); | 136 display::Screen::SetScreenInstance(nullptr); |
| 123 test_screen_.reset(); | 137 test_screen_.reset(); |
| 124 | 138 |
| 125 #if defined(USE_X11) | 139 #if defined(USE_X11) |
| 126 ui::test::ResetXCursorCache(); | 140 ui::test::ResetXCursorCache(); |
| 127 #endif | 141 #endif |
| 128 | 142 |
| 129 window_tree_client_setup_.reset(); | 143 window_tree_client_setup_.reset(); |
| 130 focus_client_.reset(); | 144 focus_client_.reset(); |
| 131 capture_client_.reset(); | 145 capture_client_.reset(); |
| 132 | 146 |
| 133 ui::ShutdownInputMethodForTesting(); | 147 ui::ShutdownInputMethodForTesting(); |
| 134 | 148 |
| 135 if (env_) { | 149 if (env_) { |
| 136 env_.reset(); | 150 env_.reset(); |
| 137 } else if (use_mus_) { | 151 } else { |
| 138 EnvTestHelper(Env::GetInstance()) | 152 EnvTestHelper(Env::GetInstance()) |
| 139 .SetWindowPortFactory(Env::WindowPortFactory()); | 153 .SetWindowPortFactory(Env::WindowPortFactory()); |
| 140 } | 154 } |
| 141 wm_state_.reset(); | 155 wm_state_.reset(); |
| 142 } | 156 } |
| 143 | 157 |
| 144 void AuraTestHelper::RunAllPendingInMessageLoop() { | 158 void AuraTestHelper::RunAllPendingInMessageLoop() { |
| 145 // TODO(jbates) crbug.com/134753 Find quitters of this RunLoop and have them | 159 // TODO(jbates) crbug.com/134753 Find quitters of this RunLoop and have them |
| 146 // use run_loop.QuitClosure(). | 160 // use run_loop.QuitClosure(). |
| 147 base::RunLoop run_loop; | 161 base::RunLoop run_loop; |
| 148 run_loop.RunUntilIdle(); | 162 run_loop.RunUntilIdle(); |
| 149 } | 163 } |
| 150 | 164 |
| 151 TestWindowTree* AuraTestHelper::window_tree() { | 165 TestWindowTree* AuraTestHelper::window_tree() { |
| 152 return window_tree_client_setup_->window_tree(); | 166 return window_tree_client_setup_->window_tree(); |
| 153 } | 167 } |
| 154 | 168 |
| 155 WindowTreeClient* AuraTestHelper::window_tree_client() { | 169 WindowTreeClient* AuraTestHelper::window_tree_client() { |
| 156 return window_tree_client_setup_->window_tree_client(); | 170 return window_tree_client_; |
| 157 } | 171 } |
| 158 | 172 |
| 159 client::CaptureClient* AuraTestHelper::capture_client() { | 173 client::CaptureClient* AuraTestHelper::capture_client() { |
| 160 return capture_client_.get(); | 174 return capture_client_.get(); |
| 161 } | 175 } |
| 162 | 176 |
| 163 Env::WindowPortFactory AuraTestHelper::InitMus() { | 177 void AuraTestHelper::InitWindowTreeClient() { |
| 164 window_tree_client_setup_ = base::MakeUnique<TestWindowTreeClientSetup>(); | 178 window_tree_client_setup_ = base::MakeUnique<TestWindowTreeClientSetup>(); |
| 165 window_tree_client_setup_->InitForWindowManager(window_tree_delegate_, | 179 window_tree_client_setup_->InitForWindowManager(window_tree_delegate_, |
| 166 window_manager_delegate_); | 180 window_manager_delegate_); |
| 167 return base::Bind(&AuraTestHelper::CreateWindowPortMus, | 181 window_tree_client_ = window_tree_client_setup_->window_tree_client(); |
| 168 base::Unretained(this)); | |
| 169 } | 182 } |
| 170 | 183 |
| 171 std::unique_ptr<WindowPort> AuraTestHelper::CreateWindowPortMus( | 184 std::unique_ptr<WindowPort> AuraTestHelper::CreateWindowPort(Window* window) { |
| 172 Window* window) { | 185 if (mode_ == Mode::LOCAL) { |
| 173 return base::MakeUnique<WindowPortMus>(window_tree_client(), | 186 std::unique_ptr<WindowPortLocal> window_port = |
| 174 WindowMusType::LOCAL); | 187 base::MakeUnique<WindowPortLocal>(window); |
| 188 return std::move(window_port); |
| 189 } |
| 190 std::unique_ptr<WindowPortMus> window_port = base::MakeUnique<WindowPortMus>( |
| 191 window_tree_client_, WindowMusType::LOCAL); |
| 192 return std::move(window_port); |
| 175 } | 193 } |
| 176 | 194 |
| 177 } // namespace test | 195 } // namespace test |
| 178 } // namespace aura | 196 } // namespace aura |
| OLD | NEW |