 Chromium Code Reviews
 Chromium Code Reviews Issue 2694623016:
  chromeos: Makes AshTestBase/Helper target mash when appropriate  (Closed)
    
  
    Issue 2694623016:
  chromeos: Makes AshTestBase/Helper target mash when appropriate  (Closed) 
  | Index: ash/test/ash_test_helper.cc | 
| diff --git a/ash/test/ash_test_helper.cc b/ash/test/ash_test_helper.cc | 
| index df2ebb668a441b84b4ba9cd42811d6aa085b2139..1e2f216eba20504e84b823578cc23fc9bed7470e 100644 | 
| --- a/ash/test/ash_test_helper.cc | 
| +++ b/ash/test/ash_test_helper.cc | 
| @@ -9,6 +9,10 @@ | 
| #include "ash/common/test/test_system_tray_delegate.h" | 
| #include "ash/common/test/wm_shell_test_api.h" | 
| #include "ash/common/wm_shell.h" | 
| +#include "ash/common/wm_window.h" | 
| +#include "ash/mus/screen_mus.h" | 
| +#include "ash/mus/window_manager.h" | 
| +#include "ash/mus/window_manager_application.h" | 
| #include "ash/shell.h" | 
| #include "ash/shell_init_params.h" | 
| #include "ash/system/chromeos/screen_layout_observer.h" | 
| @@ -19,22 +23,29 @@ | 
| #include "ash/test/test_shell_delegate.h" | 
| #include "base/memory/ptr_util.h" | 
| #include "base/run_loop.h" | 
| +#include "base/strings/string_number_conversions.h" | 
| +#include "base/strings/string_split.h" | 
| +#include "base/test/sequenced_worker_pool_owner.h" | 
| #include "chromeos/audio/cras_audio_handler.h" | 
| #include "chromeos/dbus/dbus_thread_manager.h" | 
| #include "device/bluetooth/bluetooth_adapter_factory.h" | 
| #include "device/bluetooth/dbus/bluez_dbus_manager.h" | 
| #include "ui/aura/env.h" | 
| #include "ui/aura/input_state_lookup.h" | 
| +#include "ui/aura/mus/window_tree_client.h" | 
| #include "ui/aura/test/env_test_helper.h" | 
| #include "ui/aura/test/event_generator_delegate_aura.h" | 
| +#include "ui/aura/test/mus/window_tree_client_private.h" | 
| #include "ui/base/ime/input_method_initializer.h" | 
| #include "ui/base/material_design/material_design_controller.h" | 
| #include "ui/base/platform_window_defaults.h" | 
| #include "ui/base/test/material_design_controller_test_api.h" | 
| #include "ui/compositor/scoped_animation_duration_scale_mode.h" | 
| #include "ui/compositor/test/context_factories_for_test.h" | 
| +#include "ui/display/manager/display_manager.h" | 
| #include "ui/display/manager/managed_display_info.h" | 
| #include "ui/display/test/display_manager_test_api.h" | 
| +#include "ui/gfx/geometry/dip_util.h" | 
| #include "ui/message_center/message_center.h" | 
| #include "ui/wm/core/capture_controller.h" | 
| #include "ui/wm/core/cursor_manager.h" | 
| @@ -42,6 +53,62 @@ | 
| namespace ash { | 
| namespace test { | 
| +namespace { | 
| + | 
| +bool IsMash() { | 
| + return aura::Env::GetInstance()->mode() == aura::Env::Mode::MUS; | 
| +} | 
| + | 
| +bool CompareByDisplayId(RootWindowController* root1, | 
| + RootWindowController* root2) { | 
| + return root1->GetWindow()->GetDisplayNearestWindow().id() < | 
| + root2->GetWindow()->GetDisplayNearestWindow().id(); | 
| +} | 
| + | 
| +struct DisplayInfo { | 
| + gfx::Rect bounds; | 
| + int scale_factor = 1; | 
| +}; | 
| + | 
| +// TODO(sky): at some point this needs to support everything in DisplayInfo, | 
| +// for now just the bare minimum, which is [x+y-]wxh[*scale_factor]. | 
| +DisplayInfo ParseDisplayBounds(const std::string& spec) { | 
| 
James Cook
2017/02/18 02:23:21
This seems like it should share code with ManagedD
 
sky
2017/02/18 18:00:57
Done.
 | 
| + DisplayInfo result; | 
| + const std::vector<std::string> parts = | 
| + base::SplitString(spec, "-", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 
| + std::string size_spec; | 
| + if (parts.size() == 2u) { | 
| + size_spec = parts[1]; | 
| + const std::vector<std::string> origin_parts = base::SplitString( | 
| + parts[0], "+", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 
| + CHECK_EQ(2u, origin_parts.size()); | 
| + int x, y; | 
| + CHECK(base::StringToInt(origin_parts[0], &x)); | 
| + CHECK(base::StringToInt(origin_parts[1], &y)); | 
| + result.bounds.set_origin(gfx::Point(x, y)); | 
| + } else { | 
| + CHECK_EQ(1u, parts.size()); | 
| + size_spec = spec; | 
| + } | 
| + const std::vector<std::string> size_parts = base::SplitString( | 
| + size_spec, "x", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 
| + CHECK_EQ(2u, size_parts.size()); | 
| + std::string height_string = size_parts[1]; | 
| + const size_t scale_factor_index = height_string.find('*'); | 
| + if (scale_factor_index != std::string::npos) { | 
| + CHECK(base::StringToInt(height_string.substr(scale_factor_index + 1), | 
| + &(result.scale_factor))); | 
| + height_string = height_string.substr(0, scale_factor_index); | 
| + } | 
| + int w = 0, h = 0; | 
| + CHECK(base::StringToInt(size_parts[0], &w)); | 
| + CHECK(base::StringToInt(height_string, &h)); | 
| + result.bounds.set_size(gfx::Size(w, h)); | 
| + result.bounds = gfx::ConvertRectToDIP(result.scale_factor, result.bounds); | 
| + return result; | 
| +} | 
| + | 
| +} // namespace | 
| AshTestHelper::AshTestHelper(AshTestEnvironment* ash_test_environment) | 
| : ash_test_environment_(ash_test_environment), | 
| @@ -51,13 +118,17 @@ AshTestHelper::AshTestHelper(AshTestEnvironment* ash_test_environment) | 
| bluez_dbus_manager_initialized_(false) { | 
| ui::test::EnableTestConfigForPlatformWindows(); | 
| aura::test::InitializeAuraEventGeneratorDelegate(); | 
| + aura::test::EnvTestHelper().SetAlwaysUseLastMouseLocation(true); | 
| } | 
| AshTestHelper::~AshTestHelper() {} | 
| void AshTestHelper::SetUp(bool start_session) { | 
| display::ResetDisplayIdForTest(); | 
| - wm_state_ = base::MakeUnique<::wm::WMState>(); | 
| + const bool is_mash = IsMash(); | 
| + // WindowManager creates WMState for mash. | 
| + if (!is_mash) | 
| + wm_state_ = base::MakeUnique<::wm::WMState>(); | 
| views_delegate_ = ash_test_environment_->CreateViewsDelegate(); | 
| // Disable animations during tests. | 
| @@ -65,38 +136,35 @@ void AshTestHelper::SetUp(bool start_session) { | 
| ui::ScopedAnimationDurationScaleMode::ZERO_DURATION)); | 
| ui::InitializeInputMethodForTesting(); | 
| - bool enable_pixel_output = false; | 
| - ui::ContextFactory* context_factory = nullptr; | 
| - ui::ContextFactoryPrivate* context_factory_private = nullptr; | 
| - ui::InitializeContextFactoryForTests(enable_pixel_output, &context_factory, | 
| - &context_factory_private); | 
| - | 
| // Creates Shell and hook with Desktop. | 
| if (!test_shell_delegate_) | 
| test_shell_delegate_ = new TestShellDelegate; | 
| - // Creates MessageCenter since g_browser_process is not created in AshTestBase | 
| - // tests. | 
| - message_center::MessageCenter::Initialize(); | 
| - | 
| - // Create DBusThreadManager for testing. | 
| - if (!chromeos::DBusThreadManager::IsInitialized()) { | 
| - chromeos::DBusThreadManager::Initialize( | 
| - chromeos::DBusThreadManager::PROCESS_ASH); | 
| - dbus_thread_manager_initialized_ = true; | 
| - } | 
| - | 
| - if (!bluez::BluezDBusManager::IsInitialized()) { | 
| - bluez::BluezDBusManager::Initialize( | 
| - chromeos::DBusThreadManager::Get()->GetSystemBus(), | 
| - chromeos::DBusThreadManager::Get()->IsUsingFakes()); | 
| - bluez_dbus_manager_initialized_ = true; | 
| + if (!is_mash) { | 
| + // All of this initialization is done in WindowManagerApplication for mash. | 
| + | 
| + // Creates MessageCenter since g_browser_process is not created in | 
| + // AshTestBase tests. | 
| + message_center::MessageCenter::Initialize(); | 
| + | 
| + if (!chromeos::DBusThreadManager::IsInitialized()) { | 
| + chromeos::DBusThreadManager::Initialize( | 
| + chromeos::DBusThreadManager::PROCESS_ASH); | 
| + dbus_thread_manager_initialized_ = true; | 
| + } | 
| + | 
| + if (!bluez::BluezDBusManager::IsInitialized()) { | 
| + bluez::BluezDBusManager::Initialize( | 
| + chromeos::DBusThreadManager::Get()->GetSystemBus(), | 
| + chromeos::DBusThreadManager::Get()->IsUsingFakes()); | 
| + bluez_dbus_manager_initialized_ = true; | 
| + } | 
| + | 
| + // Create CrasAudioHandler for testing since g_browser_process is not | 
| + // created in AshTestBase tests. | 
| + chromeos::CrasAudioHandler::InitializeForTesting(); | 
| } | 
| - // Create CrasAudioHandler for testing since g_browser_process is not | 
| - // created in AshTestBase tests. | 
| - chromeos::CrasAudioHandler::InitializeForTesting(); | 
| - | 
| ash_test_environment_->SetUp(); | 
| // Reset the global state for the cursor manager. This includes the | 
| // last cursor visibility state, etc. | 
| @@ -107,12 +175,47 @@ void AshTestHelper::SetUp(bool start_session) { | 
| ui::test::MaterialDesignControllerTestAPI::Uninitialize(); | 
| ui::MaterialDesignController::Initialize(); | 
| - ShellInitParams init_params; | 
| - init_params.delegate = test_shell_delegate_; | 
| - init_params.context_factory = context_factory; | 
| - init_params.context_factory_private = context_factory_private; | 
| - init_params.blocking_pool = ash_test_environment_->GetBlockingPool(); | 
| - Shell::CreateInstance(init_params); | 
| + if (is_mash) { | 
| + window_manager_app_ = base::MakeUnique<mus::WindowManagerApplication>(); | 
| + const size_t kMaxNumberThreads = 3u; // Matches that of content. | 
| + const char kThreadNamePrefix[] = "MashBlockingForTesting"; | 
| + blocking_pool_owner_ = base::MakeUnique<base::SequencedWorkerPoolOwner>( | 
| 
James Cook
2017/02/18 02:23:21
Could WindowManagerApplication handle the creation
 
sky
2017/02/18 18:00:57
I think you may be right, but I would like to hold
 | 
| + kMaxNumberThreads, kThreadNamePrefix); | 
| + | 
| + window_manager_app_->window_manager_.reset(new mus::WindowManager(nullptr)); | 
| + window_manager_app_->window_manager()->shell_delegate_for_test_.reset( | 
| + test_shell_delegate_); | 
| + window_manager_app_->window_manager() | 
| + ->create_session_state_delegate_stub_for_test_ = false; | 
| + | 
| + window_tree_client_setup_.InitForWindowManager( | 
| + window_manager_app_->window_manager_.get(), | 
| + window_manager_app_->window_manager_.get()); | 
| + aura::test::EnvTestHelper().SetWindowTreeClient( | 
| + window_tree_client_setup_.window_tree_client()); | 
| + window_manager_app_->InitWindowManager( | 
| + window_tree_client_setup_.OwnWindowTreeClient(), | 
| + blocking_pool_owner_->pool()); | 
| + | 
| + aura::WindowTreeClient* window_tree_client = | 
| + window_manager_app_->window_manager()->window_tree_client(); | 
| + window_tree_client_private_ = | 
| + base::MakeUnique<aura::WindowTreeClientPrivate>(window_tree_client); | 
| + int next_x = 0; | 
| + CreateRootWindowController("800x600", &next_x); | 
| 
James Cook
2017/02/18 02:23:21
This code and functions below are very similar to
 
sky
2017/02/18 18:00:57
Indeed. I would like to remove WmTestHelper and mo
 | 
| + } else { | 
| + ui::ContextFactory* context_factory = nullptr; | 
| + ui::ContextFactoryPrivate* context_factory_private = nullptr; | 
| + bool enable_pixel_output = false; | 
| + ui::InitializeContextFactoryForTests(enable_pixel_output, &context_factory, | 
| + &context_factory_private); | 
| + ShellInitParams init_params; | 
| + init_params.delegate = test_shell_delegate_; | 
| + init_params.context_factory = context_factory; | 
| + init_params.context_factory_private = context_factory_private; | 
| + init_params.blocking_pool = ash_test_environment_->GetBlockingPool(); | 
| + Shell::CreateInstance(init_params); | 
| + } | 
| 
James Cook
2017/02/18 02:23:21
This function is kind of long. Can you pull some o
 
sky
2017/02/18 18:00:57
Done.
 | 
| aura::test::EnvTestHelper(aura::Env::GetInstance()) | 
| .SetInputStateLookup(std::unique_ptr<aura::InputStateLookup>()); | 
| @@ -122,22 +225,39 @@ void AshTestHelper::SetUp(bool start_session) { | 
| GetTestSessionStateDelegate()->SetHasActiveUser(true); | 
| } | 
| - // Tests that change the display configuration generally don't care about the | 
| - // notifications and the popup UI can interfere with things like cursors. | 
| - shell->screen_layout_observer()->set_show_notifications_for_testing(false); | 
| - | 
| - display::test::DisplayManagerTestApi(Shell::GetInstance()->display_manager()) | 
| - .DisableChangeDisplayUponHostResize(); | 
| - ShellTestApi(shell).DisableDisplayAnimator(); | 
| - | 
| - test_screenshot_delegate_ = new TestScreenshotDelegate(); | 
| - shell->accelerator_controller_delegate()->SetScreenshotDelegate( | 
| - std::unique_ptr<ScreenshotDelegate>(test_screenshot_delegate_)); | 
| + if (!is_mash) { | 
| + // ScreenLayoutObserver is specific to cash. | 
| + // Tests that change the display configuration generally don't care about | 
| + // the | 
| 
James Cook
2017/02/18 02:23:21
nit: rewrap
 
sky
2017/02/18 18:00:57
Done.
 | 
| + // notifications and the popup UI can interfere with things like cursors. | 
| + shell->screen_layout_observer()->set_show_notifications_for_testing(false); | 
| + | 
| + // DisplayManager is specific to cash. | 
| + display::test::DisplayManagerTestApi( | 
| + Shell::GetInstance()->display_manager()) | 
| + .DisableChangeDisplayUponHostResize(); | 
| + ShellTestApi(shell).DisableDisplayAnimator(); | 
| + | 
| + // TODO: disabled for mash as AcceleratorControllerDelegateAura isn't | 
| + // created in mash http://crbug.com/632111. | 
| + test_screenshot_delegate_ = new TestScreenshotDelegate(); | 
| + shell->accelerator_controller_delegate()->SetScreenshotDelegate( | 
| + std::unique_ptr<ScreenshotDelegate>(test_screenshot_delegate_)); | 
| + } | 
| } | 
| void AshTestHelper::TearDown() { | 
| - // Tear down the shell. | 
| - Shell::DeleteInstance(); | 
| + window_manager_app_.reset(); | 
| + | 
| + base::RunLoop().RunUntilIdle(); | 
| + blocking_pool_owner_.reset(); | 
| + base::RunLoop().RunUntilIdle(); | 
| + | 
| + const bool is_mash = IsMash(); | 
| + | 
| + // WindowManger owns the Shell in mash. | 
| + if (!is_mash) | 
| + Shell::DeleteInstance(); | 
| // Suspend the tear down until all resources are returned via | 
| // MojoCompositorFrameSinkClient::ReclaimResources() | 
| @@ -146,15 +266,19 @@ void AshTestHelper::TearDown() { | 
| test_screenshot_delegate_ = NULL; | 
| - // Remove global message center state. | 
| - message_center::MessageCenter::Shutdown(); | 
| + if (!is_mash) { | 
| + // Remove global message center state. | 
| + message_center::MessageCenter::Shutdown(); | 
| + | 
| + chromeos::CrasAudioHandler::Shutdown(); | 
| + } | 
| - chromeos::CrasAudioHandler::Shutdown(); | 
| if (bluez_dbus_manager_initialized_) { | 
| device::BluetoothAdapterFactory::Shutdown(); | 
| bluez::BluezDBusManager::Shutdown(); | 
| bluez_dbus_manager_initialized_ = false; | 
| } | 
| + | 
| if (dbus_thread_manager_initialized_) { | 
| chromeos::DBusThreadManager::Shutdown(); | 
| dbus_thread_manager_initialized_ = false; | 
| @@ -168,7 +292,8 @@ void AshTestHelper::TearDown() { | 
| views_delegate_.reset(); | 
| wm_state_.reset(); | 
| - CHECK(!::wm::CaptureController::Get()); | 
| + // WindowManager owns the CaptureController for mash. | 
| + CHECK(is_mash || !::wm::CaptureController::Get()); | 
| } | 
| void AshTestHelper::RunAllPendingInMessageLoop() { | 
| @@ -191,5 +316,87 @@ aura::Window* AshTestHelper::CurrentContext() { | 
| return root_window; | 
| } | 
| +void AshTestHelper::UpdateDisplayForMash(const std::string& display_spec) { | 
| + CHECK(IsMash()); | 
| + const std::vector<std::string> parts = base::SplitString( | 
| + display_spec, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 
| + std::vector<RootWindowController*> root_window_controllers = | 
| + GetRootsOrderedByDisplayId(); | 
| + int next_x = 0; | 
| + for (size_t i = 0, | 
| + end = std::min(parts.size(), root_window_controllers.size()); | 
| + i < end; ++i) { | 
| + UpdateDisplay(root_window_controllers[i], parts[i], &next_x); | 
| + } | 
| + for (size_t i = root_window_controllers.size(); i < parts.size(); ++i) { | 
| + root_window_controllers.push_back( | 
| + CreateRootWindowController(parts[i], &next_x)); | 
| + } | 
| + const bool in_shutdown = false; | 
| 
James Cook
2017/02/18 02:23:21
Hooray for named constants instead of bare "false"
 | 
| + while (root_window_controllers.size() > parts.size()) { | 
| + window_manager_app_->window_manager()->DestroyRootWindowController( | 
| + root_window_controllers.back(), in_shutdown); | 
| + root_window_controllers.pop_back(); | 
| + } | 
| +} | 
| + | 
| +display::Display AshTestHelper::GetSecondaryDisplay() { | 
| + if (!IsMash()) | 
| + return Shell::GetInstance()->display_manager()->GetSecondaryDisplay(); | 
| + | 
| + std::vector<RootWindowController*> roots = GetRootsOrderedByDisplayId(); | 
| + return roots.size() < 2 ? display::Display() | 
| 
James Cook
2017/02/18 02:23:21
Maybe it should be an error to try to get the seco
 
sky
2017/02/18 18:00:57
Done.
 | 
| + : roots[1]->GetWindow()->GetDisplayNearestWindow(); | 
| +} | 
| + | 
| +RootWindowController* AshTestHelper::CreateRootWindowController( | 
| + const std::string& display_spec, | 
| + int* next_x) { | 
| + DisplayInfo display_info = ParseDisplayBounds(display_spec); | 
| + display_info.bounds.set_x(*next_x); | 
| + *next_x += display_info.bounds.size().width(); | 
| + display::Display display(next_display_id_++, display_info.bounds); | 
| + display.set_device_scale_factor(display_info.scale_factor); | 
| + gfx::Rect work_area(display.bounds()); | 
| + // Offset the height slightly to give a different work area. -20 is arbitrary, | 
| + // it could be anything. | 
| + work_area.set_height(std::max(0, work_area.height() - 20)); | 
| + display.set_work_area(work_area); | 
| + window_tree_client_private_->CallWmNewDisplayAdded(display); | 
| + return GetRootsOrderedByDisplayId().back(); | 
| +} | 
| + | 
| +void AshTestHelper::UpdateDisplay(RootWindowController* root_window_controller, | 
| + const std::string& display_spec, | 
| + int* next_x) { | 
| + DisplayInfo display_info = ParseDisplayBounds(display_spec); | 
| + display_info.bounds.set_x(*next_x); | 
| + *next_x += display_info.bounds.size().width(); | 
| + display::Display updated_display = | 
| + root_window_controller->GetWindow()->GetDisplayNearestWindow(); | 
| + gfx::Insets work_area_insets = updated_display.GetWorkAreaInsets(); | 
| + updated_display.set_bounds(display_info.bounds); | 
| + updated_display.UpdateWorkAreaFromInsets(work_area_insets); | 
| + updated_display.set_device_scale_factor(display_info.scale_factor); | 
| + root_window_controller->GetWindow()->SetBounds( | 
| + gfx::Rect(display_info.bounds.size())); | 
| + ScreenMus* screen = window_manager_app_->window_manager()->screen_.get(); | 
| + const bool is_primary = | 
| + screen->display_list().FindDisplayById(updated_display.id()) == | 
| + screen->display_list().GetPrimaryDisplayIterator(); | 
| + screen->display_list().UpdateDisplay( | 
| + updated_display, is_primary ? display::DisplayList::Type::PRIMARY | 
| + : display::DisplayList::Type::NOT_PRIMARY); | 
| +} | 
| + | 
| +std::vector<RootWindowController*> AshTestHelper::GetRootsOrderedByDisplayId() { | 
| + std::set<RootWindowController*> roots = | 
| + window_manager_app_->window_manager()->GetRootWindowControllers(); | 
| + std::vector<RootWindowController*> ordered_roots; | 
| + ordered_roots.insert(ordered_roots.begin(), roots.begin(), roots.end()); | 
| + std::sort(ordered_roots.begin(), ordered_roots.end(), &CompareByDisplayId); | 
| + return ordered_roots; | 
| +} | 
| + | 
| } // namespace test | 
| } // namespace ash |