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 void OnGeometryCacheChanged(const std::string& extension_id,
50 const std::string& window_id,
51 const gfx::Rect& bounds) override {
52 if (extension_id != extension_id_ || window_id != window_id_)
55 if (bounds_.x() != bounds.x() &&
56 bounds_.y() != bounds.y() &&
57 bounds_.width() != bounds.width() &&
58 bounds_.height() != bounds.height()) {
60 cache_->RemoveObserver(this);
63 base::MessageLoopForUI::current()->Quit();
68 AppWindowGeometryCache* cache_;
69 std::string extension_id_;
70 std::string window_id_;
76 // Helper class for tests related to the Apps Window API (chrome.app.window).
77 class AppWindowAPITest : public extensions::PlatformAppBrowserTest {
79 bool RunAppWindowAPITest(const char* testName) {
80 if (!BeginAppWindowAPITest(testName))
83 ResultCatcher catcher;
84 if (!catcher.GetNextResult()) {
85 message_ = catcher.message();
92 bool RunAppWindowAPITestAndWaitForRoundTrip(const char* testName) {
93 if (!BeginAppWindowAPITest(testName))
96 ExtensionTestMessageListener round_trip_listener("WaitForRoundTrip", true);
97 if (!round_trip_listener.WaitUntilSatisfied()) {
98 message_ = "Did not get the 'WaitForRoundTrip' message.";
102 round_trip_listener.Reply("");
104 ResultCatcher catcher;
105 if (!catcher.GetNextResult()) {
106 message_ = catcher.message();
114 bool BeginAppWindowAPITest(const char* testName) {
115 ExtensionTestMessageListener launched_listener("Launched", true);
116 LoadAndLaunchPlatformApp("window_api", &launched_listener);
117 if (!launched_listener.WaitUntilSatisfied()) {
118 message_ = "Did not get the 'Launched' message.";
122 launched_listener.Reply(testName);
127 // These tests are flaky after https://codereview.chromium.org/57433010/.
128 // See http://crbug.com/319613.
130 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, TestCreate) {
131 ASSERT_TRUE(RunAppWindowAPITest("testCreate")) << message_;
134 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, TestSingleton) {
135 ASSERT_TRUE(RunAppWindowAPITest("testSingleton")) << message_;
138 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, TestCloseEvent) {
139 ASSERT_TRUE(RunAppWindowAPITest("testCloseEvent")) << message_;
142 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, DISABLED_TestMaximize) {
143 ASSERT_TRUE(RunAppWindowAPITest("testMaximize")) << message_;
146 // Flaky on Linux. http://crbug.com/424399.
147 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
148 #define MAYBE_TestMinimize DISABLED_TestMinimize
150 #define MAYBE_TestMinimize TestMinimize
153 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, MAYBE_TestMinimize) {
154 ASSERT_TRUE(RunAppWindowAPITest("testMinimize")) << message_;
157 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, DISABLED_TestRestore) {
158 ASSERT_TRUE(RunAppWindowAPITest("testRestore")) << message_;
161 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, DISABLED_TestRestoreAfterClose) {
162 ASSERT_TRUE(RunAppWindowAPITest("testRestoreAfterClose")) << message_;
165 // These tests will be flaky in Linux as window bounds change asynchronously.
166 #if defined(OS_LINUX)
167 #define MAYBE_TestDeprecatedBounds DISABLED_TestDeprecatedBounds
168 #define MAYBE_TestInitialBounds DISABLED_TestInitialBounds
169 #define MAYBE_TestInitialConstraints DISABLED_TestInitialConstraints
170 #define MAYBE_TestSetBounds DISABLED_TestSetBounds
171 #define MAYBE_TestSetSizeConstraints DISABLED_TestSetSizeConstraints
173 #define MAYBE_TestDeprecatedBounds TestDeprecatedBounds
174 #define MAYBE_TestInitialBounds TestInitialBounds
175 #define MAYBE_TestInitialConstraints TestInitialConstraints
176 #define MAYBE_TestSetBounds TestSetBounds
177 #define MAYBE_TestSetSizeConstraints TestSetSizeConstraints
180 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, MAYBE_TestDeprecatedBounds) {
181 ASSERT_TRUE(RunAppWindowAPITest("testDeprecatedBounds")) << message_;
184 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, MAYBE_TestInitialBounds) {
185 ASSERT_TRUE(RunAppWindowAPITest("testInitialBounds")) << message_;
188 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, MAYBE_TestInitialConstraints) {
189 ASSERT_TRUE(RunAppWindowAPITest("testInitialConstraints")) << message_;
192 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, MAYBE_TestSetBounds) {
193 ASSERT_TRUE(RunAppWindowAPITest("testSetBounds")) << message_;
196 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, MAYBE_TestSetSizeConstraints) {
197 ASSERT_TRUE(RunAppWindowAPITest("testSetSizeConstraints")) << message_;
200 // Flaky failures on mac_rel and WinXP, see http://crbug.com/324915.
201 IN_PROC_BROWSER_TEST_F(AppWindowAPITest,
202 DISABLED_TestRestoreGeometryCacheChange) {
203 // This test is similar to the other AppWindowAPI tests except that at some
204 // point the app will send a 'ListenGeometryChange' message at which point the
205 // test will check if the geometry cache entry for the test window has
206 // changed. When the change happens, the test will let the app know so it can
208 ExtensionTestMessageListener launched_listener("Launched", true);
210 content::WindowedNotificationObserver app_loaded_observer(
211 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
212 content::NotificationService::AllSources());
214 const extensions::Extension* extension = LoadExtension(
215 test_data_dir_.AppendASCII("platform_apps").AppendASCII("window_api"));
216 EXPECT_TRUE(extension);
218 OpenApplication(AppLaunchParams(browser()->profile(),
220 extensions::LAUNCH_CONTAINER_NONE,
223 ExtensionTestMessageListener geometry_listener("ListenGeometryChange", true);
225 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
226 launched_listener.Reply("testRestoreAfterGeometryCacheChange");
228 ASSERT_TRUE(geometry_listener.WaitUntilSatisfied());
230 GeometryCacheChangeHelper geo_change_helper_1(
231 AppWindowGeometryCache::Get(browser()->profile()),
233 // The next line has information that has to stay in sync with the app.
235 gfx::Rect(200, 200, 200, 200));
237 GeometryCacheChangeHelper geo_change_helper_2(
238 AppWindowGeometryCache::Get(browser()->profile()),
240 // The next line has information that has to stay in sync with the app.
242 gfx::Rect(200, 200, 200, 200));
244 // These calls will block until the app window geometry cache will change.
245 geo_change_helper_1.WaitForEntirelyChanged();
246 geo_change_helper_2.WaitForEntirelyChanged();
248 ResultCatcher catcher;
249 geometry_listener.Reply("");
250 ASSERT_TRUE(catcher.GetNextResult());
253 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, TestBadging) {
255 RunAppWindowAPITestAndWaitForRoundTrip("testBadging")) << message_;
258 // TODO(benwells): Implement on Mac.
259 #if defined(USE_AURA)
260 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, TestFrameColors) {
261 ASSERT_TRUE(RunAppWindowAPITest("testFrameColors")) << message_;
265 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, TestVisibleOnAllWorkspaces) {
267 RunAppWindowAPITestAndWaitForRoundTrip("testVisibleOnAllWorkspaces"))