Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1156)

Unified Diff: ui/views/controls/label_unittest.cc

Issue 2065003002: Do not SchedulePaint() inside views::Label::OnPaint() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add a test Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/views/controls/label.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/views/controls/label_unittest.cc
diff --git a/ui/views/controls/label_unittest.cc b/ui/views/controls/label_unittest.cc
index 43f09fb092b06a1968e2432352781ce0bc42ba86..e2ab5080acce68b819cc58bf0bbcd746d16d6d98 100644
--- a/ui/views/controls/label_unittest.cc
+++ b/ui/views/controls/label_unittest.cc
@@ -22,8 +22,12 @@
using base::ASCIIToUTF16;
namespace views {
+namespace {
-typedef ViewsTestBase LabelTest;
+// All text sizing measurements (width and height) should be greater than this.
+const int kMinTextDimension = 4;
+
+using LabelTest = ViewsTestBase;
class LabelFocusTest : public FocusManagerTest {
public:
@@ -31,7 +35,7 @@ class LabelFocusTest : public FocusManagerTest {
~LabelFocusTest() override {}
protected:
- views::Label* label() { return label_; }
+ Label* label() { return label_; }
private:
// FocusManagerTest:
@@ -40,11 +44,31 @@ class LabelFocusTest : public FocusManagerTest {
GetContentsView()->AddChildView(label_);
}
- views::Label* label_;
+ Label* label_;
};
-// All text sizing measurements (width and height) should be greater than this.
-const int kMinTextDimension = 4;
+class TestLabel : public Label {
+ public:
+ TestLabel() : Label(ASCIIToUTF16("TestLabel")) { SizeToPreferredSize(); }
+
+ int schedule_paint_count() const { return schedule_paint_count_; }
+
+ void SimulatePaint() {
+ gfx::Canvas canvas(bounds().size(), 1.0, false /* is_opaque */);
+ Paint(ui::CanvasPainter(&canvas, 1.f).context());
+ }
+
+ // View:
+ void SchedulePaintInRect(const gfx::Rect& r) override {
+ ++schedule_paint_count_;
+ Label::SchedulePaintInRect(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) {
@@ -53,6 +77,15 @@ void SetRTL(bool rtl) {
EXPECT_EQ(rtl, base::i18n::IsRTL());
}
+// Returns true if |current| is bigger than |last|. Sets |last| to |current|.
+bool Increased(int current, int* last) {
+ bool increased = current > *last;
+ *last = current;
+ return increased;
+}
+
+} // namespace
+
// Crashes on Linux only. http://crbug.com/612406
#if defined(OS_LINUX)
#define MAYBE_FontPropertySymbol DISABLED_FontPropertySymbol
@@ -612,6 +645,36 @@ TEST_F(LabelTest, MultilineSupportedRenderText) {
}
#endif
+// Ensures SchedulePaint() calls are not made in OnPaint().
+TEST_F(LabelTest, NoSchedulePaintInOnPaint) {
+ TestLabel label;
+
+ // Initialization should schedule at least one paint, but the precise number
+ // doesn't really matter.
+ int count = label.schedule_paint_count();
+ EXPECT_LT(0, count);
+
+ // Painting should never schedule another paint.
+ label.SimulatePaint();
+ EXPECT_EQ(count, label.schedule_paint_count());
+
+ // Test a few things that should schedule paints. Multiple times is OK.
+ label.SetEnabled(false);
+ EXPECT_TRUE(Increased(label.schedule_paint_count(), &count));
+
+ label.SetText(label.text() + ASCIIToUTF16("Changed"));
+ EXPECT_TRUE(Increased(label.schedule_paint_count(), &count));
+
+ label.SizeToPreferredSize();
+ EXPECT_TRUE(Increased(label.schedule_paint_count(), &count));
+
+ label.SetEnabledColor(SK_ColorBLUE);
+ EXPECT_TRUE(Increased(label.schedule_paint_count(), &count));
+
+ label.SimulatePaint();
+ EXPECT_EQ(count, label.schedule_paint_count()); // Unchanged.
+}
+
TEST_F(LabelFocusTest, FocusBounds) {
label()->SetText(ASCIIToUTF16("Example"));
gfx::Size normal_size = label()->GetPreferredSize();
« no previous file with comments | « ui/views/controls/label.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698