| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/controls/label.h" | 5 #include "ui/views/controls/label.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "ui/accessibility/ax_node_data.h" | 13 #include "ui/accessibility/ax_node_data.h" |
| 14 #include "ui/base/clipboard/clipboard.h" |
| 14 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
| 15 #include "ui/compositor/canvas_painter.h" | 16 #include "ui/compositor/canvas_painter.h" |
| 17 #include "ui/events/base_event_utils.h" |
| 16 #include "ui/gfx/canvas.h" | 18 #include "ui/gfx/canvas.h" |
| 17 #include "ui/gfx/render_text.h" | 19 #include "ui/gfx/render_text.h" |
| 18 #include "ui/views/border.h" | 20 #include "ui/views/border.h" |
| 19 #include "ui/views/test/focus_manager_test.h" | 21 #include "ui/views/test/focus_manager_test.h" |
| 20 #include "ui/views/test/views_test_base.h" | 22 #include "ui/views/test/views_test_base.h" |
| 21 #include "ui/views/widget/widget.h" | 23 #include "ui/views/widget/widget.h" |
| 22 | 24 |
| 23 using base::ASCIIToUTF16; | 25 using base::ASCIIToUTF16; |
| 24 | 26 |
| 27 #define EXPECT_STR_EQ(ascii, utf16) EXPECT_EQ(ASCIIToUTF16(ascii), utf16) |
| 28 |
| 25 namespace views { | 29 namespace views { |
| 30 |
| 31 class LabelTest : public ViewsTestBase { |
| 32 public: |
| 33 LabelTest() {} |
| 34 |
| 35 // ViewsTestBase overrides: |
| 36 void SetUp() override { |
| 37 ViewsTestBase::SetUp(); |
| 38 |
| 39 Widget::InitParams params = |
| 40 CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 41 params.bounds = gfx::Rect(200, 200); |
| 42 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 43 widget_.Init(params); |
| 44 View* container = new View(); |
| 45 widget_.SetContentsView(container); |
| 46 |
| 47 label_ = new Label(); |
| 48 container->AddChildView(label_); |
| 49 |
| 50 widget_.Show(); |
| 51 } |
| 52 |
| 53 void TearDown() override { |
| 54 widget_.Close(); |
| 55 ViewsTestBase::TearDown(); |
| 56 } |
| 57 |
| 58 protected: |
| 59 Label* label() { return label_; } |
| 60 |
| 61 Widget* widget() { return &widget_; } |
| 62 |
| 63 View* GetFocusedView() { |
| 64 return widget()->GetFocusManager()->GetFocusedView(); |
| 65 } |
| 66 |
| 67 void PerformMousePress(const gfx::Point& point, int extra_flags = 0) { |
| 68 ui::MouseEvent pressed_event = ui::MouseEvent( |
| 69 ui::ET_MOUSE_PRESSED, point, point, ui::EventTimeForNow(), |
| 70 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON | extra_flags); |
| 71 label()->OnMousePressed(pressed_event); |
| 72 } |
| 73 |
| 74 void PerformMouseRelease(const gfx::Point& point) { |
| 75 ui::MouseEvent released_event = ui::MouseEvent( |
| 76 ui::ET_MOUSE_RELEASED, point, point, ui::EventTimeForNow(), |
| 77 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); |
| 78 label()->OnMouseReleased(released_event); |
| 79 } |
| 80 |
| 81 void PerformClick(const gfx::Point& point, int extra_flags = 0) { |
| 82 PerformMousePress(point, extra_flags); |
| 83 PerformMouseRelease(point); |
| 84 } |
| 85 |
| 86 void PerformMouseDragTo(const gfx::Point& point) { |
| 87 ui::MouseEvent drag(ui::ET_MOUSE_DRAGGED, point, point, |
| 88 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0); |
| 89 label()->OnMouseDragged(drag); |
| 90 } |
| 91 |
| 92 gfx::Point GetCursorPoint(int cursor_pos) { |
| 93 return label() |
| 94 ->GetRenderTextForSelectionController() |
| 95 ->GetCursorBounds(gfx::SelectionModel(cursor_pos, gfx::CURSOR_FORWARD), |
| 96 false) |
| 97 .origin(); |
| 98 } |
| 99 |
| 100 base::string16 GetSelectedText() { |
| 101 const gfx::RenderText* render_text = |
| 102 label()->GetRenderTextForSelectionController(); |
| 103 return render_text->GetTextFromRange(render_text->selection()); |
| 104 } |
| 105 |
| 106 private: |
| 107 Label* label_ = nullptr; |
| 108 Widget widget_; |
| 109 |
| 110 DISALLOW_COPY_AND_ASSIGN(LabelTest); |
| 111 }; |
| 112 |
| 26 namespace { | 113 namespace { |
| 27 | 114 |
| 28 // All text sizing measurements (width and height) should be greater than this. | 115 // All text sizing measurements (width and height) should be greater than this. |
| 29 const int kMinTextDimension = 4; | 116 const int kMinTextDimension = 4; |
| 30 | 117 |
| 31 using LabelTest = ViewsTestBase; | |
| 32 | |
| 33 class LabelFocusTest : public FocusManagerTest { | |
| 34 public: | |
| 35 LabelFocusTest() {} | |
| 36 ~LabelFocusTest() override {} | |
| 37 | |
| 38 protected: | |
| 39 Label* label() { return label_; } | |
| 40 | |
| 41 private: | |
| 42 // FocusManagerTest: | |
| 43 void InitContentView() override { | |
| 44 label_ = new views::Label(); | |
| 45 GetContentsView()->AddChildView(label_); | |
| 46 } | |
| 47 | |
| 48 Label* label_; | |
| 49 }; | |
| 50 | |
| 51 class TestLabel : public Label { | 118 class TestLabel : public Label { |
| 52 public: | 119 public: |
| 53 TestLabel() : Label(ASCIIToUTF16("TestLabel")) { SizeToPreferredSize(); } | 120 TestLabel() : Label(ASCIIToUTF16("TestLabel")) { SizeToPreferredSize(); } |
| 54 | 121 |
| 55 int schedule_paint_count() const { return schedule_paint_count_; } | 122 int schedule_paint_count() const { return schedule_paint_count_; } |
| 56 | 123 |
| 57 void SimulatePaint() { | 124 void SimulatePaint() { |
| 58 gfx::Canvas canvas(bounds().size(), 1.0, false /* is_opaque */); | 125 gfx::Canvas canvas(bounds().size(), 1.0, false /* is_opaque */); |
| 59 Paint(ui::CanvasPainter(&canvas, 1.f).context()); | 126 Paint(ui::CanvasPainter(&canvas, 1.f).context()); |
| 60 } | 127 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 78 EXPECT_EQ(rtl, base::i18n::IsRTL()); | 145 EXPECT_EQ(rtl, base::i18n::IsRTL()); |
| 79 } | 146 } |
| 80 | 147 |
| 81 // Returns true if |current| is bigger than |last|. Sets |last| to |current|. | 148 // Returns true if |current| is bigger than |last|. Sets |last| to |current|. |
| 82 bool Increased(int current, int* last) { | 149 bool Increased(int current, int* last) { |
| 83 bool increased = current > *last; | 150 bool increased = current > *last; |
| 84 *last = current; | 151 *last = current; |
| 85 return increased; | 152 return increased; |
| 86 } | 153 } |
| 87 | 154 |
| 155 base::string16 GetSelectionClipboardText() { |
| 156 base::string16 selection_clipboard_text; |
| 157 ui::Clipboard::GetForCurrentThread()->ReadText(ui::CLIPBOARD_TYPE_SELECTION, |
| 158 &selection_clipboard_text); |
| 159 return selection_clipboard_text; |
| 160 } |
| 161 |
| 88 } // namespace | 162 } // namespace |
| 89 | 163 |
| 90 // Crashes on Linux only. http://crbug.com/612406 | 164 // Crashes on Linux only. http://crbug.com/612406 |
| 91 #if defined(OS_LINUX) | 165 #if defined(OS_LINUX) |
| 92 #define MAYBE_FontPropertySymbol DISABLED_FontPropertySymbol | 166 #define MAYBE_FontPropertySymbol DISABLED_FontPropertySymbol |
| 93 #else | 167 #else |
| 94 #define MAYBE_FontPropertySymbol FontPropertySymbol | 168 #define MAYBE_FontPropertySymbol FontPropertySymbol |
| 95 #endif | 169 #endif |
| 96 TEST_F(LabelTest, MAYBE_FontPropertySymbol) { | 170 TEST_F(LabelTest, MAYBE_FontPropertySymbol) { |
| 97 Label label; | |
| 98 std::string font_name("symbol"); | 171 std::string font_name("symbol"); |
| 99 gfx::Font font(font_name, 26); | 172 gfx::Font font(font_name, 26); |
| 100 label.SetFontList(gfx::FontList(font)); | 173 label()->SetFontList(gfx::FontList(font)); |
| 101 gfx::Font font_used = label.font_list().GetPrimaryFont(); | 174 gfx::Font font_used = label()->font_list().GetPrimaryFont(); |
| 102 EXPECT_EQ(font_name, font_used.GetFontName()); | 175 EXPECT_EQ(font_name, font_used.GetFontName()); |
| 103 EXPECT_EQ(26, font_used.GetFontSize()); | 176 EXPECT_EQ(26, font_used.GetFontSize()); |
| 104 } | 177 } |
| 105 | 178 |
| 106 TEST_F(LabelTest, FontPropertyArial) { | 179 TEST_F(LabelTest, FontPropertyArial) { |
| 107 Label label; | |
| 108 std::string font_name("arial"); | 180 std::string font_name("arial"); |
| 109 gfx::Font font(font_name, 30); | 181 gfx::Font font(font_name, 30); |
| 110 label.SetFontList(gfx::FontList(font)); | 182 label()->SetFontList(gfx::FontList(font)); |
| 111 gfx::Font font_used = label.font_list().GetPrimaryFont(); | 183 gfx::Font font_used = label()->font_list().GetPrimaryFont(); |
| 112 EXPECT_EQ(font_name, font_used.GetFontName()); | 184 EXPECT_EQ(font_name, font_used.GetFontName()); |
| 113 EXPECT_EQ(30, font_used.GetFontSize()); | 185 EXPECT_EQ(30, font_used.GetFontSize()); |
| 114 } | 186 } |
| 115 | 187 |
| 116 TEST_F(LabelTest, TextProperty) { | 188 TEST_F(LabelTest, TextProperty) { |
| 117 Label label; | |
| 118 base::string16 test_text(ASCIIToUTF16("A random string.")); | 189 base::string16 test_text(ASCIIToUTF16("A random string.")); |
| 119 label.SetText(test_text); | 190 label()->SetText(test_text); |
| 120 EXPECT_EQ(test_text, label.text()); | 191 EXPECT_EQ(test_text, label()->text()); |
| 121 } | 192 } |
| 122 | 193 |
| 123 TEST_F(LabelTest, ColorProperty) { | 194 TEST_F(LabelTest, ColorProperty) { |
| 124 Label label; | |
| 125 SkColor color = SkColorSetARGB(20, 40, 10, 5); | 195 SkColor color = SkColorSetARGB(20, 40, 10, 5); |
| 126 label.SetAutoColorReadabilityEnabled(false); | 196 label()->SetAutoColorReadabilityEnabled(false); |
| 127 label.SetEnabledColor(color); | 197 label()->SetEnabledColor(color); |
| 128 EXPECT_EQ(color, label.enabled_color()); | 198 EXPECT_EQ(color, label()->enabled_color()); |
| 129 } | 199 } |
| 130 | 200 |
| 131 TEST_F(LabelTest, AlignmentProperty) { | 201 TEST_F(LabelTest, AlignmentProperty) { |
| 132 const bool was_rtl = base::i18n::IsRTL(); | 202 const bool was_rtl = base::i18n::IsRTL(); |
| 133 | 203 |
| 134 Label label; | |
| 135 for (size_t i = 0; i < 2; ++i) { | 204 for (size_t i = 0; i < 2; ++i) { |
| 136 // Toggle the application default text direction (to try each direction). | 205 // Toggle the application default text direction (to try each direction). |
| 137 SetRTL(!base::i18n::IsRTL()); | 206 SetRTL(!base::i18n::IsRTL()); |
| 138 bool reverse_alignment = base::i18n::IsRTL(); | 207 bool reverse_alignment = base::i18n::IsRTL(); |
| 139 | 208 |
| 140 // The alignment should be flipped in RTL UI. | 209 // The alignment should be flipped in RTL UI. |
| 141 label.SetHorizontalAlignment(gfx::ALIGN_RIGHT); | 210 label()->SetHorizontalAlignment(gfx::ALIGN_RIGHT); |
| 142 EXPECT_EQ(reverse_alignment ? gfx::ALIGN_LEFT : gfx::ALIGN_RIGHT, | 211 EXPECT_EQ(reverse_alignment ? gfx::ALIGN_LEFT : gfx::ALIGN_RIGHT, |
| 143 label.horizontal_alignment()); | 212 label()->horizontal_alignment()); |
| 144 label.SetHorizontalAlignment(gfx::ALIGN_LEFT); | 213 label()->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 145 EXPECT_EQ(reverse_alignment ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT, | 214 EXPECT_EQ(reverse_alignment ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT, |
| 146 label.horizontal_alignment()); | 215 label()->horizontal_alignment()); |
| 147 label.SetHorizontalAlignment(gfx::ALIGN_CENTER); | 216 label()->SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 148 EXPECT_EQ(gfx::ALIGN_CENTER, label.horizontal_alignment()); | 217 EXPECT_EQ(gfx::ALIGN_CENTER, label()->horizontal_alignment()); |
| 149 | 218 |
| 150 for (size_t j = 0; j < 2; ++j) { | 219 for (size_t j = 0; j < 2; ++j) { |
| 151 label.SetHorizontalAlignment(gfx::ALIGN_TO_HEAD); | 220 label()->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD); |
| 152 const bool rtl = j == 0; | 221 const bool rtl = j == 0; |
| 153 label.SetText(rtl ? base::WideToUTF16(L"\x5d0") : ASCIIToUTF16("A")); | 222 label()->SetText(rtl ? base::WideToUTF16(L"\x5d0") : ASCIIToUTF16("A")); |
| 154 EXPECT_EQ(gfx::ALIGN_TO_HEAD, label.horizontal_alignment()); | 223 EXPECT_EQ(gfx::ALIGN_TO_HEAD, label()->horizontal_alignment()); |
| 155 } | 224 } |
| 156 } | 225 } |
| 157 | 226 |
| 158 EXPECT_EQ(was_rtl, base::i18n::IsRTL()); | 227 EXPECT_EQ(was_rtl, base::i18n::IsRTL()); |
| 159 } | 228 } |
| 160 | 229 |
| 161 TEST_F(LabelTest, ElideBehavior) { | 230 TEST_F(LabelTest, ElideBehavior) { |
| 162 Label label; | |
| 163 base::string16 text(ASCIIToUTF16("This is example text.")); | 231 base::string16 text(ASCIIToUTF16("This is example text.")); |
| 164 label.SetText(text); | 232 label()->SetText(text); |
| 165 EXPECT_EQ(gfx::ELIDE_TAIL, label.elide_behavior()); | 233 EXPECT_EQ(gfx::ELIDE_TAIL, label()->elide_behavior()); |
| 166 gfx::Size size = label.GetPreferredSize(); | 234 gfx::Size size = label()->GetPreferredSize(); |
| 167 label.SetBoundsRect(gfx::Rect(size)); | 235 label()->SetBoundsRect(gfx::Rect(size)); |
| 168 EXPECT_EQ(text, label.GetDisplayTextForTesting()); | 236 EXPECT_EQ(text, label()->GetDisplayTextForTesting()); |
| 169 | 237 |
| 170 size.set_width(size.width() / 2); | 238 size.set_width(size.width() / 2); |
| 171 label.SetBoundsRect(gfx::Rect(size)); | 239 label()->SetBoundsRect(gfx::Rect(size)); |
| 172 EXPECT_GT(text.size(), label.GetDisplayTextForTesting().size()); | 240 EXPECT_GT(text.size(), label()->GetDisplayTextForTesting().size()); |
| 173 | 241 |
| 174 label.SetElideBehavior(gfx::NO_ELIDE); | 242 label()->SetElideBehavior(gfx::NO_ELIDE); |
| 175 EXPECT_EQ(text, label.GetDisplayTextForTesting()); | 243 EXPECT_EQ(text, label()->GetDisplayTextForTesting()); |
| 176 } | 244 } |
| 177 | 245 |
| 178 TEST_F(LabelTest, MultiLineProperty) { | 246 TEST_F(LabelTest, MultiLineProperty) { |
| 179 Label label; | 247 EXPECT_FALSE(label()->multi_line()); |
| 180 EXPECT_FALSE(label.multi_line()); | 248 label()->SetMultiLine(true); |
| 181 label.SetMultiLine(true); | 249 EXPECT_TRUE(label()->multi_line()); |
| 182 EXPECT_TRUE(label.multi_line()); | 250 label()->SetMultiLine(false); |
| 183 label.SetMultiLine(false); | 251 EXPECT_FALSE(label()->multi_line()); |
| 184 EXPECT_FALSE(label.multi_line()); | |
| 185 } | 252 } |
| 186 | 253 |
| 187 TEST_F(LabelTest, ObscuredProperty) { | 254 TEST_F(LabelTest, ObscuredProperty) { |
| 188 Label label; | |
| 189 base::string16 test_text(ASCIIToUTF16("Password!")); | 255 base::string16 test_text(ASCIIToUTF16("Password!")); |
| 190 label.SetText(test_text); | 256 label()->SetText(test_text); |
| 191 label.SizeToPreferredSize(); | 257 label()->SizeToPreferredSize(); |
| 192 | 258 |
| 193 // The text should be unobscured by default. | 259 // The text should be unobscured by default. |
| 194 EXPECT_FALSE(label.obscured()); | 260 EXPECT_FALSE(label()->obscured()); |
| 195 EXPECT_EQ(test_text, label.GetDisplayTextForTesting()); | 261 EXPECT_EQ(test_text, label()->GetDisplayTextForTesting()); |
| 196 EXPECT_EQ(test_text, label.text()); | 262 EXPECT_EQ(test_text, label()->text()); |
| 197 | 263 |
| 198 label.SetObscured(true); | 264 label()->SetObscured(true); |
| 199 label.SizeToPreferredSize(); | 265 label()->SizeToPreferredSize(); |
| 200 EXPECT_TRUE(label.obscured()); | 266 EXPECT_TRUE(label()->obscured()); |
| 201 EXPECT_EQ(base::string16(test_text.size(), | 267 EXPECT_EQ(base::string16(test_text.size(), |
| 202 gfx::RenderText::kPasswordReplacementChar), | 268 gfx::RenderText::kPasswordReplacementChar), |
| 203 label.GetDisplayTextForTesting()); | 269 label()->GetDisplayTextForTesting()); |
| 204 EXPECT_EQ(test_text, label.text()); | 270 EXPECT_EQ(test_text, label()->text()); |
| 205 | 271 |
| 206 label.SetText(test_text + test_text); | 272 label()->SetText(test_text + test_text); |
| 207 label.SizeToPreferredSize(); | 273 label()->SizeToPreferredSize(); |
| 208 EXPECT_EQ(base::string16(test_text.size() * 2, | 274 EXPECT_EQ(base::string16(test_text.size() * 2, |
| 209 gfx::RenderText::kPasswordReplacementChar), | 275 gfx::RenderText::kPasswordReplacementChar), |
| 210 label.GetDisplayTextForTesting()); | 276 label()->GetDisplayTextForTesting()); |
| 211 EXPECT_EQ(test_text + test_text, label.text()); | 277 EXPECT_EQ(test_text + test_text, label()->text()); |
| 212 | 278 |
| 213 label.SetObscured(false); | 279 label()->SetObscured(false); |
| 214 label.SizeToPreferredSize(); | 280 label()->SizeToPreferredSize(); |
| 215 EXPECT_FALSE(label.obscured()); | 281 EXPECT_FALSE(label()->obscured()); |
| 216 EXPECT_EQ(test_text + test_text, label.GetDisplayTextForTesting()); | 282 EXPECT_EQ(test_text + test_text, label()->GetDisplayTextForTesting()); |
| 217 EXPECT_EQ(test_text + test_text, label.text()); | 283 EXPECT_EQ(test_text + test_text, label()->text()); |
| 218 } | 284 } |
| 219 | 285 |
| 220 TEST_F(LabelTest, ObscuredSurrogatePair) { | 286 TEST_F(LabelTest, ObscuredSurrogatePair) { |
| 221 // 'MUSICAL SYMBOL G CLEF': represented in UTF-16 as two characters | 287 // 'MUSICAL SYMBOL G CLEF': represented in UTF-16 as two characters |
| 222 // forming the surrogate pair 0x0001D11E. | 288 // forming the surrogate pair 0x0001D11E. |
| 223 Label label; | |
| 224 base::string16 test_text = base::UTF8ToUTF16("\xF0\x9D\x84\x9E"); | 289 base::string16 test_text = base::UTF8ToUTF16("\xF0\x9D\x84\x9E"); |
| 225 label.SetText(test_text); | 290 label()->SetText(test_text); |
| 226 label.SetObscured(true); | 291 label()->SetObscured(true); |
| 227 label.SizeToPreferredSize(); | 292 label()->SizeToPreferredSize(); |
| 228 EXPECT_EQ(base::string16(1, gfx::RenderText::kPasswordReplacementChar), | 293 EXPECT_EQ(base::string16(1, gfx::RenderText::kPasswordReplacementChar), |
| 229 label.GetDisplayTextForTesting()); | 294 label()->GetDisplayTextForTesting()); |
| 230 EXPECT_EQ(test_text, label.text()); | 295 EXPECT_EQ(test_text, label()->text()); |
| 231 } | 296 } |
| 232 | 297 |
| 233 // This test case verifies the label preferred size will change based on the | 298 // This test case verifies the label preferred size will change based on the |
| 234 // current layout, which may seem wrong. However many of our code base assumes | 299 // current layout, which may seem wrong. However many of our code base assumes |
| 235 // this behavior, therefore this behavior will have to be kept until the code | 300 // this behavior, therefore this behavior will have to be kept until the code |
| 236 // with this assumption is fixed. See http://crbug.com/468494 and | 301 // with this assumption is fixed. See http://crbug.com/468494 and |
| 237 // http://crbug.com/467526. | 302 // http://crbug.com/467526. |
| 238 // TODO(mukai): fix the code assuming this behavior and then fix Label | 303 // TODO(mukai): fix the code assuming this behavior and then fix Label |
| 239 // implementation, and remove this test case. | 304 // implementation, and remove this test case. |
| 240 TEST_F(LabelTest, MultilinePreferredSizeTest) { | 305 TEST_F(LabelTest, MultilinePreferredSizeTest) { |
| 241 Label label; | 306 label()->SetText(ASCIIToUTF16("This is an example.")); |
| 242 label.SetText(ASCIIToUTF16("This is an example.")); | |
| 243 | 307 |
| 244 gfx::Size single_line_size = label.GetPreferredSize(); | 308 gfx::Size single_line_size = label()->GetPreferredSize(); |
| 245 | 309 |
| 246 label.SetMultiLine(true); | 310 label()->SetMultiLine(true); |
| 247 gfx::Size multi_line_size = label.GetPreferredSize(); | 311 gfx::Size multi_line_size = label()->GetPreferredSize(); |
| 248 EXPECT_EQ(single_line_size, multi_line_size); | 312 EXPECT_EQ(single_line_size, multi_line_size); |
| 249 | 313 |
| 250 int new_width = multi_line_size.width() / 2; | 314 int new_width = multi_line_size.width() / 2; |
| 251 label.SetBounds(0, 0, new_width, label.GetHeightForWidth(new_width)); | 315 label()->SetBounds(0, 0, new_width, label()->GetHeightForWidth(new_width)); |
| 252 gfx::Size new_size = label.GetPreferredSize(); | 316 gfx::Size new_size = label()->GetPreferredSize(); |
| 253 EXPECT_GT(multi_line_size.width(), new_size.width()); | 317 EXPECT_GT(multi_line_size.width(), new_size.width()); |
| 254 EXPECT_LT(multi_line_size.height(), new_size.height()); | 318 EXPECT_LT(multi_line_size.height(), new_size.height()); |
| 255 } | 319 } |
| 256 | 320 |
| 257 TEST_F(LabelTest, TooltipProperty) { | 321 TEST_F(LabelTest, TooltipProperty) { |
| 258 Label label; | 322 label()->SetText(ASCIIToUTF16("My cool string.")); |
| 259 label.SetText(ASCIIToUTF16("My cool string.")); | |
| 260 | 323 |
| 261 // Initially, label has no bounds, its text does not fit, and therefore its | 324 // Initially, label has no bounds, its text does not fit, and therefore its |
| 262 // text should be returned as the tooltip text. | 325 // text should be returned as the tooltip text. |
| 263 base::string16 tooltip; | 326 base::string16 tooltip; |
| 264 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); | 327 EXPECT_TRUE(label()->GetTooltipText(gfx::Point(), &tooltip)); |
| 265 EXPECT_EQ(label.text(), tooltip); | 328 EXPECT_EQ(label()->text(), tooltip); |
| 266 | 329 |
| 267 // While tooltip handling is disabled, GetTooltipText() should fail. | 330 // While tooltip handling is disabled, GetTooltipText() should fail. |
| 268 label.SetHandlesTooltips(false); | 331 label()->SetHandlesTooltips(false); |
| 269 EXPECT_FALSE(label.GetTooltipText(gfx::Point(), &tooltip)); | 332 EXPECT_FALSE(label()->GetTooltipText(gfx::Point(), &tooltip)); |
| 270 label.SetHandlesTooltips(true); | 333 label()->SetHandlesTooltips(true); |
| 271 | 334 |
| 272 // When set, custom tooltip text should be returned instead of the label's | 335 // When set, custom tooltip text should be returned instead of the label's |
| 273 // text. | 336 // text. |
| 274 base::string16 tooltip_text(ASCIIToUTF16("The tooltip!")); | 337 base::string16 tooltip_text(ASCIIToUTF16("The tooltip!")); |
| 275 label.SetTooltipText(tooltip_text); | 338 label()->SetTooltipText(tooltip_text); |
| 276 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); | 339 EXPECT_TRUE(label()->GetTooltipText(gfx::Point(), &tooltip)); |
| 277 EXPECT_EQ(tooltip_text, tooltip); | 340 EXPECT_EQ(tooltip_text, tooltip); |
| 278 | 341 |
| 279 // While tooltip handling is disabled, GetTooltipText() should fail. | 342 // While tooltip handling is disabled, GetTooltipText() should fail. |
| 280 label.SetHandlesTooltips(false); | 343 label()->SetHandlesTooltips(false); |
| 281 EXPECT_FALSE(label.GetTooltipText(gfx::Point(), &tooltip)); | 344 EXPECT_FALSE(label()->GetTooltipText(gfx::Point(), &tooltip)); |
| 282 label.SetHandlesTooltips(true); | 345 label()->SetHandlesTooltips(true); |
| 283 | 346 |
| 284 // When the tooltip text is set to an empty string, the original behavior is | 347 // When the tooltip text is set to an empty string, the original behavior is |
| 285 // restored. | 348 // restored. |
| 286 label.SetTooltipText(base::string16()); | 349 label()->SetTooltipText(base::string16()); |
| 287 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); | 350 EXPECT_TRUE(label()->GetTooltipText(gfx::Point(), &tooltip)); |
| 288 EXPECT_EQ(label.text(), tooltip); | 351 EXPECT_EQ(label()->text(), tooltip); |
| 289 | 352 |
| 290 // While tooltip handling is disabled, GetTooltipText() should fail. | 353 // While tooltip handling is disabled, GetTooltipText() should fail. |
| 291 label.SetHandlesTooltips(false); | 354 label()->SetHandlesTooltips(false); |
| 292 EXPECT_FALSE(label.GetTooltipText(gfx::Point(), &tooltip)); | 355 EXPECT_FALSE(label()->GetTooltipText(gfx::Point(), &tooltip)); |
| 293 label.SetHandlesTooltips(true); | 356 label()->SetHandlesTooltips(true); |
| 294 | 357 |
| 295 // Make the label big enough to hold the text | 358 // Make the label big enough to hold the text |
| 296 // and expect there to be no tooltip. | 359 // and expect there to be no tooltip. |
| 297 label.SetBounds(0, 0, 1000, 40); | 360 label()->SetBounds(0, 0, 1000, 40); |
| 298 EXPECT_FALSE(label.GetTooltipText(gfx::Point(), &tooltip)); | 361 EXPECT_FALSE(label()->GetTooltipText(gfx::Point(), &tooltip)); |
| 299 | 362 |
| 300 // Shrinking the single-line label's height shouldn't trigger a tooltip. | 363 // Shrinking the single-line label's height shouldn't trigger a tooltip. |
| 301 label.SetBounds(0, 0, 1000, label.GetPreferredSize().height() / 2); | 364 label()->SetBounds(0, 0, 1000, label()->GetPreferredSize().height() / 2); |
| 302 EXPECT_FALSE(label.GetTooltipText(gfx::Point(), &tooltip)); | 365 EXPECT_FALSE(label()->GetTooltipText(gfx::Point(), &tooltip)); |
| 303 | 366 |
| 304 // Verify that explicitly set tooltip text is shown, regardless of size. | 367 // Verify that explicitly set tooltip text is shown, regardless of size. |
| 305 label.SetTooltipText(tooltip_text); | 368 label()->SetTooltipText(tooltip_text); |
| 306 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); | 369 EXPECT_TRUE(label()->GetTooltipText(gfx::Point(), &tooltip)); |
| 307 EXPECT_EQ(tooltip_text, tooltip); | 370 EXPECT_EQ(tooltip_text, tooltip); |
| 308 // Clear out the explicitly set tooltip text. | 371 // Clear out the explicitly set tooltip text. |
| 309 label.SetTooltipText(base::string16()); | 372 label()->SetTooltipText(base::string16()); |
| 310 | 373 |
| 311 // Shrink the bounds and the tooltip should come back. | 374 // Shrink the bounds and the tooltip should come back. |
| 312 label.SetBounds(0, 0, 10, 10); | 375 label()->SetBounds(0, 0, 10, 10); |
| 313 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); | 376 EXPECT_TRUE(label()->GetTooltipText(gfx::Point(), &tooltip)); |
| 314 | 377 |
| 315 // Make the label obscured and there is no tooltip. | 378 // Make the label obscured and there is no tooltip. |
| 316 label.SetObscured(true); | 379 label()->SetObscured(true); |
| 317 EXPECT_FALSE(label.GetTooltipText(gfx::Point(), &tooltip)); | 380 EXPECT_FALSE(label()->GetTooltipText(gfx::Point(), &tooltip)); |
| 318 | 381 |
| 319 // Obscuring the text shouldn't permanently clobber the tooltip. | 382 // Obscuring the text shouldn't permanently clobber the tooltip. |
| 320 label.SetObscured(false); | 383 label()->SetObscured(false); |
| 321 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); | 384 EXPECT_TRUE(label()->GetTooltipText(gfx::Point(), &tooltip)); |
| 322 | 385 |
| 323 // Making the label multiline shouldn't eliminate the tooltip. | 386 // Making the label multiline shouldn't eliminate the tooltip. |
| 324 label.SetMultiLine(true); | 387 label()->SetMultiLine(true); |
| 325 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); | 388 EXPECT_TRUE(label()->GetTooltipText(gfx::Point(), &tooltip)); |
| 326 // Expanding the multiline label bounds should eliminate the tooltip. | 389 // Expanding the multiline label bounds should eliminate the tooltip. |
| 327 label.SetBounds(0, 0, 1000, 1000); | 390 label()->SetBounds(0, 0, 1000, 1000); |
| 328 EXPECT_FALSE(label.GetTooltipText(gfx::Point(), &tooltip)); | 391 EXPECT_FALSE(label()->GetTooltipText(gfx::Point(), &tooltip)); |
| 329 | 392 |
| 330 // Verify that setting the tooltip still shows it. | 393 // Verify that setting the tooltip still shows it. |
| 331 label.SetTooltipText(tooltip_text); | 394 label()->SetTooltipText(tooltip_text); |
| 332 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); | 395 EXPECT_TRUE(label()->GetTooltipText(gfx::Point(), &tooltip)); |
| 333 EXPECT_EQ(tooltip_text, tooltip); | 396 EXPECT_EQ(tooltip_text, tooltip); |
| 334 // Clear out the tooltip. | 397 // Clear out the tooltip. |
| 335 label.SetTooltipText(base::string16()); | 398 label()->SetTooltipText(base::string16()); |
| 336 } | 399 } |
| 337 | 400 |
| 338 TEST_F(LabelTest, Accessibility) { | 401 TEST_F(LabelTest, Accessibility) { |
| 339 Label label; | 402 label()->SetText(ASCIIToUTF16("My special text.")); |
| 340 label.SetText(ASCIIToUTF16("My special text.")); | |
| 341 | 403 |
| 342 ui::AXNodeData node_data; | 404 ui::AXNodeData node_data; |
| 343 label.GetAccessibleNodeData(&node_data); | 405 label()->GetAccessibleNodeData(&node_data); |
| 344 EXPECT_EQ(ui::AX_ROLE_STATIC_TEXT, node_data.role); | 406 EXPECT_EQ(ui::AX_ROLE_STATIC_TEXT, node_data.role); |
| 345 EXPECT_EQ(label.text(), node_data.GetString16Attribute(ui::AX_ATTR_NAME)); | 407 EXPECT_EQ(label()->text(), node_data.GetString16Attribute(ui::AX_ATTR_NAME)); |
| 346 EXPECT_TRUE(node_data.HasStateFlag(ui::AX_STATE_READ_ONLY)); | 408 EXPECT_TRUE(node_data.HasStateFlag(ui::AX_STATE_READ_ONLY)); |
| 347 } | 409 } |
| 348 | 410 |
| 349 TEST_F(LabelTest, TextChangeWithoutLayout) { | 411 TEST_F(LabelTest, TextChangeWithoutLayout) { |
| 350 Label label; | 412 label()->SetText(ASCIIToUTF16("Example")); |
| 351 label.SetText(ASCIIToUTF16("Example")); | 413 label()->SetBounds(0, 0, 200, 200); |
| 352 label.SetBounds(0, 0, 200, 200); | |
| 353 | 414 |
| 354 gfx::Canvas canvas(gfx::Size(200, 200), 1.0f, true); | 415 gfx::Canvas canvas(gfx::Size(200, 200), 1.0f, true); |
| 355 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); | 416 label()->OnPaint(&canvas); |
| 356 EXPECT_EQ(1u, label.lines_.size()); | 417 EXPECT_EQ(1u, label()->lines_.size()); |
| 357 EXPECT_EQ(ASCIIToUTF16("Example"), label.lines_[0]->GetDisplayText()); | 418 EXPECT_EQ(ASCIIToUTF16("Example"), label()->lines_[0]->GetDisplayText()); |
| 358 | 419 |
| 359 label.SetText(ASCIIToUTF16("Altered")); | 420 label()->SetText(ASCIIToUTF16("Altered")); |
| 360 // The altered text should be painted even though Layout() or SetBounds() are | 421 // The altered text should be painted even though Layout() or SetBounds() are |
| 361 // not called. | 422 // not called. |
| 362 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); | 423 label()->OnPaint(&canvas); |
| 363 EXPECT_EQ(1u, label.lines_.size()); | 424 EXPECT_EQ(1u, label()->lines_.size()); |
| 364 EXPECT_EQ(ASCIIToUTF16("Altered"), label.lines_[0]->GetDisplayText()); | 425 EXPECT_EQ(ASCIIToUTF16("Altered"), label()->lines_[0]->GetDisplayText()); |
| 365 } | 426 } |
| 366 | 427 |
| 367 TEST_F(LabelTest, EmptyLabelSizing) { | 428 TEST_F(LabelTest, EmptyLabelSizing) { |
| 368 Label label; | 429 const gfx::Size expected_size(0, label()->font_list().GetHeight()); |
| 369 const gfx::Size expected_size(0, label.font_list().GetHeight()); | 430 EXPECT_EQ(expected_size, label()->GetPreferredSize()); |
| 370 EXPECT_EQ(expected_size, label.GetPreferredSize()); | 431 label()->SetMultiLine(!label()->multi_line()); |
| 371 label.SetMultiLine(!label.multi_line()); | 432 EXPECT_EQ(expected_size, label()->GetPreferredSize()); |
| 372 EXPECT_EQ(expected_size, label.GetPreferredSize()); | |
| 373 } | 433 } |
| 374 | 434 |
| 375 TEST_F(LabelTest, SingleLineSizing) { | 435 TEST_F(LabelTest, SingleLineSizing) { |
| 376 Label label; | 436 label()->SetText(ASCIIToUTF16("A not so random string in one line.")); |
| 377 label.SetText(ASCIIToUTF16("A not so random string in one line.")); | 437 const gfx::Size size = label()->GetPreferredSize(); |
| 378 const gfx::Size size = label.GetPreferredSize(); | |
| 379 EXPECT_GT(size.height(), kMinTextDimension); | 438 EXPECT_GT(size.height(), kMinTextDimension); |
| 380 EXPECT_GT(size.width(), kMinTextDimension); | 439 EXPECT_GT(size.width(), kMinTextDimension); |
| 381 | 440 |
| 382 // Setting a size smaller than preferred should not change the preferred size. | 441 // Setting a size smaller than preferred should not change the preferred size. |
| 383 label.SetSize(gfx::Size(size.width() / 2, size.height() / 2)); | 442 label()->SetSize(gfx::Size(size.width() / 2, size.height() / 2)); |
| 384 EXPECT_EQ(size, label.GetPreferredSize()); | 443 EXPECT_EQ(size, label()->GetPreferredSize()); |
| 385 | 444 |
| 386 const gfx::Insets border(10, 20, 30, 40); | 445 const gfx::Insets border(10, 20, 30, 40); |
| 387 label.SetBorder(CreateEmptyBorder(border.top(), border.left(), | 446 label()->SetBorder(CreateEmptyBorder(border.top(), border.left(), |
| 388 border.bottom(), border.right())); | 447 border.bottom(), border.right())); |
| 389 const gfx::Size size_with_border = label.GetPreferredSize(); | 448 const gfx::Size size_with_border = label()->GetPreferredSize(); |
| 390 EXPECT_EQ(size_with_border.height(), size.height() + border.height()); | 449 EXPECT_EQ(size_with_border.height(), size.height() + border.height()); |
| 391 EXPECT_EQ(size_with_border.width(), size.width() + border.width()); | 450 EXPECT_EQ(size_with_border.width(), size.width() + border.width()); |
| 392 EXPECT_EQ(size.height() + border.height(), | 451 EXPECT_EQ(size.height() + border.height(), |
| 393 label.GetHeightForWidth(size_with_border.width())); | 452 label()->GetHeightForWidth(size_with_border.width())); |
| 394 } | 453 } |
| 395 | 454 |
| 396 TEST_F(LabelTest, MultilineSmallAvailableWidthSizing) { | 455 TEST_F(LabelTest, MultilineSmallAvailableWidthSizing) { |
| 397 Label label; | 456 label()->SetMultiLine(true); |
| 398 label.SetMultiLine(true); | 457 label()->SetAllowCharacterBreak(true); |
| 399 label.SetAllowCharacterBreak(true); | 458 label()->SetText(ASCIIToUTF16("Too Wide.")); |
| 400 label.SetText(ASCIIToUTF16("Too Wide.")); | |
| 401 | 459 |
| 402 // Check that Label can be laid out at a variety of small sizes, | 460 // Check that Label can be laid out at a variety of small sizes, |
| 403 // splitting the words into up to one character per line if necessary. | 461 // splitting the words into up to one character per line if necessary. |
| 404 // Incorrect word splitting may cause infinite loops in text layout. | 462 // Incorrect word splitting may cause infinite loops in text layout. |
| 405 gfx::Size required_size = label.GetPreferredSize(); | 463 gfx::Size required_size = label()->GetPreferredSize(); |
| 406 for (int i = 1; i < required_size.width(); ++i) | 464 for (int i = 1; i < required_size.width(); ++i) |
| 407 EXPECT_GT(label.GetHeightForWidth(i), 0); | 465 EXPECT_GT(label()->GetHeightForWidth(i), 0); |
| 408 } | 466 } |
| 409 | 467 |
| 410 // Verifies if SetAllowCharacterBreak(true) doesn't change the preferred size. | 468 // Verifies if SetAllowCharacterBreak(true) doesn't change the preferred size. |
| 411 // See crbug.com/469559 | 469 // See crbug.com/469559 |
| 412 TEST_F(LabelTest, PreferredSizeForAllowCharacterBreak) { | 470 TEST_F(LabelTest, PreferredSizeForAllowCharacterBreak) { |
| 413 Label label(base::ASCIIToUTF16("Example")); | 471 label()->SetText(base::ASCIIToUTF16("Example")); |
| 414 gfx::Size preferred_size = label.GetPreferredSize(); | 472 gfx::Size preferred_size = label()->GetPreferredSize(); |
| 415 | 473 |
| 416 label.SetMultiLine(true); | 474 label()->SetMultiLine(true); |
| 417 label.SetAllowCharacterBreak(true); | 475 label()->SetAllowCharacterBreak(true); |
| 418 EXPECT_EQ(preferred_size, label.GetPreferredSize()); | 476 EXPECT_EQ(preferred_size, label()->GetPreferredSize()); |
| 419 } | 477 } |
| 420 | 478 |
| 421 TEST_F(LabelTest, MultiLineSizing) { | 479 TEST_F(LabelTest, MultiLineSizing) { |
| 422 Label label; | 480 label()->SetText( |
| 423 label.SetText( | |
| 424 ASCIIToUTF16("A random string\nwith multiple lines\nand returns!")); | 481 ASCIIToUTF16("A random string\nwith multiple lines\nand returns!")); |
| 425 label.SetMultiLine(true); | 482 label()->SetMultiLine(true); |
| 426 | 483 |
| 427 // GetPreferredSize | 484 // GetPreferredSize |
| 428 gfx::Size required_size = label.GetPreferredSize(); | 485 gfx::Size required_size = label()->GetPreferredSize(); |
| 429 EXPECT_GT(required_size.height(), kMinTextDimension); | 486 EXPECT_GT(required_size.height(), kMinTextDimension); |
| 430 EXPECT_GT(required_size.width(), kMinTextDimension); | 487 EXPECT_GT(required_size.width(), kMinTextDimension); |
| 431 | 488 |
| 432 // SizeToFit with unlimited width. | 489 // SizeToFit with unlimited width. |
| 433 label.SizeToFit(0); | 490 label()->SizeToFit(0); |
| 434 int required_width = label.GetLocalBounds().width(); | 491 int required_width = label()->GetLocalBounds().width(); |
| 435 EXPECT_GT(required_width, kMinTextDimension); | 492 EXPECT_GT(required_width, kMinTextDimension); |
| 436 | 493 |
| 437 // SizeToFit with limited width. | 494 // SizeToFit with limited width. |
| 438 label.SizeToFit(required_width - 1); | 495 label()->SizeToFit(required_width - 1); |
| 439 int constrained_width = label.GetLocalBounds().width(); | 496 int constrained_width = label()->GetLocalBounds().width(); |
| 440 #if defined(OS_WIN) | 497 #if defined(OS_WIN) |
| 441 // Canvas::SizeStringInt (in ui/gfx/canvas_linux.cc) | 498 // Canvas::SizeStringInt (in ui/gfx/canvas_linux.cc) |
| 442 // has to be fixed to return the size that fits to given width/height. | 499 // has to be fixed to return the size that fits to given width/height. |
| 443 EXPECT_LT(constrained_width, required_width); | 500 EXPECT_LT(constrained_width, required_width); |
| 444 #endif | 501 #endif |
| 445 EXPECT_GT(constrained_width, kMinTextDimension); | 502 EXPECT_GT(constrained_width, kMinTextDimension); |
| 446 | 503 |
| 447 // Change the width back to the desire width. | 504 // Change the width back to the desire width. |
| 448 label.SizeToFit(required_width); | 505 label()->SizeToFit(required_width); |
| 449 EXPECT_EQ(required_width, label.GetLocalBounds().width()); | 506 EXPECT_EQ(required_width, label()->GetLocalBounds().width()); |
| 450 | 507 |
| 451 // General tests for GetHeightForWidth. | 508 // General tests for GetHeightForWidth. |
| 452 int required_height = label.GetHeightForWidth(required_width); | 509 int required_height = label()->GetHeightForWidth(required_width); |
| 453 EXPECT_GT(required_height, kMinTextDimension); | 510 EXPECT_GT(required_height, kMinTextDimension); |
| 454 int height_for_constrained_width = label.GetHeightForWidth(constrained_width); | 511 int height_for_constrained_width = |
| 512 label()->GetHeightForWidth(constrained_width); |
| 455 #if defined(OS_WIN) | 513 #if defined(OS_WIN) |
| 456 // Canvas::SizeStringInt (in ui/gfx/canvas_linux.cc) | 514 // Canvas::SizeStringInt (in ui/gfx/canvas_linux.cc) |
| 457 // has to be fixed to return the size that fits to given width/height. | 515 // has to be fixed to return the size that fits to given width/height. |
| 458 EXPECT_GT(height_for_constrained_width, required_height); | 516 EXPECT_GT(height_for_constrained_width, required_height); |
| 459 #endif | 517 #endif |
| 460 // Using the constrained width or the required_width - 1 should give the | 518 // Using the constrained width or the required_width - 1 should give the |
| 461 // same result for the height because the constrainted width is the tight | 519 // same result for the height because the constrainted width is the tight |
| 462 // width when given "required_width - 1" as the max width. | 520 // width when given "required_width - 1" as the max width. |
| 463 EXPECT_EQ(height_for_constrained_width, | 521 EXPECT_EQ(height_for_constrained_width, |
| 464 label.GetHeightForWidth(required_width - 1)); | 522 label()->GetHeightForWidth(required_width - 1)); |
| 465 | 523 |
| 466 // Test everything with borders. | 524 // Test everything with borders. |
| 467 gfx::Insets border(10, 20, 30, 40); | 525 gfx::Insets border(10, 20, 30, 40); |
| 468 label.SetBorder(CreateEmptyBorder(border.top(), border.left(), | 526 label()->SetBorder(CreateEmptyBorder(border.top(), border.left(), |
| 469 border.bottom(), border.right())); | 527 border.bottom(), border.right())); |
| 470 | 528 |
| 471 // SizeToFit and borders. | 529 // SizeToFit and borders. |
| 472 label.SizeToFit(0); | 530 label()->SizeToFit(0); |
| 473 int required_width_with_border = label.GetLocalBounds().width(); | 531 int required_width_with_border = label()->GetLocalBounds().width(); |
| 474 EXPECT_EQ(required_width_with_border, required_width + border.width()); | 532 EXPECT_EQ(required_width_with_border, required_width + border.width()); |
| 475 | 533 |
| 476 // GetHeightForWidth and borders. | 534 // GetHeightForWidth and borders. |
| 477 int required_height_with_border = | 535 int required_height_with_border = |
| 478 label.GetHeightForWidth(required_width_with_border); | 536 label()->GetHeightForWidth(required_width_with_border); |
| 479 EXPECT_EQ(required_height_with_border, required_height + border.height()); | 537 EXPECT_EQ(required_height_with_border, required_height + border.height()); |
| 480 | 538 |
| 481 // Test that the border width is subtracted before doing the height | 539 // Test that the border width is subtracted before doing the height |
| 482 // calculation. If it is, then the height will grow when width | 540 // calculation. If it is, then the height will grow when width |
| 483 // is shrunk. | 541 // is shrunk. |
| 484 int height1 = label.GetHeightForWidth(required_width_with_border - 1); | 542 int height1 = label()->GetHeightForWidth(required_width_with_border - 1); |
| 485 #if defined(OS_WIN) | 543 #if defined(OS_WIN) |
| 486 // Canvas::SizeStringInt (in ui/gfx/canvas_linux.cc) | 544 // Canvas::SizeStringInt (in ui/gfx/canvas_linux.cc) |
| 487 // has to be fixed to return the size that fits to given width/height. | 545 // has to be fixed to return the size that fits to given width/height. |
| 488 EXPECT_GT(height1, required_height_with_border); | 546 EXPECT_GT(height1, required_height_with_border); |
| 489 #endif | 547 #endif |
| 490 EXPECT_EQ(height1, height_for_constrained_width + border.height()); | 548 EXPECT_EQ(height1, height_for_constrained_width + border.height()); |
| 491 | 549 |
| 492 // GetPreferredSize and borders. | 550 // GetPreferredSize and borders. |
| 493 label.SetBounds(0, 0, 0, 0); | 551 label()->SetBounds(0, 0, 0, 0); |
| 494 gfx::Size required_size_with_border = label.GetPreferredSize(); | 552 gfx::Size required_size_with_border = label()->GetPreferredSize(); |
| 495 EXPECT_EQ(required_size_with_border.height(), | 553 EXPECT_EQ(required_size_with_border.height(), |
| 496 required_size.height() + border.height()); | 554 required_size.height() + border.height()); |
| 497 EXPECT_EQ(required_size_with_border.width(), | 555 EXPECT_EQ(required_size_with_border.width(), |
| 498 required_size.width() + border.width()); | 556 required_size.width() + border.width()); |
| 499 } | 557 } |
| 500 | 558 |
| 501 // Verifies if the combination of text eliding and multiline doesn't cause | 559 // Verifies if the combination of text eliding and multiline doesn't cause |
| 502 // any side effects of size / layout calculation. | 560 // any side effects of size / layout calculation. |
| 503 TEST_F(LabelTest, MultiLineSizingWithElide) { | 561 TEST_F(LabelTest, MultiLineSizingWithElide) { |
| 504 const base::string16 text = | 562 const base::string16 text = |
| 505 ASCIIToUTF16("A random string\nwith multiple lines\nand returns!"); | 563 ASCIIToUTF16("A random string\nwith multiple lines\nand returns!"); |
| 506 Label label; | 564 label()->SetText(text); |
| 507 label.SetText(text); | 565 label()->SetMultiLine(true); |
| 508 label.SetMultiLine(true); | |
| 509 | 566 |
| 510 gfx::Size required_size = label.GetPreferredSize(); | 567 gfx::Size required_size = label()->GetPreferredSize(); |
| 511 EXPECT_GT(required_size.height(), kMinTextDimension); | 568 EXPECT_GT(required_size.height(), kMinTextDimension); |
| 512 EXPECT_GT(required_size.width(), kMinTextDimension); | 569 EXPECT_GT(required_size.width(), kMinTextDimension); |
| 513 label.SetBoundsRect(gfx::Rect(required_size)); | 570 label()->SetBoundsRect(gfx::Rect(required_size)); |
| 514 | 571 |
| 515 label.SetElideBehavior(gfx::ELIDE_TAIL); | 572 label()->SetElideBehavior(gfx::ELIDE_TAIL); |
| 516 EXPECT_EQ(required_size.ToString(), label.GetPreferredSize().ToString()); | 573 EXPECT_EQ(required_size.ToString(), label()->GetPreferredSize().ToString()); |
| 517 EXPECT_EQ(text, label.GetDisplayTextForTesting()); | 574 EXPECT_EQ(text, label()->GetDisplayTextForTesting()); |
| 518 | 575 |
| 519 label.SizeToFit(required_size.width() - 1); | 576 label()->SizeToFit(required_size.width() - 1); |
| 520 gfx::Size narrow_size = label.GetPreferredSize(); | 577 gfx::Size narrow_size = label()->GetPreferredSize(); |
| 521 EXPECT_GT(required_size.width(), narrow_size.width()); | 578 EXPECT_GT(required_size.width(), narrow_size.width()); |
| 522 EXPECT_LT(required_size.height(), narrow_size.height()); | 579 EXPECT_LT(required_size.height(), narrow_size.height()); |
| 523 | 580 |
| 524 // SetBounds() doesn't change the preferred size. | 581 // SetBounds() doesn't change the preferred size. |
| 525 label.SetBounds(0, 0, narrow_size.width() - 1, narrow_size.height()); | 582 label()->SetBounds(0, 0, narrow_size.width() - 1, narrow_size.height()); |
| 526 EXPECT_EQ(narrow_size.ToString(), label.GetPreferredSize().ToString()); | 583 EXPECT_EQ(narrow_size.ToString(), label()->GetPreferredSize().ToString()); |
| 527 | 584 |
| 528 // Paint() doesn't change the preferred size. | 585 // Paint() doesn't change the preferred size. |
| 529 gfx::Canvas canvas; | 586 gfx::Canvas canvas; |
| 530 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); | 587 label()->OnPaint(&canvas); |
| 531 EXPECT_EQ(narrow_size.ToString(), label.GetPreferredSize().ToString()); | 588 EXPECT_EQ(narrow_size.ToString(), label()->GetPreferredSize().ToString()); |
| 532 } | 589 } |
| 533 | 590 |
| 534 // Check that labels support GetTooltipHandlerForPoint. | 591 // Check that labels support GetTooltipHandlerForPoint. |
| 535 TEST_F(LabelTest, GetTooltipHandlerForPoint) { | 592 TEST_F(LabelTest, GetTooltipHandlerForPoint) { |
| 536 // A root view must be defined for this test because the hit-testing | 593 label()->SetText( |
| 537 // behaviour used by GetTooltipHandlerForPoint() is defined by | |
| 538 // the ViewTargeter installed on the root view. | |
| 539 Widget widget; | |
| 540 Widget::InitParams init_params = | |
| 541 CreateParams(Widget::InitParams::TYPE_POPUP); | |
| 542 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
| 543 init_params.bounds = gfx::Rect(0, 0, 200, 200); | |
| 544 widget.Init(init_params); | |
| 545 | |
| 546 Label label; | |
| 547 label.SetText( | |
| 548 ASCIIToUTF16("A string that's long enough to exceed the bounds")); | 594 ASCIIToUTF16("A string that's long enough to exceed the bounds")); |
| 549 label.SetBounds(0, 0, 10, 10); | 595 label()->SetBounds(0, 0, 10, 10); |
| 550 widget.SetContentsView(&label); | |
| 551 | 596 |
| 552 // By default, labels start out as tooltip handlers. | 597 // By default, labels start out as tooltip handlers. |
| 553 ASSERT_TRUE(label.handles_tooltips()); | 598 ASSERT_TRUE(label()->handles_tooltips()); |
| 554 | 599 |
| 555 // There's a default tooltip if the text is too big to fit. | 600 // There's a default tooltip if the text is too big to fit. |
| 556 EXPECT_EQ(&label, label.GetTooltipHandlerForPoint(gfx::Point(2, 2))); | 601 EXPECT_EQ(label(), label()->GetTooltipHandlerForPoint(gfx::Point(2, 2))); |
| 557 | 602 |
| 558 // If tooltip handling is disabled, the label should not provide a tooltip | 603 // If tooltip handling is disabled, the label should not provide a tooltip |
| 559 // handler. | 604 // handler. |
| 560 label.SetHandlesTooltips(false); | 605 label()->SetHandlesTooltips(false); |
| 561 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(2, 2))); | 606 EXPECT_FALSE(label()->GetTooltipHandlerForPoint(gfx::Point(2, 2))); |
| 562 label.SetHandlesTooltips(true); | 607 label()->SetHandlesTooltips(true); |
| 563 | 608 |
| 564 // If there's no default tooltip, this should return NULL. | 609 // If there's no default tooltip, this should return NULL. |
| 565 label.SetBounds(0, 0, 500, 50); | 610 label()->SetBounds(0, 0, 500, 50); |
| 566 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(2, 2))); | 611 EXPECT_FALSE(label()->GetTooltipHandlerForPoint(gfx::Point(2, 2))); |
| 567 | 612 |
| 568 label.SetTooltipText(ASCIIToUTF16("a tooltip")); | 613 label()->SetTooltipText(ASCIIToUTF16("a tooltip")); |
| 569 // If the point hits the label, and tooltip is set, the label should be | 614 // If the point hits the label, and tooltip is set, the label should be |
| 570 // returned as its tooltip handler. | 615 // returned as its tooltip handler. |
| 571 EXPECT_EQ(&label, label.GetTooltipHandlerForPoint(gfx::Point(2, 2))); | 616 EXPECT_EQ(label(), label()->GetTooltipHandlerForPoint(gfx::Point(2, 2))); |
| 572 | 617 |
| 573 // Additionally, GetTooltipHandlerForPoint should verify that the label | 618 // Additionally, GetTooltipHandlerForPoint should verify that the label |
| 574 // actually contains the point. | 619 // actually contains the point. |
| 575 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(2, 51))); | 620 EXPECT_FALSE(label()->GetTooltipHandlerForPoint(gfx::Point(2, 51))); |
| 576 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(-1, 20))); | 621 EXPECT_FALSE(label()->GetTooltipHandlerForPoint(gfx::Point(-1, 20))); |
| 577 | 622 |
| 578 // Again, if tooltip handling is disabled, the label should not provide a | 623 // Again, if tooltip handling is disabled, the label should not provide a |
| 579 // tooltip handler. | 624 // tooltip handler. |
| 580 label.SetHandlesTooltips(false); | 625 label()->SetHandlesTooltips(false); |
| 581 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(2, 2))); | 626 EXPECT_FALSE(label()->GetTooltipHandlerForPoint(gfx::Point(2, 2))); |
| 582 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(2, 51))); | 627 EXPECT_FALSE(label()->GetTooltipHandlerForPoint(gfx::Point(2, 51))); |
| 583 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(-1, 20))); | 628 EXPECT_FALSE(label()->GetTooltipHandlerForPoint(gfx::Point(-1, 20))); |
| 584 label.SetHandlesTooltips(true); | 629 label()->SetHandlesTooltips(true); |
| 585 | 630 |
| 586 // GetTooltipHandlerForPoint works should work in child bounds. | 631 // GetTooltipHandlerForPoint works should work in child bounds. |
| 587 label.SetBounds(2, 2, 10, 10); | 632 label()->SetBounds(2, 2, 10, 10); |
| 588 EXPECT_EQ(&label, label.GetTooltipHandlerForPoint(gfx::Point(1, 5))); | 633 EXPECT_EQ(label(), label()->GetTooltipHandlerForPoint(gfx::Point(1, 5))); |
| 589 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(3, 11))); | 634 EXPECT_FALSE(label()->GetTooltipHandlerForPoint(gfx::Point(3, 11))); |
| 590 } | 635 } |
| 591 | 636 |
| 592 // Check that label releases its internal layout data when it's unnecessary. | 637 // Check that label releases its internal layout data when it's unnecessary. |
| 593 TEST_F(LabelTest, ResetRenderTextData) { | 638 TEST_F(LabelTest, ResetRenderTextData) { |
| 594 Label label; | 639 label()->SetText(ASCIIToUTF16("Example")); |
| 595 label.SetText(ASCIIToUTF16("Example")); | 640 label()->SizeToPreferredSize(); |
| 596 label.SizeToPreferredSize(); | 641 gfx::Size preferred_size = label()->GetPreferredSize(); |
| 597 gfx::Size preferred_size = label.GetPreferredSize(); | |
| 598 | 642 |
| 599 EXPECT_NE(gfx::Size().ToString(), preferred_size.ToString()); | 643 EXPECT_NE(gfx::Size().ToString(), preferred_size.ToString()); |
| 600 EXPECT_EQ(0u, label.lines_.size()); | 644 EXPECT_EQ(0u, label()->lines_.size()); |
| 601 | 645 |
| 602 gfx::Canvas canvas(preferred_size, 1.0f, true); | 646 gfx::Canvas canvas(preferred_size, 1.0f, true); |
| 603 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); | 647 label()->OnPaint(&canvas); |
| 604 EXPECT_EQ(1u, label.lines_.size()); | 648 EXPECT_EQ(1u, label()->lines_.size()); |
| 605 | 649 |
| 606 // Label should recreate its RenderText object when it's invisible, to release | 650 // Label should recreate its RenderText object when it's invisible, to release |
| 607 // the layout structures and data. | 651 // the layout structures and data. |
| 608 label.SetVisible(false); | 652 label()->SetVisible(false); |
| 609 EXPECT_EQ(0u, label.lines_.size()); | 653 EXPECT_EQ(0u, label()->lines_.size()); |
| 610 | 654 |
| 611 // Querying fields or size information should not recompute the layout | 655 // Querying fields or size information should not recompute the layout |
| 612 // unnecessarily. | 656 // unnecessarily. |
| 613 EXPECT_EQ(ASCIIToUTF16("Example"), label.text()); | 657 EXPECT_EQ(ASCIIToUTF16("Example"), label()->text()); |
| 614 EXPECT_EQ(0u, label.lines_.size()); | 658 EXPECT_EQ(0u, label()->lines_.size()); |
| 615 | 659 |
| 616 EXPECT_EQ(preferred_size.ToString(), label.GetPreferredSize().ToString()); | 660 EXPECT_EQ(preferred_size.ToString(), label()->GetPreferredSize().ToString()); |
| 617 EXPECT_EQ(0u, label.lines_.size()); | 661 EXPECT_EQ(0u, label()->lines_.size()); |
| 618 | 662 |
| 619 // RenderText data should be back when it's necessary. | 663 // RenderText data should be back when it's necessary. |
| 620 label.SetVisible(true); | 664 label()->SetVisible(true); |
| 621 EXPECT_EQ(0u, label.lines_.size()); | 665 EXPECT_EQ(0u, label()->lines_.size()); |
| 622 | 666 |
| 623 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); | 667 label()->OnPaint(&canvas); |
| 624 EXPECT_EQ(1u, label.lines_.size()); | 668 EXPECT_EQ(1u, label()->lines_.size()); |
| 625 | 669 |
| 626 // Changing layout just resets |lines_|. It'll recover next time it's drawn. | 670 // Changing layout just resets |lines_|. It'll recover next time it's drawn. |
| 627 label.SetBounds(0, 0, 10, 10); | 671 label()->SetBounds(0, 0, 10, 10); |
| 628 EXPECT_EQ(0u, label.lines_.size()); | 672 EXPECT_EQ(0u, label()->lines_.size()); |
| 629 | 673 |
| 630 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); | 674 label()->OnPaint(&canvas); |
| 631 EXPECT_EQ(1u, label.lines_.size()); | 675 EXPECT_EQ(1u, label()->lines_.size()); |
| 632 } | 676 } |
| 633 | 677 |
| 634 #if !defined(OS_MACOSX) | 678 #if !defined(OS_MACOSX) |
| 635 TEST_F(LabelTest, MultilineSupportedRenderText) { | 679 TEST_F(LabelTest, MultilineSupportedRenderText) { |
| 636 std::unique_ptr<gfx::RenderText> render_text( | 680 std::unique_ptr<gfx::RenderText> render_text( |
| 637 gfx::RenderText::CreateInstance()); | 681 gfx::RenderText::CreateInstance()); |
| 638 ASSERT_TRUE(render_text->MultilineSupported()); | 682 ASSERT_TRUE(render_text->MultilineSupported()); |
| 639 | 683 |
| 640 Label label; | 684 label()->SetText(ASCIIToUTF16("Example of\nmultilined label")); |
| 641 label.SetText(ASCIIToUTF16("Example of\nmultilined label")); | 685 label()->SetMultiLine(true); |
| 642 label.SetMultiLine(true); | 686 label()->SizeToPreferredSize(); |
| 643 label.SizeToPreferredSize(); | |
| 644 | 687 |
| 645 gfx::Canvas canvas(label.GetPreferredSize(), 1.0f, true); | 688 gfx::Canvas canvas(label()->GetPreferredSize(), 1.0f, true); |
| 646 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); | 689 label()->OnPaint(&canvas); |
| 647 | 690 |
| 648 // There's only one 'line', RenderText itself supports multiple lines. | 691 // There's only one 'line', RenderText itself supports multiple lines. |
| 649 EXPECT_EQ(1u, label.lines_.size()); | 692 EXPECT_EQ(1u, label()->lines_.size()); |
| 650 } | 693 } |
| 651 #endif | 694 #endif |
| 652 | 695 |
| 653 // Ensures SchedulePaint() calls are not made in OnPaint(). | 696 // Ensures SchedulePaint() calls are not made in OnPaint(). |
| 654 TEST_F(LabelTest, NoSchedulePaintInOnPaint) { | 697 TEST_F(LabelTest, NoSchedulePaintInOnPaint) { |
| 655 TestLabel label; | 698 TestLabel label; |
| 656 | 699 |
| 657 // Initialization should schedule at least one paint, but the precise number | 700 // Initialization should schedule at least one paint, but the precise number |
| 658 // doesn't really matter. | 701 // doesn't really matter. |
| 659 int count = label.schedule_paint_count(); | 702 int count = label.schedule_paint_count(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 673 label.SizeToPreferredSize(); | 716 label.SizeToPreferredSize(); |
| 674 EXPECT_TRUE(Increased(label.schedule_paint_count(), &count)); | 717 EXPECT_TRUE(Increased(label.schedule_paint_count(), &count)); |
| 675 | 718 |
| 676 label.SetEnabledColor(SK_ColorBLUE); | 719 label.SetEnabledColor(SK_ColorBLUE); |
| 677 EXPECT_TRUE(Increased(label.schedule_paint_count(), &count)); | 720 EXPECT_TRUE(Increased(label.schedule_paint_count(), &count)); |
| 678 | 721 |
| 679 label.SimulatePaint(); | 722 label.SimulatePaint(); |
| 680 EXPECT_EQ(count, label.schedule_paint_count()); // Unchanged. | 723 EXPECT_EQ(count, label.schedule_paint_count()); // Unchanged. |
| 681 } | 724 } |
| 682 | 725 |
| 683 TEST_F(LabelFocusTest, FocusBounds) { | 726 TEST_F(LabelTest, FocusBounds) { |
| 684 label()->SetText(ASCIIToUTF16("Example")); | 727 label()->SetText(ASCIIToUTF16("Example")); |
| 685 gfx::Size normal_size = label()->GetPreferredSize(); | 728 gfx::Size normal_size = label()->GetPreferredSize(); |
| 686 | 729 |
| 687 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS); | 730 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
| 688 label()->RequestFocus(); | 731 label()->RequestFocus(); |
| 689 gfx::Size focusable_size = label()->GetPreferredSize(); | 732 gfx::Size focusable_size = label()->GetPreferredSize(); |
| 690 // Focusable label requires larger size to paint the focus rectangle. | 733 // Focusable label requires larger size to paint the focus rectangle. |
| 691 EXPECT_GT(focusable_size.width(), normal_size.width()); | 734 EXPECT_GT(focusable_size.width(), normal_size.width()); |
| 692 EXPECT_GT(focusable_size.height(), normal_size.height()); | 735 EXPECT_GT(focusable_size.height(), normal_size.height()); |
| 693 | 736 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 713 EXPECT_EQ(focusable_size.ToString(), focus_bounds.size().ToString()); | 756 EXPECT_EQ(focusable_size.ToString(), focus_bounds.size().ToString()); |
| 714 | 757 |
| 715 label()->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 758 label()->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 716 label()->SetElideBehavior(gfx::FADE_TAIL); | 759 label()->SetElideBehavior(gfx::FADE_TAIL); |
| 717 label()->SetBounds(0, 0, focusable_size.width() / 2, focusable_size.height()); | 760 label()->SetBounds(0, 0, focusable_size.width() / 2, focusable_size.height()); |
| 718 focus_bounds = label()->GetFocusBounds(); | 761 focus_bounds = label()->GetFocusBounds(); |
| 719 EXPECT_EQ(0, focus_bounds.x()); | 762 EXPECT_EQ(0, focus_bounds.x()); |
| 720 EXPECT_EQ(focusable_size.width() / 2, focus_bounds.width()); | 763 EXPECT_EQ(focusable_size.width() / 2, focus_bounds.width()); |
| 721 } | 764 } |
| 722 | 765 |
| 723 TEST_F(LabelFocusTest, EmptyLabel) { | 766 TEST_F(LabelTest, EmptyLabel) { |
| 724 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS); | 767 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
| 725 label()->RequestFocus(); | 768 label()->RequestFocus(); |
| 726 label()->SizeToPreferredSize(); | 769 label()->SizeToPreferredSize(); |
| 727 | 770 |
| 728 gfx::Rect focus_bounds = label()->GetFocusBounds(); | 771 gfx::Rect focus_bounds = label()->GetFocusBounds(); |
| 729 EXPECT_FALSE(focus_bounds.IsEmpty()); | 772 EXPECT_FALSE(focus_bounds.IsEmpty()); |
| 730 EXPECT_LT(label()->font_list().GetHeight(), focus_bounds.height()); | 773 EXPECT_LT(label()->font_list().GetHeight(), focus_bounds.height()); |
| 731 } | 774 } |
| 732 | 775 |
| 776 // Disabled on Mac since it uses RenderTextMac, which does not support text |
| 777 // selection. |
| 778 // TODO(crbug.com/661394): Re-enable these once Mac uses RenderTextHarfBuzz for |
| 779 // Labels. |
| 780 #if !defined(OS_MACOSX) |
| 781 TEST_F(LabelTest, Selectable) { |
| 782 // By default, labels don't support text selection. |
| 783 EXPECT_FALSE(label()->selectable()); |
| 784 |
| 785 ASSERT_TRUE(label()->SetSelectable(true)); |
| 786 EXPECT_TRUE(label()->selectable()); |
| 787 |
| 788 // Verify that making a label multiline causes the label to not support text |
| 789 // selection. |
| 790 label()->SetMultiLine(true); |
| 791 EXPECT_FALSE(label()->selectable()); |
| 792 } |
| 793 |
| 794 // Verify that labels supporting text selection get focus on clicks. |
| 795 TEST_F(LabelTest, FocusOnClick) { |
| 796 label()->SetText(ASCIIToUTF16("text")); |
| 797 label()->SizeToPreferredSize(); |
| 798 |
| 799 // By default, labels don't get focus on click. |
| 800 PerformClick(gfx::Point()); |
| 801 EXPECT_NE(label(), GetFocusedView()); |
| 802 |
| 803 ASSERT_TRUE(label()->SetSelectable(true)); |
| 804 PerformClick(gfx::Point()); |
| 805 EXPECT_EQ(label(), GetFocusedView()); |
| 806 } |
| 807 |
| 808 // Verify that labels supporting text selection do not get focus on tab |
| 809 // traversal by default. |
| 810 TEST_F(LabelTest, FocusTraversal) { |
| 811 // Add another view before |label()|. |
| 812 View* view = new View(); |
| 813 view->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
| 814 widget()->GetContentsView()->AddChildViewAt(view, 0); |
| 815 |
| 816 // By default, labels are not focusable. |
| 817 view->RequestFocus(); |
| 818 EXPECT_EQ(view, GetFocusedView()); |
| 819 widget()->GetFocusManager()->AdvanceFocus(false); |
| 820 EXPECT_NE(label(), GetFocusedView()); |
| 821 |
| 822 // On enabling text selection, labels can get focus on clicks but not via tab |
| 823 // traversal. |
| 824 view->RequestFocus(); |
| 825 EXPECT_EQ(view, GetFocusedView()); |
| 826 EXPECT_TRUE(label()->SetSelectable(true)); |
| 827 widget()->GetFocusManager()->AdvanceFocus(false); |
| 828 EXPECT_NE(label(), GetFocusedView()); |
| 829 |
| 830 // A label with FocusBehavior::ALWAYS should get focus via tab traversal. |
| 831 view->RequestFocus(); |
| 832 EXPECT_EQ(view, GetFocusedView()); |
| 833 EXPECT_TRUE(label()->SetSelectable(false)); |
| 834 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
| 835 widget()->GetFocusManager()->AdvanceFocus(false); |
| 836 EXPECT_EQ(label(), GetFocusedView()); |
| 837 } |
| 838 |
| 839 // Verify label text selection behavior on double and triple clicks. |
| 840 TEST_F(LabelTest, DoubleTripleClick) { |
| 841 label()->SetText(ASCIIToUTF16("Label double click")); |
| 842 label()->SizeToPreferredSize(); |
| 843 ASSERT_TRUE(label()->SetSelectable(true)); |
| 844 |
| 845 PerformClick(gfx::Point()); |
| 846 EXPECT_TRUE(GetSelectedText().empty()); |
| 847 |
| 848 // Double clicking should select the word under cursor. |
| 849 PerformClick(gfx::Point(), ui::EF_IS_DOUBLE_CLICK); |
| 850 EXPECT_STR_EQ("Label", GetSelectedText()); |
| 851 |
| 852 // Triple clicking should select all the text. |
| 853 PerformClick(gfx::Point()); |
| 854 EXPECT_EQ(label()->text(), GetSelectedText()); |
| 855 |
| 856 // Clicking again should alternate to double click. |
| 857 PerformClick(gfx::Point()); |
| 858 EXPECT_STR_EQ("Label", GetSelectedText()); |
| 859 |
| 860 // Clicking at another location should clear the selection. |
| 861 PerformClick(GetCursorPoint(8)); |
| 862 EXPECT_TRUE(GetSelectedText().empty()); |
| 863 PerformClick(GetCursorPoint(8), ui::EF_IS_DOUBLE_CLICK); |
| 864 EXPECT_STR_EQ("double", GetSelectedText()); |
| 865 } |
| 866 |
| 867 // Verify label text selection behavior on mouse drag. |
| 868 TEST_F(LabelTest, MouseDrag) { |
| 869 label()->SetText(ASCIIToUTF16("Label mouse drag")); |
| 870 label()->SizeToPreferredSize(); |
| 871 ASSERT_TRUE(label()->SetSelectable(true)); |
| 872 |
| 873 PerformMousePress(GetCursorPoint(5)); |
| 874 PerformMouseDragTo(gfx::Point()); |
| 875 EXPECT_STR_EQ("Label", GetSelectedText()); |
| 876 |
| 877 PerformMouseDragTo(GetCursorPoint(8)); |
| 878 EXPECT_STR_EQ(" mo", GetSelectedText()); |
| 879 |
| 880 PerformMouseDragTo(gfx::Point(200, 0)); |
| 881 PerformMouseRelease(gfx::Point(200, 0)); |
| 882 EXPECT_STR_EQ(" mouse drag", GetSelectedText()); |
| 883 } |
| 884 |
| 885 // Verify the initially selected word on a double click, remains selected on |
| 886 // mouse dragging. |
| 887 TEST_F(LabelTest, MouseDragWord) { |
| 888 label()->SetText(ASCIIToUTF16("Label drag word")); |
| 889 label()->SizeToPreferredSize(); |
| 890 ASSERT_TRUE(label()->SetSelectable(true)); |
| 891 |
| 892 PerformClick(GetCursorPoint(8)); |
| 893 PerformMousePress(GetCursorPoint(8), ui::EF_IS_DOUBLE_CLICK); |
| 894 EXPECT_STR_EQ("drag", GetSelectedText()); |
| 895 |
| 896 PerformMouseDragTo(gfx::Point()); |
| 897 EXPECT_STR_EQ("Label drag", GetSelectedText()); |
| 898 |
| 899 PerformMouseDragTo(gfx::Point(200, 0)); |
| 900 PerformMouseRelease(gfx::Point(200, 0)); |
| 901 EXPECT_STR_EQ("drag word", GetSelectedText()); |
| 902 } |
| 903 #endif // OS_MACOSX |
| 904 |
| 905 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 906 // Verify selection clipboard behavior on text selection. Disabled due to |
| 907 // http://crbug.com/396477. |
| 908 TEST_F(LabelTest, DISABLED_SelectionClipboard) { |
| 909 label()->SetText(ASCIIToUTF16("Label selection clipboard")); |
| 910 label()->SizeToPreferredSize(); |
| 911 ASSERT_TRUE(label()->SetSelectable(true)); |
| 912 |
| 913 // Verify programmatic modification of selection, does not modify the |
| 914 // selection clipboard. |
| 915 label()->SelectRange(gfx::Range(2, 5)); |
| 916 EXPECT_STR_EQ("bel", GetSelectedText()); |
| 917 EXPECT_TRUE(GetSelectionClipboardText().empty()); |
| 918 |
| 919 // Verify text selection using the mouse updates the selection clipboard. |
| 920 PerformMousePress(GetCursorPoint(5)); |
| 921 PerformMouseDragTo(gfx::Point()); |
| 922 PerformMouseRelease(gfx::Point()); |
| 923 EXPECT_STR_EQ("Label", GetSelectedText()); |
| 924 EXPECT_STR_EQ("Label", GetSelectionClipboardText()); |
| 925 } |
| 926 #endif |
| 927 |
| 733 } // namespace views | 928 } // namespace views |
| OLD | NEW |