Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / apps / app_window_interactive_uitest.cc
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 #include "apps/ui/native_app_window.h"
6 #include "chrome/browser/apps/app_browsertest_util.h"
7 #include "chrome/browser/extensions/extension_test_message_listener.h"
8 #include "chrome/test/base/interactive_test_utils.h"
9
10 #if defined(OS_MACOSX) && !defined(OS_IOS)
11 #include "base/mac/mac_util.h"
12 #endif
13
14 using apps::NativeAppWindow;
15
16 // Helper class that has to be created in the stack to check if the fullscreen
17 // setting of a NativeWindow has changed since the creation of the object.
18 class FullscreenChangeWaiter {
19  public:
20   explicit FullscreenChangeWaiter(NativeAppWindow* window)
21       : window_(window),
22         initial_fullscreen_state_(window_->IsFullscreen()) {}
23
24   void Wait() {
25     while (initial_fullscreen_state_ == window_->IsFullscreen())
26       content::RunAllPendingInMessageLoop();
27   }
28
29  private:
30   NativeAppWindow* window_;
31   bool initial_fullscreen_state_;
32
33   DISALLOW_COPY_AND_ASSIGN(FullscreenChangeWaiter);
34 };
35
36 class AppWindowInteractiveTest : public extensions::PlatformAppBrowserTest {
37  public:
38   bool RunAppWindowInteractiveTest(const char* testName) {
39     ExtensionTestMessageListener launched_listener("Launched", true);
40     LoadAndLaunchPlatformApp("window_api_interactive");
41     if (!launched_listener.WaitUntilSatisfied()) {
42       message_ = "Did not get the 'Launched' message.";
43       return false;
44     }
45
46     ResultCatcher catcher;
47     launched_listener.Reply(testName);
48
49     if (!catcher.GetNextResult()) {
50       message_ = catcher.message();
51       return false;
52     }
53
54     return true;
55   }
56
57   bool SimulateKeyPress(ui::KeyboardCode key) {
58     return ui_test_utils::SendKeyPressToWindowSync(
59       GetFirstShellWindow()->GetNativeWindow(),
60       key,
61       false,
62       false,
63       false,
64       false);
65   }
66
67   // This method will wait until the application is able to ack a key event.
68   void WaitUntilKeyFocus() {
69     ExtensionTestMessageListener key_listener("KeyReceived", false);
70
71     while (!key_listener.was_satisfied()) {
72       ASSERT_TRUE(SimulateKeyPress(ui::VKEY_Z));
73       content::RunAllPendingInMessageLoop();
74     }
75   }
76 };
77
78 IN_PROC_BROWSER_TEST_F(AppWindowInteractiveTest, ESCLeavesFullscreenWindow) {
79 // This test is flaky on MacOS 10.6.
80 #if defined(OS_MACOSX) && !defined(OS_IOS)
81   if (base::mac::IsOSSnowLeopard())
82     return;
83 #endif
84
85   ExtensionTestMessageListener launched_listener("Launched", true);
86   LoadAndLaunchPlatformApp("leave_fullscreen");
87   ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
88
89   // We start by making sure the window is actually focused.
90   ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(
91                   GetFirstShellWindow()->GetNativeWindow()));
92
93   // When receiving the reply, the application will try to go fullscreen using
94   // the Window API but there is no synchronous way to know if that actually
95   // succeeded. Also, failure will not be notified. A failure case will only be
96   // known with a timeout.
97   {
98     FullscreenChangeWaiter fs_changed(GetFirstShellWindow()->GetBaseWindow());
99
100     launched_listener.Reply("window");
101
102     fs_changed.Wait();
103   }
104
105   // Depending on the platform, going fullscreen might create an animation.
106   // We want to make sure that the ESC key we will send next is actually going
107   // to be received and the application might not receive key events during the
108   // animation so we should wait for the key focus to be back.
109   WaitUntilKeyFocus();
110
111   // Same idea as above but for leaving fullscreen. Fullscreen mode should be
112   // left when ESC is received.
113   {
114     FullscreenChangeWaiter fs_changed(GetFirstShellWindow()->GetBaseWindow());
115
116     ASSERT_TRUE(SimulateKeyPress(ui::VKEY_ESCAPE));
117
118     fs_changed.Wait();
119   }
120 }
121
122 IN_PROC_BROWSER_TEST_F(AppWindowInteractiveTest, ESCLeavesFullscreenDOM) {
123 // This test is flaky on MacOS 10.6.
124 #if defined(OS_MACOSX) && !defined(OS_IOS)
125   if (base::mac::IsOSSnowLeopard())
126     return;
127 #endif
128
129   ExtensionTestMessageListener launched_listener("Launched", true);
130   LoadAndLaunchPlatformApp("leave_fullscreen");
131   ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
132
133   // We start by making sure the window is actually focused.
134   ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(
135                 GetFirstShellWindow()->GetNativeWindow()));
136
137   launched_listener.Reply("dom");
138
139   // Because the DOM way to go fullscreen requires user gesture, we simulate a
140   // key event to get the window entering in fullscreen mode. The reply will
141   // make the window listen for the key event. The reply will be sent to the
142   // renderer process before the keypress and should be received in that order.
143   // When receiving the key event, the application will try to go fullscreen
144   // using the Window API but there is no synchronous way to know if that
145   // actually succeeded. Also, failure will not be notified. A failure case will
146   // only be known with a timeout.
147   {
148     FullscreenChangeWaiter fs_changed(GetFirstShellWindow()->GetBaseWindow());
149
150     WaitUntilKeyFocus();
151     ASSERT_TRUE(SimulateKeyPress(ui::VKEY_A));
152
153     fs_changed.Wait();
154   }
155
156   // Depending on the platform, going fullscreen might create an animation.
157   // We want to make sure that the ESC key we will send next is actually going
158   // to be received and the application might not receive key events during the
159   // animation so we should wait for the key focus to be back.
160   WaitUntilKeyFocus();
161
162   // Same idea as above but for leaving fullscreen. Fullscreen mode should be
163   // left when ESC is received.
164   {
165     FullscreenChangeWaiter fs_changed(GetFirstShellWindow()->GetBaseWindow());
166
167     ASSERT_TRUE(SimulateKeyPress(ui::VKEY_ESCAPE));
168
169     fs_changed.Wait();
170   }
171 }
172
173 IN_PROC_BROWSER_TEST_F(AppWindowInteractiveTest,
174                        ESCDoesNotLeaveFullscreenWindow) {
175 // This test is flaky on MacOS 10.6.
176 #if defined(OS_MACOSX) && !defined(OS_IOS)
177   if (base::mac::IsOSSnowLeopard())
178     return;
179 #endif
180
181   ExtensionTestMessageListener launched_listener("Launched", true);
182   LoadAndLaunchPlatformApp("prevent_leave_fullscreen");
183   ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
184
185   // We start by making sure the window is actually focused.
186   ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(
187                 GetFirstShellWindow()->GetNativeWindow()));
188
189   // When receiving the reply, the application will try to go fullscreen using
190   // the Window API but there is no synchronous way to know if that actually
191   // succeeded. Also, failure will not be notified. A failure case will only be
192   // known with a timeout.
193   {
194     FullscreenChangeWaiter fs_changed(GetFirstShellWindow()->GetBaseWindow());
195
196     launched_listener.Reply("window");
197
198     fs_changed.Wait();
199   }
200
201   // Depending on the platform, going fullscreen might create an animation.
202   // We want to make sure that the ESC key we will send next is actually going
203   // to be received and the application might not receive key events during the
204   // animation so we should wait for the key focus to be back.
205   WaitUntilKeyFocus();
206
207   ASSERT_TRUE(SimulateKeyPress(ui::VKEY_ESCAPE));
208
209   ExtensionTestMessageListener second_key_listener("B_KEY_RECEIVED", false);
210
211   ASSERT_TRUE(SimulateKeyPress(ui::VKEY_B));
212
213   ASSERT_TRUE(second_key_listener.WaitUntilSatisfied());
214
215   // We assume that at that point, if we had to leave fullscreen, we should be.
216   // However, by nature, we can not guarantee that and given that we do test
217   // that nothing happens, we might end up with random-success when the feature
218   // is broken.
219   EXPECT_TRUE(GetFirstShellWindow()->GetBaseWindow()->IsFullscreen());
220 }
221
222 IN_PROC_BROWSER_TEST_F(AppWindowInteractiveTest,
223                        ESCDoesNotLeaveFullscreenDOM) {
224 // This test is flaky on MacOS 10.6.
225 #if defined(OS_MACOSX) && !defined(OS_IOS)
226   if (base::mac::IsOSSnowLeopard())
227     return;
228 #endif
229
230   ExtensionTestMessageListener launched_listener("Launched", true);
231   LoadAndLaunchPlatformApp("prevent_leave_fullscreen");
232   ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
233
234   // We start by making sure the window is actually focused.
235   ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(
236                 GetFirstShellWindow()->GetNativeWindow()));
237
238   launched_listener.Reply("dom");
239
240   // Because the DOM way to go fullscreen requires user gesture, we simulate a
241   // key event to get the window entering in fullscreen mode. The reply will
242   // make the window listen for the key event. The reply will be sent to the
243   // renderer process before the keypress and should be received in that order.
244   // When receiving the key event, the application will try to go fullscreen
245   // using the Window API but there is no synchronous way to know if that
246   // actually succeeded. Also, failure will not be notified. A failure case will
247   // only be known with a timeout.
248   {
249     FullscreenChangeWaiter fs_changed(GetFirstShellWindow()->GetBaseWindow());
250
251     WaitUntilKeyFocus();
252     ASSERT_TRUE(SimulateKeyPress(ui::VKEY_A));
253
254     fs_changed.Wait();
255   }
256
257   // Depending on the platform, going fullscreen might create an animation.
258   // We want to make sure that the ESC key we will send next is actually going
259   // to be received and the application might not receive key events during the
260   // animation so we should wait for the key focus to be back.
261   WaitUntilKeyFocus();
262
263   ASSERT_TRUE(SimulateKeyPress(ui::VKEY_ESCAPE));
264
265   ExtensionTestMessageListener second_key_listener("B_KEY_RECEIVED", false);
266
267   ASSERT_TRUE(SimulateKeyPress(ui::VKEY_B));
268
269   ASSERT_TRUE(second_key_listener.WaitUntilSatisfied());
270
271   // We assume that at that point, if we had to leave fullscreen, we should be.
272   // However, by nature, we can not guarantee that and given that we do test
273   // that nothing happens, we might end up with random-success when the feature
274   // is broken.
275   EXPECT_TRUE(GetFirstShellWindow()->GetBaseWindow()->IsFullscreen());
276 }
277
278 // This test does not work on Linux Aura because ShowInactive() is not
279 // implemented. See http://crbug.com/325142
280 // It also does not work on Windows because of the document being focused even
281 // though the window is not activated. See http://crbug.com/326986
282 // It also does not work on MacOS because ::ShowInactive() ends up behaving like
283 // ::Show() because of Cocoa conventions. See http://crbug.com/326987
284 // Those tests should be disabled on Linux GTK when they are enabled on the
285 // other platforms, see http://crbug.com/328829
286 #if (defined(OS_LINUX) && defined(USE_AURA)) || \
287     defined(OS_WIN) || defined(OS_MACOSX)
288 #define MAYBE_TestCreate DISABLED_TestCreate
289 #define MAYBE_TestShow DISABLED_TestShow
290 #else
291 #define MAYBE_TestCreate TestCreate
292 #define MAYBE_TestShow TestShow
293 #endif
294
295 IN_PROC_BROWSER_TEST_F(AppWindowInteractiveTest, MAYBE_TestCreate) {
296   ASSERT_TRUE(RunAppWindowInteractiveTest("testCreate")) << message_;
297 }
298
299 IN_PROC_BROWSER_TEST_F(AppWindowInteractiveTest, MAYBE_TestShow) {
300   ASSERT_TRUE(RunAppWindowInteractiveTest("testShow")) << message_;
301 }