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 "base/files/file_path.h"
6#include "base/strings/utf_string_conversions.h"
7#include "chrome/browser/ui/browser.h"
8#include "chrome/browser/ui/browser_window.h"
9#include "chrome/browser/ui/tabs/tab_strip_model.h"
10#include "chrome/test/base/in_process_browser_test.h"
11#include "chrome/test/base/ui_test_utils.h"
12#include "content/public/browser/web_contents.h"
13#include "content/public/test/browser_test_utils.h"
14#include "ui/base/test/ui_controls.h"
15
16namespace {
17
18class MouseLeaveTest : public InProcessBrowserTest {
19 public:
20  MouseLeaveTest() {}
21
22  void MouseLeaveTestCommon() {
23    GURL test_url = ui_test_utils::GetTestUrl(
24        base::FilePath(), base::FilePath(FILE_PATH_LITERAL("mouseleave.html")));
25
26    content::WebContents* tab =
27        browser()->tab_strip_model()->GetActiveWebContents();
28    gfx::Rect tab_view_bounds = tab->GetContainerBounds();
29
30    gfx::Point in_content_point(
31        tab_view_bounds.x() + tab_view_bounds.width() / 2,
32        tab_view_bounds.y() + 10);
33    gfx::Point above_content_point(
34        tab_view_bounds.x() + tab_view_bounds.width() / 2,
35        tab_view_bounds.y() - 2);
36
37    // Start by moving the point just above the content.
38    ui_controls::SendMouseMove(above_content_point.x(),
39                               above_content_point.y());
40
41    // Navigate to the test html page.
42    base::string16 load_expected_title(base::ASCIIToUTF16("onload"));
43    content::TitleWatcher load_title_watcher(tab, load_expected_title);
44    ui_test_utils::NavigateToURL(browser(), test_url);
45    // Wait for the onload() handler to complete so we can do the
46    // next part of the test.
47    EXPECT_EQ(load_expected_title, load_title_watcher.WaitAndGetTitle());
48
49    // Move the cursor to the top-center of the content, which will trigger
50    // a javascript onMouseOver event.
51    ui_controls::SendMouseMove(in_content_point.x(), in_content_point.y());
52
53    // Wait on the correct intermediate title.
54    base::string16 entered_expected_title(base::ASCIIToUTF16("entered"));
55    content::TitleWatcher entered_title_watcher(tab, entered_expected_title);
56    EXPECT_EQ(entered_expected_title, entered_title_watcher.WaitAndGetTitle());
57
58    // Move the cursor above the content again, which should trigger
59    // a javascript onMouseOut event.
60    ui_controls::SendMouseMove(above_content_point.x(),
61                               above_content_point.y());
62
63    // Wait on the correct final value of the cookie.
64    base::string16 left_expected_title(base::ASCIIToUTF16("left"));
65    content::TitleWatcher left_title_watcher(tab, left_expected_title);
66    EXPECT_EQ(left_expected_title, left_title_watcher.WaitAndGetTitle());
67  }
68
69  DISALLOW_COPY_AND_ASSIGN(MouseLeaveTest);
70};
71
72#if defined(OS_MACOSX)
73// Missing automation provider support: http://crbug.com/45892
74#define MAYBE_TestOnMouseOut DISABLED_TestOnMouseOut
75#elif defined(OS_LINUX)
76// http://crbug.com/133361
77#define MAYBE_TestOnMouseOut DISABLED_TestOnMouseOut
78#else
79#define MAYBE_TestOnMouseOut TestOnMouseOut
80#endif
81
82IN_PROC_BROWSER_TEST_F(MouseLeaveTest, MAYBE_TestOnMouseOut) {
83  MouseLeaveTestCommon();
84}
85
86#if defined(OS_WIN)
87// For MAC: Missing automation provider support: http://crbug.com/45892
88// For linux : http://crbug.com/133361. interactive mouse tests are flaky.
89IN_PROC_BROWSER_TEST_F(MouseLeaveTest, MouseDownOnBrowserCaption) {
90  gfx::Rect browser_bounds = browser()->window()->GetBounds();
91  ui_controls::SendMouseMove(browser_bounds.x() + 200,
92                             browser_bounds.y() + 10);
93  ui_controls::SendMouseClick(ui_controls::LEFT);
94
95  MouseLeaveTestCommon();
96}
97#endif
98
99}  // namespace
100