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