summaryrefslogtreecommitdiff
path: root/chromium/ui/compositor/compositor_unittest.cc
blob: 50180ad1edb9bdb28fd97dab8ea96103727add9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// 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 "base/macros.h"
#include "base/run_loop.h"
#include "base/thread_task_runner_handle.h"
#include "cc/output/begin_frame_args.h"
#include "cc/test/begin_frame_args_test.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/test/context_factories_for_test.h"
#include "ui/compositor/test/draw_waiter_for_test.h"

using testing::Mock;
using testing::_;

namespace ui {
namespace {

ACTION_P2(RemoveObserver, compositor, observer) {
  compositor->RemoveBeginFrameObserver(observer);
}

class MockCompositorBeginFrameObserver : public CompositorBeginFrameObserver {
 public:
  MOCK_METHOD1(OnSendBeginFrame, void(const cc::BeginFrameArgs&));
};

// Test fixture for tests that require a ui::Compositor with a real task
// runner.
class CompositorTest : public testing::Test {
 public:
  CompositorTest() {}
  ~CompositorTest() override {}

  void SetUp() override {
    task_runner_ = base::ThreadTaskRunnerHandle::Get();

    ui::ContextFactory* context_factory =
        ui::InitializeContextFactoryForTests(false);

    compositor_.reset(new ui::Compositor(context_factory, task_runner_));
    compositor_->SetAcceleratedWidget(gfx::kNullAcceleratedWidget);
  }
  void TearDown() override {
    compositor_.reset();
    ui::TerminateContextFactoryForTests();
  }

 protected:
  base::SingleThreadTaskRunner* task_runner() { return task_runner_.get(); }
  ui::Compositor* compositor() { return compositor_.get(); }

 private:
  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  scoped_ptr<ui::Compositor> compositor_;

  DISALLOW_COPY_AND_ASSIGN(CompositorTest);
};

}  // namespace

TEST_F(CompositorTest, LocksTimeOut) {
  scoped_refptr<ui::CompositorLock> lock;
  {
    base::RunLoop run_loop;
    // Ensure that the lock times out by default.
    lock = compositor()->GetCompositorLock();
    EXPECT_TRUE(compositor()->IsLocked());
    task_runner()->PostDelayedTask(
        FROM_HERE, run_loop.QuitClosure(),
        base::TimeDelta::FromMilliseconds(kCompositorLockTimeoutMs));
    run_loop.Run();
    EXPECT_FALSE(compositor()->IsLocked());
  }

  {
    base::RunLoop run_loop;
    // Ensure that the lock does not time out when set.
    compositor()->SetLocksWillTimeOut(false);
    lock = compositor()->GetCompositorLock();
    EXPECT_TRUE(compositor()->IsLocked());
    task_runner()->PostDelayedTask(
        FROM_HERE, run_loop.QuitClosure(),
        base::TimeDelta::FromMilliseconds(kCompositorLockTimeoutMs));
    run_loop.Run();
    EXPECT_TRUE(compositor()->IsLocked());
  }
}

TEST_F(CompositorTest, AddAndRemoveBeginFrameObserver) {
  cc::BeginFrameArgs args =
    cc::CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE,
                                       base::TimeTicks::FromInternalValue(33));

  MockCompositorBeginFrameObserver test_observer;
  MockCompositorBeginFrameObserver test_observer2;

  // Add a single observer.
  compositor()->AddBeginFrameObserver(&test_observer);
  Mock::VerifyAndClearExpectations(&test_observer);

  // When |missed_begin_frame_args_| is sent, its type is set to MISSED.
  cc::BeginFrameArgs expected_args(args);
  cc::BeginFrameArgs expected_missed_args(args);
  expected_missed_args.type = cc::BeginFrameArgs::MISSED;

  // Simulate to trigger new BeginFrame by using |args|.
  EXPECT_CALL(test_observer, OnSendBeginFrame(expected_args));
  compositor()->SendBeginFramesToChildren(args);
  Mock::VerifyAndClearExpectations(&test_observer);

  // When new observer is added, Compositor immediately calls OnSendBeginFrame
  // with |missed_begin_frame_args_|.
  EXPECT_CALL(test_observer2, OnSendBeginFrame(expected_missed_args));
  compositor()->AddBeginFrameObserver(&test_observer2);
  Mock::VerifyAndClearExpectations(&test_observer);
  Mock::VerifyAndClearExpectations(&test_observer2);

  // When |test_observer2| is removed and added again, it will be called again.
  EXPECT_CALL(test_observer2, OnSendBeginFrame(expected_missed_args));
  compositor()->RemoveBeginFrameObserver(&test_observer2);
  compositor()->AddBeginFrameObserver(&test_observer2);
  Mock::VerifyAndClearExpectations(&test_observer2);

  // When all observer is removed, |missed_begin_frame_args_| is invalidated.
  // So, it is not used for newly added observer.
  EXPECT_CALL(test_observer2, OnSendBeginFrame(_)).Times(0);
  compositor()->RemoveBeginFrameObserver(&test_observer);
  compositor()->RemoveBeginFrameObserver(&test_observer2);
  compositor()->SendBeginFramesToChildren(args);
  compositor()->AddBeginFrameObserver(&test_observer2);
  Mock::VerifyAndClearExpectations(&test_observer2);

  compositor()->RemoveBeginFrameObserver(&test_observer2);
}

TEST_F(CompositorTest, RemoveBeginFrameObserverWhileSendingBeginFrame) {
  cc::BeginFrameArgs args = cc::CreateBeginFrameArgsForTesting(
      BEGINFRAME_FROM_HERE, base::TimeTicks::FromInternalValue(33));

  cc::BeginFrameArgs expected_args(args);
  cc::BeginFrameArgs expected_missed_args(args);
  expected_missed_args.type = cc::BeginFrameArgs::MISSED;

  // Add both observers, and simulate removal of |test_observer2| during
  // BeginFrame dispatch (implicitly triggered when the observer is added).
  MockCompositorBeginFrameObserver test_observer;
  MockCompositorBeginFrameObserver test_observer2;
  EXPECT_CALL(test_observer, OnSendBeginFrame(expected_args));
  EXPECT_CALL(test_observer2, OnSendBeginFrame(expected_missed_args))
      .WillOnce(RemoveObserver(compositor(), &test_observer2));

  // When a new observer is added, Compositor immediately calls OnSendBeginFrame
  // with |missed_begin_frame_args_|.
  compositor()->AddBeginFrameObserver(&test_observer);
  compositor()->SendBeginFramesToChildren(args);
  compositor()->AddBeginFrameObserver(&test_observer2);
  Mock::VerifyAndClearExpectations(&test_observer);
  Mock::VerifyAndClearExpectations(&test_observer2);

  // |test_observer2| was removed during the previous implicit BeginFrame
  // dispatch, and should not get the new frame.
  expected_args.type = cc::BeginFrameArgs::NORMAL;
  EXPECT_CALL(test_observer, OnSendBeginFrame(expected_args));
  EXPECT_CALL(test_observer2, OnSendBeginFrame(_)).Times(0);
  compositor()->SendBeginFramesToChildren(args);
  Mock::VerifyAndClearExpectations(&test_observer);
  Mock::VerifyAndClearExpectations(&test_observer2);

  // Now remove |test_observer| during explicit BeginFrame dispatch.
  EXPECT_CALL(test_observer, OnSendBeginFrame(expected_args))
      .WillOnce(RemoveObserver(compositor(), &test_observer));
  EXPECT_CALL(test_observer2, OnSendBeginFrame(_)).Times(0);
  compositor()->SendBeginFramesToChildren(args);
  Mock::VerifyAndClearExpectations(&test_observer);
  Mock::VerifyAndClearExpectations(&test_observer2);

  // No observers should get the new frame.
  EXPECT_CALL(test_observer, OnSendBeginFrame(_)).Times(0);
  EXPECT_CALL(test_observer2, OnSendBeginFrame(_)).Times(0);
  compositor()->SendBeginFramesToChildren(args);
  Mock::VerifyAndClearExpectations(&test_observer);
  Mock::VerifyAndClearExpectations(&test_observer2);

  // Adding a new observer should not trigger a missed frame, as the
  // previous frame had no observers.
  EXPECT_CALL(test_observer, OnSendBeginFrame(_)).Times(0);
  compositor()->AddBeginFrameObserver(&test_observer);
  compositor()->RemoveBeginFrameObserver(&test_observer);
  Mock::VerifyAndClearExpectations(&test_observer);
}

TEST_F(CompositorTest, ReleaseWidgetWithOutputSurfaceNeverCreated) {
  compositor()->SetVisible(false);
  EXPECT_EQ(gfx::kNullAcceleratedWidget,
            compositor()->ReleaseAcceleratedWidget());
  compositor()->SetAcceleratedWidget(gfx::kNullAcceleratedWidget);
  compositor()->SetVisible(true);
}

TEST_F(CompositorTest, CreateAndReleaseOutputSurface) {
  scoped_ptr<Layer> root_layer(new Layer(ui::LAYER_SOLID_COLOR));
  root_layer->SetBounds(gfx::Rect(10, 10));
  compositor()->SetRootLayer(root_layer.get());
  compositor()->SetScaleAndSize(1.0f, gfx::Size(10, 10));
  DCHECK(compositor()->IsVisible());
  compositor()->ScheduleDraw();
  DrawWaiterForTest::WaitForCompositingEnded(compositor());
  compositor()->SetVisible(false);
  EXPECT_EQ(gfx::kNullAcceleratedWidget,
            compositor()->ReleaseAcceleratedWidget());
  compositor()->SetAcceleratedWidget(gfx::kNullAcceleratedWidget);
  compositor()->SetVisible(true);
  compositor()->ScheduleDraw();
  DrawWaiterForTest::WaitForCompositingEnded(compositor());
  compositor()->SetRootLayer(nullptr);
}

}  // namespace ui