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

android_webview / browser / gfx / begin_frame_source_webview_unittest.cc [blame]

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "android_webview/browser/gfx/begin_frame_source_webview.h"

#include "components/viz/test/begin_frame_source_test.h"

namespace android_webview {
namespace {

class TestBeginFrameSource : public viz::ExternalBeginFrameSource,
                             public viz::ExternalBeginFrameSourceClient {
 public:
  TestBeginFrameSource() : ExternalBeginFrameSource(this) {}
  ~TestBeginFrameSource() override = default;

  void OnNeedsBeginFrames(bool needs_begin_frames) override {
    needs_begin_frames_ = needs_begin_frames;
  }

  bool needs_begin_frames() const { return needs_begin_frames_; }

 private:
  bool needs_begin_frames_ = false;
};

}  // namespace

class BeginFrameSourceWebViewTest : public ::testing::Test {
 public:
  BeginFrameSourceWebViewTest() {
    root_begin_frame_source_.ObserveBeginFrameSource(&test_begin_frame_source_);
  }

  viz::BeginFrameArgs BeginFrameArgsForTesting(uint64_t sequence) {
    return viz::CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, 1,
                                               sequence);
  }

 protected:
  BeginFrameSourceWebView begin_frame_source_;
  RootBeginFrameSourceWebView root_begin_frame_source_;

  // Test classes used to issue and observe begin frames.
  testing::NiceMock<viz::MockBeginFrameObserver> observer_;
  TestBeginFrameSource test_begin_frame_source_;
};

TEST_F(BeginFrameSourceWebViewTest, RootBeginFrame) {
  EXPECT_FALSE(test_begin_frame_source_.needs_begin_frames());

  // Send BeginFrame without observer.
  auto args1 = BeginFrameArgsForTesting(1);
  test_begin_frame_source_.OnBeginFrame(args1);

  root_begin_frame_source_.AddObserver(&observer_);
  EXPECT_TRUE(test_begin_frame_source_.needs_begin_frames());

  // Send BeginFrame with observer, verify it gets it.
  auto args2 = BeginFrameArgsForTesting(2);
  EXPECT_BEGIN_FRAME_ARGS_USED(observer_, args2);
  test_begin_frame_source_.OnBeginFrame(args2);

  root_begin_frame_source_.RemoveObserver(&observer_);
  EXPECT_FALSE(test_begin_frame_source_.needs_begin_frames());
}

TEST_F(BeginFrameSourceWebViewTest, RootPausedWithObservers) {
  // External BFS is not paused => observer will be unpaused on Add.
  EXPECT_BEGIN_FRAME_SOURCE_PAUSED(observer_, false);
  root_begin_frame_source_.AddObserver(&observer_);

  // External BFS is paused => observer will be paused.
  EXPECT_BEGIN_FRAME_SOURCE_PAUSED(observer_, true);
  test_begin_frame_source_.OnSetBeginFrameSourcePaused(true);

  // External BFS is unpaused => observer will be unpaused.
  EXPECT_BEGIN_FRAME_SOURCE_PAUSED(observer_, false);
  test_begin_frame_source_.OnSetBeginFrameSourcePaused(false);
  root_begin_frame_source_.RemoveObserver(&observer_);
}

TEST_F(BeginFrameSourceWebViewTest, RootPausedWithoutObservers) {
  test_begin_frame_source_.OnSetBeginFrameSourcePaused(true);

  // External BFS is paused => observer will be paused on Add.
  EXPECT_BEGIN_FRAME_SOURCE_PAUSED(observer_, true);
  root_begin_frame_source_.AddObserver(&observer_);

  // External BFS is unpaused => observer will be unpaused.
  EXPECT_BEGIN_FRAME_SOURCE_PAUSED(observer_, false);
  test_begin_frame_source_.OnSetBeginFrameSourcePaused(false);

  root_begin_frame_source_.RemoveObserver(&observer_);

  // External BFS is unpaused => observer will be unpaused on Add.
  EXPECT_BEGIN_FRAME_SOURCE_PAUSED(observer_, false);
  root_begin_frame_source_.AddObserver(&observer_);

  root_begin_frame_source_.RemoveObserver(&observer_);
}

TEST_F(BeginFrameSourceWebViewTest, ChildBeginFrame) {
  begin_frame_source_.SetParentSource(&root_begin_frame_source_);

  EXPECT_FALSE(test_begin_frame_source_.needs_begin_frames());

  // Send BeginFrame without observer.
  auto args1 = BeginFrameArgsForTesting(1);
  test_begin_frame_source_.OnBeginFrame(args1);

  begin_frame_source_.AddObserver(&observer_);
  EXPECT_TRUE(test_begin_frame_source_.needs_begin_frames());

  // Send BeginFrame with observer, verify it gets it.
  auto args2 = BeginFrameArgsForTesting(2);
  EXPECT_BEGIN_FRAME_ARGS_USED(observer_, args2);
  test_begin_frame_source_.OnBeginFrame(args2);

  begin_frame_source_.RemoveObserver(&observer_);
  EXPECT_FALSE(test_begin_frame_source_.needs_begin_frames());
}

TEST_F(BeginFrameSourceWebViewTest, ChildPausedWithObservers) {
  begin_frame_source_.SetParentSource(&root_begin_frame_source_);

  // External BFS is not paused => observer will be not paused on Add.
  EXPECT_BEGIN_FRAME_SOURCE_PAUSED(observer_, false);
  begin_frame_source_.AddObserver(&observer_);

  // External BFS paused => observer will be paused.
  EXPECT_BEGIN_FRAME_SOURCE_PAUSED(observer_, true);
  test_begin_frame_source_.OnSetBeginFrameSourcePaused(true);

  // External BFS unpaused => observer will be unpaused.
  EXPECT_BEGIN_FRAME_SOURCE_PAUSED(observer_, false);
  test_begin_frame_source_.OnSetBeginFrameSourcePaused(false);
  begin_frame_source_.RemoveObserver(&observer_);
}

TEST_F(BeginFrameSourceWebViewTest, ChildPausedWithoutObservers) {
  begin_frame_source_.SetParentSource(&root_begin_frame_source_);
  test_begin_frame_source_.OnSetBeginFrameSourcePaused(true);

  // External BFS is paused => observer will be paused on Add.
  EXPECT_BEGIN_FRAME_SOURCE_PAUSED(observer_, true);
  begin_frame_source_.AddObserver(&observer_);

  // External BFS unpaused => observer will be unpaused.
  EXPECT_BEGIN_FRAME_SOURCE_PAUSED(observer_, false);
  test_begin_frame_source_.OnSetBeginFrameSourcePaused(false);

  begin_frame_source_.RemoveObserver(&observer_);

  // External BFS is unpaused => observer will be unpaused on Add.
  EXPECT_BEGIN_FRAME_SOURCE_PAUSED(observer_, false);
  begin_frame_source_.AddObserver(&observer_);

  begin_frame_source_.RemoveObserver(&observer_);
}

TEST_F(BeginFrameSourceWebViewTest, ChildPausedNoParent) {
  // BFS doesn't have parent so it is paused => observer will be paused on Add.
  EXPECT_BEGIN_FRAME_SOURCE_PAUSED(observer_, true);
  begin_frame_source_.AddObserver(&observer_);

  // As there is no parent we are always paused, so we don't expect the call.
  EXPECT_CALL(observer_, OnBeginFrameSourcePausedChanged(testing::_)).Times(0);
  test_begin_frame_source_.OnSetBeginFrameSourcePaused(true);

  // As there is no parent we are always paused, so we don't expect the call.
  EXPECT_CALL(observer_, OnBeginFrameSourcePausedChanged(testing::_)).Times(0);
  test_begin_frame_source_.OnSetBeginFrameSourcePaused(false);

  begin_frame_source_.RemoveObserver(&observer_);
}

TEST_F(BeginFrameSourceWebViewTest, ChildPauseChangeOnSetParent) {
  // BFS doesn't have parent so it is paused => observer will be paused on Add.
  EXPECT_BEGIN_FRAME_SOURCE_PAUSED(observer_, true);
  begin_frame_source_.AddObserver(&observer_);

  // We set parent and it's unpaused => observer will be unpaused.
  EXPECT_BEGIN_FRAME_SOURCE_PAUSED(observer_, false);
  begin_frame_source_.SetParentSource(&root_begin_frame_source_);

  // We remove parent so BFS becomes paused => observer will be paused.
  EXPECT_BEGIN_FRAME_SOURCE_PAUSED(observer_, true);
  begin_frame_source_.SetParentSource(nullptr);

  test_begin_frame_source_.OnSetBeginFrameSourcePaused(true);

  // We set parent and it's paused => observer will be paused.
  // As it's already paused we don't expect the call.
  EXPECT_CALL(observer_, OnBeginFrameSourcePausedChanged(testing::_)).Times(0);
  begin_frame_source_.SetParentSource(&root_begin_frame_source_);

  begin_frame_source_.RemoveObserver(&observer_);
}

TEST_F(BeginFrameSourceWebViewTest, Reentrancy) {
  begin_frame_source_.SetParentSource(&root_begin_frame_source_);
  begin_frame_source_.AddObserver(&observer_);

  // Re-Add observer inside OnBeginFrame so it will trigger missed BeginFrame
  EXPECT_CALL(observer_, OnBeginFrame(testing::_))
      .WillRepeatedly(testing::Invoke([&](const viz::BeginFrameArgs& args) {
        if (args.type == viz::BeginFrameArgs::MISSED)
          return;
        begin_frame_source_.RemoveObserver(&observer_);
        begin_frame_source_.AddObserver(&observer_);
      }));

  test_begin_frame_source_.OnBeginFrame(BeginFrameArgsForTesting(1));

  begin_frame_source_.RemoveObserver(&observer_);
}

}  // namespace android_webview