summaryrefslogtreecommitdiffstats
path: root/chrome/browser/apps/window_controls_browsertest.cc
blob: 7da2e4628278cca1bbbc876d458db6b5ed52cab1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/apps/app_browsertest_util.h"
#include "chrome/browser/extensions/extension_test_message_listener.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/browser/web_contents_view.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/process_manager.h"

class WindowControlsTest : public extensions::PlatformAppBrowserTest {
 protected:
  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
    extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line);
    command_line->AppendSwitch(switches::kEnableAppWindowControls);
  }
  content::WebContents* GetWebContentsForExtensionWindow(
      const extensions::Extension* extension);
};

content::WebContents* WindowControlsTest::GetWebContentsForExtensionWindow(
    const extensions::Extension* extension) {
  extensions::ProcessManager* process_manager =
      extensions::ExtensionSystem::Get(profile())->process_manager();

  // Lookup render view host for background page.
  const extensions::ExtensionHost* extension_host =
      process_manager->GetBackgroundHostForExtension(extension->id());
  content::RenderViewHost* background_view_host =
      extension_host->render_view_host();

  // Go through all active views, looking for the first window of the extension
  const extensions::ProcessManager::ViewSet all_views =
      process_manager->GetAllViews();
  extensions::ProcessManager::ViewSet::const_iterator it = all_views.begin();
  for (; it != all_views.end(); ++it) {
    content::RenderViewHost* host = *it;

    // Filter out views not part of this extension
    if (process_manager->GetExtensionForRenderViewHost(host) == extension) {
      // Filter out the background page view
      if (host != background_view_host) {
        content::WebContents* web_contents =
            content::WebContents::FromRenderViewHost(host);
        return web_contents;
      }
    }
  }
  return NULL;
}

IN_PROC_BROWSER_TEST_F(WindowControlsTest, CloseControlWorks) {
  // Launch app and wait for window to show up
  ExtensionTestMessageListener window_opened("window-opened", false);
  const extensions::Extension* extension =
      LoadAndLaunchPlatformApp("window_controls/buttons");
  ASSERT_TRUE(window_opened.WaitUntilSatisfied());

  // Find WebContents of window
  content::WebContents* web_contents =
      GetWebContentsForExtensionWindow(extension);
  ASSERT_TRUE(web_contents != NULL);

  // Send a left click on the "Close" button and wait for the close action
  // to happen.
  ExtensionTestMessageListener window_closed("window-closed", false);

  // Send mouse click somewhere inside the [x] button
  const int controlOffset = 25;
  int x = web_contents->GetView()->GetContainerSize().width() - controlOffset;
  int y = controlOffset;
  content::SimulateMouseClickAt(web_contents,
                                0,
                                blink::WebMouseEvent::ButtonLeft,
                                gfx::Point(x, y));

  ASSERT_TRUE(window_closed.WaitUntilSatisfied());
}