chromium/chrome/browser/ui/views/tooltip/tooltip_browsertest.cc

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/test_simple_task_runner.h"
#include "build/build_config.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/hit_test_region_observer.h"
#include "net/dns/mock_host_resolver.h"
#include "ui/base/test/ui_controls.h"
#include "ui/base/ui_base_features.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/views/corewm/tooltip_aura.h"
#include "ui/views/corewm/tooltip_controller.h"
#include "ui/views/corewm/tooltip_controller_test_helper.h"
#include "ui/views/widget/any_widget_observer.h"
#include "ui/views/widget/widget.h"
#include "ui/wm/public/tooltip_observer.h"

RenderFrameHost;
RenderWidgetHostView;
WebContents;
TooltipController;
TooltipControllerTestHelper;

#if BUILDFLAG(IS_CHROMEOS_LACROS)
class TooltipMonitor : public wm::TooltipObserver {
 public:
  TooltipMonitor() = default;
  TooltipMonitor(const TooltipMonitor&) = delete;
  TooltipMonitor& operator=(const TooltipMonitor&) = delete;
  ~TooltipMonitor() override = default;

  bool IsWidgetActive() const { return is_tooltip_active_; }

  void WaitUntilTooltipShown() {
    if (!is_tooltip_active_) {
      run_loop_ = std::make_unique<base::RunLoop>();
      run_loop_->Run();
    }
  }

  void WaitUntilTooltipClosed() {
    if (is_tooltip_active_) {
      run_loop_ = std::make_unique<base::RunLoop>();
      run_loop_->Run();
    }
  }

  void OnTooltipShown(aura::Window* window,
                      const std::u16string& text,
                      const gfx::Rect& bounds) override {
    is_tooltip_active_ = true;
    if (run_loop_ && run_loop_->running()) {
      run_loop_->Quit();
    }
  }

  void OnTooltipHidden(aura::Window* window) override {
    is_tooltip_active_ = false;
    if (run_loop_ && run_loop_->running()) {
      run_loop_->Quit();
    }
  }

 private:
  bool is_tooltip_active_ = false;
  std::unique_ptr<base::RunLoop> run_loop_;
};
#else
TooltipAura;

class TooltipMonitor {};  // class TooltipMonitor
#endif  // BUILDFLAG(IS_CHROMEOS_LACROS)

// Browser tests for tooltips on platforms that use TooltipAura to display the
// tooltip.
class TooltipBrowserTest : public InProcessBrowserTest {};  // class TooltipBrowserTest

// TOOD(crbug.com/40768202): Flakily fails on Windows
#if BUILDFLAG(IS_WIN)
#define MAYBE_ShowTooltipFromWebContentWithCursor
#else
#define MAYBE_ShowTooltipFromWebContentWithCursor
#endif
IN_PROC_BROWSER_TEST_F(TooltipBrowserTest,
                       MAYBE_ShowTooltipFromWebContentWithCursor) {}

#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
// https://crbug.com/1212403. Flaky on linux-chromeos-rel and other linux bots.
// TODO(elkurin): Keyboard triggered tooltip needs command line switch on server
// side while Lacros cannot set Ash flag.
#define MAYBE_ShowTooltipFromWebContentWithKeyboard
#else
#define MAYBE_ShowTooltipFromWebContentWithKeyboard
#endif
IN_PROC_BROWSER_TEST_F(TooltipBrowserTest,
                       MAYBE_ShowTooltipFromWebContentWithKeyboard) {}

// https://crbug.com/1212403. Flaky on linux-chromeos-rel.
// https://crbug.com/1241736. Flaky on Win.
// TODO(elkurin): Keyboard triggered tooltip needs command line switch on server
// side while Lacros cannot set Ash flag.
IN_PROC_BROWSER_TEST_F(TooltipBrowserTest,
                       DISABLED_ShowTooltipFromIFrameWithKeyboard) {}

#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
// https://crbug.com/1212403. Flaky on linux-chromeos-rel.
// https://crbug.com/1241736. Flaky on Win.
#define MAYBE_HideTooltipOnKeyPressTriggeredByCursor
#else
#define MAYBE_HideTooltipOnKeyPressTriggeredByCursor
#endif
IN_PROC_BROWSER_TEST_F(TooltipBrowserTest,
                       MAYBE_HideTooltipOnKeyPressTriggeredByCursor) {}

#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
// https://crbug.com/1212403. Flaky on linux-chromeos-rel.
// https://crbug.com/1241736. Flaky on Win.
// TODO(elkurin): Keyboard triggered tooltip needs command line switch on server
// side while Lacros cannot set Ash flag.
#define MAYBE_HideTooltipOnKeyPressTriggeredByKeyboard
#else
#define MAYBE_HideTooltipOnKeyPressTriggeredByKeyboard
#endif
IN_PROC_BROWSER_TEST_F(TooltipBrowserTest,
                       MAYBE_HideTooltipOnKeyPressTriggeredByKeyboard) {}

#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
// https://crbug.com/1212403. Flaky on linux-chromeos-rel, windows, linux.
// TODO(elkurin): Keyboard triggered tooltip needs command line switch on server
// side while Lacros cannot set Ash flag.
#define MAYBE_ScriptFocusHidesKeyboardTriggeredTooltip
#else
#define MAYBE_ScriptFocusHidesKeyboardTriggeredTooltip
#endif
IN_PROC_BROWSER_TEST_F(TooltipBrowserTest,
                       MAYBE_ScriptFocusHidesKeyboardTriggeredTooltip) {}

IN_PROC_BROWSER_TEST_F(TooltipBrowserTest, ResetTooltipOnClosingWindow) {}