ash_test_helper.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright 2013 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. #ifndef ASH_TEST_ASH_TEST_HELPER_H_
  5. #define ASH_TEST_ASH_TEST_HELPER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <utility>
  9. #include "ash/assistant/test/test_assistant_service.h"
  10. #include "ash/public/cpp/test/test_system_tray_client.h"
  11. #include "ash/session/test_pref_service_provider.h"
  12. #include "ash/session/test_session_controller_client.h"
  13. #include "ash/shell_delegate.h"
  14. #include "ash/system/message_center/test_notifier_settings_controller.h"
  15. #include "base/test/scoped_command_line.h"
  16. #include "chromeos/services/bluetooth_config/scoped_bluetooth_config_test_helper.h"
  17. #include "chromeos/system/fake_statistics_provider.h"
  18. #include "ui/aura/test/aura_test_helper.h"
  19. class PrefService;
  20. namespace aura {
  21. class Window;
  22. } // namespace aura
  23. namespace base {
  24. class SystemMonitor;
  25. } // namespace base
  26. namespace display {
  27. class Display;
  28. } // namespace display
  29. namespace ui {
  30. class ContextFactory;
  31. } // namespace ui
  32. namespace views {
  33. class TestViewsDelegate;
  34. } // namespace views
  35. namespace ash {
  36. class AppListTestHelper;
  37. class AmbientAshTestHelper;
  38. class AshTestUiStabilizer;
  39. class TestKeyboardControllerObserver;
  40. class TestNewWindowDelegateProvider;
  41. class TestWallpaperControllerClient;
  42. namespace input_method {
  43. class MockInputMethodManager;
  44. } // namespace input_method
  45. namespace pixel_test {
  46. struct InitParams;
  47. } // namespace pixel_test
  48. // A helper class that does common initialization required for Ash. Creates a
  49. // root window and an ash::Shell instance with a test delegate.
  50. class AshTestHelper : public aura::test::AuraTestHelper {
  51. public:
  52. struct InitParams {
  53. InitParams();
  54. InitParams(InitParams&&);
  55. InitParams& operator=(InitParams&&) = default;
  56. ~InitParams();
  57. // True if the user should log in.
  58. bool start_session = true;
  59. // If this is not set, a TestShellDelegate will be used automatically.
  60. std::unique_ptr<ShellDelegate> delegate;
  61. PrefService* local_state = nullptr;
  62. // Used only when setting up a pixel diff test.
  63. base::raw_ptr<pixel_test::InitParams> pixel_test_init_params = nullptr;
  64. };
  65. // Instantiates/destroys an AshTestHelper. This can happen in a
  66. // single-threaded phase without a backing task environment or ViewsDelegate,
  67. // and must not create those lest the caller wish to do so.
  68. explicit AshTestHelper(ui::ContextFactory* context_factory = nullptr);
  69. AshTestHelper(const AshTestHelper&) = delete;
  70. AshTestHelper& operator=(const AshTestHelper&) = delete;
  71. ~AshTestHelper() override;
  72. // Calls through to SetUp() below, see comments there.
  73. void SetUp() override;
  74. // Tears down everything but the Screen instance, which some tests access
  75. // after this point. This will be called automatically on destruction if it
  76. // is not called manually earlier.
  77. void TearDown() override;
  78. aura::Window* GetContext() override;
  79. aura::WindowTreeHost* GetHost() override;
  80. aura::TestScreen* GetTestScreen() override;
  81. aura::client::FocusClient* GetFocusClient() override;
  82. aura::client::CaptureClient* GetCaptureClient() override;
  83. // Creates the ash::Shell and performs associated initialization according
  84. // to |init_params|. When this function returns it guarantees a task
  85. // environment and ViewsDelegate will exist, the shell will be started, and a
  86. // window will be showing.
  87. void SetUp(InitParams init_params);
  88. display::Display GetSecondaryDisplay() const;
  89. // Simulates a user sign-in. It creates a new user session, adds it to
  90. // existing user sessions and makes it the active user session.
  91. // NOTE: call `StabilizeUIForPixelTest()` after calling this function in a
  92. // pixel test.
  93. void SimulateUserLogin(
  94. const AccountId& account_id,
  95. user_manager::UserType user_type = user_manager::USER_TYPE_REGULAR);
  96. // Stabilizes the variable UI components (such as the battery view).
  97. void StabilizeUIForPixelTest();
  98. TestSessionControllerClient* test_session_controller_client() {
  99. return session_controller_client_.get();
  100. }
  101. void set_test_session_controller_client(
  102. std::unique_ptr<TestSessionControllerClient> session_controller_client) {
  103. session_controller_client_ = std::move(session_controller_client);
  104. }
  105. TestNotifierSettingsController* notifier_settings_controller() {
  106. return notifier_settings_controller_.get();
  107. }
  108. TestSystemTrayClient* system_tray_client() {
  109. return system_tray_client_.get();
  110. }
  111. TestPrefServiceProvider* prefs_provider() { return prefs_provider_.get(); }
  112. AppListTestHelper* app_list_test_helper() {
  113. return app_list_test_helper_.get();
  114. }
  115. TestKeyboardControllerObserver* test_keyboard_controller_observer() {
  116. return test_keyboard_controller_observer_.get();
  117. }
  118. TestAssistantService* test_assistant_service() {
  119. return assistant_service_.get();
  120. }
  121. AmbientAshTestHelper* ambient_ash_test_helper() {
  122. return ambient_ash_test_helper_.get();
  123. }
  124. chromeos::bluetooth_config::ScopedBluetoothConfigTestHelper*
  125. bluetooth_config_test_helper() {
  126. return &scoped_bluetooth_config_test_helper_;
  127. }
  128. private:
  129. // Scoping objects to manage init/teardown of services.
  130. class BluezDBusManagerInitializer;
  131. class PowerPolicyControllerInitializer;
  132. // Must be constructed so that `base::SystemMonitor::Get()` returns a valid
  133. // instance.
  134. std::unique_ptr<base::SystemMonitor> system_monitor_;
  135. std::unique_ptr<base::test::ScopedCommandLine> command_line_ =
  136. std::make_unique<base::test::ScopedCommandLine>();
  137. std::unique_ptr<chromeos::system::ScopedFakeStatisticsProvider>
  138. statistics_provider_ =
  139. std::make_unique<chromeos::system::ScopedFakeStatisticsProvider>();
  140. std::unique_ptr<TestPrefServiceProvider> prefs_provider_ =
  141. std::make_unique<TestPrefServiceProvider>();
  142. std::unique_ptr<TestNotifierSettingsController>
  143. notifier_settings_controller_ =
  144. std::make_unique<TestNotifierSettingsController>();
  145. std::unique_ptr<TestAssistantService> assistant_service_ =
  146. std::make_unique<TestAssistantService>();
  147. std::unique_ptr<TestSystemTrayClient> system_tray_client_ =
  148. std::make_unique<TestSystemTrayClient>();
  149. std::unique_ptr<AppListTestHelper> app_list_test_helper_;
  150. std::unique_ptr<BluezDBusManagerInitializer> bluez_dbus_manager_initializer_;
  151. std::unique_ptr<PowerPolicyControllerInitializer>
  152. power_policy_controller_initializer_;
  153. std::unique_ptr<TestNewWindowDelegateProvider> new_window_delegate_provider_;
  154. std::unique_ptr<views::TestViewsDelegate> test_views_delegate_;
  155. std::unique_ptr<TestSessionControllerClient> session_controller_client_;
  156. std::unique_ptr<TestKeyboardControllerObserver>
  157. test_keyboard_controller_observer_;
  158. std::unique_ptr<AmbientAshTestHelper> ambient_ash_test_helper_;
  159. std::unique_ptr<TestWallpaperControllerClient> wallpaper_controller_client_;
  160. // Used only for pixel tests.
  161. std::unique_ptr<AshTestUiStabilizer> ui_stabilizer_;
  162. chromeos::bluetooth_config::ScopedBluetoothConfigTestHelper
  163. scoped_bluetooth_config_test_helper_;
  164. // InputMethodManager is not owned by this class. It is stored in a
  165. // global that is registered via InputMethodManager::Initialize().
  166. input_method::MockInputMethodManager* input_method_manager_ = nullptr;
  167. };
  168. } // namespace ash
  169. #endif // ASH_TEST_ASH_TEST_HELPER_H_