12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch#include "apps/native_app_window.h"
6eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "apps/shell_window.h"
74311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch#include "apps/shell_window_registry.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/run_loop.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/extensions/extension_test_message_listener.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/extensions/platform_app_browsertest_util.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/test/base/testing_profile.h"
1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/base/base_window.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/rect.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef TOOLKIT_GTK
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/test/test_utils.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing apps::ShellWindow;
22eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class TestShellWindowRegistryObserver
264311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch    : public apps::ShellWindowRegistry::Observer {
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  explicit TestShellWindowRegistryObserver(Profile* profile)
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      : profile_(profile),
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        icon_updates_(0) {
314311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch    apps::ShellWindowRegistry::Get(profile_)->AddObserver(this);
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~TestShellWindowRegistryObserver() {
344311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch    apps::ShellWindowRegistry::Get(profile_)->RemoveObserver(this);
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Overridden from ShellWindowRegistry::Observer:
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void OnShellWindowAdded(ShellWindow* shell_window) OVERRIDE {}
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void OnShellWindowIconChanged(ShellWindow* shell_window) OVERRIDE {
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ++icon_updates_;
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void OnShellWindowRemoved(ShellWindow* shell_window) OVERRIDE {
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int icon_updates() { return icon_updates_; }
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Profile* profile_;
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int icon_updates_;
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TestShellWindowRegistryObserver);
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace extensions {
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Flaky, http://crbug.com/164735 .
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DISABLED_WindowsApiBounds) {
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ExtensionTestMessageListener background_listener("background_ok", false);
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ExtensionTestMessageListener ready_listener("ready", true /* will_reply */);
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ExtensionTestMessageListener success_listener("success", false);
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  LoadAndLaunchPlatformApp("windows_api_bounds");
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ASSERT_TRUE(background_listener.WaitUntilSatisfied());
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ShellWindow* window = GetFirstShellWindow();
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Rect new_bounds(100, 200, 300, 400);
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  new_bounds.Inset(-window->GetBaseWindow()->GetFrameInsets());
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  window->GetBaseWindow()->SetBounds(new_bounds);
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(jeremya/asargent) figure out why in GTK the window doesn't end up
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // with exactly the bounds we set. Is it a bug in our shell window
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // implementation?  crbug.com/160252
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef TOOLKIT_GTK
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int slop = 50;
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int slop = 0;
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // !TOOLKIT_GTK
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ready_listener.Reply(base::IntToString(slop));
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef TOOLKIT_GTK
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(asargent)- this is here to help track down the root cause of
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // crbug.com/164735.
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  {
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::Rect last_bounds;
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    while (!success_listener.was_satisfied()) {
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      gfx::Rect current_bounds = window->GetBaseWindow()->GetBounds();
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (current_bounds != last_bounds) {
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        LOG(INFO) << "new bounds: " << current_bounds.ToString();
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      last_bounds = current_bounds;
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      content::RunAllPendingInMessageLoop();
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ASSERT_TRUE(success_listener.WaitUntilSatisfied());
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Tests chrome.app.window.setIcon.
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(ExperimentalPlatformAppBrowserTest, WindowsApiSetIcon) {
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<TestShellWindowRegistryObserver> test_observer(
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new TestShellWindowRegistryObserver(browser()->profile()));
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ExtensionTestMessageListener listener("IconSet", false);
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  LoadAndLaunchPlatformApp("windows_api_set_icon");
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(0, test_observer->icon_updates());
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Wait until the icon load has been requested.
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ASSERT_TRUE(listener.WaitUntilSatisfied());
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Now wait until the WebContent has decoded the icon and chrome has
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // processed it. This needs to be in a loop since the renderer runs in a
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // different process.
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  while (test_observer->icon_updates() < 1) {
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::RunLoop run_loop;
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    run_loop.RunUntilIdle();
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ShellWindow* shell_window = GetFirstShellWindow();
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ASSERT_TRUE(shell_window);
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_NE(std::string::npos,
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            shell_window->app_icon_url().spec().find("icon.png"));
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(1, test_observer->icon_updates());
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// TODO(asargent) - Figure out what to do about the fact that minimize events
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// don't work under ubuntu unity.
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// (crbug.com/162794 and https://bugs.launchpad.net/unity/+bug/998073).
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// TODO(linux_aura) http://crbug.com/163931
130c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Flaky on Mac, http://crbug.com/232330
131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if defined(TOOLKIT_VIEWS) && !(defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA))
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApiProperties) {
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      RunExtensionTest("platform_apps/windows_api_properties")) << message_;
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // defined(TOOLKIT_VIEWS)
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace extensions
141