summaryrefslogtreecommitdiff
path: root/chromium/media/base/fallback_video_decoder_unittest.cc
blob: ecf80f36b23aacabf1ae92e00e6aeba31215987f (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
// Copyright 2018 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 <tuple>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/run_loop.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/task_environment.h"
#include "media/base/decoder_buffer.h"
#include "media/base/fallback_video_decoder.h"
#include "media/base/mock_filters.h"
#include "media/base/test_helpers.h"
#include "media/base/video_decoder.h"
#include "media/base/video_decoder_config.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest-param-test.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::base::test::RunOnceCallback;
using ::testing::_;
using ::testing::StrictMock;

namespace media {

class FallbackVideoDecoderUnittest : public ::testing::TestWithParam<bool> {
 public:
  FallbackVideoDecoderUnittest()
      : backup_decoder_(nullptr),
        preferred_decoder_(nullptr),
        fallback_decoder_(nullptr) {}

  ~FallbackVideoDecoderUnittest() override { Destroy(); }

  std::unique_ptr<VideoDecoder> MakeMockDecoderWithExpectations(
      bool is_fallback,
      bool preferred_should_succeed) {
    std::string n = is_fallback ? "Fallback" : "Preferred";
    StrictMock<MockVideoDecoder>* result = new StrictMock<MockVideoDecoder>(n);

    if (is_fallback && !preferred_should_succeed) {
      EXPECT_CALL(*result, Initialize_(_, _, _, _, _, _))
          .WillOnce(RunOnceCallback<3>(OkStatus()));
    }

    if (!is_fallback) {
      preferred_decoder_ = result;
      EXPECT_CALL(*result, Initialize_(_, _, _, _, _, _))
          .WillOnce(RunOnceCallback<3>(preferred_should_succeed
                                           ? OkStatus()
                                           : StatusCode::kCodeOnlyForTesting));
    } else {
      backup_decoder_ = result;
    }

    return std::unique_ptr<VideoDecoder>(result);
  }

  void Initialize(bool preferred_should_succeed) {
    fallback_decoder_ = new FallbackVideoDecoder(
        MakeMockDecoderWithExpectations(false, preferred_should_succeed),
        MakeMockDecoderWithExpectations(true, preferred_should_succeed));

    fallback_decoder_->Initialize(
        video_decoder_config_, false, nullptr,
        base::BindOnce([](Status status) { EXPECT_TRUE(status.is_ok()); }),
        base::DoNothing(), base::DoNothing());
  }

 protected:
  void Destroy() { std::default_delete<VideoDecoder>()(fallback_decoder_); }

  bool PreferredShouldSucceed() { return GetParam(); }

  base::test::TaskEnvironment task_environment_;

  StrictMock<MockVideoDecoder>* backup_decoder_;
  StrictMock<MockVideoDecoder>* preferred_decoder_;
  VideoDecoder* fallback_decoder_;
  VideoDecoderConfig video_decoder_config_;

 private:
  DISALLOW_COPY_AND_ASSIGN(FallbackVideoDecoderUnittest);
};

INSTANTIATE_TEST_SUITE_P(DoesPreferredInitFail,
                         FallbackVideoDecoderUnittest,
                         testing::ValuesIn({true, false}));

#define EXPECT_ON_CORRECT_DECODER(method)     \
  if (PreferredShouldSucceed())               \
    EXPECT_CALL(*preferred_decoder_, method); \
  else                                        \
    EXPECT_CALL(*backup_decoder_, method)  // Intentionally leave off semicolon.

// Do not test the name lookup; it is NOTREACHED.
TEST_P(FallbackVideoDecoderUnittest, MethodsRedirectedAsExpected) {
  Initialize(PreferredShouldSucceed());

  EXPECT_ON_CORRECT_DECODER(Decode_(_, _));
  fallback_decoder_->Decode(nullptr, base::DoNothing());

  EXPECT_ON_CORRECT_DECODER(Reset_(_));
  fallback_decoder_->Reset(base::DoNothing());

  EXPECT_ON_CORRECT_DECODER(NeedsBitstreamConversion());
  fallback_decoder_->NeedsBitstreamConversion();

  EXPECT_ON_CORRECT_DECODER(CanReadWithoutStalling());
  fallback_decoder_->CanReadWithoutStalling();

  EXPECT_ON_CORRECT_DECODER(GetMaxDecodeRequests());
  fallback_decoder_->GetMaxDecodeRequests();
}

//               │    first initialization   │   second initialization   │
//   preferred   │  preferred  │   backup    │  preferred  │   backup    │
// will succeed  │ init called │ init called │ init called │ init called │
//───────────────┼─────────────┼─────────────┼─────────────┼─────────────┤
//     false     │      ✓      │      ✓      │       x     │      ✓      │
//     true      │      ✓      │      x      │       ✓     │      ✓      │
TEST_P(FallbackVideoDecoderUnittest, ReinitializeWithPreferredFailing) {
  Initialize(PreferredShouldSucceed());

  // If we succeedd the first time, it should still be alive.
  if (PreferredShouldSucceed()) {  // fail initialization
    EXPECT_CALL(*preferred_decoder_, Initialize_(_, _, _, _, _, _))
        .WillOnce(RunOnceCallback<3>(StatusCode::kCodeOnlyForTesting));
  }
  EXPECT_CALL(*backup_decoder_, Initialize_(_, _, _, _, _, _))
      .WillOnce(RunOnceCallback<3>(OkStatus()));

  fallback_decoder_->Initialize(
      video_decoder_config_, false, nullptr,
      base::BindOnce([](Status status) { EXPECT_TRUE(status.is_ok()); }),
      base::DoNothing(), base::DoNothing());
}

//               │    first initialization   │   second initialization   │
//   preferred   │  preferred  │   backup    │  preferred  │   backup    │
// will succeed  │ init called │ init called │ init called │ init called │
//───────────────┼─────────────┼─────────────┼─────────────┼─────────────┤
//     false     │      ✓      │      ✓      │       x     │      ✓      │
//     true      │      ✓      │      x      │       ✓     │      x      │
TEST_P(FallbackVideoDecoderUnittest, ReinitializeWithPreferredSuccessful) {
  Initialize(PreferredShouldSucceed());

  // If we succeedd the first time, it should still be alive.
  if (PreferredShouldSucceed()) {
    EXPECT_CALL(*preferred_decoder_, Initialize_(_, _, _, _, _, _))
        .WillOnce(RunOnceCallback<3>(OkStatus()));  // pass initialization
  } else {
    // Otherwise, preferred was deleted, and we only backup still exists.
    EXPECT_CALL(*backup_decoder_, Initialize_(_, _, _, _, _, _))
        .WillOnce(RunOnceCallback<3>(OkStatus()));
  }

  fallback_decoder_->Initialize(
      video_decoder_config_, false, nullptr,
      base::BindOnce([](Status status) { EXPECT_TRUE(status.is_ok()); }),
      base::DoNothing(), base::DoNothing());
}

}  // namespace media