Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / apps / app_window_browsertest.cc
blobc7597510957eeba78231a863b13bea5f84297ac3
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 {
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);
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;
44 waiting_ = true;
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)
52 override {
53 if (extension_id != extension_id_ || window_id != window_id_)
54 return;
56 if (bounds_.x() != bounds.x() &&
57 bounds_.y() != bounds.y() &&
58 bounds_.width() != bounds.width() &&
59 bounds_.height() != bounds.height()) {
60 satisfied_ = true;
61 cache_->RemoveObserver(this);
63 if (waiting_)
64 base::MessageLoopForUI::current()->Quit();
68 private:
69 AppWindowGeometryCache* cache_;
70 std::string extension_id_;
71 std::string window_id_;
72 gfx::Rect bounds_;
73 bool satisfied_;
74 bool waiting_;
77 // Helper class for tests related to the Apps Window API (chrome.app.window).
78 class AppWindowAPITest : public extensions::PlatformAppBrowserTest {
79 protected:
80 bool RunAppWindowAPITest(const char* testName) {
81 if (!BeginAppWindowAPITest(testName))
82 return false;
84 ResultCatcher catcher;
85 if (!catcher.GetNextResult()) {
86 message_ = catcher.message();
87 return false;
90 return true;
93 bool RunAppWindowAPITestAndWaitForRoundTrip(const char* testName) {
94 if (!BeginAppWindowAPITest(testName))
95 return false;
97 ExtensionTestMessageListener round_trip_listener("WaitForRoundTrip", true);
98 if (!round_trip_listener.WaitUntilSatisfied()) {
99 message_ = "Did not get the 'WaitForRoundTrip' message.";
100 return false;
103 round_trip_listener.Reply("");
105 ResultCatcher catcher;
106 if (!catcher.GetNextResult()) {
107 message_ = catcher.message();
108 return false;
111 return true;
114 private:
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.";
120 return false;
123 launched_listener.Reply(testName);
124 return true;
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
166 #else
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
172 #endif
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
201 // continue running.
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(),
213 extension,
214 extensions::LAUNCH_CONTAINER_NONE,
215 NEW_WINDOW));
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()),
226 extension->id(),
227 // The next line has information that has to stay in sync with the app.
228 "test-ra",
229 gfx::Rect(200, 200, 200, 200));
231 GeometryCacheChangeHelper geo_change_helper_2(
232 AppWindowGeometryCache::Get(browser()->profile()),
233 extension->id(),
234 // The next line has information that has to stay in sync with the app.
235 "test-rb",
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) {
248 ASSERT_TRUE(
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_;
257 #endif
259 IN_PROC_BROWSER_TEST_F(AppWindowAPITest, TestVisibleOnAllWorkspaces) {
260 ASSERT_TRUE(
261 RunAppWindowAPITestAndWaitForRoundTrip("testVisibleOnAllWorkspaces"))
262 << message_;