Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / app_window / app_window_apitest.cc
1 // Copyright (c) 2012 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/app_window.h"
6 #include "apps/app_window_registry.h"
7 #include "apps/ui/native_app_window.h"
8 #include "base/run_loop.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/browser/apps/app_browsertest_util.h"
11 #include "chrome/browser/extensions/extension_test_message_listener.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/common/extensions/features/feature_channel.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "ui/base/base_window.h"
16 #include "ui/gfx/rect.h"
17
18 #ifdef TOOLKIT_GTK
19 #include "content/public/test/test_utils.h"
20 #endif
21
22 using apps::AppWindow;
23
24 namespace {
25
26 class TestAppWindowRegistryObserver : public apps::AppWindowRegistry::Observer {
27  public:
28   explicit TestAppWindowRegistryObserver(Profile* profile)
29       : profile_(profile), icon_updates_(0) {
30     apps::AppWindowRegistry::Get(profile_)->AddObserver(this);
31   }
32   virtual ~TestAppWindowRegistryObserver() {
33     apps::AppWindowRegistry::Get(profile_)->RemoveObserver(this);
34   }
35
36   // Overridden from AppWindowRegistry::Observer:
37   virtual void OnAppWindowAdded(AppWindow* app_window) OVERRIDE {}
38   virtual void OnAppWindowIconChanged(AppWindow* app_window) OVERRIDE {
39     ++icon_updates_;
40   }
41   virtual void OnAppWindowRemoved(AppWindow* app_window) OVERRIDE {}
42
43   int icon_updates() { return icon_updates_; }
44
45  private:
46   Profile* profile_;
47   int icon_updates_;
48
49   DISALLOW_COPY_AND_ASSIGN(TestAppWindowRegistryObserver);
50 };
51
52 }  // namespace
53
54 namespace extensions {
55
56 // Flaky, http://crbug.com/164735 .
57 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DISABLED_WindowsApiBounds) {
58   ExtensionTestMessageListener background_listener("background_ok", false);
59   ExtensionTestMessageListener ready_listener("ready", true /* will_reply */);
60   ExtensionTestMessageListener success_listener("success", false);
61
62   LoadAndLaunchPlatformApp("windows_api_bounds");
63   ASSERT_TRUE(background_listener.WaitUntilSatisfied());
64   ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
65   AppWindow* window = GetFirstAppWindow();
66
67   gfx::Rect new_bounds(100, 200, 300, 400);
68   new_bounds.Inset(-window->GetBaseWindow()->GetFrameInsets());
69   window->GetBaseWindow()->SetBounds(new_bounds);
70
71   // TODO(jeremya/asargent) figure out why in GTK the window doesn't end up
72 // with exactly the bounds we set. Is it a bug in our app window
73 // implementation?  crbug.com/160252
74 #ifdef TOOLKIT_GTK
75   int slop = 50;
76 #else
77   int slop = 0;
78 #endif  // !TOOLKIT_GTK
79
80   ready_listener.Reply(base::IntToString(slop));
81
82 #ifdef TOOLKIT_GTK
83   // TODO(asargent)- this is here to help track down the root cause of
84   // crbug.com/164735.
85   {
86     gfx::Rect last_bounds;
87     while (!success_listener.was_satisfied()) {
88       gfx::Rect current_bounds = window->GetBaseWindow()->GetBounds();
89       if (current_bounds != last_bounds) {
90         LOG(INFO) << "new bounds: " << current_bounds.ToString();
91       }
92       last_bounds = current_bounds;
93       content::RunAllPendingInMessageLoop();
94     }
95   }
96 #endif
97
98   ASSERT_TRUE(success_listener.WaitUntilSatisfied());
99 }
100
101 // Tests chrome.app.window.setIcon.
102 IN_PROC_BROWSER_TEST_F(ExperimentalPlatformAppBrowserTest, WindowsApiSetIcon) {
103   scoped_ptr<TestAppWindowRegistryObserver> test_observer(
104       new TestAppWindowRegistryObserver(browser()->profile()));
105   ExtensionTestMessageListener listener("IconSet", false);
106   LoadAndLaunchPlatformApp("windows_api_set_icon");
107   EXPECT_EQ(0, test_observer->icon_updates());
108   // Wait until the icon load has been requested.
109   ASSERT_TRUE(listener.WaitUntilSatisfied());
110   // Now wait until the WebContent has decoded the icon and chrome has
111   // processed it. This needs to be in a loop since the renderer runs in a
112   // different process.
113   while (test_observer->icon_updates() < 1) {
114     base::RunLoop run_loop;
115     run_loop.RunUntilIdle();
116   }
117   AppWindow* app_window = GetFirstAppWindow();
118   ASSERT_TRUE(app_window);
119   EXPECT_NE(std::string::npos,
120             app_window->app_icon_url().spec().find("icon.png"));
121   EXPECT_EQ(1, test_observer->icon_updates());
122 }
123
124 // TODO(asargent) - Figure out what to do about the fact that minimize events
125 // don't work under ubuntu unity.
126 // (crbug.com/162794 and https://bugs.launchpad.net/unity/+bug/998073).
127 // TODO(linux_aura) http://crbug.com/163931
128 // Flaky on Mac, http://crbug.com/232330
129 #if defined(TOOLKIT_VIEWS) && !(defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA))
130
131 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApiProperties) {
132   EXPECT_TRUE(
133       RunExtensionTest("platform_apps/windows_api_properties")) << message_;
134 }
135
136 #endif  // defined(TOOLKIT_VIEWS)
137
138 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
139                        WindowsApiAlwaysOnTopWithPermissions) {
140   EXPECT_TRUE(RunPlatformAppTest(
141       "platform_apps/windows_api_always_on_top/has_permissions")) << message_;
142 }
143
144 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
145                        WindowsApiAlwaysOnTopNoPermissions) {
146   EXPECT_TRUE(RunPlatformAppTest(
147       "platform_apps/windows_api_always_on_top/no_permissions")) << message_;
148 }
149
150 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApiGet) {
151   EXPECT_TRUE(RunPlatformAppTest("platform_apps/windows_api_get"))
152       << message_;
153 }
154
155 }  // namespace extensions