File: delegating_decoder_buffer_provider_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (130 lines) | stat: -rw-r--r-- 4,689 bytes parent folder | download | duplicates (11)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/cast_streaming/renderer/web_codecs/delegating_decoder_buffer_provider.h"

#include <memory>

#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "components/cast_streaming/renderer/public/decoder_buffer_provider.h"
#include "media/base/audio_decoder_config.h"
#include "media/base/decoder_buffer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace cast_streaming::webcodecs {
namespace {

class MockDecoderBufferProvider
    : public DecoderBufferProvider<media::AudioDecoderConfig> {
 public:
  using NewBufferCb =
      typename DecoderBufferProvider<media::AudioDecoderConfig>::NewBufferCb;
  using GetConfigCb =
      typename DecoderBufferProvider<media::AudioDecoderConfig>::GetConfigCb;
  using DeletionCb =
      typename DecoderBufferProvider<media::AudioDecoderConfig>::DeletionCb;

  MockDecoderBufferProvider() : weak_factory_(this) {}
  ~MockDecoderBufferProvider() override = default;

  MOCK_CONST_METHOD0(IsValid, bool());
  MOCK_CONST_METHOD1(GetConfigAsync, void(GetConfigCb));
  MOCK_METHOD1(ReadBufferAsync, void(NewBufferCb));
  MOCK_METHOD1(SetInvalidationCallback, void(DeletionCb));

  base::WeakPtrFactory<MockDecoderBufferProvider> weak_factory_;
};

class MockClient {
 public:
  MOCK_METHOD1(OnConfig, void(media::AudioDecoderConfig));
  MOCK_METHOD1(OnBuffer, void(scoped_refptr<media::DecoderBuffer>));
  MOCK_METHOD0(OnDeletion, void());
};

}  // namespace

class DelegatingDecoderBufferProviderTest : public testing::Test {
 public:
  DelegatingDecoderBufferProviderTest()
      : provider_impl_(
            std::make_unique<testing::StrictMock<MockDecoderBufferProvider>>()),
        provider_(std::make_unique<
                  DelegatingDecoderBufferProvider<media::AudioDecoderConfig>>(
            provider_impl_->weak_factory_.GetWeakPtr(),
            task_environment_.GetMainThreadTaskRunner())) {}

  ~DelegatingDecoderBufferProviderTest() override = default;

 protected:
  base::test::SingleThreadTaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};

  std::unique_ptr<testing::StrictMock<MockDecoderBufferProvider>>
      provider_impl_;
  testing::StrictMock<MockClient> client_;
  std::unique_ptr<DelegatingDecoderBufferProvider<media::AudioDecoderConfig>>
      provider_;
};

TEST_F(DelegatingDecoderBufferProviderTest, GetConfigAsyncSucceeds) {
  EXPECT_TRUE(provider_->IsValid());
  EXPECT_CALL(*provider_impl_, GetConfigAsync(testing::_));
  provider_->GetConfigAsync(
      base::BindOnce(&MockClient::OnConfig, base::Unretained(&client_)));
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(provider_->IsValid());
}

TEST_F(DelegatingDecoderBufferProviderTest, GetConfigAsyncFails) {
  provider_impl_.reset();
  EXPECT_TRUE(provider_->IsValid());
  provider_->GetConfigAsync(
      base::BindOnce(&MockClient::OnConfig, base::Unretained(&client_)));
  task_environment_.RunUntilIdle();
  EXPECT_FALSE(provider_->IsValid());
}

TEST_F(DelegatingDecoderBufferProviderTest, ReadBufferAsyncSucceeds) {
  EXPECT_TRUE(provider_->IsValid());
  EXPECT_CALL(*provider_impl_, ReadBufferAsync(testing::_));
  provider_->ReadBufferAsync(
      base::BindOnce(&MockClient::OnBuffer, base::Unretained(&client_)));
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(provider_->IsValid());
}

TEST_F(DelegatingDecoderBufferProviderTest, ReadBufferAsyncFails) {
  provider_impl_.reset();
  EXPECT_TRUE(provider_->IsValid());
  provider_->ReadBufferAsync(
      base::BindOnce(&MockClient::OnBuffer, base::Unretained(&client_)));
  task_environment_.RunUntilIdle();
  EXPECT_FALSE(provider_->IsValid());
}

TEST_F(DelegatingDecoderBufferProviderTest, SetInvalidationCallbackSucceeds) {
  EXPECT_TRUE(provider_->IsValid());
  EXPECT_CALL(*provider_impl_, SetInvalidationCallback(testing::_));
  provider_->SetInvalidationCallback(
      base::BindOnce(&MockClient::OnDeletion, base::Unretained(&client_)));
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(provider_->IsValid());
}

TEST_F(DelegatingDecoderBufferProviderTest, SetInvalidationCallbackFails) {
  provider_impl_.reset();
  EXPECT_CALL(client_, OnDeletion());
  EXPECT_TRUE(provider_->IsValid());
  provider_->SetInvalidationCallback(
      base::BindOnce(&MockClient::OnDeletion, base::Unretained(&client_)));
  task_environment_.RunUntilIdle();
  EXPECT_FALSE(provider_->IsValid());
}

}  // namespace cast_streaming::webcodecs