Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / apps / app_window_browsertest.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 "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"
16
17 using extensions::AppWindowGeometryCache;
18 using extensions::ResultCatcher;
19
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 {
23  public:
24   GeometryCacheChangeHelper(AppWindowGeometryCache* cache,
25                             const std::string& extension_id,
26                             const std::string& window_id,
27                             const gfx::Rect& bounds)
28       : cache_(cache),
29         extension_id_(extension_id),
30         window_id_(window_id),
31         bounds_(bounds),
32         satisfied_(false),
33         waiting_(false) {
34     cache_->AddObserver(this);
35   }
36
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() {
41     if (satisfied_)
42       return;
43
44     waiting_ = true;
45     content::RunMessageLoop();
46   }
47
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_)
53       return;
54
55     if (bounds_.x() != bounds.x() &&
56         bounds_.y() != bounds.y() &&
57         bounds_.width() != bounds.width() &&
58         bounds_.height() != bounds.height()) {
59       satisfied_ = true;
60       cache_->RemoveObserver(this);
61
62       if (waiting_)
63         base::MessageLoopForUI::current()->Quit();
64     }
65   }
66
67  private:
68   AppWindowGeometryCache* cache_;
69   std::string extension_id_;
70   std::string window_id_;
71   gfx::Rect bounds_;
72   bool satisfied_;
73   bool waiting_;
74 };
75
76 // Helper class for tests related to the Apps Window API (chrome.app.window).
77 class AppWindowAPITest : public extensions::PlatformAppBrowserTest {
78  protected:
79   bool RunAppWindowAPITest(const char* testName) {
80     if (!BeginAppWindowAPITest(testName))
81       return false;
82
83     ResultCatcher catcher;
84     if (!catcher.GetNextResult()) {
85       message_ = catcher.message();
86       return false;
87     }
88
89     return true;
90   }
91
92   bool RunAppWindowAPITestAndWaitForRoundTrip(const char* testName) {
93     if (!BeginAppWindowAPITest(testName))
94       return false;
95
96     ExtensionTestMessageListener round_trip_listener("WaitForRoundTrip", true);
97     if (!round_trip_listener.WaitUntilSatisfied()) {
98       message_ = "Did not get the 'WaitForRoundTrip' message.";
99       return false;
100     }
101
102     round_trip_listener.Reply("");
103
104     ResultCatcher catcher;
105     if (!catcher.GetNextResult()) {
106       message_ = catcher.message();
107       return false;
108     }
109
110     return true;
111   }
112
113  private:
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.";
119       return false;
120     }
121
122     launched_listener.Reply(testName);
123     return true;
124   }
125 };
126
127 // These tests are flaky after https://codereview.chromium.org/57433010/.
128 // See http://crbug.com/319613.
129
130 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, TestCreate) {
131   ASSERT_TRUE(RunAppWindowAPITest("testCreate")) << message_;
132 }
133
134 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, TestSingleton) {
135   ASSERT_TRUE(RunAppWindowAPITest("testSingleton")) << message_;
136 }
137
138 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, TestCloseEvent) {
139   ASSERT_TRUE(RunAppWindowAPITest("testCloseEvent")) << message_;
140 }
141
142 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, DISABLED_TestMaximize) {
143   ASSERT_TRUE(RunAppWindowAPITest("testMaximize")) << message_;
144 }
145
146 // Flaky on Linux. http://crbug.com/424399.
147 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
148 #define MAYBE_TestMinimize DISABLED_TestMinimize
149 #else
150 #define MAYBE_TestMinimize TestMinimize
151 #endif
152
153 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, MAYBE_TestMinimize) {
154   ASSERT_TRUE(RunAppWindowAPITest("testMinimize")) << message_;
155 }
156
157 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, DISABLED_TestRestore) {
158   ASSERT_TRUE(RunAppWindowAPITest("testRestore")) << message_;
159 }
160
161 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, DISABLED_TestRestoreAfterClose) {
162   ASSERT_TRUE(RunAppWindowAPITest("testRestoreAfterClose")) << message_;
163 }
164
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
172 #else
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
178 #endif
179
180 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, MAYBE_TestDeprecatedBounds) {
181   ASSERT_TRUE(RunAppWindowAPITest("testDeprecatedBounds")) << message_;
182 }
183
184 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, MAYBE_TestInitialBounds) {
185   ASSERT_TRUE(RunAppWindowAPITest("testInitialBounds")) << message_;
186 }
187
188 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, MAYBE_TestInitialConstraints) {
189   ASSERT_TRUE(RunAppWindowAPITest("testInitialConstraints")) << message_;
190 }
191
192 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, MAYBE_TestSetBounds) {
193   ASSERT_TRUE(RunAppWindowAPITest("testSetBounds")) << message_;
194 }
195
196 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, MAYBE_TestSetSizeConstraints) {
197   ASSERT_TRUE(RunAppWindowAPITest("testSetSizeConstraints")) << message_;
198 }
199
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
207   // continue running.
208   ExtensionTestMessageListener launched_listener("Launched", true);
209
210   content::WindowedNotificationObserver app_loaded_observer(
211       content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
212       content::NotificationService::AllSources());
213
214   const extensions::Extension* extension = LoadExtension(
215       test_data_dir_.AppendASCII("platform_apps").AppendASCII("window_api"));
216   EXPECT_TRUE(extension);
217
218   OpenApplication(AppLaunchParams(browser()->profile(),
219                                   extension,
220                                   extensions::LAUNCH_CONTAINER_NONE,
221                                   NEW_WINDOW));
222
223   ExtensionTestMessageListener geometry_listener("ListenGeometryChange", true);
224
225   ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
226   launched_listener.Reply("testRestoreAfterGeometryCacheChange");
227
228   ASSERT_TRUE(geometry_listener.WaitUntilSatisfied());
229
230   GeometryCacheChangeHelper geo_change_helper_1(
231       AppWindowGeometryCache::Get(browser()->profile()),
232       extension->id(),
233       // The next line has information that has to stay in sync with the app.
234       "test-ra",
235       gfx::Rect(200, 200, 200, 200));
236
237   GeometryCacheChangeHelper geo_change_helper_2(
238       AppWindowGeometryCache::Get(browser()->profile()),
239       extension->id(),
240       // The next line has information that has to stay in sync with the app.
241       "test-rb",
242       gfx::Rect(200, 200, 200, 200));
243
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();
247
248   ResultCatcher catcher;
249   geometry_listener.Reply("");
250   ASSERT_TRUE(catcher.GetNextResult());
251 }
252
253 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, TestBadging) {
254   ASSERT_TRUE(
255       RunAppWindowAPITestAndWaitForRoundTrip("testBadging")) << message_;
256 }
257
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_;
262 }
263 #endif
264
265 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, TestVisibleOnAllWorkspaces) {
266   ASSERT_TRUE(
267       RunAppWindowAPITestAndWaitForRoundTrip("testVisibleOnAllWorkspaces"))
268       << message_;
269 }