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

media / gpu / mac / video_toolbox_output_queue_unittest.cc [blame]

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

#include "media/gpu/mac/video_toolbox_output_queue.h"

#include "base/functional/bind.h"
#include "base/memory/scoped_refptr.h"
#include "base/test/task_environment.h"
#include "media/base/decoder_status.h"
#include "media/base/video_frame.h"
#include "media/gpu/codec_picture.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;

namespace media {

namespace {

MATCHER_P(StatusEq, code, "") {
  return arg.code() == code;
}

}  // namespace

class VideoToolboxOutputQueueTest : public testing::Test {
 public:
  VideoToolboxOutputQueueTest() {
    output_queue_.SetOutputCB(base::BindRepeating(
        &VideoToolboxOutputQueueTest::OnOutput, base::Unretained(this)));
  }

  ~VideoToolboxOutputQueueTest() override = default;

 protected:
  MOCK_METHOD1(OnOutput, void(scoped_refptr<VideoFrame>));
  MOCK_METHOD1(OnFlush, void(DecoderStatus));

  void Flush() {
    output_queue_.Flush(base::BindOnce(&VideoToolboxOutputQueueTest::OnFlush,
                                       base::Unretained(this)));
  }

  base::test::TaskEnvironment task_environment_;
  VideoToolboxOutputQueue output_queue_{
      task_environment_.GetMainThreadTaskRunner()};
};

TEST_F(VideoToolboxOutputQueueTest, Construct) {}

TEST_F(VideoToolboxOutputQueueTest, Basic) {
  auto pic = base::MakeRefCounted<CodecPicture>();
  auto frame = VideoFrame::CreateEOSFrame();

  EXPECT_CALL(*this, OnOutput(frame));

  output_queue_.SchedulePicture(pic);
  output_queue_.FulfillPicture(pic, frame);

  task_environment_.RunUntilIdle();
}

TEST_F(VideoToolboxOutputQueueTest, Reordered) {
  auto pic1 = base::MakeRefCounted<CodecPicture>();
  auto pic2 = base::MakeRefCounted<CodecPicture>();
  auto frame1 = VideoFrame::CreateEOSFrame();
  auto frame2 = VideoFrame::CreateEOSFrame();

  testing::InSequence in_sequence;
  EXPECT_CALL(*this, OnOutput(frame2));
  EXPECT_CALL(*this, OnOutput(frame1));

  output_queue_.SchedulePicture(pic2);
  output_queue_.FulfillPicture(pic1, frame1);
  output_queue_.FulfillPicture(pic2, frame2);
  output_queue_.SchedulePicture(pic1);

  task_environment_.RunUntilIdle();
}

TEST_F(VideoToolboxOutputQueueTest, Flush) {
  auto pic = base::MakeRefCounted<CodecPicture>();
  auto frame = VideoFrame::CreateEOSFrame();

  testing::InSequence in_sequence;
  EXPECT_CALL(*this, OnOutput(frame));
  EXPECT_CALL(*this, OnFlush(_));

  output_queue_.SchedulePicture(pic);
  Flush();
  output_queue_.FulfillPicture(pic, frame);

  task_environment_.RunUntilIdle();
}

TEST_F(VideoToolboxOutputQueueTest, Reset) {
  auto pic1 = base::MakeRefCounted<CodecPicture>();
  auto pic2 = base::MakeRefCounted<CodecPicture>();
  auto frame1 = VideoFrame::CreateEOSFrame();
  auto frame2 = VideoFrame::CreateEOSFrame();

  output_queue_.SchedulePicture(pic2);
  output_queue_.FulfillPicture(pic1, frame1);

  EXPECT_CALL(*this, OnFlush(StatusEq(DecoderStatus::Codes::kAborted)));

  Flush();
  output_queue_.Reset(DecoderStatus::Codes::kAborted);

  EXPECT_CALL(*this, OnOutput(_)).Times(0);

  output_queue_.FulfillPicture(pic2, frame2);
  output_queue_.SchedulePicture(pic1);

  task_environment_.RunUntilIdle();
}

}  // namespace media