summaryrefslogtreecommitdiffstats
path: root/ash/tooltips
diff options
context:
space:
mode:
authorsky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-02-23 19:08:04 +0000
committersky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-02-23 19:08:04 +0000
commita1b7a82fbb37bce916ef205bf862cb940b5307df (patch)
tree91be529daf93c6b1d3186876b042ac3e2adcfa58 /ash/tooltips
parent4447016b31992cea934c3f83fb5d0ddd4147f56e (diff)
downloadchromium_src-a1b7a82fbb37bce916ef205bf862cb940b5307df.zip
chromium_src-a1b7a82fbb37bce916ef205bf862cb940b5307df.tar.gz
chromium_src-a1b7a82fbb37bce916ef205bf862cb940b5307df.tar.bz2
Gets tooltips to work for desktop aura. This constrains the tooltips
to the bounds of the Widget. We'll see how well that works. BUG=175441 TEST=make sure tooltips work for win-aura. R=varunjain@chromium.org Review URL: https://chromiumcodereview.appspot.com/12334017 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@184324 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ash/tooltips')
-rw-r--r--ash/tooltips/tooltip_controller.cc477
-rw-r--r--ash/tooltips/tooltip_controller.h114
-rw-r--r--ash/tooltips/tooltip_controller_unittest.cc413
3 files changed, 33 insertions, 971 deletions
diff --git a/ash/tooltips/tooltip_controller.cc b/ash/tooltips/tooltip_controller.cc
deleted file mode 100644
index c8bde19..0000000
--- a/ash/tooltips/tooltip_controller.cc
+++ /dev/null
@@ -1,477 +0,0 @@
-// Copyright (c) 2012 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 "ash/tooltips/tooltip_controller.h"
-
-#include <vector>
-
-#include "ash/ash_switches.h"
-#include "ash/shell.h"
-#include "ash/wm/coordinate_conversion.h"
-#include "ash/wm/cursor_manager.h"
-#include "base/command_line.h"
-#include "base/location.h"
-#include "base/string_split.h"
-#include "base/time.h"
-#include "ui/aura/client/drag_drop_client.h"
-#include "ui/aura/env.h"
-#include "ui/aura/root_window.h"
-#include "ui/aura/window.h"
-#include "ui/base/events/event.h"
-#include "ui/base/resource/resource_bundle.h"
-#include "ui/base/text/text_elider.h"
-#include "ui/gfx/font.h"
-#include "ui/gfx/point.h"
-#include "ui/gfx/rect.h"
-#include "ui/gfx/screen.h"
-#include "ui/views/background.h"
-#include "ui/views/border.h"
-#include "ui/views/controls/label.h"
-#include "ui/views/widget/widget.h"
-#include "ui/views/widget/widget_observer.h"
-
-namespace {
-
-const SkColor kTooltipBackground = 0xFFFFFFCC;
-const SkColor kTooltipBorder = 0xFF646450;
-const int kTooltipBorderWidth = 1;
-const int kTooltipHorizontalPadding = 3;
-
-// Max visual tooltip width. If a tooltip is greater than this width, it will
-// be wrapped.
-const int kTooltipMaxWidthPixels = 400;
-
-// Maximum number of lines we allow in the tooltip.
-const size_t kMaxLines = 10;
-
-// TODO(derat): This padding is needed on Chrome OS devices but seems excessive
-// when running the same binary on a Linux workstation; presumably there's a
-// difference in font metrics. Rationalize this.
-const int kTooltipVerticalPadding = 2;
-const int kTooltipTimeoutMs = 500;
-const int kTooltipShownTimeoutMs = 10000;
-
-// FIXME: get cursor offset from actual cursor size.
-const int kCursorOffsetX = 10;
-const int kCursorOffsetY = 15;
-
-// Maximum number of characters we allow in a tooltip.
-const size_t kMaxTooltipLength = 1024;
-
-gfx::Font GetDefaultFont() {
- // TODO(varunjain): implementation duplicated in tooltip_manager_aura. Figure
- // out a way to merge.
- return ui::ResourceBundle::GetSharedInstance().GetFont(
- ui::ResourceBundle::BaseFont);
-}
-
-int GetMaxWidth(int x, int y) {
- // TODO(varunjain): implementation duplicated in tooltip_manager_aura. Figure
- // out a way to merge.
- gfx::Rect display_bounds = ash::Shell::GetScreen()->GetDisplayNearestPoint(
- gfx::Point(x, y)).bounds();
- return (display_bounds.width() + 1) / 2;
-}
-
-// Creates a widget of type TYPE_TOOLTIP
-views::Widget* CreateTooltip(const gfx::Point location) {
- views::Widget* widget = new views::Widget;
- views::Widget::InitParams params;
- // For aura, since we set the type to TOOLTIP_TYPE, the widget will get
- // auto-parented to the MenuAndTooltipsContainer.
- params.type = views::Widget::InitParams::TYPE_TOOLTIP;
- params.context = ash::wm::GetRootWindowAt(location);
- DCHECK(params.context);
- params.keep_on_top = true;
- params.accept_events = false;
- widget->Init(params);
- return widget;
-}
-
-} // namespace
-
-namespace ash {
-namespace internal {
-
-// Displays a widget with tooltip using a views::Label.
-class TooltipController::Tooltip : public views::WidgetObserver {
- public:
- Tooltip(TooltipController* controller)
- : controller_(controller), widget_(NULL) {
- label_.set_background(
- views::Background::CreateSolidBackground(kTooltipBackground));
- if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAuraNoShadows)) {
- label_.set_border(
- views::Border::CreateSolidBorder(kTooltipBorderWidth,
- kTooltipBorder));
- }
- label_.set_owned_by_client();
- }
-
- virtual ~Tooltip() {
- if (widget_) {
- widget_->RemoveObserver(this);
- widget_->Close();
- }
- }
-
- // Updates the text on the tooltip and resizes to fit.
- void SetText(string16 tooltip_text, gfx::Point location) {
- int max_width, line_count;
- TrimTooltipToFit(&tooltip_text, &max_width, &line_count,
- location.x(), location.y());
- label_.SetText(tooltip_text);
-
- int width = max_width + 2 * kTooltipHorizontalPadding;
- int height = label_.GetPreferredSize().height() +
- 2 * kTooltipVerticalPadding;
- if (CommandLine::ForCurrentProcess()->HasSwitch(
- switches::kAuraNoShadows)) {
- width += 2 * kTooltipBorderWidth;
- height += 2 * kTooltipBorderWidth;
- }
- SetTooltipBounds(location, width, height);
- }
-
- // Shows the tooltip.
- void Show() {
- GetWidget()->Show();
- }
-
- // Hides the tooltip.
- void Hide() {
- if (widget_)
- widget_->Hide();
- }
-
- bool IsVisible() {
- return widget_? widget_->IsVisible() : false;
- }
-
- // Overriden from views::WidgetObserver.
- virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE {
- DCHECK_EQ(widget_, widget);
- widget_ = NULL;
- }
-
- private:
- views::Label label_;
- TooltipController* controller_;
- views::Widget* widget_;
-
- // Adjusts the bounds given by the arguments to fit inside the desktop
- // and applies the adjusted bounds to the label_.
- void SetTooltipBounds(gfx::Point mouse_pos,
- int tooltip_width,
- int tooltip_height) {
- gfx::Rect tooltip_rect(mouse_pos.x(), mouse_pos.y(), tooltip_width,
- tooltip_height);
-
- tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY);
- gfx::Rect display_bounds = Shell::GetScreen()->GetDisplayNearestPoint(
- tooltip_rect.origin()).bounds();
-
- // If tooltip is out of bounds on the x axis, we simply shift it
- // horizontally by the offset.
- if (tooltip_rect.right() > display_bounds.right()) {
- int h_offset = tooltip_rect.right() - display_bounds.right();
- tooltip_rect.Offset(-h_offset, 0);
- }
-
- // If tooltip is out of bounds on the y axis, we flip it to appear above the
- // mouse cursor instead of below.
- if (tooltip_rect.bottom() > display_bounds.bottom())
- tooltip_rect.set_y(mouse_pos.y() - tooltip_height);
-
- tooltip_rect.AdjustToFit(display_bounds);
- GetWidget()->SetBounds(tooltip_rect);
- }
-
- views::Widget* GetWidget() {
- if (!widget_) {
- widget_ = CreateTooltip(controller_->mouse_location());
- widget_->SetContentsView(&label_);
- widget_->AddObserver(this);
- }
- return widget_;
- }
-};
-
-////////////////////////////////////////////////////////////////////////////////
-// TooltipController public:
-
-TooltipController::TooltipController(
- aura::client::DragDropClient* drag_drop_client)
- : drag_drop_client_(drag_drop_client),
- tooltip_window_(NULL),
- tooltip_window_at_mouse_press_(NULL),
- mouse_pressed_(false),
- tooltips_enabled_(true) {
- tooltip_timer_.Start(FROM_HERE,
- base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs),
- this, &TooltipController::TooltipTimerFired);
- DCHECK(drag_drop_client_);
-}
-
-TooltipController::~TooltipController() {
- if (tooltip_window_)
- tooltip_window_->RemoveObserver(this);
-}
-
-void TooltipController::UpdateTooltip(aura::Window* target) {
- // If tooltip is visible, we may want to hide it. If it is not, we are ok.
- if (tooltip_window_ == target && GetTooltip()->IsVisible())
- UpdateIfRequired();
-
- // If we had stopped the tooltip timer for some reason, we must restart it if
- // there is a change in the tooltip.
- if (!tooltip_timer_.IsRunning()) {
- if (tooltip_window_ != target || (tooltip_window_ &&
- tooltip_text_ != aura::client::GetTooltipText(tooltip_window_))) {
- tooltip_timer_.Start(FROM_HERE,
- base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs),
- this, &TooltipController::TooltipTimerFired);
- }
- }
-}
-
-void TooltipController::SetTooltipsEnabled(bool enable) {
- if (tooltips_enabled_ == enable)
- return;
- tooltips_enabled_ = enable;
- UpdateTooltip(tooltip_window_);
-}
-
-void TooltipController::OnKeyEvent(ui::KeyEvent* event) {
- // On key press, we want to hide the tooltip and not show it until change.
- // This is the same behavior as hiding tooltips on timeout. Hence, we can
- // simply simulate a timeout.
- if (tooltip_shown_timer_.IsRunning()) {
- tooltip_shown_timer_.Stop();
- TooltipShownTimerFired();
- }
-}
-
-void TooltipController::OnMouseEvent(ui::MouseEvent* event) {
- aura::Window* target = static_cast<aura::Window*>(event->target());
- switch (event->type()) {
- case ui::ET_MOUSE_MOVED:
- case ui::ET_MOUSE_DRAGGED:
- if (tooltip_window_ != target) {
- if (tooltip_window_)
- tooltip_window_->RemoveObserver(this);
- tooltip_window_ = target;
- tooltip_window_->AddObserver(this);
- }
- curr_mouse_loc_ = event->location();
- if (tooltip_timer_.IsRunning())
- tooltip_timer_.Reset();
-
- if (GetTooltip()->IsVisible())
- UpdateIfRequired();
- break;
- case ui::ET_MOUSE_PRESSED:
- mouse_pressed_ = true;
- tooltip_window_at_mouse_press_ = target;
- if (target)
- tooltip_text_at_mouse_press_ = aura::client::GetTooltipText(target);
- GetTooltip()->Hide();
- break;
- case ui::ET_MOUSE_RELEASED:
- mouse_pressed_ = false;
- break;
- case ui::ET_MOUSE_CAPTURE_CHANGED:
- // We will not received a mouse release, so reset mouse pressed state.
- mouse_pressed_ = false;
- case ui::ET_MOUSEWHEEL:
- // Hide the tooltip for click, release, drag, wheel events.
- if (GetTooltip()->IsVisible())
- GetTooltip()->Hide();
- break;
- default:
- break;
- }
-}
-
-void TooltipController::OnTouchEvent(ui::TouchEvent* event) {
- // TODO(varunjain): need to properly implement tooltips for
- // touch events.
- // Hide the tooltip for touch events.
- if (GetTooltip()->IsVisible())
- GetTooltip()->Hide();
- if (tooltip_window_)
- tooltip_window_->RemoveObserver(this);
- tooltip_window_ = NULL;
-}
-
-void TooltipController::OnCancelMode(ui::CancelModeEvent* event) {
- if (tooltip_.get() && tooltip_->IsVisible())
- tooltip_->Hide();
-}
-
-void TooltipController::OnWindowDestroyed(aura::Window* window) {
- if (tooltip_window_ == window) {
- tooltip_window_->RemoveObserver(this);
- tooltip_window_ = NULL;
- }
-}
-
-////////////////////////////////////////////////////////////////////////////////
-// TooltipController private:
-
-// static
-void TooltipController::TrimTooltipToFit(string16* text,
- int* max_width,
- int* line_count,
- int x,
- int y) {
- *max_width = 0;
- *line_count = 0;
-
- // Clamp the tooltip length to kMaxTooltipLength so that we don't
- // accidentally DOS the user with a mega tooltip.
- if (text->length() > kMaxTooltipLength)
- *text = text->substr(0, kMaxTooltipLength);
-
- // Determine the available width for the tooltip.
- int available_width = std::min(kTooltipMaxWidthPixels, GetMaxWidth(x, y));
-
- std::vector<string16> lines;
- base::SplitString(*text, '\n', &lines);
- std::vector<string16> result_lines;
-
- // Format each line to fit.
- gfx::Font font = GetDefaultFont();
- for (std::vector<string16>::iterator l = lines.begin(); l != lines.end();
- ++l) {
- // We break the line at word boundaries, then stuff as many words as we can
- // in the available width to the current line, and move the remaining words
- // to a new line.
- std::vector<string16> words;
- base::SplitStringDontTrim(*l, ' ', &words);
- int current_width = 0;
- string16 line;
- for (std::vector<string16>::iterator w = words.begin(); w != words.end();
- ++w) {
- string16 word = *w;
- if (w + 1 != words.end())
- word.push_back(' ');
- int word_width = font.GetStringWidth(word);
- if (current_width + word_width > available_width) {
- // Current width will exceed the available width. Must start a new line.
- if (!line.empty())
- result_lines.push_back(line);
- current_width = 0;
- line.clear();
- }
- current_width += word_width;
- line.append(word);
- }
- result_lines.push_back(line);
- }
-
- // Clamp number of lines to |kMaxLines|.
- if (result_lines.size() > kMaxLines) {
- result_lines.resize(kMaxLines);
- // Add ellipses character to last line.
- result_lines[kMaxLines - 1] = ui::TruncateString(
- result_lines.back(), result_lines.back().length() - 1);
- }
- *line_count = result_lines.size();
-
- // Flatten the result.
- string16 result;
- for (std::vector<string16>::iterator l = result_lines.begin();
- l != result_lines.end(); ++l) {
- if (!result.empty())
- result.push_back('\n');
- int line_width = font.GetStringWidth(*l);
- // Since we only break at word boundaries, it could happen that due to some
- // very long word, line_width is greater than the available_width. In such
- // case, we simply truncate at available_width and add ellipses at the end.
- if (line_width > available_width) {
- *max_width = available_width;
- result.append(ui::ElideText(*l, font, available_width, ui::ELIDE_AT_END));
- } else {
- *max_width = std::max(*max_width, line_width);
- result.append(*l);
- }
- }
- *text = result;
-}
-
-void TooltipController::TooltipTimerFired() {
- UpdateIfRequired();
-}
-
-void TooltipController::TooltipShownTimerFired() {
- GetTooltip()->Hide();
-
- // Since the user presumably no longer needs the tooltip, we also stop the
- // tooltip timer so that tooltip does not pop back up. We will restart this
- // timer if the tooltip changes (see UpdateTooltip()).
- tooltip_timer_.Stop();
-}
-
-void TooltipController::UpdateIfRequired() {
- if (!tooltips_enabled_ || mouse_pressed_ || IsDragDropInProgress() ||
- !ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible()) {
- GetTooltip()->Hide();
- return;
- }
-
- string16 tooltip_text;
- if (tooltip_window_)
- tooltip_text = aura::client::GetTooltipText(tooltip_window_);
-
- // If the user pressed a mouse button. We will hide the tooltip and not show
- // it until there is a change in the tooltip.
- if (tooltip_window_at_mouse_press_) {
- if (tooltip_window_ == tooltip_window_at_mouse_press_ &&
- tooltip_text == tooltip_text_at_mouse_press_) {
- GetTooltip()->Hide();
- return;
- }
- tooltip_window_at_mouse_press_ = NULL;
- }
-
- // We add the !GetTooltip()->IsVisible() below because when we come here from
- // TooltipTimerFired(), the tooltip_text may not have changed but we still
- // want to update the tooltip because the timer has fired.
- // If we come here from UpdateTooltip(), we have already checked for tooltip
- // visibility and this check below will have no effect.
- if (tooltip_text_ != tooltip_text || !GetTooltip()->IsVisible()) {
- tooltip_shown_timer_.Stop();
- tooltip_text_ = tooltip_text;
- if (tooltip_text_.empty()) {
- GetTooltip()->Hide();
- } else {
- string16 tooltip_text(tooltip_text_);
- gfx::Point widget_loc = curr_mouse_loc_ +
- tooltip_window_->GetBoundsInScreen().OffsetFromOrigin();
- GetTooltip()->SetText(tooltip_text, widget_loc);
- GetTooltip()->Show();
- tooltip_shown_timer_.Start(FROM_HERE,
- base::TimeDelta::FromMilliseconds(kTooltipShownTimeoutMs),
- this, &TooltipController::TooltipShownTimerFired);
- }
- }
-}
-
-bool TooltipController::IsTooltipVisible() {
- return GetTooltip()->IsVisible();
-}
-
-bool TooltipController::IsDragDropInProgress() {
- return drag_drop_client_->IsDragDropInProgress();
-}
-
-TooltipController::Tooltip* TooltipController::GetTooltip() {
- if (!tooltip_.get())
- tooltip_.reset(new Tooltip(this));
- return tooltip_.get();
-}
-
-} // namespace internal
-} // namespace ash
diff --git a/ash/tooltips/tooltip_controller.h b/ash/tooltips/tooltip_controller.h
deleted file mode 100644
index 3022aee..0000000
--- a/ash/tooltips/tooltip_controller.h
+++ /dev/null
@@ -1,114 +0,0 @@
-// Copyright (c) 2012 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.
-
-#ifndef ASH_TOOLTIPS_TOOLTIP_CONTROLLER_H_
-#define ASH_TOOLTIPS_TOOLTIP_CONTROLLER_H_
-
-#include "ash/ash_export.h"
-#include "base/memory/scoped_ptr.h"
-#include "base/string16.h"
-#include "base/timer.h"
-#include "ui/aura/client/tooltip_client.h"
-#include "ui/aura/window_observer.h"
-#include "ui/base/events/event_handler.h"
-#include "ui/gfx/point.h"
-
-namespace aura {
-class Window;
-namespace client {
-class DragDropClient;
-}
-}
-
-namespace ash {
-
-namespace test {
-class TooltipControllerTest;
-} // namespace test
-
-namespace internal {
-
-// TooltipController provides tooltip functionality for aura shell.
-class ASH_EXPORT TooltipController : public aura::client::TooltipClient,
- public ui::EventHandler,
- public aura::WindowObserver {
- public:
- explicit TooltipController(aura::client::DragDropClient* drag_drop_client);
- virtual ~TooltipController();
-
- // Overridden from aura::client::TooltipClient.
- virtual void UpdateTooltip(aura::Window* target) OVERRIDE;
- virtual void SetTooltipsEnabled(bool enable) OVERRIDE;
-
- // Overridden from ui::EventHandler.
- virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE;
- virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE;
- virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE;
- virtual void OnCancelMode(ui::CancelModeEvent* event) OVERRIDE;
-
- // Overridden from aura::WindowObserver.
- virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE;
-
- gfx::Point mouse_location() const { return curr_mouse_loc_; }
-
- private:
- friend class ash::test::TooltipControllerTest;
-
- class Tooltip;
-
- // Trims the tooltip to fit, setting |text| to the clipped result,
- // |max_width| to the width (in pixels) of the clipped text and |line_count|
- // to the number of lines of text in the tooltip. |x| and |y| give the
- // location of the tooltip in screen coordinates.
- static void TrimTooltipToFit(string16* text,
- int* max_width,
- int* line_count,
- int x,
- int y);
-
- void TooltipTimerFired();
- void TooltipShownTimerFired();
-
- // Updates the tooltip if required (if there is any change in the tooltip
- // text or the aura::Window.
- void UpdateIfRequired();
-
- // Only used in tests.
- bool IsTooltipVisible();
-
- bool IsDragDropInProgress();
-
- // This lazily creates the Tooltip instance so that the tooltip window will
- // be initialized with appropriate drop shadows.
- Tooltip* GetTooltip();
-
- aura::client::DragDropClient* drag_drop_client_;
-
- aura::Window* tooltip_window_;
- string16 tooltip_text_;
-
- // These fields are for tracking state when the user presses a mouse button.
- aura::Window* tooltip_window_at_mouse_press_;
- string16 tooltip_text_at_mouse_press_;
- bool mouse_pressed_;
-
- scoped_ptr<Tooltip> tooltip_;
-
- base::RepeatingTimer<TooltipController> tooltip_timer_;
-
- // Timer to timeout the life of an on-screen tooltip. We hide the tooltip when
- // this timer fires.
- base::OneShotTimer<TooltipController> tooltip_shown_timer_;
-
- gfx::Point curr_mouse_loc_;
-
- bool tooltips_enabled_;
-
- DISALLOW_COPY_AND_ASSIGN(TooltipController);
-};
-
-} // namespace internal
-} // namespace ash
-
-#endif // ASH_TOOLTIPS_TOOLTIP_CONTROLLER_H_
diff --git a/ash/tooltips/tooltip_controller_unittest.cc b/ash/tooltips/tooltip_controller_unittest.cc
index f594de4..85ded1e 100644
--- a/ash/tooltips/tooltip_controller_unittest.cc
+++ b/ash/tooltips/tooltip_controller_unittest.cc
@@ -5,7 +5,6 @@
#include "ash/display/display_controller.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
-#include "ash/tooltips/tooltip_controller.h"
#include "ash/wm/cursor_manager.h"
#include "base/utf_string_conversions.h"
#include "ui/aura/client/tooltip_client.h"
@@ -14,37 +13,26 @@
#include "ui/aura/test/event_generator.h"
#include "ui/aura/window.h"
#include "ui/base/resource/resource_bundle.h"
-#include "ui/base/text/text_elider.h"
#include "ui/gfx/font.h"
#include "ui/gfx/point.h"
+#include "ui/views/corewm/tooltip_controller.h"
+#include "ui/views/corewm/tooltip_controller_test_helper.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
+using views::corewm::TooltipController;
+using views::corewm::test::TooltipTestView;
+using views::corewm::test::TooltipControllerTestHelper;
+
+// The tests in this file exercise bits of TooltipController that are hard to
+// test outside of ash. Meaning these tests require the shell and related things
+// to be installed.
+
namespace ash {
namespace test {
namespace {
-class TooltipTestView : public views::View {
- public:
- TooltipTestView() : views::View() {
- }
-
- void set_tooltip_text(string16 tooltip_text) { tooltip_text_ = tooltip_text; }
-
- // Overridden from views::View
- virtual bool GetTooltipText(const gfx::Point& p,
- string16* tooltip) const OVERRIDE {
- *tooltip = tooltip_text_;
- return true;
- }
-
- private:
- string16 tooltip_text_;
-
- DISALLOW_COPY_AND_ASSIGN(TooltipTestView);
-};
-
views::Widget* CreateNewWidgetWithBoundsOn(int display,
const gfx::Rect& bounds) {
views::Widget* widget = new views::Widget;
@@ -80,8 +68,8 @@ void AddViewToWidgetAndResize(views::Widget* widget, views::View* view) {
contents_view_bounds.size()));
}
-ash::internal::TooltipController* GetController() {
- return static_cast<ash::internal::TooltipController*>(
+TooltipController* GetController() {
+ return static_cast<TooltipController*>(
aura::client::GetTooltipClient(Shell::GetPrimaryRootWindow()));
}
@@ -97,43 +85,13 @@ class TooltipControllerTest : public AshTestBase {
TooltipControllerTest() {}
virtual ~TooltipControllerTest() {}
- string16 GetTooltipText() {
- return GetController()->tooltip_text_;
- }
-
- aura::Window* GetTooltipWindow() {
- return GetController()->tooltip_window_;
+ virtual void SetUp() OVERRIDE {
+ AshTestBase::SetUp();
+ helper_.reset(new TooltipControllerTestHelper(GetController()));
}
- void FireTooltipTimer() {
- GetController()->TooltipTimerFired();
- }
-
- bool IsTooltipTimerRunning() {
- return GetController()->tooltip_timer_.IsRunning();
- }
-
- void FireTooltipShownTimer() {
- GetController()->tooltip_shown_timer_.Stop();
- GetController()->TooltipShownTimerFired();
- }
-
- bool IsTooltipShownTimerRunning() {
- return GetController()->tooltip_shown_timer_.IsRunning();
- }
-
- bool IsTooltipVisible() {
- return GetController()->IsTooltipVisible();
- }
-
- void TrimTooltipToFit(string16* text,
- int* max_width,
- int* line_count,
- int x,
- int y) {
- ash::internal::TooltipController::TrimTooltipToFit(text, max_width,
- line_count, x, y);
- }
+ protected:
+ scoped_ptr<TooltipControllerTestHelper> helper_;
private:
DISALLOW_COPY_AND_ASSIGN(TooltipControllerTest);
@@ -142,112 +100,9 @@ class TooltipControllerTest : public AshTestBase {
TEST_F(TooltipControllerTest, NonNullTooltipClient) {
EXPECT_TRUE(aura::client::GetTooltipClient(Shell::GetPrimaryRootWindow())
!= NULL);
- EXPECT_EQ(string16(), GetTooltipText());
- EXPECT_EQ(NULL, GetTooltipWindow());
- EXPECT_FALSE(IsTooltipVisible());
-}
-
-TEST_F(TooltipControllerTest, ViewTooltip) {
- scoped_ptr<views::Widget> widget(CreateNewWidgetOn(0));
- TooltipTestView* view = new TooltipTestView;
- AddViewToWidgetAndResize(widget.get(), view);
- view->set_tooltip_text(ASCIIToUTF16("Tooltip Text"));
- EXPECT_EQ(string16(), GetTooltipText());
- EXPECT_EQ(NULL, GetTooltipWindow());
- aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
- generator.MoveMouseToCenterOf(widget->GetNativeView());
-
- aura::Window* window = widget->GetNativeView();
- EXPECT_EQ(window, Shell::GetPrimaryRootWindow()->GetEventHandlerForPoint(
- generator.current_location()));
- string16 expected_tooltip = ASCIIToUTF16("Tooltip Text");
- EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
- EXPECT_EQ(string16(), GetTooltipText());
- EXPECT_EQ(window, GetTooltipWindow());
-
- // Fire tooltip timer so tooltip becomes visible.
- FireTooltipTimer();
-
- EXPECT_TRUE(IsTooltipVisible());
- generator.MoveMouseBy(1, 0);
-
- EXPECT_TRUE(IsTooltipVisible());
- EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
- EXPECT_EQ(expected_tooltip, GetTooltipText());
- EXPECT_EQ(window, GetTooltipWindow());
-}
-
-TEST_F(TooltipControllerTest, TooltipsInMultipleViews) {
- scoped_ptr<views::Widget> widget(CreateNewWidgetOn(0));
- TooltipTestView* view1 = new TooltipTestView;
- AddViewToWidgetAndResize(widget.get(), view1);
- view1->set_tooltip_text(ASCIIToUTF16("Tooltip Text"));
- EXPECT_EQ(string16(), GetTooltipText());
- EXPECT_EQ(NULL, GetTooltipWindow());
-
- TooltipTestView* view2 = new TooltipTestView;
- AddViewToWidgetAndResize(widget.get(), view2);
-
- aura::Window* window = widget->GetNativeView();
-
- // Fire tooltip timer so tooltip becomes visible.
- aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
- generator.MoveMouseRelativeTo(window,
- view1->bounds().CenterPoint());
- FireTooltipTimer();
- EXPECT_TRUE(IsTooltipVisible());
- for (int i = 0; i < 49; ++i) {
- generator.MoveMouseBy(1, 0);
- EXPECT_TRUE(IsTooltipVisible());
- EXPECT_EQ(window,
- Shell::GetPrimaryRootWindow()->GetEventHandlerForPoint(
- generator.current_location()));
- string16 expected_tooltip = ASCIIToUTF16("Tooltip Text");
- EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
- EXPECT_EQ(expected_tooltip, GetTooltipText());
- EXPECT_EQ(window, GetTooltipWindow());
- }
- for (int i = 0; i < 49; ++i) {
- generator.MoveMouseBy(1, 0);
- EXPECT_FALSE(IsTooltipVisible());
- EXPECT_EQ(window,
- Shell::GetPrimaryRootWindow()->GetEventHandlerForPoint(
- generator.current_location()));
- string16 expected_tooltip; // = ""
- EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
- EXPECT_EQ(expected_tooltip, GetTooltipText());
- EXPECT_EQ(window, GetTooltipWindow());
- }
-}
-
-TEST_F(TooltipControllerTest, EnableOrDisableTooltips) {
- scoped_ptr<views::Widget> widget(CreateNewWidgetOn(0));
- TooltipTestView* view = new TooltipTestView;
- AddViewToWidgetAndResize(widget.get(), view);
- view->set_tooltip_text(ASCIIToUTF16("Tooltip Text"));
- EXPECT_EQ(string16(), GetTooltipText());
- EXPECT_EQ(NULL, GetTooltipWindow());
-
- aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
- generator.MoveMouseRelativeTo(widget->GetNativeView(),
- view->bounds().CenterPoint());
- string16 expected_tooltip = ASCIIToUTF16("Tooltip Text");
-
- // Fire tooltip timer so tooltip becomes visible.
- FireTooltipTimer();
- EXPECT_TRUE(IsTooltipVisible());
-
- // Diable tooltips and check again.
- GetController()->SetTooltipsEnabled(false);
- EXPECT_FALSE(IsTooltipVisible());
- FireTooltipTimer();
- EXPECT_FALSE(IsTooltipVisible());
-
- // Enable tooltips back and check again.
- GetController()->SetTooltipsEnabled(true);
- EXPECT_FALSE(IsTooltipVisible());
- FireTooltipTimer();
- EXPECT_TRUE(IsTooltipVisible());
+ EXPECT_EQ(string16(), helper_->GetTooltipText());
+ EXPECT_EQ(NULL, helper_->GetTooltipWindow());
+ EXPECT_FALSE(helper_->IsTooltipVisible());
}
TEST_F(TooltipControllerTest, HideTooltipWhenCursorHidden) {
@@ -255,8 +110,8 @@ TEST_F(TooltipControllerTest, HideTooltipWhenCursorHidden) {
TooltipTestView* view = new TooltipTestView;
AddViewToWidgetAndResize(widget.get(), view);
view->set_tooltip_text(ASCIIToUTF16("Tooltip Text"));
- EXPECT_EQ(string16(), GetTooltipText());
- EXPECT_EQ(NULL, GetTooltipWindow());
+ EXPECT_EQ(string16(), helper_->GetTooltipText());
+ EXPECT_EQ(NULL, helper_->GetTooltipWindow());
aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
generator.MoveMouseRelativeTo(widget->GetNativeView(),
@@ -264,220 +119,18 @@ TEST_F(TooltipControllerTest, HideTooltipWhenCursorHidden) {
string16 expected_tooltip = ASCIIToUTF16("Tooltip Text");
// Fire tooltip timer so tooltip becomes visible.
- FireTooltipTimer();
- EXPECT_TRUE(IsTooltipVisible());
+ helper_->FireTooltipTimer();
+ EXPECT_TRUE(helper_->IsTooltipVisible());
// Hide the cursor and check again.
ash::Shell::GetInstance()->cursor_manager()->DisableMouseEvents();
- FireTooltipTimer();
- EXPECT_FALSE(IsTooltipVisible());
+ helper_->FireTooltipTimer();
+ EXPECT_FALSE(helper_->IsTooltipVisible());
// Show the cursor and re-check.
ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents();
- FireTooltipTimer();
- EXPECT_TRUE(IsTooltipVisible());
-}
-
-TEST_F(TooltipControllerTest, TrimTooltipToFitTests) {
- string16 tooltip;
- int max_width, line_count, expect_lines;
- int max_pixel_width = 400; // copied from constants in tooltip_controller.cc
- int max_lines = 10; // copied from constants in tooltip_controller.cc
- gfx::Font font = GetDefaultFont();
- size_t tooltip_len;
-
- // Error in computed size vs. expected size should not be greater than the
- // size of the longest word.
- int error_in_pixel_width = font.GetStringWidth(ASCIIToUTF16("tooltip"));
-
- // Long tooltips should wrap to next line
- tooltip.clear();
- max_width = line_count = -1;
- expect_lines = 3;
- for (; font.GetStringWidth(tooltip) <= (expect_lines - 1) * max_pixel_width;)
- tooltip.append(ASCIIToUTF16("This is part of the tooltip"));
- tooltip_len = tooltip.length();
- TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0);
- EXPECT_NEAR(max_pixel_width, max_width, error_in_pixel_width);
- EXPECT_EQ(expect_lines, line_count);
- EXPECT_EQ(tooltip_len + expect_lines - 1, tooltip.length());
-
- // More than |max_lines| lines should get truncated at 10 lines.
- tooltip.clear();
- max_width = line_count = -1;
- expect_lines = 13;
- for (; font.GetStringWidth(tooltip) <= (expect_lines - 1) * max_pixel_width;)
- tooltip.append(ASCIIToUTF16("This is part of the tooltip"));
- TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0);
- EXPECT_NEAR(max_pixel_width, max_width, error_in_pixel_width);
- EXPECT_EQ(max_lines, line_count);
-
- // Long multi line tooltips should wrap individual lines.
- tooltip.clear();
- max_width = line_count = -1;
- expect_lines = 4;
- for (; font.GetStringWidth(tooltip) <= (expect_lines - 2) * max_pixel_width;)
- tooltip.append(ASCIIToUTF16("This is part of the tooltip"));
- tooltip.insert(tooltip.length() / 2, ASCIIToUTF16("\n"));
- tooltip_len = tooltip.length();
- TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0);
- EXPECT_NEAR(max_pixel_width, max_width, error_in_pixel_width);
- EXPECT_EQ(expect_lines, line_count);
- // We may have inserted the line break above near a space which will get
- // trimmed. Hence we may be off by 1 in the final tooltip length calculation.
- EXPECT_NEAR(tooltip_len + expect_lines - 2, tooltip.length(), 1);
-
-#if !defined(OS_WIN)
- // Tooltip with really long word gets elided.
- tooltip.clear();
- max_width = line_count = -1;
- tooltip = UTF8ToUTF16(std::string('a', max_pixel_width));
- TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0);
- EXPECT_NEAR(max_pixel_width, max_width, 5);
- EXPECT_EQ(1, line_count);
- EXPECT_EQ(ui::ElideText(UTF8ToUTF16(std::string('a', max_pixel_width)), font,
- max_pixel_width, ui::ELIDE_AT_END), tooltip);
-#endif
-
- // Normal small tooltip should stay as is.
- tooltip.clear();
- max_width = line_count = -1;
- tooltip = ASCIIToUTF16("Small Tooltip");
- TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0);
- EXPECT_EQ(font.GetStringWidth(ASCIIToUTF16("Small Tooltip")), max_width);
- EXPECT_EQ(1, line_count);
- EXPECT_EQ(ASCIIToUTF16("Small Tooltip"), tooltip);
-
- // Normal small multi-line tooltip should stay as is.
- tooltip.clear();
- max_width = line_count = -1;
- tooltip = ASCIIToUTF16("Multi line\nTooltip");
- TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0);
- int expected_width = font.GetStringWidth(ASCIIToUTF16("Multi line"));
- expected_width = std::max(expected_width,
- font.GetStringWidth(ASCIIToUTF16("Tooltip")));
- EXPECT_EQ(expected_width, max_width);
- EXPECT_EQ(2, line_count);
- EXPECT_EQ(ASCIIToUTF16("Multi line\nTooltip"), tooltip);
-
- // Whitespaces in tooltips are preserved.
- tooltip.clear();
- max_width = line_count = -1;
- tooltip = ASCIIToUTF16("Small Tool t\tip");
- TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0);
- EXPECT_EQ(font.GetStringWidth(ASCIIToUTF16("Small Tool t\tip")), max_width);
- EXPECT_EQ(1, line_count);
- EXPECT_EQ(ASCIIToUTF16("Small Tool t\tip"), tooltip);
-}
-
-TEST_F(TooltipControllerTest, TooltipHidesOnKeyPressAndStaysHiddenUntilChange) {
- scoped_ptr<views::Widget> widget(CreateNewWidgetOn(0));
- TooltipTestView* view1 = new TooltipTestView;
- AddViewToWidgetAndResize(widget.get(), view1);
- view1->set_tooltip_text(ASCIIToUTF16("Tooltip Text for view 1"));
- EXPECT_EQ(string16(), GetTooltipText());
- EXPECT_EQ(NULL, GetTooltipWindow());
-
- TooltipTestView* view2 = new TooltipTestView;
- AddViewToWidgetAndResize(widget.get(), view2);
- view2->set_tooltip_text(ASCIIToUTF16("Tooltip Text for view 2"));
-
- aura::Window* window = widget->GetNativeView();
-
- // Fire tooltip timer so tooltip becomes visible.
- aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
- generator.MoveMouseRelativeTo(window,
- view1->bounds().CenterPoint());
- FireTooltipTimer();
- EXPECT_TRUE(IsTooltipVisible());
- EXPECT_TRUE(IsTooltipShownTimerRunning());
-
- generator.PressKey(ui::VKEY_1, 0);
- EXPECT_FALSE(IsTooltipVisible());
- EXPECT_FALSE(IsTooltipTimerRunning());
- EXPECT_FALSE(IsTooltipShownTimerRunning());
-
- // Moving the mouse inside |view1| should not change the state of the tooltip
- // or the timers.
- for (int i = 0; i < 49; i++) {
- generator.MoveMouseBy(1, 0);
- EXPECT_FALSE(IsTooltipVisible());
- EXPECT_FALSE(IsTooltipTimerRunning());
- EXPECT_FALSE(IsTooltipShownTimerRunning());
- EXPECT_EQ(window,
- Shell::GetPrimaryRootWindow()->GetEventHandlerForPoint(
- generator.current_location()));
- string16 expected_tooltip = ASCIIToUTF16("Tooltip Text for view 1");
- EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
- EXPECT_EQ(expected_tooltip, GetTooltipText());
- EXPECT_EQ(window, GetTooltipWindow());
- }
-
- // Now we move the mouse on to |view2|. It should re-start the tooltip timer.
- generator.MoveMouseBy(1, 0);
- EXPECT_TRUE(IsTooltipTimerRunning());
- FireTooltipTimer();
- EXPECT_TRUE(IsTooltipVisible());
- EXPECT_TRUE(IsTooltipShownTimerRunning());
- string16 expected_tooltip = ASCIIToUTF16("Tooltip Text for view 2");
- EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
- EXPECT_EQ(expected_tooltip, GetTooltipText());
- EXPECT_EQ(window, GetTooltipWindow());
-}
-
-TEST_F(TooltipControllerTest, TooltipHidesOnTimeoutAndStaysHiddenUntilChange) {
- scoped_ptr<views::Widget> widget(CreateNewWidgetOn(0));
- TooltipTestView* view1 = new TooltipTestView;
- AddViewToWidgetAndResize(widget.get(), view1);
- view1->set_tooltip_text(ASCIIToUTF16("Tooltip Text for view 1"));
- EXPECT_EQ(string16(), GetTooltipText());
- EXPECT_EQ(NULL, GetTooltipWindow());
-
- TooltipTestView* view2 = new TooltipTestView;
- AddViewToWidgetAndResize(widget.get(), view2);
- view2->set_tooltip_text(ASCIIToUTF16("Tooltip Text for view 2"));
-
- aura::Window* window = widget->GetNativeView();
-
- // Fire tooltip timer so tooltip becomes visible.
- aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
- generator.MoveMouseRelativeTo(window,
- view1->bounds().CenterPoint());
- FireTooltipTimer();
- EXPECT_TRUE(IsTooltipVisible());
- EXPECT_TRUE(IsTooltipShownTimerRunning());
-
- FireTooltipShownTimer();
- EXPECT_FALSE(IsTooltipVisible());
- EXPECT_FALSE(IsTooltipTimerRunning());
- EXPECT_FALSE(IsTooltipShownTimerRunning());
-
- // Moving the mouse inside |view1| should not change the state of the tooltip
- // or the timers.
- for (int i = 0; i < 49; ++i) {
- generator.MoveMouseBy(1, 0);
- EXPECT_FALSE(IsTooltipVisible());
- EXPECT_FALSE(IsTooltipTimerRunning());
- EXPECT_FALSE(IsTooltipShownTimerRunning());
- EXPECT_EQ(window,
- Shell::GetPrimaryRootWindow()->GetEventHandlerForPoint(
- generator.current_location()));
- string16 expected_tooltip = ASCIIToUTF16("Tooltip Text for view 1");
- EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
- EXPECT_EQ(expected_tooltip, GetTooltipText());
- EXPECT_EQ(window, GetTooltipWindow());
- }
-
- // Now we move the mouse on to |view2|. It should re-start the tooltip timer.
- generator.MoveMouseBy(1, 0);
- EXPECT_TRUE(IsTooltipTimerRunning());
- FireTooltipTimer();
- EXPECT_TRUE(IsTooltipVisible());
- EXPECT_TRUE(IsTooltipShownTimerRunning());
- string16 expected_tooltip = ASCIIToUTF16("Tooltip Text for view 2");
- EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
- EXPECT_EQ(expected_tooltip, GetTooltipText());
- EXPECT_EQ(window, GetTooltipWindow());
+ helper_->FireTooltipTimer();
+ EXPECT_TRUE(helper_->IsTooltipVisible());
}
#if defined(OS_WIN)
@@ -510,8 +163,8 @@ TEST_F(TooltipControllerTest, MAYBE_TooltipsOnMultiDisplayShouldNotCrash) {
aura::test::EventGenerator generator(root_windows[1]);
generator.MoveMouseRelativeTo(widget2->GetNativeView(),
view2->bounds().CenterPoint());
- FireTooltipTimer();
- EXPECT_TRUE(IsTooltipVisible());
+ helper_->FireTooltipTimer();
+ EXPECT_TRUE(helper_->IsTooltipVisible());
// Get rid of secondary display. This destroy's the tooltip's aura window. If
// we have handled this case, we will not crash in the following statement.
@@ -520,7 +173,7 @@ TEST_F(TooltipControllerTest, MAYBE_TooltipsOnMultiDisplayShouldNotCrash) {
// TODO(cpu): Detangle the window destruction notification. Currently
// the TooltipController::OnWindowDestroyed is not being called then the
// display is torn down so the tooltip is is still there.
- EXPECT_FALSE(IsTooltipVisible());
+ EXPECT_FALSE(helper_->IsTooltipVisible());
#endif
EXPECT_EQ(widget2->GetNativeView()->GetRootWindow(), root_windows[0]);
@@ -529,8 +182,8 @@ TEST_F(TooltipControllerTest, MAYBE_TooltipsOnMultiDisplayShouldNotCrash) {
aura::test::EventGenerator generator1(root_windows[0]);
generator1.MoveMouseRelativeTo(widget1->GetNativeView(),
view1->bounds().CenterPoint());
- FireTooltipTimer();
- EXPECT_TRUE(IsTooltipVisible());
+ helper_->FireTooltipTimer();
+ EXPECT_TRUE(helper_->IsTooltipVisible());
}
} // namespace test