ash_test_base.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // Copyright (c) 2012 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_BASE_H_
  5. #define ASH_TEST_ASH_TEST_BASE_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include "ash/constants/app_types.h"
  11. #include "ash/public/cpp/shell_window_ids.h"
  12. #include "ash/session/test_session_controller_client.h"
  13. #include "ash/test/ash_pixel_test_init_params.h"
  14. #include "ash/wm/desks/desks_util.h"
  15. #include "ash/wm/overview/overview_types.h"
  16. #include "base/compiler_specific.h"
  17. #include "base/test/task_environment.h"
  18. #include "base/threading/thread.h"
  19. #include "base/traits_bag.h"
  20. #include "components/prefs/testing_pref_service.h"
  21. #include "components/user_manager/user_type.h"
  22. #include "testing/gtest/include/gtest/gtest.h"
  23. #include "third_party/skia/include/core/SkColor.h"
  24. #include "ui/aura/client/window_types.h"
  25. #include "ui/aura/env.h"
  26. #include "ui/display/display.h"
  27. #include "ui/events/event_constants.h"
  28. #include "ui/events/test/event_generator.h"
  29. namespace aura {
  30. class Window;
  31. class WindowDelegate;
  32. } // namespace aura
  33. namespace base {
  34. namespace test {
  35. class TaskEnvironment;
  36. }
  37. } // namespace base
  38. namespace chromeos {
  39. class FakePowerManagerClient;
  40. }
  41. namespace display {
  42. class Display;
  43. class DisplayManager;
  44. namespace test {
  45. class DisplayManagerTestApi;
  46. } // namespace test
  47. } // namespace display
  48. namespace gfx {
  49. class Rect;
  50. }
  51. namespace views {
  52. class View;
  53. class Widget;
  54. class WidgetDelegate;
  55. } // namespace views
  56. namespace ash {
  57. class AmbientAshTestHelper;
  58. class AppListTestHelper;
  59. class AshTestHelper;
  60. class Shelf;
  61. class TestAppListClient;
  62. class TestShellDelegate;
  63. class TestSystemTrayClient;
  64. class UnifiedSystemTray;
  65. class WorkAreaInsets;
  66. // Base class for most tests in //ash. Constructs ash::Shell and all its
  67. // dependencies. Provides a user login session (use NoSessionAshTestBase for
  68. // tests that start at the login screen or need unusual user types). Sets
  69. // animation durations to zero via AshTestHelper/AuraTestHelper.
  70. class AshTestBase : public testing::Test {
  71. public:
  72. // Constructs an AshTestBase with |traits| being forwarded to its
  73. // TaskEnvironment. MainThreadType always defaults to UI and must not be
  74. // specified.
  75. template <typename... TaskEnvironmentTraits>
  76. NOINLINE explicit AshTestBase(TaskEnvironmentTraits&&... traits)
  77. : AshTestBase(std::make_unique<base::test::TaskEnvironment>(
  78. base::test::TaskEnvironment::MainThreadType::UI,
  79. std::forward<TaskEnvironmentTraits>(traits)...)) {}
  80. // Alternatively a subclass may pass a TaskEnvironment directly.
  81. explicit AshTestBase(
  82. std::unique_ptr<base::test::TaskEnvironment> task_environment);
  83. AshTestBase(const AshTestBase&) = delete;
  84. AshTestBase& operator=(const AshTestBase&) = delete;
  85. ~AshTestBase() override;
  86. // testing::Test:
  87. void SetUp() override;
  88. void SetUp(std::unique_ptr<TestShellDelegate> delegate);
  89. void TearDown() override;
  90. // Returns the Shelf for the primary display.
  91. static Shelf* GetPrimaryShelf();
  92. // Returns the unified system tray on the primary display.
  93. static UnifiedSystemTray* GetPrimaryUnifiedSystemTray();
  94. // Returns WorkAreaInsets for the primary display.
  95. static WorkAreaInsets* GetPrimaryWorkAreaInsets();
  96. // Update the display configuration as given in |display_specs|.
  97. // See ash::DisplayManagerTestApi::UpdateDisplay for more details.
  98. void UpdateDisplay(const std::string& display_specs);
  99. // Returns a root Window. Usually this is the active root Window, but that
  100. // method can return NULL sometimes, and in those cases, we fall back on the
  101. // primary root Window.
  102. aura::Window* GetContext();
  103. // Creates and shows a widget. See ash/public/cpp/shell_window_ids.h for
  104. // values for |container_id|.
  105. static std::unique_ptr<views::Widget> CreateTestWidget(
  106. views::WidgetDelegate* delegate = nullptr,
  107. int container_id = desks_util::GetActiveDeskContainerId(),
  108. const gfx::Rect& bounds = gfx::Rect(),
  109. bool show = true);
  110. // Creates a frameless widget for testing.
  111. static std::unique_ptr<views::Widget> CreateFramelessTestWidget();
  112. // Creates a widget with a visible WINDOW_TYPE_NORMAL window with the given
  113. // |app_type|. If |app_type| is AppType::NON_APP, this window is considered a
  114. // non-app window.
  115. // If |bounds_in_screen| is empty the window is added to the primary root
  116. // window, otherwise the window is added to the display matching
  117. // |bounds_in_screen|. |shell_window_id| is the shell window id to give to
  118. // the new window.
  119. // If |delegate| is empty, a new |TestWidgetDelegate| instance will be set as
  120. // this widget's delegate.
  121. std::unique_ptr<aura::Window> CreateAppWindow(
  122. const gfx::Rect& bounds_in_screen = gfx::Rect(),
  123. AppType app_type = AppType::SYSTEM_APP,
  124. int shell_window_id = kShellWindowId_Invalid,
  125. views::WidgetDelegate* delegate = nullptr);
  126. // Creates a visible window in the appropriate container. If
  127. // |bounds_in_screen| is empty the window is added to the primary root
  128. // window, otherwise the window is added to the display matching
  129. // |bounds_in_screen|. |shell_window_id| is the shell window id to give to
  130. // the new window.
  131. // If |type| is WINDOW_TYPE_NORMAL this creates a views::Widget, otherwise
  132. // this creates an aura::Window.
  133. std::unique_ptr<aura::Window> CreateTestWindow(
  134. const gfx::Rect& bounds_in_screen = gfx::Rect(),
  135. aura::client::WindowType type = aura::client::WINDOW_TYPE_NORMAL,
  136. int shell_window_id = kShellWindowId_Invalid);
  137. // Creates a visible top-level window with a delegate.
  138. std::unique_ptr<aura::Window> CreateToplevelTestWindow(
  139. const gfx::Rect& bounds_in_screen = gfx::Rect(),
  140. int shell_window_id = kShellWindowId_Invalid);
  141. // Versions of the functions in aura::test:: that go through our shell
  142. // StackingController instead of taking a parent.
  143. aura::Window* CreateTestWindowInShellWithId(int id);
  144. aura::Window* CreateTestWindowInShellWithBounds(const gfx::Rect& bounds);
  145. aura::Window* CreateTestWindowInShellWithDelegate(
  146. aura::WindowDelegate* delegate,
  147. int id,
  148. const gfx::Rect& bounds);
  149. aura::Window* CreateTestWindowInShellWithDelegateAndType(
  150. aura::WindowDelegate* delegate,
  151. aura::client::WindowType type,
  152. int id,
  153. const gfx::Rect& bounds);
  154. // Attach |window| to the current shell's root window.
  155. void ParentWindowInPrimaryRootWindow(aura::Window* window);
  156. // Prepares for the pixel diff test. When getting called, this function sets
  157. // the default pixel diff test init params. Users need to call
  158. // `SetPixelTestInitParam()` if they want to customize the pixel test setup.
  159. // NOTE: this function should be called before `setup_called_` becomes true.
  160. void PrepareForPixelDiffTest();
  161. // Sets the pixel diff test init params. Only called if users want to
  162. // customize the pixel test setup.
  163. // NOTE: `PrepareForPixelDiffTest()` has to be called before. In addition,
  164. // call this function before `setup_called_` becomes true.
  165. void SetPixelTestInitParam(const pixel_test::InitParams& params);
  166. // Stabilizes the variable UI components (such as the battery view). It should
  167. // be called after the active user changes since some UI components are
  168. // associated with the active account.
  169. void StabilizeUIForPixelTest();
  170. // Returns the EventGenerator that uses screen coordinates and works
  171. // across multiple displays. It creates a new generator if it
  172. // hasn't been created yet.
  173. ui::test::EventGenerator* GetEventGenerator();
  174. // Convenience method to return the DisplayManager.
  175. display::DisplayManager* display_manager();
  176. // Convenience method to return the FakePowerManagerClient.
  177. chromeos::FakePowerManagerClient* power_manager_client() const;
  178. // Test if moving a mouse to |point_in_screen| warps it to another
  179. // display.
  180. bool TestIfMouseWarpsAt(ui::test::EventGenerator* event_generator,
  181. const gfx::Point& point_in_screen);
  182. // Presses and releases a key to simulate typing one character.
  183. void PressAndReleaseKey(ui::KeyboardCode key_code, int flags = ui::EF_NONE);
  184. // Moves the mouse to the center of the view and generates a left mouse button
  185. // click event.
  186. void LeftClickOn(const views::View* view);
  187. // Moves the mouse to the center of the view and generates a right mouse
  188. // button click event.
  189. void RightClickOn(const views::View* view);
  190. // Generates a tap event on the center of `view`.
  191. void GestureTapOn(const views::View* view);
  192. // Enters/Exits overview mode with the given animation type `type`.
  193. bool EnterOverview(
  194. OverviewEnterExitType type = OverviewEnterExitType::kNormal);
  195. bool ExitOverview(
  196. OverviewEnterExitType type = OverviewEnterExitType::kNormal);
  197. protected:
  198. enum UserSessionBlockReason {
  199. FIRST_BLOCK_REASON,
  200. BLOCKED_BY_LOCK_SCREEN = FIRST_BLOCK_REASON,
  201. BLOCKED_BY_LOGIN_SCREEN,
  202. BLOCKED_BY_USER_ADDING_SCREEN,
  203. NUMBER_OF_BLOCK_REASONS
  204. };
  205. // Returns the rotation currentl active for the display |id|.
  206. static display::Display::Rotation GetActiveDisplayRotation(int64_t id);
  207. // Returns the rotation currently active for the internal display.
  208. static display::Display::Rotation GetCurrentInternalDisplayRotation();
  209. void set_start_session(bool start_session) { start_session_ = start_session; }
  210. base::test::TaskEnvironment* task_environment() {
  211. return task_environment_.get();
  212. }
  213. TestingPrefServiceSimple* local_state() { return &local_state_; }
  214. AshTestHelper* ash_test_helper() { return ash_test_helper_.get(); }
  215. void SetUserPref(const std::string& user_email,
  216. const std::string& path,
  217. const base::Value& value);
  218. TestSessionControllerClient* GetSessionControllerClient();
  219. TestSystemTrayClient* GetSystemTrayClient();
  220. AppListTestHelper* GetAppListTestHelper();
  221. TestAppListClient* GetTestAppListClient();
  222. AmbientAshTestHelper* GetAmbientAshTestHelper();
  223. // Emulates an ash session that have |session_count| user sessions running.
  224. // Note that existing user sessions will be cleared.
  225. void CreateUserSessions(int session_count);
  226. // Simulates a user sign-in. It creates a new user session, adds it to
  227. // existing user sessions and makes it the active user session.
  228. //
  229. // For convenience |user_email| is used to create an |AccountId|. For testing
  230. // behavior where |AccountId|s are compared, prefer the method of the same
  231. // name that takes an |AccountId| created with a valid storage key instead.
  232. // See the documentation for|AccountId::GetUserEmail| for discussion.
  233. // NOTE: call `StabilizeUIForPixelTest()` after using this function in a pixel
  234. // test.
  235. void SimulateUserLogin(
  236. const std::string& user_email,
  237. user_manager::UserType user_type = user_manager::USER_TYPE_REGULAR);
  238. // Simulates a user sign-in. It creates a new user session, adds it to
  239. // existing user sessions and makes it the active user session.
  240. // NOTE: call `StabilizeUIForPixelTest()` after using this function in a pixel
  241. // test.
  242. void SimulateUserLogin(
  243. const AccountId& account_id,
  244. user_manager::UserType user_type = user_manager::USER_TYPE_REGULAR);
  245. // Simular to SimulateUserLogin but for a newly created user first ever login.
  246. // NOTE: call `StabilizeUIForPixelTest()` after using this function in a pixel
  247. // test.
  248. void SimulateNewUserFirstLogin(const std::string& user_email);
  249. // Similar to SimulateUserLogin but for a guest user.
  250. // NOTE: call `StabilizeUIForPixelTest()` after using this function in a pixel
  251. // test.
  252. void SimulateGuestLogin();
  253. // Simulates kiosk mode. |user_type| must correlate to a kiosk type user.
  254. // NOTE: call `StabilizeUIForPixelTest()` after using this function in a pixel
  255. // test.
  256. void SimulateKioskMode(user_manager::UserType user_type);
  257. // Simulates setting height of the accessibility panel.
  258. // Note: Accessibility panel widget needs to be setup first.
  259. void SetAccessibilityPanelHeight(int panel_height);
  260. // Clears all user sessions and resets to the primary login screen state.
  261. void ClearLogin();
  262. // Emulates whether the active user can lock screen.
  263. void SetCanLockScreen(bool can_lock);
  264. // Emulates whether the screen should be locked automatically.
  265. void SetShouldLockScreenAutomatically(bool should_lock);
  266. // Emulates whether the user adding screen is running.
  267. void SetUserAddingScreenRunning(bool user_adding_screen_running);
  268. // Methods to emulate blocking and unblocking user session with given
  269. // |block_reason|.
  270. void BlockUserSession(UserSessionBlockReason block_reason);
  271. void UnblockUserSession();
  272. // Enable or disable the virtual on-screen keyboard and run the message loop
  273. // to allow observer operations to complete.
  274. void SetVirtualKeyboardEnabled(bool enabled);
  275. void DisableIME();
  276. // Swap the primary display with the secondary.
  277. void SwapPrimaryDisplay();
  278. display::Display GetPrimaryDisplay() const;
  279. display::Display GetSecondaryDisplay() const;
  280. private:
  281. void CreateWindowTreeIfNecessary();
  282. bool setup_called_ = false;
  283. bool teardown_called_ = false;
  284. // SetUp() doesn't activate session if this is set to false.
  285. bool start_session_ = true;
  286. // The parameters to initialize the pixel diff test. Used only when the ash
  287. // test is also a pixel diff test.
  288. absl::optional<pixel_test::InitParams> pixel_diff_init_params_;
  289. // |task_environment_| is initialized-once at construction time but
  290. // subclasses may elect to provide their own.
  291. std::unique_ptr<base::test::TaskEnvironment> task_environment_;
  292. // A pref service used for local state.
  293. TestingPrefServiceSimple local_state_;
  294. // Must be constructed after |task_environment_|.
  295. std::unique_ptr<AshTestHelper> ash_test_helper_;
  296. std::unique_ptr<ui::test::EventGenerator> event_generator_;
  297. };
  298. class NoSessionAshTestBase : public AshTestBase {
  299. public:
  300. NoSessionAshTestBase();
  301. explicit NoSessionAshTestBase(
  302. base::test::TaskEnvironment::TimeSource time_source);
  303. NoSessionAshTestBase(const NoSessionAshTestBase&) = delete;
  304. NoSessionAshTestBase& operator=(const NoSessionAshTestBase&) = delete;
  305. ~NoSessionAshTestBase() override;
  306. };
  307. } // namespace ash
  308. #endif // ASH_TEST_ASH_TEST_BASE_H_