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

Unified Diff: ui/views/border_unittest.cc

Issue 1517463003: Added comprehensive tests for views::Border. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years 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/DEPS ('k') | ui/views/views.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/views/border_unittest.cc
diff --git a/ui/views/border_unittest.cc b/ui/views/border_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..2c68dea1a42aad6984fbea54edac7b2f502cd639
--- /dev/null
+++ b/ui/views/border_unittest.cc
@@ -0,0 +1,160 @@
+// Copyright 2015 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/border.h"
+
+#include "base/memory/scoped_ptr.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/skia/include/core/SkCanvas.h"
+#include "third_party/skia/include/core/SkPaint.h"
+#include "third_party/skia/include/core/SkRRect.h"
+#include "third_party/skia/include/core/SkRect.h"
+#include "ui/gfx/canvas.h"
+#include "ui/gfx/geometry/size.h"
+#include "ui/views/painter.h"
+#include "ui/views/test/views_test_base.h"
+#include "ui/views/view.h"
+
+using namespace testing;
+
+namespace {
+
+class MockCanvas : public SkCanvas {
+ public:
+ MockCanvas(int width, int height) : SkCanvas(width, height) {}
+
+ MOCK_METHOD2(onDrawRect, void(const SkRect&, const SkPaint&));
+ MOCK_METHOD2(onDrawRRect, void(const SkRRect&, const SkPaint&));
+};
+
+class MockPainter : public views::Painter {
+ public:
+ MOCK_CONST_METHOD0(GetMinimumSize, gfx::Size());
+ MOCK_METHOD2(Paint, void(gfx::Canvas*, const gfx::Size&));
+};
+
+} // namespace
+
+namespace views {
+
+class BorderTest : public ViewsTestBase {
+ public:
+ void SetUp() override {
+ ViewsTestBase::SetUp();
+
+ view_.reset(new views::View());
+ view_->SetSize(gfx::Size(100, 50));
+ // The canvas should be much bigger than the view.
+ sk_canvas_.reset(new MockCanvas(1000, 500));
+ canvas_.reset(new gfx::Canvas(sk_canvas_.get(), 1.0f));
danakj 2015/12/10 20:01:27 Can you make a TearDown() method that cleans this
Matt Giuca 2015/12/11 00:03:21 Done. Thanks, I forgot that these would not be au
+ }
+
+ protected:
+ scoped_ptr<views::View> view_;
+ scoped_ptr<MockCanvas> sk_canvas_;
+ scoped_ptr<gfx::Canvas> canvas_;
+};
+
+TEST_F(BorderTest, NullBorder) {
+ scoped_ptr<Border> border(Border::NullBorder());
+ EXPECT_FALSE(border);
+}
+
+TEST_F(BorderTest, SolidBorder) {
+ scoped_ptr<Border> border(Border::CreateSolidBorder(3, SK_ColorBLUE));
+ EXPECT_EQ(gfx::Size(6, 6), border->GetMinimumSize());
+ EXPECT_EQ(gfx::Insets(3, 3, 3, 3), border->GetInsets());
+
+ // Order doesn't matter.
+ EXPECT_CALL(*sk_canvas_,
+ onDrawRect(SkRect::MakeLTRB(0, 0, 100, 3),
+ Property(&SkPaint::getColor, SK_ColorBLUE)));
+ EXPECT_CALL(*sk_canvas_,
+ onDrawRect(SkRect::MakeLTRB(0, 47, 100, 50),
+ Property(&SkPaint::getColor, SK_ColorBLUE)));
+ EXPECT_CALL(*sk_canvas_,
+ onDrawRect(SkRect::MakeLTRB(0, 3, 3, 47),
+ Property(&SkPaint::getColor, SK_ColorBLUE)));
+ EXPECT_CALL(*sk_canvas_,
+ onDrawRect(SkRect::MakeLTRB(97, 3, 100, 47),
+ Property(&SkPaint::getColor, SK_ColorBLUE)));
+ border->Paint(*view_, canvas_.get());
+}
+
+// TODO(mgiuca): Fails due to http://crbug.com/565801.
+TEST_F(BorderTest, DISABLED_RoundedRectBorder) {
+ scoped_ptr<Border> border(
+ Border::CreateRoundedRectBorder(3, 4, SK_ColorBLUE));
+ EXPECT_EQ(gfx::Size(6, 6), border->GetMinimumSize());
+ EXPECT_EQ(gfx::Insets(3, 3, 3, 3), border->GetInsets());
+
+ SkRRect expected_rrect;
+ expected_rrect.setRectXY(SkRect::MakeLTRB(1.5, 1.5, 98.5, 48.5), 4, 4);
+ EXPECT_CALL(
+ *sk_canvas_,
+ onDrawRRect(expected_rrect,
+ AllOf(Property(&SkPaint::getStrokeWidth, 3),
+ Property(&SkPaint::getColor, SK_ColorBLUE),
+ Property(&SkPaint::getStyle, SkPaint::kStroke_Style),
+ Property(&SkPaint::isAntiAlias, true))));
+ border->Paint(*view_, canvas_.get());
+}
+
+TEST_F(BorderTest, EmptyBorder) {
+ const gfx::Insets kInsets(1, 2, 3, 4);
+
+ scoped_ptr<Border> border(Border::CreateEmptyBorder(
+ kInsets.top(), kInsets.left(), kInsets.bottom(), kInsets.right()));
+ // The EmptyBorder has no minimum size despite nonzero insets.
+ EXPECT_EQ(gfx::Size(), border->GetMinimumSize());
+ EXPECT_EQ(kInsets, border->GetInsets());
+ // Should have no effect.
+ border->Paint(*view_, canvas_.get());
+
+ scoped_ptr<Border> border2(Border::CreateEmptyBorder(kInsets));
+ EXPECT_EQ(kInsets, border2->GetInsets());
+}
+
+TEST_F(BorderTest, SolidSidedBorder) {
+ const gfx::Insets kInsets(1, 2, 3, 4);
+
+ scoped_ptr<Border> border(Border::CreateSolidSidedBorder(
+ kInsets.top(), kInsets.left(), kInsets.bottom(), kInsets.right(),
+ SK_ColorBLUE));
+ EXPECT_EQ(gfx::Size(6, 4), border->GetMinimumSize());
+ EXPECT_EQ(kInsets, border->GetInsets());
+
+ // Order doesn't matter.
+ EXPECT_CALL(*sk_canvas_,
+ onDrawRect(SkRect::MakeLTRB(0, 0, 100, 1),
+ Property(&SkPaint::getColor, SK_ColorBLUE)));
+ EXPECT_CALL(*sk_canvas_,
+ onDrawRect(SkRect::MakeLTRB(0, 47, 100, 50),
+ Property(&SkPaint::getColor, SK_ColorBLUE)));
+ EXPECT_CALL(*sk_canvas_,
+ onDrawRect(SkRect::MakeLTRB(0, 1, 2, 47),
+ Property(&SkPaint::getColor, SK_ColorBLUE)));
+ EXPECT_CALL(*sk_canvas_,
+ onDrawRect(SkRect::MakeLTRB(96, 1, 100, 47),
+ Property(&SkPaint::getColor, SK_ColorBLUE)));
+ border->Paint(*view_, canvas_.get());
+}
+
+TEST_F(BorderTest, BorderPainter) {
+ const gfx::Insets kInsets(1, 2, 3, 4);
+
+ MockPainter* painter = new MockPainter();
+ scoped_ptr<Border> border(Border::CreateBorderPainter(painter, kInsets));
+ EXPECT_CALL(*painter, GetMinimumSize()).WillOnce(Return(gfx::Size(60, 40)));
+ EXPECT_EQ(gfx::Size(60, 40), border->GetMinimumSize());
+
+ EXPECT_EQ(kInsets, border->GetInsets());
+
+ // Expect that the Painter is called with our canvas and the view's size.
+ EXPECT_CALL(*painter, Paint(canvas_.get(), view_->size()));
+ border->Paint(*view_, canvas_.get());
+}
+
+} // namespace views
« no previous file with comments | « ui/views/DEPS ('k') | ui/views/views.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698