1 // Copyright 2014 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 "athena/test/athena_test_helper.h" 6 7 #include "athena/main/athena_launcher.h" 8 #include "athena/test/sample_activity_factory.h" 9 #include "athena/test/test_app_model_builder.h" 10 #include "base/message_loop/message_loop.h" 11 #include "base/run_loop.h" 12 #include "ui/aura/client/aura_constants.h" 13 #include "ui/aura/env.h" 14 #include "ui/aura/input_state_lookup.h" 15 #include "ui/aura/test/env_test_helper.h" 16 #include "ui/aura/test/test_focus_client.h" 17 #include "ui/aura/test/test_screen.h" 18 #include "ui/aura/window_event_dispatcher.h" 19 #include "ui/base/ime/input_method_initializer.h" 20 #include "ui/compositor/scoped_animation_duration_scale_mode.h" 21 #include "ui/gfx/screen.h" 22 #include "ui/wm/core/default_activation_client.h" 23 #include "ui/wm/core/input_method_event_filter.h" 24 25 #if defined(USE_X11) 26 #include "ui/base/x/x11_util.h" 27 #endif 28 29 namespace athena { 30 namespace test { 31 AthenaTestHelper(base::MessageLoopForUI * message_loop)32AthenaTestHelper::AthenaTestHelper(base::MessageLoopForUI* message_loop) 33 : setup_called_(false), teardown_called_(false) { 34 DCHECK(message_loop); 35 message_loop_ = message_loop; 36 // Disable animations during tests. 37 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode( 38 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION)); 39 } 40 ~AthenaTestHelper()41AthenaTestHelper::~AthenaTestHelper() { 42 CHECK(setup_called_) << "AthenaTestHelper::SetUp() never called."; 43 CHECK(teardown_called_) << "AthenaTestHelper::TearDown() never called."; 44 } 45 SetUp(ui::ContextFactory * context_factory)46void AthenaTestHelper::SetUp(ui::ContextFactory* context_factory) { 47 setup_called_ = true; 48 49 aura::Env::CreateInstance(true); 50 aura::Env::GetInstance()->set_context_factory(context_factory); 51 52 // Unit tests generally don't want to query the system, rather use the state 53 // from RootWindow. 54 aura::test::EnvTestHelper(aura::Env::GetInstance()) 55 .SetInputStateLookup(scoped_ptr<aura::InputStateLookup>()); 56 57 ui::InitializeInputMethodForTesting(); 58 59 const gfx::Size host_size(800, 600); 60 test_screen_.reset(aura::TestScreen::Create(host_size)); 61 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen_.get()); 62 host_.reset(test_screen_->CreateHostForPrimaryDisplay()); 63 64 input_method_filter_.reset(new ::wm::InputMethodEventFilter( 65 root_window()->GetHost()->GetAcceleratedWidget())); 66 input_method_filter_->SetInputMethodPropertyInRootWindow( 67 root_window()); 68 69 // TODO(oshima): Switch to athena implementation. 70 focus_client_.reset(new aura::test::TestFocusClient); 71 aura::client::SetFocusClient(root_window(), 72 focus_client_.get()); 73 new ::wm::DefaultActivationClient(root_window()); 74 75 root_window()->Show(); 76 // Ensure width != height so tests won't confuse them. 77 host()->SetBounds(gfx::Rect(host_size)); 78 79 athena::StartAthena(root_window(), 80 new SampleActivityFactory(), 81 new TestAppModelBuilder()); 82 } 83 TearDown()84void AthenaTestHelper::TearDown() { 85 teardown_called_ = true; 86 87 athena::ShutdownAthena(); 88 89 aura::client::SetFocusClient(root_window(), NULL); 90 focus_client_.reset(); 91 input_method_filter_.reset(); 92 93 host_.reset(); 94 ui::GestureRecognizer::Reset(); 95 test_screen_.reset(); 96 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL); 97 98 #if defined(USE_X11) 99 ui::test::ResetXCursorCache(); 100 #endif 101 102 ui::ShutdownInputMethodForTesting(); 103 104 aura::Env::DeleteInstance(); 105 } 106 RunAllPendingInMessageLoop()107void AthenaTestHelper::RunAllPendingInMessageLoop() { 108 // TODO(jbates) crbug.com/134753 Find quitters of this RunLoop and have them 109 // use run_loop.QuitClosure(). 110 base::RunLoop run_loop; 111 run_loop.RunUntilIdle(); 112 } 113 114 } // namespace test 115 } // namespace athena 116