// 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 "ui/views/controls/label.h" #include #include #include #include #include "base/bind.h" #include "base/command_line.h" #include "base/i18n/rtl.h" #include "base/test/gtest_util.h" #include "build/build_config.h" #include "build/chromeos_buildflags.h" #include "testing/gtest/include/gtest/gtest.h" #include "ui/accessibility/ax_enums.mojom.h" #include "ui/accessibility/ax_node_data.h" #include "ui/base/clipboard/clipboard.h" #include "ui/base/l10n/l10n_util.h" #include "ui/base/ui_base_switches.h" #include "ui/compositor/canvas_painter.h" #include "ui/compositor/layer.h" #include "ui/events/base_event_utils.h" #include "ui/events/test/event_generator.h" #include "ui/gfx/canvas.h" #include "ui/gfx/render_text.h" #include "ui/gfx/text_constants.h" #include "ui/gfx/text_elider.h" #include "ui/strings/grit/ui_strings.h" #include "ui/views/background.h" #include "ui/views/border.h" #include "ui/views/controls/base_control_test_widget.h" #include "ui/views/controls/link.h" #include "ui/views/style/typography.h" #include "ui/views/test/ax_event_counter.h" #include "ui/views/test/focus_manager_test.h" #include "ui/views/test/view_metadata_test_utils.h" #include "ui/views/test/views_test_base.h" #include "ui/views/widget/unique_widget_ptr.h" #include "ui/views/widget/widget.h" #include "ui/views/widget/widget_utils.h" using base::WideToUTF16; namespace views { namespace { #if defined(OS_MAC) const int kControlCommandModifier = ui::EF_COMMAND_DOWN; #else const int kControlCommandModifier = ui::EF_CONTROL_DOWN; #endif // All text sizing measurements (width and height) should be greater than this. const int kMinTextDimension = 4; class TestLabel : public Label { public: TestLabel() : Label(u"TestLabel") { SizeToPreferredSize(); } int schedule_paint_count() const { return schedule_paint_count_; } void SimulatePaint() { SkBitmap bitmap; SkColor color = SK_ColorTRANSPARENT; Paint(PaintInfo::CreateRootPaintInfo( ui::CanvasPainter(&bitmap, bounds().size(), 1.f, color, false) .context(), bounds().size())); } // View: void OnDidSchedulePaint(const gfx::Rect& r) override { ++schedule_paint_count_; Label::OnDidSchedulePaint(r); } private: int schedule_paint_count_ = 0; DISALLOW_COPY_AND_ASSIGN(TestLabel); }; // A test utility function to set the application default text direction. void SetRTL(bool rtl) { // Override the current locale/direction. base::i18n::SetICUDefaultLocale(rtl ? "he" : "en"); EXPECT_EQ(rtl, base::i18n::IsRTL()); } std::u16string GetClipboardText(ui::ClipboardBuffer clipboard_buffer) { std::u16string clipboard_text; ui::Clipboard::GetForCurrentThread()->ReadText( clipboard_buffer, /* data_dst = */ nullptr, &clipboard_text); return clipboard_text; } // Makes an RTL string by mapping 0..6 to [א,ב,ג,ד,ה,ו,ז]. std::u16string ToRTL(const char* ascii) { std::u16string rtl; for (const char* c = ascii; *c; ++c) { if (*c >= '0' && *c <= '6') rtl += L'\x5d0' + (*c - '0'); else rtl += static_cast(*c); } return rtl; } } // namespace class LabelTest : public test::BaseControlTestWidget { public: LabelTest() = default; LabelTest(const LabelTest&) = delete; LabelTest& operator=(const LabelTest&) = delete; ~LabelTest() override = default; protected: void CreateWidgetContent(View* container) override { label_ = container->AddChildView(std::make_unique