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

media / base / media_url_demuxer_unittest.cc [blame]

// Copyright 2016 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/base/media_url_demuxer.h"

#include <memory>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/task_environment.h"
#include "net/cookies/site_for_cookies.h"
#include "net/storage_access_api/status.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media {

class MediaUrlDemuxerTest : public testing::Test {
 public:
  MediaUrlDemuxerTest()
      : default_media_url_(GURL("http://example.com/file.mp4")),
        default_first_party_url_(GURL("http://example.com/")) {}

  MediaUrlDemuxerTest(const MediaUrlDemuxerTest&) = delete;
  MediaUrlDemuxerTest& operator=(const MediaUrlDemuxerTest&) = delete;

  void InitializeTest(const GURL& media_url,
                      const GURL& first_party,
                      bool allow_credentials) {
    demuxer_ = std::make_unique<MediaUrlDemuxer>(
        base::SingleThreadTaskRunner::GetCurrentDefault(), media_url,
        net::SiteForCookies::FromUrl(first_party),
        url::Origin::Create(first_party), net::StorageAccessApiStatus::kNone,
        allow_credentials, false);
  }

  void InitializeTest() {
    InitializeTest(default_media_url_, default_first_party_url_, true);
  }

  void VerifyCallbackOk(PipelineStatus status) {
    EXPECT_EQ(PIPELINE_OK, status);
  }

  const GURL default_media_url_;
  const GURL default_first_party_url_;
  std::unique_ptr<Demuxer> demuxer_;

  // Necessary, or else base::SingleThreadTaskRunner::GetCurrentDefault() fails.
  base::test::SingleThreadTaskEnvironment task_environment_;
};

TEST_F(MediaUrlDemuxerTest, BaseCase) {
  InitializeTest();

  EXPECT_EQ(MediaResource::Type::KUrl, demuxer_->GetType());

  const MediaUrlParams& params = demuxer_->GetMediaUrlParams();
  EXPECT_EQ(default_media_url_, params.media_url);
  EXPECT_TRUE(net::SiteForCookies::FromUrl(default_first_party_url_)
                  .IsEquivalent(params.site_for_cookies));
  EXPECT_EQ(true, params.allow_credentials);
}

TEST_F(MediaUrlDemuxerTest, AcceptsEmptyStrings) {
  InitializeTest(GURL(), GURL(), false);

  const MediaUrlParams& params = demuxer_->GetMediaUrlParams();
  EXPECT_EQ(GURL(), params.media_url);
  EXPECT_TRUE(net::SiteForCookies::FromUrl(GURL()).IsEquivalent(
      params.site_for_cookies));
  EXPECT_EQ(false, params.allow_credentials);
}

TEST_F(MediaUrlDemuxerTest, InitializeReturnsPipelineOk) {
  InitializeTest();
  demuxer_->Initialize(nullptr,
                       base::BindOnce(&MediaUrlDemuxerTest::VerifyCallbackOk,
                                      base::Unretained(this)));

  base::RunLoop().RunUntilIdle();
}

TEST_F(MediaUrlDemuxerTest, SeekReturnsPipelineOk) {
  InitializeTest();
  demuxer_->Seek(base::TimeDelta(),
                 base::BindOnce(&MediaUrlDemuxerTest::VerifyCallbackOk,
                                base::Unretained(this)));

  base::RunLoop().RunUntilIdle();
}

}  // namespace media