1 // Copyright 2014 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 #include "extensions/shell/browser/shell_desktop_controller_aura.h"
6
7 #include <memory>
8
9 #include "base/macros.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/time/time.h"
12 #include "build/chromeos_buildflags.h"
13 #include "content/public/browser/browser_context.h"
14 #include "content/public/browser/web_contents.h"
15 #include "extensions/browser/app_window/app_window.h"
16 #include "extensions/browser/app_window/app_window_client.h"
17 #include "extensions/browser/app_window/app_window_registry.h"
18 #include "extensions/browser/app_window/native_app_window.h"
19 #include "extensions/browser/app_window/test_app_window_contents.h"
20 #include "extensions/common/extension_builder.h"
21 #include "extensions/shell/browser/shell_app_delegate.h"
22 #include "extensions/shell/test/shell_test_base_aura.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "ui/aura/window.h"
25 #include "ui/aura/window_event_dispatcher.h"
26 #include "ui/aura/window_tree_host.h"
27 #include "ui/base/ime/dummy_text_input_client.h"
28 #include "ui/base/ime/init/input_method_factory.h"
29 #include "ui/base/ime/input_method.h"
30 #include "ui/base/ime/input_method_minimal.h"
31 #include "ui/display/display.h"
32 #include "ui/display/screen_base.h"
33 #include "ui/events/event.h"
34 #include "ui/events/event_dispatcher.h"
35 #include "ui/events/keycodes/dom/dom_code.h"
36 #include "ui/events/keycodes/keyboard_codes.h"
37 #include "ui/gfx/geometry/rect.h"
38
39 #if BUILDFLAG(IS_CHROMEOS_ASH)
40 #include "chromeos/dbus/power/fake_power_manager_client.h"
41 #endif
42
43 namespace extensions {
44
45 class ShellDesktopControllerAuraTest : public ShellTestBaseAura {
46 public:
47 ShellDesktopControllerAuraTest() = default;
48 ~ShellDesktopControllerAuraTest() override = default;
49
SetUp()50 void SetUp() override {
51 ShellTestBaseAura::SetUp();
52
53 // Set up a screen with 2 displays.
54 screen_ = std::make_unique<display::ScreenBase>();
55 display::Screen::SetScreenInstance(screen_.get());
56 screen_->display_list().AddDisplay(
57 display::Display(100, gfx::Rect(0, 0, 1920, 1080)),
58 display::DisplayList::Type::PRIMARY);
59 screen_->display_list().AddDisplay(
60 display::Display(200, gfx::Rect(1920, 1080, 800, 600)),
61 display::DisplayList::Type::NOT_PRIMARY);
62
63 #if BUILDFLAG(IS_CHROMEOS_ASH)
64 chromeos::PowerManagerClient::InitializeFake();
65 #endif
66
67 controller_ =
68 std::make_unique<ShellDesktopControllerAura>(browser_context());
69 }
70
TearDown()71 void TearDown() override {
72 controller_.reset();
73 #if BUILDFLAG(IS_CHROMEOS_ASH)
74 chromeos::PowerManagerClient::Shutdown();
75 #endif
76 screen_.reset();
77 display::Screen::SetScreenInstance(nullptr);
78 ShellTestBaseAura::TearDown();
79 }
80
81 protected:
CreateAppWindow(const Extension * extension,gfx::Rect bounds={})82 AppWindow* CreateAppWindow(const Extension* extension,
83 gfx::Rect bounds = {}) {
84 AppWindow* app_window =
85 AppWindowClient::Get()->CreateAppWindow(browser_context(), extension);
86 InitAppWindow(app_window, bounds);
87 return app_window;
88 }
89
90 std::unique_ptr<display::ScreenBase> screen_;
91 std::unique_ptr<ShellDesktopControllerAura> controller_;
92
93 private:
94 DISALLOW_COPY_AND_ASSIGN(ShellDesktopControllerAuraTest);
95 };
96
97 #if BUILDFLAG(IS_CHROMEOS_ASH)
98 // Tests that a shutdown request is sent to the power manager when the power
99 // button is pressed.
TEST_F(ShellDesktopControllerAuraTest,PowerButton)100 TEST_F(ShellDesktopControllerAuraTest, PowerButton) {
101 // Ignore button releases.
102 auto* power_manager_client = chromeos::FakePowerManagerClient::Get();
103 power_manager_client->SendPowerButtonEvent(false /* down */,
104 base::TimeTicks());
105 EXPECT_EQ(0, power_manager_client->num_request_shutdown_calls());
106
107 // A button press should trigger a shutdown request.
108 power_manager_client->SendPowerButtonEvent(true /* down */,
109 base::TimeTicks());
110 EXPECT_EQ(1, power_manager_client->num_request_shutdown_calls());
111 }
112 #endif
113
114 // Tests that basic input events are handled and forwarded to the host.
115 // TODO(michaelpg): Test other types of input.
TEST_F(ShellDesktopControllerAuraTest,InputEvents)116 TEST_F(ShellDesktopControllerAuraTest, InputEvents) {
117 scoped_refptr<const Extension> extension = ExtensionBuilder("Test").Build();
118 CreateAppWindow(extension.get());
119
120 ui::InputMethod* input_method =
121 controller_->GetPrimaryHost()->GetInputMethod();
122 ASSERT_TRUE(input_method);
123
124 // Set up a focused text input to receive the keypress event.
125 ui::DummyTextInputClient client(ui::TEXT_INPUT_TYPE_TEXT);
126 input_method->SetFocusedTextInputClient(&client);
127 EXPECT_EQ(0, client.insert_char_count());
128
129 // Dispatch a keypress on the window tree host to verify it is processed.
130 ui::KeyEvent key_press(base::char16(97), ui::VKEY_A, ui::DomCode::NONE,
131 ui::EF_NONE);
132 ui::EventDispatchDetails details =
133 controller_->GetPrimaryHost()->dispatcher()->DispatchEvent(
134 controller_->GetPrimaryHost()->window(), &key_press);
135 EXPECT_FALSE(details.dispatcher_destroyed);
136 EXPECT_FALSE(details.target_destroyed);
137 EXPECT_TRUE(key_press.handled());
138 EXPECT_EQ(1, client.insert_char_count());
139
140 // Clean up.
141 input_method->DetachTextInputClient(&client);
142 }
143
144 // Tests closing all AppWindows.
TEST_F(ShellDesktopControllerAuraTest,CloseAppWindows)145 TEST_F(ShellDesktopControllerAuraTest, CloseAppWindows) {
146 const AppWindowRegistry* app_window_registry =
147 AppWindowRegistry::Get(browser_context());
148 scoped_refptr<const Extension> extension = ExtensionBuilder("Test").Build();
149 for (int i = 0; i < 3; i++)
150 CreateAppWindow(extension.get());
151 EXPECT_EQ(3u, app_window_registry->app_windows().size());
152
153 controller_->CloseAppWindows();
154 EXPECT_EQ(0u, app_window_registry->app_windows().size());
155 }
156
157 // Tests that the AppWindows are removed when the desktop controller goes away.
TEST_F(ShellDesktopControllerAuraTest,OnAppWindowClose)158 TEST_F(ShellDesktopControllerAuraTest, OnAppWindowClose) {
159 const AppWindowRegistry* app_window_registry =
160 AppWindowRegistry::Get(browser_context());
161 scoped_refptr<const Extension> extension = ExtensionBuilder("Test").Build();
162 for (int i = 0; i < 3; i++)
163 CreateAppWindow(extension.get());
164 EXPECT_EQ(3u, app_window_registry->app_windows().size());
165
166 // Deleting the controller closes all app windows.
167 controller_.reset();
168 EXPECT_EQ(0u, app_window_registry->app_windows().size());
169 }
170
171 // Tests that multiple displays result in multiple root windows.
TEST_F(ShellDesktopControllerAuraTest,MultipleDisplays)172 TEST_F(ShellDesktopControllerAuraTest, MultipleDisplays) {
173 const AppWindowRegistry* app_window_registry =
174 AppWindowRegistry::Get(browser_context());
175 scoped_refptr<const Extension> extension = ExtensionBuilder("Test").Build();
176
177 // Create two apps window on the primary display. Both should be hosted in the
178 // same RootWindowController.
179 AppWindow* primary_app_window = CreateAppWindow(extension.get());
180 AppWindow* primary_app_window_2 = CreateAppWindow(extension.get());
181 EXPECT_EQ(2u, app_window_registry->app_windows().size());
182 EXPECT_EQ(1u, controller_->GetAllRootWindows().size());
183
184 // Create an app window near the secondary display, which should result in
185 // creating a RootWindowController for that display.
186 AppWindow* secondary_app_window =
187 CreateAppWindow(extension.get(), gfx::Rect(1900, 1000, 600, 400));
188 EXPECT_EQ(3u, app_window_registry->app_windows().size());
189 EXPECT_EQ(2u, controller_->GetAllRootWindows().size());
190
191 aura::Window* primary_root = controller_->GetPrimaryHost()->window();
192
193 // Move an app window from the second display to the primary display.
194 EXPECT_NE(secondary_app_window->GetNativeWindow()->GetRootWindow(),
195 primary_root);
196 controller_->SetWindowBoundsInScreen(secondary_app_window,
197 gfx::Rect(0, 0, 100, 100));
198 EXPECT_EQ(secondary_app_window->GetNativeWindow()->GetRootWindow(),
199 primary_root);
200
201 // Move an app window from the primary display to the secondary display.
202 EXPECT_EQ(primary_app_window->GetNativeWindow()->GetRootWindow(),
203 primary_root);
204 controller_->SetWindowBoundsInScreen(primary_app_window,
205 gfx::Rect(1920, 1080, 800, 600));
206 EXPECT_NE(primary_app_window->GetNativeWindow()->GetRootWindow(),
207 primary_root);
208
209 // Test moving an app window within its display.
210 EXPECT_EQ(primary_app_window_2->GetNativeWindow()->GetRootWindow(),
211 primary_root);
212 controller_->SetWindowBoundsInScreen(primary_app_window_2,
213 gfx::Rect(100, 100, 500, 600));
214 EXPECT_EQ(primary_app_window_2->GetNativeWindow()->GetRootWindow(),
215 primary_root);
216 }
217
218 } // namespace extensions
219