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/mus/server_view_drawn_tracker.h"
7 #include "components/mus/server_view.h"
8 #include "components/mus/server_view_drawn_tracker_observer.h"
9 #include "components/mus/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 server_view_delegate
.set_root_view(view
.get());
55 TestServerViewDrawnTrackerObserver drawn_observer
;
56 ServerViewDrawnTracker
tracker(view
.get(), &drawn_observer
);
57 view
->SetVisible(true);
58 EXPECT_EQ(1u, drawn_observer
.change_count());
59 EXPECT_EQ(view
.get(), drawn_observer
.view());
60 EXPECT_EQ(nullptr, drawn_observer
.ancestor());
61 EXPECT_TRUE(drawn_observer
.is_drawn());
62 drawn_observer
.clear_change_count();
64 view
->SetVisible(false);
65 EXPECT_EQ(1u, drawn_observer
.change_count());
66 EXPECT_EQ(view
.get(), drawn_observer
.view());
67 EXPECT_EQ(nullptr, drawn_observer
.ancestor());
68 EXPECT_FALSE(drawn_observer
.is_drawn());
69 drawn_observer
.clear_change_count();
71 view
->SetVisible(true);
72 EXPECT_EQ(1u, drawn_observer
.change_count());
73 EXPECT_EQ(view
.get(), drawn_observer
.view());
74 EXPECT_EQ(nullptr, drawn_observer
.ancestor());
75 EXPECT_TRUE(drawn_observer
.is_drawn());
76 drawn_observer
.clear_change_count();
79 EXPECT_EQ(1u, drawn_observer
.change_count());
80 EXPECT_EQ(view
.get(), drawn_observer
.view());
81 EXPECT_EQ(nullptr, drawn_observer
.ancestor());
82 EXPECT_FALSE(drawn_observer
.is_drawn());
85 TEST(ServerViewDrawnTrackerTest
, ChangeBecauseOfRemovingFromRoot
) {
86 TestServerViewDelegate server_view_delegate
;
87 ServerView
root(&server_view_delegate
, ViewId());
88 server_view_delegate
.set_root_view(&root
);
89 root
.SetVisible(true);
90 ServerView
child(&server_view_delegate
, ViewId());
91 child
.SetVisible(true);
94 TestServerViewDrawnTrackerObserver drawn_observer
;
95 ServerViewDrawnTracker
tracker(&child
, &drawn_observer
);
97 EXPECT_EQ(1u, drawn_observer
.change_count());
98 EXPECT_EQ(&child
, drawn_observer
.view());
99 EXPECT_EQ(&root
, drawn_observer
.ancestor());
100 EXPECT_FALSE(drawn_observer
.is_drawn());
101 drawn_observer
.clear_change_count();
104 EXPECT_EQ(1u, drawn_observer
.change_count());
105 EXPECT_EQ(&child
, drawn_observer
.view());
106 EXPECT_EQ(nullptr, drawn_observer
.ancestor());
107 EXPECT_TRUE(drawn_observer
.is_drawn());
110 TEST(ServerViewDrawnTrackerTest
, ChangeBecauseOfRemovingAncestorFromRoot
) {
111 TestServerViewDelegate server_view_delegate
;
112 ServerView
root(&server_view_delegate
, ViewId());
113 server_view_delegate
.set_root_view(&root
);
114 root
.SetVisible(true);
115 ServerView
child(&server_view_delegate
, ViewId());
116 child
.SetVisible(true);
119 ServerView
child_child(&server_view_delegate
, ViewId());
120 child_child
.SetVisible(true);
121 child
.Add(&child_child
);
123 TestServerViewDrawnTrackerObserver drawn_observer
;
124 ServerViewDrawnTracker
tracker(&child_child
, &drawn_observer
);
126 EXPECT_EQ(1u, drawn_observer
.change_count());
127 EXPECT_EQ(&child_child
, drawn_observer
.view());
128 EXPECT_EQ(&root
, drawn_observer
.ancestor());
129 EXPECT_FALSE(drawn_observer
.is_drawn());
130 drawn_observer
.clear_change_count();
132 root
.Add(&child_child
);
133 EXPECT_EQ(1u, drawn_observer
.change_count());
134 EXPECT_EQ(&child_child
, drawn_observer
.view());
135 EXPECT_EQ(nullptr, drawn_observer
.ancestor());
136 EXPECT_TRUE(drawn_observer
.is_drawn());
139 } // namespace view_manager