diff options
author | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-28 22:35:40 +0000 |
---|---|---|
committer | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-28 22:35:40 +0000 |
commit | a82e12ea4dbc7089cad6501d6687db8a17989911 (patch) | |
tree | 4241c0f9256e44ff8b9a61064721e4fa1b00aab2 /athena | |
parent | 2a8768230c4fe626812d1594f4d782b72b187c19 (diff) | |
download | chromium_src-a82e12ea4dbc7089cad6501d6687db8a17989911.zip chromium_src-a82e12ea4dbc7089cad6501d6687db8a17989911.tar.gz chromium_src-a82e12ea4dbc7089cad6501d6687db8a17989911.tar.bz2 |
Athena unittests framework
BUG=375537
R=abodenha@chromium.org, ben@chromium.org, sadrul@chromium.org
Review URL: https://codereview.chromium.org/301593004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@273378 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'athena')
-rw-r--r-- | athena/athena.gyp | 39 | ||||
-rw-r--r-- | athena/main/DEPS | 10 | ||||
-rw-r--r-- | athena/main/athena_launcher.cc | 53 | ||||
-rw-r--r-- | athena/main/athena_launcher.h | 20 | ||||
-rw-r--r-- | athena/main/athena_main.cc | 28 | ||||
-rw-r--r-- | athena/main/athena_main.gyp | 2 | ||||
-rw-r--r-- | athena/test/DEPS | 9 | ||||
-rw-r--r-- | athena/test/athena_test_base.cc | 54 | ||||
-rw-r--r-- | athena/test/athena_test_base.h | 49 | ||||
-rw-r--r-- | athena/test/athena_test_helper.cc | 113 | ||||
-rw-r--r-- | athena/test/athena_test_helper.h | 76 | ||||
-rw-r--r-- | athena/test/athena_unittests.cc | 52 | ||||
-rw-r--r-- | athena/wm/DEPS | 6 | ||||
-rw-r--r-- | athena/wm/window_manager_unittest.cc | 12 |
14 files changed, 496 insertions, 27 deletions
diff --git a/athena/athena.gyp b/athena/athena.gyp index 3335244..fef87f5 100644 --- a/athena/athena.gyp +++ b/athena/athena.gyp @@ -34,6 +34,45 @@ 'wm/window_manager_impl.cc', ], }, + { + 'target_name': 'athena_test_support', + 'type': 'static_library', + 'dependencies': [ + '../base/base.gyp:test_support_base', + '../skia/skia.gyp:skia', + '../testing/gtest.gyp:gtest', + '../ui/accessibility/accessibility.gyp:ax_gen', + '../ui/aura/aura.gyp:aura_test_support', + '../ui/base/ui_base.gyp:ui_base_test_support', + '../ui/compositor/compositor.gyp:compositor_test_support', + '../ui/views/views.gyp:views', + '../ui/wm/wm.gyp:wm', + 'athena_lib', + ], + 'sources': [ + 'main/athena_launcher.cc', + 'main/athena_launcher.h', + 'main/placeholder.cc', + 'main/placeholder.h', + 'test/athena_test_base.cc', + 'test/athena_test_base.h', + 'test/athena_test_helper.cc', + 'test/athena_test_helper.h', + ], + }, + { + 'target_name': 'athena_unittests', + 'type': 'executable', + 'dependencies': [ + '../testing/gtest.gyp:gtest', + 'athena_lib', + 'athena_test_support', + ], + 'sources': [ + 'test/athena_unittests.cc', + 'wm/window_manager_unittest.cc', + ], + } ], } diff --git a/athena/main/DEPS b/athena/main/DEPS index f8729fc..e2789a5 100644 --- a/athena/main/DEPS +++ b/athena/main/DEPS @@ -1,16 +1,18 @@ include_rules = [ - "+apps/shell/app", - "+apps/shell/browser", "+athena/home/public", "+athena/screen/public", "+athena/wm/public", - "+content/public/app", "+ui/aura", "+ui/wm/core", ] -# TODO(oshima): Remove this. specific_include_rules = { + "athena_main\.cc": [ + "+apps/shell/app", + "+apps/shell/browser", + "+content/public/app", + ], +# TODO(oshima): Remove this. "placeholder\.*": [ "+third_party/skia", "+ui/gfx", diff --git a/athena/main/athena_launcher.cc b/athena/main/athena_launcher.cc new file mode 100644 index 0000000..b061ff0 --- /dev/null +++ b/athena/main/athena_launcher.cc @@ -0,0 +1,53 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "athena/main/athena_launcher.h" + +#include "athena/home/public/home_card.h" +#include "athena/main/placeholder.h" +#include "athena/screen/public/screen_manager.h" +#include "athena/wm/public/window_manager.h" +#include "base/memory/scoped_ptr.h" +#include "ui/aura/window_property.h" +#include "ui/wm/core/visibility_controller.h" + +namespace athena { +struct RootWindowState; +} + +DECLARE_WINDOW_PROPERTY_TYPE(athena::RootWindowState*); + +namespace athena { + +// Athena's per root window state. +struct RootWindowState { + scoped_ptr< ::wm::VisibilityController> visibility_client; +}; + +DEFINE_OWNED_WINDOW_PROPERTY_KEY(athena::RootWindowState, + kRootWindowStateKey, + NULL); + +void StartAthena(aura::Window* root_window) { + RootWindowState* root_window_state = new RootWindowState; + root_window->SetProperty(kRootWindowStateKey, root_window_state); + + root_window_state->visibility_client.reset(new ::wm::VisibilityController); + aura::client::SetVisibilityClient(root_window, + root_window_state->visibility_client.get()); + + athena::ScreenManager::Create(root_window); + athena::WindowManager::Create(); + athena::HomeCard::Create(); + + SetupBackgroundImage(); +} + +void ShutdownAthena() { + athena::HomeCard::Shutdown(); + athena::WindowManager::Shutdown(); + athena::ScreenManager::Shutdown(); +} + +} // namespace athena diff --git a/athena/main/athena_launcher.h b/athena/main/athena_launcher.h new file mode 100644 index 0000000..072df57 --- /dev/null +++ b/athena/main/athena_launcher.h @@ -0,0 +1,20 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef ATHENA_MAIN_ATHENA_LAUNCHER_H_ +#define ATHENA_MAIN_ATHENA_LAUNCHER_H_ + +namespace aura { +class Window; +} + +namespace athena { + +// Starts/shuts down the athena shell environment. +void StartAthena(aura::Window* root_window); +void ShutdownAthena(); + +} // namespace athena + +#endif // ATHENA_MAIN_ATHENA_LAUNCHER_H_ diff --git a/athena/main/athena_main.cc b/athena/main/athena_main.cc index 8c6e7ab..6ee8ee4 100644 --- a/athena/main/athena_main.cc +++ b/athena/main/athena_main.cc @@ -5,11 +5,8 @@ #include "apps/shell/app/shell_main_delegate.h" #include "apps/shell/browser/shell_browser_main_delegate.h" #include "apps/shell/browser/shell_desktop_controller.h" -#include "athena/home/public/home_card.h" +#include "athena/main/athena_launcher.h" #include "athena/main/placeholder.h" -#include "athena/screen/public/screen_manager.h" -#include "athena/wm/public/window_manager.h" -#include "base/memory/scoped_ptr.h" #include "content/public/app/content_main.h" #include "ui/aura/window_tree_host.h" #include "ui/wm/core/visibility_controller.h" @@ -21,28 +18,13 @@ class AthenaBrowserMainDelegate : public apps::ShellBrowserMainDelegate { // apps::ShellBrowserMainDelegate: virtual void Start(content::BrowserContext* context) OVERRIDE { - aura::Window* root_window = - apps::ShellDesktopController::instance()->GetWindowTreeHost()->window(); - visibility_controller_.reset(new ::wm::VisibilityController); - aura::client::SetVisibilityClient(root_window, - visibility_controller_.get()); - - athena::ScreenManager::Create(root_window); - athena::WindowManager::Create(); - athena::HomeCard::Create(); - - SetupBackgroundImage(); + athena::StartAthena(apps::ShellDesktopController::instance() + ->GetWindowTreeHost() + ->window()); CreateTestWindows(); } - virtual void Shutdown() OVERRIDE { - athena::HomeCard::Shutdown(); - athena::WindowManager::Shutdown(); - athena::ScreenManager::Shutdown(); - visibility_controller_.reset(); - } - - scoped_ptr< ::wm::VisibilityController> visibility_controller_; + virtual void Shutdown() OVERRIDE { athena::ShutdownAthena(); } private: DISALLOW_COPY_AND_ASSIGN(AthenaBrowserMainDelegate); diff --git a/athena/main/athena_main.gyp b/athena/main/athena_main.gyp index 4a96c77..480f40c 100644 --- a/athena/main/athena_main.gyp +++ b/athena/main/athena_main.gyp @@ -21,6 +21,8 @@ '../..', ], 'sources': [ + 'athena_launcher.cc', + 'athena_launcher.h', 'athena_main.cc', 'placeholder.cc', 'placeholder.h', diff --git a/athena/test/DEPS b/athena/test/DEPS new file mode 100644 index 0000000..ffd0929 --- /dev/null +++ b/athena/test/DEPS @@ -0,0 +1,9 @@ +include_rules = [ + "+athena/main", + "+ui/aura", + "+ui/base", + "+ui/compositor", + "+ui/gfx", + "+ui/gl", + "+ui/wm/core", +] diff --git a/athena/test/athena_test_base.cc b/athena/test/athena_test_base.cc new file mode 100644 index 0000000..d1f6260 --- /dev/null +++ b/athena/test/athena_test_base.cc @@ -0,0 +1,54 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "athena/test/athena_test_base.h" + +#include "athena/test/athena_test_helper.h" +#include "ui/compositor/test/context_factories_for_test.h" + +namespace athena { +namespace test { + +AthenaTestBase::AthenaTestBase() + : setup_called_(false), teardown_called_(false) { +} + +AthenaTestBase::~AthenaTestBase() { + CHECK(setup_called_) + << "You have overridden SetUp but never called super class's SetUp"; + CHECK(teardown_called_) + << "You have overridden TearDown but never called super class's TearDown"; +} + +void AthenaTestBase::SetUp() { + setup_called_ = true; + testing::Test::SetUp(); + + // The ContextFactory must exist before any Compositors are created. + bool enable_pixel_output = false; + ui::ContextFactory* context_factory = + ui::InitializeContextFactoryForTests(enable_pixel_output); + + helper_.reset(new AthenaTestHelper(&message_loop_)); + helper_->SetUp(context_factory); +} + +void AthenaTestBase::TearDown() { + teardown_called_ = true; + + // Flush the message loop because we have pending release tasks + // and these tasks if un-executed would upset Valgrind. + RunAllPendingInMessageLoop(); + + helper_->TearDown(); + ui::TerminateContextFactoryForTests(); + testing::Test::TearDown(); +} + +void AthenaTestBase::RunAllPendingInMessageLoop() { + helper_->RunAllPendingInMessageLoop(); +} + +} // namespace test +} // namespace athena diff --git a/athena/test/athena_test_base.h b/athena/test/athena_test_base.h new file mode 100644 index 0000000..376ce92 --- /dev/null +++ b/athena/test/athena_test_base.h @@ -0,0 +1,49 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef ATHENA_TEST_ATHENA_TEST_BASE_H_ +#define ATHENA_TEST_ATHENA_TEST_BASE_H_ + +#include "athena/test/athena_test_helper.h" +#include "base/macros.h" +#include "base/message_loop/message_loop.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace aura { +class Window; +} + +namespace athena { +namespace test { + +// A base class for athena unit tests. +class AthenaTestBase : public testing::Test { + public: + AthenaTestBase(); + virtual ~AthenaTestBase(); + + // testing::Test: + virtual void SetUp() OVERRIDE; + virtual void TearDown() OVERRIDE; + + protected: + void RunAllPendingInMessageLoop(); + + aura::Window* root_window() { return helper_->root_window(); } + aura::WindowTreeHost* host() { return helper_->host(); } + + private: + bool setup_called_; + bool teardown_called_; + + base::MessageLoopForUI message_loop_; + scoped_ptr<AthenaTestHelper> helper_; + + DISALLOW_COPY_AND_ASSIGN(AthenaTestBase); +}; + +} // namespace test +} // namespace athena + +#endif // ATHENA_TEST_ATHENA_TEST_BASE_H_ diff --git a/athena/test/athena_test_helper.cc b/athena/test/athena_test_helper.cc new file mode 100644 index 0000000..701baaf --- /dev/null +++ b/athena/test/athena_test_helper.cc @@ -0,0 +1,113 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "athena/test/athena_test_helper.h" + +#include "athena/main/athena_launcher.h" +#include "base/message_loop/message_loop.h" +#include "base/run_loop.h" +#include "ui/aura/client/aura_constants.h" +#include "ui/aura/env.h" +#include "ui/aura/input_state_lookup.h" +#include "ui/aura/test/env_test_helper.h" +#include "ui/aura/test/test_focus_client.h" +#include "ui/aura/test/test_screen.h" +#include "ui/aura/window_event_dispatcher.h" +#include "ui/base/ime/input_method_initializer.h" +#include "ui/compositor/scoped_animation_duration_scale_mode.h" +#include "ui/gfx/screen.h" +#include "ui/wm/core/default_activation_client.h" +#include "ui/wm/core/input_method_event_filter.h" + +#if defined(USE_X11) +#include "ui/aura/window_tree_host_x11.h" +#include "ui/base/x/x11_util.h" +#endif + +namespace athena { +namespace test { + +AthenaTestHelper::AthenaTestHelper(base::MessageLoopForUI* message_loop) + : setup_called_(false), teardown_called_(false) { + DCHECK(message_loop); + message_loop_ = message_loop; + // Disable animations during tests. + zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode( + ui::ScopedAnimationDurationScaleMode::ZERO_DURATION)); +#if defined(USE_X11) + aura::test::SetUseOverrideRedirectWindowByDefault(true); +#endif +} + +AthenaTestHelper::~AthenaTestHelper() { + CHECK(setup_called_) << "AthenaTestHelper::SetUp() never called."; + CHECK(teardown_called_) << "AthenaTestHelper::TearDown() never called."; +} + +void AthenaTestHelper::SetUp(ui::ContextFactory* context_factory) { + setup_called_ = true; + + aura::Env::CreateInstance(true); + aura::Env::GetInstance()->set_context_factory(context_factory); + + // Unit tests generally don't want to query the system, rather use the state + // from RootWindow. + aura::test::EnvTestHelper(aura::Env::GetInstance()) + .SetInputStateLookup(scoped_ptr<aura::InputStateLookup>()); + + ui::InitializeInputMethodForTesting(); + + test_screen_.reset(aura::TestScreen::Create()); + gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen_.get()); + host_.reset(test_screen_->CreateHostForPrimaryDisplay()); + + input_method_filter_.reset(new ::wm::InputMethodEventFilter( + root_window()->GetHost()->GetAcceleratedWidget())); + input_method_filter_->SetInputMethodPropertyInRootWindow( + root_window()); + + // TODO(oshima): Switch to athena implementation. + focus_client_.reset(new aura::test::TestFocusClient); + aura::client::SetFocusClient(root_window(), + focus_client_.get()); + new ::wm::DefaultActivationClient(root_window()); + + root_window()->Show(); + // Ensure width != height so tests won't confuse them. + host()->SetBounds(gfx::Rect(800, 600)); + + athena::StartAthena(root_window()); +} + +void AthenaTestHelper::TearDown() { + teardown_called_ = true; + + aura::client::SetFocusClient(root_window(), NULL); + focus_client_.reset(); + input_method_filter_.reset(); + + athena::ShutdownAthena(); + host_.reset(); + ui::GestureRecognizer::Reset(); + test_screen_.reset(); + gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL); + +#if defined(USE_X11) + ui::test::ResetXCursorCache(); +#endif + + ui::ShutdownInputMethodForTesting(); + + aura::Env::DeleteInstance(); +} + +void AthenaTestHelper::RunAllPendingInMessageLoop() { + // TODO(jbates) crbug.com/134753 Find quitters of this RunLoop and have them + // use run_loop.QuitClosure(). + base::RunLoop run_loop; + run_loop.RunUntilIdle(); +} + +} // namespace test +} // namespace athena diff --git a/athena/test/athena_test_helper.h b/athena/test/athena_test_helper.h new file mode 100644 index 0000000..7b7a2a0 --- /dev/null +++ b/athena/test/athena_test_helper.h @@ -0,0 +1,76 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef ATHENA_TEST_ATHENA_TEST_HELPER_H_ +#define ATHENA_TEST_ATHENA_TEST_HELPER_H_ + +#include "base/basictypes.h" +#include "base/memory/scoped_ptr.h" +#include "ui/aura/window_tree_host.h" + +namespace base { +class MessageLoopForUI; +} + +namespace ui { +class ContextFactory; +class ScopedAnimationDurationScaleMode; +} + +namespace aura { +class Window; +class TestScreen; +class WindowTreeHost; +namespace client { +class FocusClient; +} +} + +namespace wm { +class InputMethodEventFilter; +} + +namespace athena { +namespace test { + +// A helper class owned by tests that does common initialization required for +// Athena use. This class creates a root window with clients and other objects +// that are necessary to run test on Athena. +class AthenaTestHelper { + public: + explicit AthenaTestHelper(base::MessageLoopForUI* message_loop); + ~AthenaTestHelper(); + + // Creates and initializes (shows and sizes) the RootWindow for use in tests. + void SetUp(ui::ContextFactory* context_factory); + + // Clean up objects that are created for tests. This also deletes the Env + // object. + void TearDown(); + + // Flushes message loop. + void RunAllPendingInMessageLoop(); + + aura::Window* root_window() { return host_->window(); } + aura::WindowTreeHost* host() { return host_.get(); } + + private: + bool setup_called_; + bool teardown_called_; + + base::MessageLoopForUI* message_loop_; + + scoped_ptr<aura::WindowTreeHost> host_; + scoped_ptr<aura::TestScreen> test_screen_; + scoped_ptr<aura::client::FocusClient> focus_client_; + scoped_ptr< ::wm::InputMethodEventFilter> input_method_filter_; + scoped_ptr<ui::ScopedAnimationDurationScaleMode> zero_duration_mode_; + + DISALLOW_COPY_AND_ASSIGN(AthenaTestHelper); +}; + +} // namespace test +} // namespace athena + +#endif // ATHENA_TEST_ATHENA_TEST_HELPER_H_ diff --git a/athena/test/athena_unittests.cc b/athena/test/athena_unittests.cc new file mode 100644 index 0000000..bdb6b73 --- /dev/null +++ b/athena/test/athena_unittests.cc @@ -0,0 +1,52 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/bind.h" +#include "base/macros.h" +#include "base/test/launcher/unit_test_launcher.h" +#include "base/test/test_suite.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "ui/base/resource/resource_bundle.h" +#include "ui/base/ui_base_paths.h" +#include "ui/gfx/gfx_paths.h" +#include "ui/gl/gl_surface.h" + +namespace { + +class AthenaTestSuite : public base::TestSuite { + public: + AthenaTestSuite(int argc, char** argv) : TestSuite(argc, argv) {} + virtual ~AthenaTestSuite() {} + + protected: + // base::TestSuite: + virtual void Initialize() OVERRIDE { + base::TestSuite::Initialize(); + gfx::GLSurface::InitializeOneOffForTests(); + gfx::RegisterPathProvider(); + ui::RegisterPathProvider(); + + // Force unittests to run using en-US so if we test against string + // output, it'll pass regardless of the system language. + ui::ResourceBundle::InitSharedInstanceWithLocale("en-US", NULL); + } + virtual void Shutdown() OVERRIDE { + ui::ResourceBundle::CleanupSharedInstance(); + base::TestSuite::Shutdown(); + } + + private: + DISALLOW_COPY_AND_ASSIGN(AthenaTestSuite); +}; + +} // namespace + +int main(int argc, char** argv) { + AthenaTestSuite test_suite(argc, argv); + + return base::LaunchUnitTestsSerially( + argc, + argv, + base::Bind(&AthenaTestSuite::Run, base::Unretained(&test_suite))); +} diff --git a/athena/wm/DEPS b/athena/wm/DEPS index 3e8398a..8a7fa8b 100644 --- a/athena/wm/DEPS +++ b/athena/wm/DEPS @@ -2,3 +2,9 @@ include_rules = [ "+athena/screen/public", "+ui/aura", ] + +specific_include_rules = { + ".*unittest\.cc": [ + "+athena/test", + ], +} diff --git a/athena/wm/window_manager_unittest.cc b/athena/wm/window_manager_unittest.cc new file mode 100644 index 0000000..35ab704 --- /dev/null +++ b/athena/wm/window_manager_unittest.cc @@ -0,0 +1,12 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "athena/wm/public/window_manager.h" + +#include "athena/test/athena_test_base.h" + +typedef athena::test::AthenaTestBase WindowManagerTest; + +TEST_F(WindowManagerTest, Empty) { +} |