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.
5 #include "chrome/browser/apps/app_browsertest_util.h"
6 #include "chrome/browser/profiles/profile.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/extensions/application_launch.h"
9 #include "content/public/browser/notification_service.h"
10 #include "content/public/test/test_utils.h"
11 #include "extensions/browser/app_window/app_window_geometry_cache.h"
12 #include "extensions/common/constants.h"
13 #include "extensions/common/extension.h"
14 #include "extensions/test/extension_test_message_listener.h"
15 #include "extensions/test/result_catcher.h"
17 using extensions::AppWindowGeometryCache
;
18 using extensions::ResultCatcher
;
20 // This helper class can be used to wait for changes in the app window
21 // geometry cache registry for a specific window in a specific extension.
22 class GeometryCacheChangeHelper
: AppWindowGeometryCache::Observer
{
24 GeometryCacheChangeHelper(AppWindowGeometryCache
* cache
,
25 const std::string
& extension_id
,
26 const std::string
& window_id
,
27 const gfx::Rect
& bounds
)
29 extension_id_(extension_id
),
30 window_id_(window_id
),
34 cache_
->AddObserver(this);
37 // This method will block until the app window geometry cache registry will
38 // provide a bound for |window_id_| that is entirely different (as in x/y/w/h)
39 // from the initial |bounds_|.
40 void WaitForEntirelyChanged() {
45 content::RunMessageLoop();
48 // Implements the content::NotificationObserver interface.
49 virtual void OnGeometryCacheChanged(const std::string
& extension_id
,
50 const std::string
& window_id
,
51 const gfx::Rect
& bounds
)
53 if (extension_id
!= extension_id_
|| window_id
!= window_id_
)
56 if (bounds_
.x() != bounds
.x() &&
57 bounds_
.y() != bounds
.y() &&
58 bounds_
.width() != bounds
.width() &&
59 bounds_
.height() != bounds
.height()) {
61 cache_
->RemoveObserver(this);
64 base::MessageLoopForUI::current()->Quit();
69 AppWindowGeometryCache
* cache_
;
70 std::string extension_id_
;
71 std::string window_id_
;
77 // Helper class for tests related to the Apps Window API (chrome.app.window).
78 class AppWindowAPITest
: public extensions::PlatformAppBrowserTest
{
80 bool RunAppWindowAPITest(const char* testName
) {
81 if (!BeginAppWindowAPITest(testName
))
84 ResultCatcher catcher
;
85 if (!catcher
.GetNextResult()) {
86 message_
= catcher
.message();
93 bool RunAppWindowAPITestAndWaitForRoundTrip(const char* testName
) {
94 if (!BeginAppWindowAPITest(testName
))
97 ExtensionTestMessageListener
round_trip_listener("WaitForRoundTrip", true);
98 if (!round_trip_listener
.WaitUntilSatisfied()) {
99 message_
= "Did not get the 'WaitForRoundTrip' message.";
103 round_trip_listener
.Reply("");
105 ResultCatcher catcher
;
106 if (!catcher
.GetNextResult()) {
107 message_
= catcher
.message();
115 bool BeginAppWindowAPITest(const char* testName
) {
116 ExtensionTestMessageListener
launched_listener("Launched", true);
117 LoadAndLaunchPlatformApp("window_api", &launched_listener
);
118 if (!launched_listener
.WaitUntilSatisfied()) {
119 message_
= "Did not get the 'Launched' message.";
123 launched_listener
.Reply(testName
);
128 // These tests are flaky after https://codereview.chromium.org/57433010/.
129 // See http://crbug.com/319613.
131 IN_PROC_BROWSER_TEST_F(AppWindowAPITest
, TestCreate
) {
132 ASSERT_TRUE(RunAppWindowAPITest("testCreate")) << message_
;
135 IN_PROC_BROWSER_TEST_F(AppWindowAPITest
, TestSingleton
) {
136 ASSERT_TRUE(RunAppWindowAPITest("testSingleton")) << message_
;
139 IN_PROC_BROWSER_TEST_F(AppWindowAPITest
, TestCloseEvent
) {
140 ASSERT_TRUE(RunAppWindowAPITest("testCloseEvent")) << message_
;
143 IN_PROC_BROWSER_TEST_F(AppWindowAPITest
, DISABLED_TestMaximize
) {
144 ASSERT_TRUE(RunAppWindowAPITest("testMaximize")) << message_
;
147 IN_PROC_BROWSER_TEST_F(AppWindowAPITest
, TestMinimize
) {
148 ASSERT_TRUE(RunAppWindowAPITest("testMinimize")) << message_
;
151 IN_PROC_BROWSER_TEST_F(AppWindowAPITest
, DISABLED_TestRestore
) {
152 ASSERT_TRUE(RunAppWindowAPITest("testRestore")) << message_
;
155 IN_PROC_BROWSER_TEST_F(AppWindowAPITest
, DISABLED_TestRestoreAfterClose
) {
156 ASSERT_TRUE(RunAppWindowAPITest("testRestoreAfterClose")) << message_
;
159 // These tests will be flaky in Linux as window bounds change asynchronously.
160 #if defined(OS_LINUX)
161 #define MAYBE_TestDeprecatedBounds DISABLED_TestDeprecatedBounds
162 #define MAYBE_TestInitialBounds DISABLED_TestInitialBounds
163 #define MAYBE_TestInitialConstraints DISABLED_TestInitialConstraints
164 #define MAYBE_TestSetBounds DISABLED_TestSetBounds
165 #define MAYBE_TestSetSizeConstraints DISABLED_TestSetSizeConstraints
167 #define MAYBE_TestDeprecatedBounds TestDeprecatedBounds
168 #define MAYBE_TestInitialBounds TestInitialBounds
169 #define MAYBE_TestInitialConstraints TestInitialConstraints
170 #define MAYBE_TestSetBounds TestSetBounds
171 #define MAYBE_TestSetSizeConstraints TestSetSizeConstraints
174 IN_PROC_BROWSER_TEST_F(AppWindowAPITest
, MAYBE_TestDeprecatedBounds
) {
175 ASSERT_TRUE(RunAppWindowAPITest("testDeprecatedBounds")) << message_
;
178 IN_PROC_BROWSER_TEST_F(AppWindowAPITest
, MAYBE_TestInitialBounds
) {
179 ASSERT_TRUE(RunAppWindowAPITest("testInitialBounds")) << message_
;
182 IN_PROC_BROWSER_TEST_F(AppWindowAPITest
, MAYBE_TestInitialConstraints
) {
183 ASSERT_TRUE(RunAppWindowAPITest("testInitialConstraints")) << message_
;
186 IN_PROC_BROWSER_TEST_F(AppWindowAPITest
, MAYBE_TestSetBounds
) {
187 ASSERT_TRUE(RunAppWindowAPITest("testSetBounds")) << message_
;
190 IN_PROC_BROWSER_TEST_F(AppWindowAPITest
, MAYBE_TestSetSizeConstraints
) {
191 ASSERT_TRUE(RunAppWindowAPITest("testSetSizeConstraints")) << message_
;
194 // Flaky failures on mac_rel and WinXP, see http://crbug.com/324915.
195 IN_PROC_BROWSER_TEST_F(AppWindowAPITest
,
196 DISABLED_TestRestoreGeometryCacheChange
) {
197 // This test is similar to the other AppWindowAPI tests except that at some
198 // point the app will send a 'ListenGeometryChange' message at which point the
199 // test will check if the geometry cache entry for the test window has
200 // changed. When the change happens, the test will let the app know so it can
202 ExtensionTestMessageListener
launched_listener("Launched", true);
204 content::WindowedNotificationObserver
app_loaded_observer(
205 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
206 content::NotificationService::AllSources());
208 const extensions::Extension
* extension
= LoadExtension(
209 test_data_dir_
.AppendASCII("platform_apps").AppendASCII("window_api"));
210 EXPECT_TRUE(extension
);
212 OpenApplication(AppLaunchParams(browser()->profile(),
214 extensions::LAUNCH_CONTAINER_NONE
,
217 ExtensionTestMessageListener
geometry_listener("ListenGeometryChange", true);
219 ASSERT_TRUE(launched_listener
.WaitUntilSatisfied());
220 launched_listener
.Reply("testRestoreAfterGeometryCacheChange");
222 ASSERT_TRUE(geometry_listener
.WaitUntilSatisfied());
224 GeometryCacheChangeHelper
geo_change_helper_1(
225 AppWindowGeometryCache::Get(browser()->profile()),
227 // The next line has information that has to stay in sync with the app.
229 gfx::Rect(200, 200, 200, 200));
231 GeometryCacheChangeHelper
geo_change_helper_2(
232 AppWindowGeometryCache::Get(browser()->profile()),
234 // The next line has information that has to stay in sync with the app.
236 gfx::Rect(200, 200, 200, 200));
238 // These calls will block until the app window geometry cache will change.
239 geo_change_helper_1
.WaitForEntirelyChanged();
240 geo_change_helper_2
.WaitForEntirelyChanged();
242 ResultCatcher catcher
;
243 geometry_listener
.Reply("");
244 ASSERT_TRUE(catcher
.GetNextResult());
247 IN_PROC_BROWSER_TEST_F(AppWindowAPITest
, TestBadging
) {
249 RunAppWindowAPITestAndWaitForRoundTrip("testBadging")) << message_
;
252 // TODO(benwells): Implement on Mac.
253 #if defined(USE_AURA)
254 IN_PROC_BROWSER_TEST_F(AppWindowAPITest
, TestFrameColors
) {
255 ASSERT_TRUE(RunAppWindowAPITest("testFrameColors")) << message_
;
259 IN_PROC_BROWSER_TEST_F(AppWindowAPITest
, TestVisibleOnAllWorkspaces
) {
261 RunAppWindowAPITestAndWaitForRoundTrip("testVisibleOnAllWorkspaces"))