Commit bfbe9c78 authored by wangxianzhu's avatar wangxianzhu Committed by Commit bot

Move condition in CollapsedBorderValue::Width() into constructor

This avoids the condition from being repeatedly checked.

Also tweek other methods to avoid unnecessary checks. Add test.

BUG=717128

Review-Url: https://codereview.chromium.org/2858143003
Cr-Commit-Position: refs/heads/master@{#469562}
parent b1c163ea
......@@ -1294,6 +1294,7 @@ source_set("unit_tests") {
"html/track/vtt/VTTScannerTest.cpp",
"input/EventHandlerTest.cpp",
"inspector/ProtocolParserTest.cpp",
"layout/CollapsedBorderValueTest.cpp",
"layout/ImageQualityControllerTest.cpp",
"layout/LayoutBlockTest.cpp",
"layout/LayoutBoxModelObjectTest.cpp",
......
......@@ -49,25 +49,23 @@ class CollapsedBorderValue {
: color_(0),
width_(0),
style_(kBorderStyleNone),
precedence_(kBorderPrecedenceOff),
transparent_(false) {}
precedence_(kBorderPrecedenceOff) {}
CollapsedBorderValue(const BorderValue& border,
const Color& color,
EBorderPrecedence precedence)
: color_(color),
width_(border.NonZero() ? border.Width() : 0),
width_(border.Style() > kBorderStyleHidden ? border.Width() : 0),
style_(border.Style()),
precedence_(precedence),
transparent_(border.IsTransparent()) {
precedence_(precedence) {
DCHECK(precedence != kBorderPrecedenceOff);
}
unsigned Width() const { return style_ > kBorderStyleHidden ? width_ : 0; }
unsigned Width() const { return width_; }
EBorderStyle Style() const { return static_cast<EBorderStyle>(style_); }
bool Exists() const { return precedence_ != kBorderPrecedenceOff; }
Color GetColor() const { return color_; }
bool IsTransparent() const { return transparent_; }
bool IsTransparent() const { return !color_.Alpha(); }
EBorderPrecedence Precedence() const {
return static_cast<EBorderPrecedence>(precedence_);
}
......@@ -80,8 +78,8 @@ class CollapsedBorderValue {
bool VisuallyEquals(const CollapsedBorderValue& o) const {
if (!IsVisible() && !o.IsVisible())
return true;
return GetColor() == o.GetColor() && IsTransparent() == o.IsTransparent() &&
IsSameIgnoringColor(o);
return GetColor() == o.GetColor() && Width() == o.Width() &&
Style() == o.Style();
}
bool IsVisible() const { return Width() && !IsTransparent(); }
......@@ -93,10 +91,9 @@ class CollapsedBorderValue {
private:
Color color_;
unsigned width_ : 24;
unsigned width_ : 25;
unsigned style_ : 4; // EBorderStyle
unsigned precedence_ : 3; // EBorderPrecedence
unsigned transparent_ : 1;
};
} // namespace blink
......
// Copyright 2017 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 "core/layout/CollapsedBorderValue.h"
#include "core/style/ComputedStyle.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace blink {
TEST(CollapsedBorderValueTest, Default) {
CollapsedBorderValue v;
EXPECT_EQ(0u, v.Width());
EXPECT_EQ(kBorderStyleNone, v.Style());
EXPECT_FALSE(v.Exists());
EXPECT_EQ(kBorderPrecedenceOff, v.Precedence());
EXPECT_FALSE(v.IsVisible());
EXPECT_TRUE(v.IsSameIgnoringColor(v));
EXPECT_TRUE(v.VisuallyEquals(v));
EXPECT_TRUE(v.IsSameIgnoringColor(CollapsedBorderValue()));
EXPECT_TRUE(v.VisuallyEquals(CollapsedBorderValue()));
}
TEST(CollapsedBorderValueTest, SolidZeroWidth) {
auto style = ComputedStyle::Create();
style->SetBorderLeftWidth(0);
style->SetBorderLeftStyle(kBorderStyleSolid);
CollapsedBorderValue v(style->BorderLeft(), Color(255, 0, 0),
kBorderPrecedenceCell);
EXPECT_TRUE(v.Exists());
EXPECT_EQ(0u, v.Width());
EXPECT_FALSE(v.IsTransparent());
EXPECT_FALSE(v.IsVisible());
EXPECT_TRUE(v.IsSameIgnoringColor(v));
EXPECT_TRUE(v.VisuallyEquals(v));
EXPECT_FALSE(v.IsSameIgnoringColor(CollapsedBorderValue()));
EXPECT_FALSE(v.IsSameIgnoringColor(
CollapsedBorderValue(ComputedStyle::Create()->BorderLeft(),
Color(0, 255, 0), kBorderPrecedenceCell)));
EXPECT_TRUE(v.VisuallyEquals(CollapsedBorderValue()));
}
TEST(CollapsedBorderValueTest, SolidNonZeroWidthTransparent) {
auto style = ComputedStyle::Create();
style->SetBorderLeftWidth(5);
style->SetBorderLeftStyle(kBorderStyleSolid);
CollapsedBorderValue v(style->BorderLeft(), Color(), kBorderPrecedenceCell);
EXPECT_TRUE(v.Exists());
EXPECT_EQ(5u, v.Width());
EXPECT_TRUE(v.IsTransparent());
EXPECT_FALSE(v.IsVisible());
EXPECT_TRUE(v.IsSameIgnoringColor(v));
EXPECT_TRUE(v.VisuallyEquals(v));
EXPECT_FALSE(v.IsSameIgnoringColor(CollapsedBorderValue()));
EXPECT_TRUE(v.IsSameIgnoringColor(CollapsedBorderValue(
style->BorderLeft(), Color(0, 255, 0), kBorderPrecedenceCell)));
EXPECT_TRUE(v.VisuallyEquals(CollapsedBorderValue()));
}
TEST(CollapsedBorderValueTest, None) {
auto style = ComputedStyle::Create();
style->SetBorderLeftWidth(5);
style->SetBorderLeftStyle(kBorderStyleNone);
CollapsedBorderValue v(style->BorderLeft(), Color(255, 0, 0),
kBorderPrecedenceCell);
EXPECT_TRUE(v.Exists());
EXPECT_EQ(0u, v.Width());
EXPECT_FALSE(v.IsTransparent());
EXPECT_FALSE(v.IsVisible());
EXPECT_TRUE(v.IsSameIgnoringColor(v));
EXPECT_TRUE(v.VisuallyEquals(v));
EXPECT_FALSE(v.IsSameIgnoringColor(CollapsedBorderValue()));
EXPECT_TRUE(v.IsSameIgnoringColor(
CollapsedBorderValue(ComputedStyle::Create()->BorderLeft(),
Color(0, 255, 0), kBorderPrecedenceCell)));
EXPECT_TRUE(v.VisuallyEquals(CollapsedBorderValue()));
}
TEST(CollapsedBorderValueTest, Hidden) {
auto style = ComputedStyle::Create();
style->SetBorderLeftWidth(5);
style->SetBorderLeftStyle(kBorderStyleHidden);
CollapsedBorderValue v(style->BorderLeft(), Color(255, 0, 0),
kBorderPrecedenceCell);
EXPECT_TRUE(v.Exists());
EXPECT_EQ(0u, v.Width());
EXPECT_FALSE(v.IsTransparent());
EXPECT_FALSE(v.IsVisible());
EXPECT_TRUE(v.IsSameIgnoringColor(v));
EXPECT_TRUE(v.VisuallyEquals(v));
EXPECT_FALSE(v.IsSameIgnoringColor(CollapsedBorderValue()));
EXPECT_FALSE(v.IsSameIgnoringColor(
CollapsedBorderValue(ComputedStyle::Create()->BorderLeft(),
Color(0, 255, 0), kBorderPrecedenceCell)));
EXPECT_TRUE(v.VisuallyEquals(CollapsedBorderValue()));
}
TEST(CollapsedBorderValueTest, SolidNonZeroWidthNonTransparent) {
auto style = ComputedStyle::Create();
style->SetBorderLeftWidth(5);
style->SetBorderLeftStyle(kBorderStyleSolid);
CollapsedBorderValue v(style->BorderLeft(), Color(255, 0, 0),
kBorderPrecedenceCell);
EXPECT_TRUE(v.Exists());
EXPECT_EQ(5u, v.Width());
EXPECT_FALSE(v.IsTransparent());
EXPECT_TRUE(v.IsVisible());
EXPECT_TRUE(v.IsSameIgnoringColor(v));
EXPECT_TRUE(v.VisuallyEquals(v));
EXPECT_FALSE(v.IsSameIgnoringColor(CollapsedBorderValue()));
EXPECT_FALSE(v.VisuallyEquals(CollapsedBorderValue()));
EXPECT_TRUE(v.IsSameIgnoringColor(CollapsedBorderValue(
style->BorderLeft(), Color(0, 255, 0), kBorderPrecedenceCell)));
EXPECT_FALSE(v.VisuallyEquals(CollapsedBorderValue(
style->BorderLeft(), Color(0, 255, 0), kBorderPrecedenceCell)));
EXPECT_FALSE(v.IsSameIgnoringColor(CollapsedBorderValue(
style->BorderLeft(), Color(255, 0, 0), kBorderPrecedenceTable)));
EXPECT_TRUE(v.VisuallyEquals(CollapsedBorderValue(
style->BorderLeft(), Color(255, 0, 0), kBorderPrecedenceTable)));
}
} // namespace blink
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment