1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/view_manager/server_view_drawn_tracker.h"
7 #include "components/view_manager/server_view.h"
8 #include "components/view_manager/server_view_drawn_tracker_observer.h"
9 #include "components/view_manager/test_server_view_delegate.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace view_manager
{
15 class TestServerViewDrawnTrackerObserver
16 : public ServerViewDrawnTrackerObserver
{
18 TestServerViewDrawnTrackerObserver()
24 void clear_change_count() { change_count_
= 0u; }
25 size_t change_count() const { return change_count_
; }
26 const ServerView
* ancestor() const { return ancestor_
; }
27 const ServerView
* view() const { return view_
; }
28 bool is_drawn() const { return is_drawn_
; }
31 // ServerViewDrawnTrackerObserver:
32 void OnDrawnStateChanged(ServerView
* ancestor
,
34 bool is_drawn
) override
{
42 const ServerView
* ancestor_
;
43 const ServerView
* view_
;
46 DISALLOW_COPY_AND_ASSIGN(TestServerViewDrawnTrackerObserver
);
51 TEST(ServerViewDrawnTrackerTest
, ChangeBecauseOfDeletionAndVisibility
) {
52 TestServerViewDelegate server_view_delegate
;
53 scoped_ptr
<ServerView
> view(new ServerView(&server_view_delegate
, ViewId()));
54 TestServerViewDrawnTrackerObserver drawn_observer
;
55 ServerViewDrawnTracker
tracker(view
.get(), view
.get(), &drawn_observer
);
56 view
->SetVisible(true);
57 EXPECT_EQ(1u, drawn_observer
.change_count());
58 EXPECT_EQ(view
.get(), drawn_observer
.view());
59 EXPECT_EQ(nullptr, drawn_observer
.ancestor());
60 EXPECT_TRUE(drawn_observer
.is_drawn());
61 drawn_observer
.clear_change_count();
63 view
->SetVisible(false);
64 EXPECT_EQ(1u, drawn_observer
.change_count());
65 EXPECT_EQ(view
.get(), drawn_observer
.view());
66 EXPECT_EQ(nullptr, drawn_observer
.ancestor());
67 EXPECT_FALSE(drawn_observer
.is_drawn());
68 drawn_observer
.clear_change_count();
70 view
->SetVisible(true);
71 EXPECT_EQ(1u, drawn_observer
.change_count());
72 EXPECT_EQ(view
.get(), drawn_observer
.view());
73 EXPECT_EQ(nullptr, drawn_observer
.ancestor());
74 EXPECT_TRUE(drawn_observer
.is_drawn());
75 drawn_observer
.clear_change_count();
78 EXPECT_EQ(1u, drawn_observer
.change_count());
79 EXPECT_EQ(view
.get(), drawn_observer
.view());
80 EXPECT_EQ(nullptr, drawn_observer
.ancestor());
81 EXPECT_FALSE(drawn_observer
.is_drawn());
84 TEST(ServerViewDrawnTrackerTest
, ChangeBecauseOfRemovingFromRoot
) {
85 TestServerViewDelegate server_view_delegate
;
86 ServerView
root(&server_view_delegate
, ViewId());
87 root
.SetVisible(true);
88 ServerView
child(&server_view_delegate
, ViewId());
89 child
.SetVisible(true);
92 TestServerViewDrawnTrackerObserver drawn_observer
;
93 ServerViewDrawnTracker
tracker(&root
, &child
, &drawn_observer
);
95 EXPECT_EQ(1u, drawn_observer
.change_count());
96 EXPECT_EQ(&child
, drawn_observer
.view());
97 EXPECT_EQ(&root
, drawn_observer
.ancestor());
98 EXPECT_FALSE(drawn_observer
.is_drawn());
99 drawn_observer
.clear_change_count();
102 EXPECT_EQ(1u, drawn_observer
.change_count());
103 EXPECT_EQ(&child
, drawn_observer
.view());
104 EXPECT_EQ(nullptr, drawn_observer
.ancestor());
105 EXPECT_TRUE(drawn_observer
.is_drawn());
108 TEST(ServerViewDrawnTrackerTest
, ChangeBecauseOfRemovingAncestorFromRoot
) {
109 TestServerViewDelegate server_view_delegate
;
110 ServerView
root(&server_view_delegate
, ViewId());
111 root
.SetVisible(true);
112 ServerView
child(&server_view_delegate
, ViewId());
113 child
.SetVisible(true);
116 ServerView
child_child(&server_view_delegate
, ViewId());
117 child_child
.SetVisible(true);
118 child
.Add(&child_child
);
120 TestServerViewDrawnTrackerObserver drawn_observer
;
121 ServerViewDrawnTracker
tracker(&root
, &child_child
, &drawn_observer
);
123 EXPECT_EQ(1u, drawn_observer
.change_count());
124 EXPECT_EQ(&child_child
, drawn_observer
.view());
125 EXPECT_EQ(&root
, drawn_observer
.ancestor());
126 EXPECT_FALSE(drawn_observer
.is_drawn());
127 drawn_observer
.clear_change_count();
129 root
.Add(&child_child
);
130 EXPECT_EQ(1u, drawn_observer
.change_count());
131 EXPECT_EQ(&child_child
, drawn_observer
.view());
132 EXPECT_EQ(nullptr, drawn_observer
.ancestor());
133 EXPECT_TRUE(drawn_observer
.is_drawn());
136 } // namespace view_manager