diff options
Diffstat (limited to 'views')
-rw-r--r-- | views/view.cc | 6 | ||||
-rw-r--r-- | views/view_unittest.cc | 22 |
2 files changed, 24 insertions, 4 deletions
diff --git a/views/view.cc b/views/view.cc index 820800a..67ac560 100644 --- a/views/view.cc +++ b/views/view.cc @@ -199,11 +199,9 @@ View* View::GetChildViewAt(int index) { } bool View::Contains(const View* view) const { - const View* child = view; - while (child) { - if (child == this) + for (const View* v = view; v; v = v->parent_) { + if (v == this) return true; - child = child->parent(); } return false; } diff --git a/views/view_unittest.cc b/views/view_unittest.cc index 5a1bf4d..97d32fa 100644 --- a/views/view_unittest.cc +++ b/views/view_unittest.cc @@ -1875,4 +1875,26 @@ TEST_F(ViewTest, SetBoundsPaint) { EXPECT_EQ(gfx::Rect(10, 10, 40, 40), paint_rect); } +TEST_F(ViewTest, Contains) { + TestView v1; + TestView* v2 = new TestView(); + TestView* v3 = new TestView(); + + v1.AddChildView(v2); + v2->AddChildView(v3); + + EXPECT_FALSE(v1.Contains(NULL)); + EXPECT_TRUE(v1.Contains(&v1)); + EXPECT_TRUE(v1.Contains(v2)); + EXPECT_TRUE(v1.Contains(v3)); + EXPECT_FALSE(v2->Contains(NULL)); + EXPECT_TRUE(v2->Contains(v2)); + EXPECT_FALSE(v2->Contains(&v1)); + EXPECT_TRUE(v2->Contains(v3)); + EXPECT_FALSE(v3->Contains(NULL)); + EXPECT_TRUE(v3->Contains(v3)); + EXPECT_FALSE(v3->Contains(&v1)); + EXPECT_FALSE(v3->Contains(v2)); +} + } // namespace views |