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/env/public/athena_env.h"
8#include "athena/extensions/public/extensions_delegate.h"
9#include "athena/main/public/athena_launcher.h"
10#include "athena/test/sample_activity_factory.h"
11#include "athena/test/test_app_model_builder.h"
12#include "base/message_loop/message_loop.h"
13#include "base/run_loop.h"
14#include "base/threading/thread.h"
15#include "chromeos/dbus/dbus_thread_manager.h"
16#include "chromeos/network/network_handler.h"
17#include "ui/aura/env.h"
18#include "ui/aura/input_state_lookup.h"
19#include "ui/aura/test/env_test_helper.h"
20#include "ui/base/ime/input_method_initializer.h"
21#include "ui/compositor/scoped_animation_duration_scale_mode.h"
22#include "ui/wm/core/focus_controller.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
29namespace athena {
30namespace test {
31
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
41AthenaTestHelper::~AthenaTestHelper() {
42  CHECK(setup_called_) << "AthenaTestHelper::SetUp() never called.";
43  CHECK(teardown_called_) << "AthenaTestHelper::TearDown() never called.";
44}
45
46void AthenaTestHelper::SetUp(ui::ContextFactory* context_factory) {
47  setup_called_ = true;
48  file_thread_.reset(new base::Thread("FileThread"));
49  base::Thread::Options options(base::MessageLoop::TYPE_IO, 0);
50  file_thread_->StartWithOptions(options);
51
52  chromeos::DBusThreadManager::Initialize();
53  chromeos::NetworkHandler::Initialize();
54  ui::InitializeInputMethodForTesting();
55  aura::Env::CreateInstance(true);
56  aura::Env::GetInstance()->set_context_factory(context_factory);
57
58  // Unit tests generally don't want to query the system, rather use the state
59  // from RootWindow.
60  aura::test::EnvTestHelper(aura::Env::GetInstance())
61      .SetInputStateLookup(scoped_ptr<aura::InputStateLookup>());
62
63  // TODO(oshima): Use a BlockingPool task runner.
64  athena::StartAthenaEnv(file_thread_->message_loop_proxy());
65  athena::ExtensionsDelegate::CreateExtensionsDelegateForTest();
66  athena::StartAthenaSession(new SampleActivityFactory(),
67                             new TestAppModelBuilder());
68}
69
70void AthenaTestHelper::TearDown() {
71  teardown_called_ = true;
72
73  athena::ShutdownAthena();
74  aura::Env::DeleteInstance();
75
76#if defined(USE_X11)
77  ui::test::ResetXCursorCache();
78#endif
79
80  ui::ShutdownInputMethodForTesting();
81  chromeos::NetworkHandler::Shutdown();
82  chromeos::DBusThreadManager::Shutdown();
83}
84
85aura::Window* AthenaTestHelper::GetRootWindow() {
86  return GetHost()->window();
87}
88
89aura::WindowTreeHost* AthenaTestHelper::GetHost() {
90  return AthenaEnv::Get()->GetHost();
91}
92
93void AthenaTestHelper::RunAllPendingInMessageLoop() {
94  // TODO(jbates) crbug.com/134753 Find quitters of this RunLoop and have them
95  //              use run_loop.QuitClosure().
96  base::RunLoop run_loop;
97  run_loop.RunUntilIdle();
98}
99
100}  // namespace test
101}  // namespace athena
102