summaryrefslogtreecommitdiffstats
path: root/cc/test/scheduler_test_common.h
blob: d9cb3cf79a3f842c9ca3b4852c9f11518a449e14 (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
// Copyright 2011 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.

#ifndef CC_TEST_SCHEDULER_TEST_COMMON_H_
#define CC_TEST_SCHEDULER_TEST_COMMON_H_

#include <string>

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/time/time.h"
#include "cc/scheduler/delay_based_time_source.h"
#include "cc/scheduler/scheduler.h"
#include "cc/test/ordered_simple_task_runner.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace cc {

class FakeTimeSourceClient : public TimeSourceClient {
 public:
  FakeTimeSourceClient() : tick_called_(false) {}
  void Reset() { tick_called_ = false; }
  bool TickCalled() const { return tick_called_; }

  // TimeSourceClient implementation.
  void OnTimerTick() override;

 protected:
  bool tick_called_;
};

class FakeDelayBasedTimeSource : public DelayBasedTimeSource {
 public:
  static scoped_refptr<FakeDelayBasedTimeSource> Create(
      base::TimeDelta interval, base::SingleThreadTaskRunner* task_runner) {
    return make_scoped_refptr(new FakeDelayBasedTimeSource(interval,
                                                           task_runner));
  }

  void SetNow(base::TimeTicks time) { now_ = time; }
  base::TimeTicks Now() const override;

 protected:
  FakeDelayBasedTimeSource(base::TimeDelta interval,
                           base::SingleThreadTaskRunner* task_runner)
      : DelayBasedTimeSource(interval, task_runner) {}
  ~FakeDelayBasedTimeSource() override {}

  base::TimeTicks now_;
};

class TestDelayBasedTimeSource : public DelayBasedTimeSource {
 public:
  static scoped_refptr<TestDelayBasedTimeSource> Create(
      scoped_refptr<TestNowSource> now_src,
      base::TimeDelta interval,
      OrderedSimpleTaskRunner* task_runner) {
    return make_scoped_refptr(
        new TestDelayBasedTimeSource(now_src, interval, task_runner));
  }

 protected:
  TestDelayBasedTimeSource(scoped_refptr<TestNowSource> now_src,
                           base::TimeDelta interval,
                           OrderedSimpleTaskRunner* task_runner);

  // Overridden from DelayBasedTimeSource
  ~TestDelayBasedTimeSource() override;
  base::TimeTicks Now() const override;
  std::string TypeString() const override;

  scoped_refptr<TestNowSource> now_src_;
};

struct FakeBeginFrameSource : public BeginFrameSourceMixIn {
  bool remaining_frames_ = false;

  BeginFrameObserver* GetObserver() { return observer_; }

  BeginFrameArgs TestLastUsedBeginFrameArgs() {
    if (observer_) {
      return observer_->LastUsedBeginFrameArgs();
    }
    return BeginFrameArgs();
  }
  void TestOnBeginFrame(const BeginFrameArgs& args) {
    return CallOnBeginFrame(args);
  }

  // BeginFrameSource
  void DidFinishFrame(size_t remaining_frames) override;
  void AsValueInto(base::debug::TracedValue* dict) const override;

  ~FakeBeginFrameSource() override {}
};

class TestBackToBackBeginFrameSource : public BackToBackBeginFrameSource {
 public:
  ~TestBackToBackBeginFrameSource() override;

  static scoped_ptr<TestBackToBackBeginFrameSource> Create(
      scoped_refptr<TestNowSource> now_src,
      base::SingleThreadTaskRunner* task_runner) {
    return make_scoped_ptr(
        new TestBackToBackBeginFrameSource(now_src, task_runner));
  }

 protected:
  TestBackToBackBeginFrameSource(scoped_refptr<TestNowSource> now_src,
                                 base::SingleThreadTaskRunner* task_runner);

  base::TimeTicks Now() override;

  scoped_refptr<TestNowSource> now_src_;
};

class TestSyntheticBeginFrameSource : public SyntheticBeginFrameSource {
 public:
  ~TestSyntheticBeginFrameSource() override;

  static scoped_ptr<TestSyntheticBeginFrameSource> Create(
      scoped_refptr<TestNowSource> now_src,
      OrderedSimpleTaskRunner* task_runner,
      base::TimeDelta initial_interval) {
    return make_scoped_ptr(
        new TestSyntheticBeginFrameSource(TestDelayBasedTimeSource::Create(
            now_src, initial_interval, task_runner)));
  }

 protected:
  TestSyntheticBeginFrameSource(
      scoped_refptr<DelayBasedTimeSource> time_source);
};

class TestScheduler;
class TestSchedulerFrameSourcesConstructor
    : public SchedulerFrameSourcesConstructor {
 public:
  ~TestSchedulerFrameSourcesConstructor() override;

 protected:
  BeginFrameSource* ConstructPrimaryFrameSource(Scheduler* scheduler) override;
  BeginFrameSource* ConstructBackgroundFrameSource(
      Scheduler* scheduler) override;

  OrderedSimpleTaskRunner* test_task_runner_;
  TestNowSource* now_src_;

 protected:
  explicit TestSchedulerFrameSourcesConstructor(
      OrderedSimpleTaskRunner* test_task_runner,
      TestNowSource* now_src);
  friend class TestScheduler;
};

class TestScheduler : public Scheduler {
 public:
  static scoped_ptr<TestScheduler> Create(
      scoped_refptr<TestNowSource> now_src,
      SchedulerClient* client,
      const SchedulerSettings& scheduler_settings,
      int layer_tree_host_id,
      const scoped_refptr<OrderedSimpleTaskRunner>& task_runner,
      base::PowerMonitor* power_monitor) {
    TestSchedulerFrameSourcesConstructor frame_sources_constructor(
        task_runner.get(), now_src.get());
    return make_scoped_ptr(new TestScheduler(now_src,
                                             client,
                                             scheduler_settings,
                                             layer_tree_host_id,
                                             task_runner,
                                             power_monitor,
                                             &frame_sources_constructor));
  }

  // Extra test helper functionality
  bool IsBeginRetroFrameArgsEmpty() const {
    return begin_retro_frame_args_.empty();
  }

  BeginFrameSource& frame_source() { return *frame_source_; }

  ~TestScheduler() override;

 protected:
  // Overridden from Scheduler.
  base::TimeTicks Now() const override;

 private:
  TestScheduler(
      scoped_refptr<TestNowSource> now_src,
      SchedulerClient* client,
      const SchedulerSettings& scheduler_settings,
      int layer_tree_host_id,
      const scoped_refptr<OrderedSimpleTaskRunner>& test_task_runner,
      base::PowerMonitor* power_monitor,
      TestSchedulerFrameSourcesConstructor* frame_sources_constructor);

  scoped_refptr<TestNowSource> now_src_;
};

}  // namespace cc

#endif  // CC_TEST_SCHEDULER_TEST_COMMON_H_