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 #include "ui/aura/test/aura_test_helper.h" 6 7 #include "base/message_loop/message_loop.h" 8 #include "base/run_loop.h" 9 #include "ui/aura/client/aura_constants.h" 10 #include "ui/aura/client/default_activation_client.h" 11 #include "ui/aura/client/default_capture_client.h" 12 #include "ui/aura/client/focus_client.h" 13 #include "ui/aura/env.h" 14 #include "ui/aura/input_state_lookup.h" 15 #include "ui/aura/root_window.h" 16 #include "ui/aura/test/env_test_helper.h" 17 #include "ui/aura/test/test_focus_client.h" 18 #include "ui/aura/test/test_screen.h" 19 #include "ui/aura/test/test_window_tree_client.h" 20 #include "ui/base/ime/dummy_input_method.h" 21 #include "ui/base/ime/input_method_initializer.h" 22 #include "ui/compositor/compositor.h" 23 #include "ui/compositor/layer_animator.h" 24 #include "ui/compositor/scoped_animation_duration_scale_mode.h" 25 #include "ui/compositor/test/context_factories_for_test.h" 26 #include "ui/gfx/screen.h" 27 28 #if defined(USE_X11) 29 #include "ui/aura/root_window_host_x11.h" 30 #include "ui/base/x/x11_util.h" 31 #endif 32 33 #if defined(USE_OZONE) 34 #include "ui/gfx/ozone/surface_factory_ozone.h" 35 #endif 36 37 namespace aura { 38 namespace test { 39 AuraTestHelper(base::MessageLoopForUI * message_loop)40AuraTestHelper::AuraTestHelper(base::MessageLoopForUI* message_loop) 41 : setup_called_(false), 42 teardown_called_(false), 43 owns_root_window_(false) { 44 DCHECK(message_loop); 45 message_loop_ = message_loop; 46 // Disable animations during tests. 47 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode( 48 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION)); 49 #if defined(USE_X11) 50 test::SetUseOverrideRedirectWindowByDefault(true); 51 #endif 52 #if defined(USE_OZONE) 53 surface_factory_.reset(gfx::SurfaceFactoryOzone::CreateTestHelper()); 54 gfx::SurfaceFactoryOzone::SetInstance(surface_factory_.get()); 55 #endif 56 } 57 ~AuraTestHelper()58AuraTestHelper::~AuraTestHelper() { 59 CHECK(setup_called_) 60 << "AuraTestHelper::SetUp() never called."; 61 CHECK(teardown_called_) 62 << "AuraTestHelper::TearDown() never called."; 63 } 64 SetUp()65void AuraTestHelper::SetUp() { 66 setup_called_ = true; 67 68 // The ContextFactory must exist before any Compositors are created. 69 bool allow_test_contexts = true; 70 ui::InitializeContextFactoryForTests(allow_test_contexts); 71 72 Env::CreateInstance(); 73 // Unit tests generally don't want to query the system, rather use the state 74 // from RootWindow. 75 EnvTestHelper(Env::GetInstance()).SetInputStateLookup( 76 scoped_ptr<InputStateLookup>()); 77 78 ui::InitializeInputMethodForTesting(); 79 80 test_screen_.reset(TestScreen::Create()); 81 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen_.get()); 82 root_window_.reset(test_screen_->CreateRootWindowForPrimaryDisplay()); 83 84 focus_client_.reset(new TestFocusClient); 85 client::SetFocusClient(root_window(), focus_client_.get()); 86 stacking_client_.reset(new TestWindowTreeClient(root_window())); 87 activation_client_.reset( 88 new client::DefaultActivationClient(root_window())); 89 capture_client_.reset(new client::DefaultCaptureClient(root_window())); 90 test_input_method_.reset(new ui::DummyInputMethod); 91 root_window()->SetProperty( 92 client::kRootWindowInputMethodKey, 93 test_input_method_.get()); 94 95 root_window()->Show(); 96 // Ensure width != height so tests won't confuse them. 97 dispatcher()->SetHostSize(gfx::Size(800, 600)); 98 } 99 TearDown()100void AuraTestHelper::TearDown() { 101 teardown_called_ = true; 102 test_input_method_.reset(); 103 stacking_client_.reset(); 104 activation_client_.reset(); 105 capture_client_.reset(); 106 focus_client_.reset(); 107 client::SetFocusClient(root_window(), NULL); 108 root_window_.reset(); 109 test_screen_.reset(); 110 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL); 111 112 #if defined(USE_X11) 113 ui::ResetXCursorCache(); 114 #endif 115 116 ui::ShutdownInputMethodForTesting(); 117 118 Env::DeleteInstance(); 119 120 ui::TerminateContextFactoryForTests(); 121 } 122 RunAllPendingInMessageLoop()123void AuraTestHelper::RunAllPendingInMessageLoop() { 124 // TODO(jbates) crbug.com/134753 Find quitters of this RunLoop and have them 125 // use run_loop.QuitClosure(). 126 base::RunLoop run_loop(Env::GetInstance()->GetDispatcher()); 127 run_loop.RunUntilIdle(); 128 } 129 130 } // namespace test 131 } // namespace aura 132