Consolidated Making MockMediaPlaylists

It was the same code to make the media playlists for each test, so this
change moves that logic into common functions.

Change-Id: Ifb8680247ad4335332251815a2db5d56d28151aa
This commit is contained in:
Aaron Vaage 2018-01-26 15:53:58 -08:00
parent 48b7282257
commit ea96b72d6a
1 changed files with 112 additions and 159 deletions

View File

@ -17,14 +17,14 @@
namespace shaka { namespace shaka {
namespace hls { namespace hls {
using base::FilePath;
using ::testing::_;
using ::testing::AtLeast; using ::testing::AtLeast;
using ::testing::NotNull; using ::testing::NotNull;
using ::testing::Return; using ::testing::Return;
using ::testing::ReturnRef; using ::testing::ReturnRef;
using ::testing::SetArgPointee; using ::testing::SetArgPointee;
using ::testing::StrEq; using ::testing::StrEq;
using ::testing::_;
using base::FilePath;
namespace { namespace {
const char kDefaultMasterPlaylistName[] = "playlist.m3u8"; const char kDefaultMasterPlaylistName[] = "playlist.m3u8";
@ -32,6 +32,58 @@ const char kDefaultLanguage[] = "en";
const uint32_t kWidth = 800; const uint32_t kWidth = 800;
const uint32_t kHeight = 600; const uint32_t kHeight = 600;
const HlsPlaylistType kVodPlaylist = HlsPlaylistType::kVod; const HlsPlaylistType kVodPlaylist = HlsPlaylistType::kVod;
std::unique_ptr<MockMediaPlaylist> CreateVideoPlaylist(
const std::string& filename,
const std::string& codec,
uint64_t bitrate) {
const char kNoName[] = "";
const char kNoGroup[] = "";
std::unique_ptr<MockMediaPlaylist> playlist(
new MockMediaPlaylist(kVodPlaylist, filename, kNoName, kNoGroup));
playlist->SetStreamTypeForTesting(
MediaPlaylist::MediaPlaylistStreamType::kVideo);
playlist->SetCodecForTesting(codec);
EXPECT_CALL(*playlist, Bitrate())
.Times(AtLeast(1))
.WillRepeatedly(Return(bitrate));
EXPECT_CALL(*playlist, GetDisplayResolution(NotNull(), NotNull()))
.WillRepeatedly(DoAll(SetArgPointee<0>(kWidth), SetArgPointee<1>(kHeight),
Return(true)));
return playlist;
}
std::unique_ptr<MockMediaPlaylist> CreateAudioPlaylist(
const std::string& filename,
const std::string& name,
const std::string& group,
const std::string& codec,
const std::string& language,
uint64_t channels,
uint64_t bitrate) {
// Note that audiocodecs should match for different audio tracks with same
// group ID.
std::unique_ptr<MockMediaPlaylist> playlist(
new MockMediaPlaylist(kVodPlaylist, filename, name, group));
EXPECT_CALL(*playlist, GetLanguage()).WillRepeatedly(Return(language));
EXPECT_CALL(*playlist, GetNumChannels()).WillRepeatedly(Return(channels));
playlist->SetStreamTypeForTesting(
MediaPlaylist::MediaPlaylistStreamType::kAudio);
playlist->SetCodecForTesting(codec);
EXPECT_CALL(*playlist, Bitrate())
.Times(AtLeast(1))
.WillRepeatedly(Return(bitrate));
EXPECT_CALL(*playlist, GetDisplayResolution(NotNull(), NotNull())).Times(0);
return playlist;
}
} // namespace } // namespace
class MasterPlaylistTest : public ::testing::Test { class MasterPlaylistTest : public ::testing::Test {
@ -44,9 +96,7 @@ class MasterPlaylistTest : public ::testing::Test {
.Append(FilePath::FromUTF8Unsafe(kDefaultMasterPlaylistName)) .Append(FilePath::FromUTF8Unsafe(kDefaultMasterPlaylistName))
.AsUTF8Unsafe()) {} .AsUTF8Unsafe()) {}
void SetUp() override { void SetUp() override { SetPackagerVersionForTesting("test"); }
SetPackagerVersionForTesting("test");
}
MasterPlaylist master_playlist_; MasterPlaylist master_playlist_;
std::string test_output_dir_; std::string test_output_dir_;
@ -60,18 +110,11 @@ TEST_F(MasterPlaylistTest, AddMediaPlaylist) {
} }
TEST_F(MasterPlaylistTest, WriteMasterPlaylistOneVideo) { TEST_F(MasterPlaylistTest, WriteMasterPlaylistOneVideo) {
std::string codec = "avc1"; const uint64_t kBitRate = 435889;
MockMediaPlaylist mock_playlist(kVodPlaylist, "media1.m3u8", "somename",
"somegroupid"); std::unique_ptr<MockMediaPlaylist> mock_playlist =
mock_playlist.SetStreamTypeForTesting( CreateVideoPlaylist("media1.m3u8", "avc1", kBitRate);
MediaPlaylist::MediaPlaylistStreamType::kVideo); master_playlist_.AddMediaPlaylist(mock_playlist.get());
mock_playlist.SetCodecForTesting(codec);
EXPECT_CALL(mock_playlist, Bitrate()).WillOnce(Return(435889));
EXPECT_CALL(mock_playlist, GetDisplayResolution(NotNull(), NotNull()))
.WillOnce(DoAll(SetArgPointee<0>(kWidth),
SetArgPointee<1>(kHeight),
Return(true)));
master_playlist_.AddMediaPlaylist(&mock_playlist);
const char kBaseUrl[] = "http://myplaylistdomain.com/"; const char kBaseUrl[] = "http://myplaylistdomain.com/";
EXPECT_TRUE(master_playlist_.WriteMasterPlaylist(kBaseUrl, test_output_dir_)); EXPECT_TRUE(master_playlist_.WriteMasterPlaylist(kBaseUrl, test_output_dir_));
@ -90,70 +133,36 @@ TEST_F(MasterPlaylistTest, WriteMasterPlaylistOneVideo) {
} }
TEST_F(MasterPlaylistTest, WriteMasterPlaylistVideoAndAudio) { TEST_F(MasterPlaylistTest, WriteMasterPlaylistVideoAndAudio) {
const uint64_t kVideo1BitRate = 300000;
const uint64_t kVideo2BitRate = 700000;
const uint64_t kAudio1BitRate = 50000;
const uint64_t kAudio2BitRate = 60000;
const uint64_t kAudio1Channels = 2;
const uint64_t kAudio2Channels = 5;
// First video, sd.m3u8. // First video, sd.m3u8.
std::string sd_video_codec = "sdvideocodec"; std::unique_ptr<MockMediaPlaylist> sd_video_playlist =
MockMediaPlaylist sd_video_playlist(kVodPlaylist, "sd.m3u8", "somename", CreateVideoPlaylist("sd.m3u8", "sdvideocodec", kVideo1BitRate);
"somegroupid"); master_playlist_.AddMediaPlaylist(sd_video_playlist.get());
sd_video_playlist.SetStreamTypeForTesting(
MediaPlaylist::MediaPlaylistStreamType::kVideo);
sd_video_playlist.SetCodecForTesting(sd_video_codec);
EXPECT_CALL(sd_video_playlist, Bitrate())
.Times(AtLeast(1))
.WillRepeatedly(Return(300000));
EXPECT_CALL(sd_video_playlist, GetDisplayResolution(NotNull(), NotNull()))
.WillRepeatedly(DoAll(SetArgPointee<0>(kWidth),
SetArgPointee<1>(kHeight),
Return(true)));
master_playlist_.AddMediaPlaylist(&sd_video_playlist);
// Second video, hd.m3u8. // Second video, hd.m3u8.
std::string hd_video_codec = "hdvideocodec"; std::unique_ptr<MockMediaPlaylist> hd_video_playlist =
MockMediaPlaylist hd_video_playlist(kVodPlaylist, "hd.m3u8", "somename", CreateVideoPlaylist("hd.m3u8", "hdvideocodec", kVideo2BitRate);
"somegroupid"); master_playlist_.AddMediaPlaylist(hd_video_playlist.get());
hd_video_playlist.SetStreamTypeForTesting(
MediaPlaylist::MediaPlaylistStreamType::kVideo);
hd_video_playlist.SetCodecForTesting(hd_video_codec);
EXPECT_CALL(hd_video_playlist, Bitrate())
.Times(AtLeast(1))
.WillRepeatedly(Return(700000));
EXPECT_CALL(hd_video_playlist, GetDisplayResolution(NotNull(), NotNull()))
.WillRepeatedly(DoAll(SetArgPointee<0>(kWidth),
SetArgPointee<1>(kHeight),
Return(true)));
master_playlist_.AddMediaPlaylist(&hd_video_playlist);
// First audio, english.m3u8. // First audio, english.m3u8.
// Note that audiocodecs should match for different audio tracks with same std::unique_ptr<MockMediaPlaylist> english_playlist =
// group ID. CreateAudioPlaylist("eng.m3u8", "english", "audiogroup", "audiocodec",
std::string audio_codec = "audiocodec"; "en", kAudio1Channels, kAudio1BitRate);
MockMediaPlaylist english_playlist(kVodPlaylist, "eng.m3u8", "english", master_playlist_.AddMediaPlaylist(english_playlist.get());
"audiogroup");
EXPECT_CALL(english_playlist, GetLanguage()).WillRepeatedly(Return("en"));
EXPECT_CALL(english_playlist, GetNumChannels()).WillRepeatedly(Return(2));
english_playlist.SetStreamTypeForTesting(
MediaPlaylist::MediaPlaylistStreamType::kAudio);
english_playlist.SetCodecForTesting(audio_codec);
EXPECT_CALL(english_playlist, Bitrate())
.Times(AtLeast(1))
.WillRepeatedly(Return(50000));
EXPECT_CALL(english_playlist, GetDisplayResolution(NotNull(), NotNull()))
.Times(0);
master_playlist_.AddMediaPlaylist(&english_playlist);
// Second audio, spanish.m3u8. // Second audio, spanish.m3u8.
MockMediaPlaylist spanish_playlist(kVodPlaylist, "spa.m3u8", "espanol", std::unique_ptr<MockMediaPlaylist> spanish_playlist =
"audiogroup"); CreateAudioPlaylist("spa.m3u8", "espanol", "audiogroup", "audiocodec",
EXPECT_CALL(spanish_playlist, GetLanguage()).WillRepeatedly(Return("es")); "es", kAudio2Channels, kAudio2BitRate);
EXPECT_CALL(spanish_playlist, GetNumChannels()).WillRepeatedly(Return(5)); master_playlist_.AddMediaPlaylist(spanish_playlist.get());
spanish_playlist.SetStreamTypeForTesting(
MediaPlaylist::MediaPlaylistStreamType::kAudio);
spanish_playlist.SetCodecForTesting(audio_codec);
EXPECT_CALL(spanish_playlist, Bitrate())
.Times(AtLeast(1))
.WillRepeatedly(Return(60000));
EXPECT_CALL(spanish_playlist, GetDisplayResolution(NotNull(), NotNull()))
.Times(0);
master_playlist_.AddMediaPlaylist(&spanish_playlist);
const char kBaseUrl[] = "http://playlists.org/"; const char kBaseUrl[] = "http://playlists.org/";
EXPECT_TRUE(master_playlist_.WriteMasterPlaylist(kBaseUrl, test_output_dir_)); EXPECT_TRUE(master_playlist_.WriteMasterPlaylist(kBaseUrl, test_output_dir_));
@ -182,53 +191,30 @@ TEST_F(MasterPlaylistTest, WriteMasterPlaylistVideoAndAudio) {
} }
TEST_F(MasterPlaylistTest, WriteMasterPlaylistMultipleAudioGroups) { TEST_F(MasterPlaylistTest, WriteMasterPlaylistMultipleAudioGroups) {
// First video, video.m3u8. const uint64_t kVideoBitRate = 300000;
std::string video_codec = "videocodec";
MockMediaPlaylist video_playlist(kVodPlaylist, "video.m3u8", "somename", const uint64_t kAudio1BitRate = 50000;
"somegroupid"); const uint64_t kAudio2BitRate = 100000;
video_playlist.SetStreamTypeForTesting(
MediaPlaylist::MediaPlaylistStreamType::kVideo); const uint64_t kAudio1Channels = 1;
video_playlist.SetCodecForTesting(video_codec); const uint64_t kAudio2Channels = 8;
EXPECT_CALL(video_playlist, Bitrate())
.Times(AtLeast(1)) // First video, sd.m3u8.
.WillRepeatedly(Return(300000)); std::unique_ptr<MockMediaPlaylist> video_playlist =
EXPECT_CALL(video_playlist, GetDisplayResolution(NotNull(), NotNull())) CreateVideoPlaylist("video.m3u8", "videocodec", kVideoBitRate);
.WillRepeatedly(DoAll(SetArgPointee<0>(kWidth), master_playlist_.AddMediaPlaylist(video_playlist.get());
SetArgPointee<1>(kHeight),
Return(true)));
master_playlist_.AddMediaPlaylist(&video_playlist);
// First audio, eng_lo.m3u8. // First audio, eng_lo.m3u8.
std::string audio_codec_lo = "audiocodec_lo"; std::unique_ptr<MockMediaPlaylist> eng_lo_playlist = CreateAudioPlaylist(
MockMediaPlaylist eng_lo_playlist(kVodPlaylist, "eng_lo.m3u8", "english_lo", "eng_lo.m3u8", "english_lo", "audio_lo", "audiocodec_lo", "en",
"audio_lo"); kAudio1Channels, kAudio1BitRate);
EXPECT_CALL(eng_lo_playlist, GetLanguage()).WillRepeatedly(Return("en")); master_playlist_.AddMediaPlaylist(eng_lo_playlist.get());
EXPECT_CALL(eng_lo_playlist, GetNumChannels()).WillRepeatedly(Return(1));
eng_lo_playlist.SetStreamTypeForTesting(
MediaPlaylist::MediaPlaylistStreamType::kAudio);
eng_lo_playlist.SetCodecForTesting(audio_codec_lo);
EXPECT_CALL(eng_lo_playlist, Bitrate())
.Times(AtLeast(1))
.WillRepeatedly(Return(50000));
EXPECT_CALL(eng_lo_playlist, GetDisplayResolution(NotNull(), NotNull()))
.Times(0);
master_playlist_.AddMediaPlaylist(&eng_lo_playlist);
// Second audio, eng_hi.m3u8. // Second audio, eng_hi.m3u8.
std::string audio_codec_hi = "audiocodec_hi"; std::unique_ptr<MockMediaPlaylist> eng_hi_playlist = CreateAudioPlaylist(
MockMediaPlaylist eng_hi_playlist(kVodPlaylist, "eng_hi.m3u8", "english_hi", "eng_hi.m3u8", "english_hi", "audio_hi", "audiocodec_hi", "en",
"audio_hi"); kAudio2Channels, kAudio2BitRate);
EXPECT_CALL(eng_hi_playlist, GetLanguage()).WillRepeatedly(Return("en")); master_playlist_.AddMediaPlaylist(eng_hi_playlist.get());
EXPECT_CALL(eng_hi_playlist, GetNumChannels()).WillRepeatedly(Return(8));
eng_hi_playlist.SetStreamTypeForTesting(
MediaPlaylist::MediaPlaylistStreamType::kAudio);
eng_hi_playlist.SetCodecForTesting(audio_codec_hi);
EXPECT_CALL(eng_hi_playlist, Bitrate())
.Times(AtLeast(1))
.WillRepeatedly(Return(100000));
EXPECT_CALL(eng_hi_playlist, GetDisplayResolution(NotNull(), NotNull()))
.Times(0);
master_playlist_.AddMediaPlaylist(&eng_hi_playlist);
const char kBaseUrl[] = "http://anydomain.com/"; const char kBaseUrl[] = "http://anydomain.com/";
EXPECT_TRUE(master_playlist_.WriteMasterPlaylist(kBaseUrl, test_output_dir_)); EXPECT_TRUE(master_playlist_.WriteMasterPlaylist(kBaseUrl, test_output_dir_));
@ -258,50 +244,18 @@ TEST_F(MasterPlaylistTest, WriteMasterPlaylistMultipleAudioGroups) {
TEST_F(MasterPlaylistTest, WriteMasterPlaylistSameAudioGroupSameLanguage) { TEST_F(MasterPlaylistTest, WriteMasterPlaylistSameAudioGroupSameLanguage) {
// First video, video.m3u8. // First video, video.m3u8.
std::string video_codec = "videocodec"; std::unique_ptr<MockMediaPlaylist> video_playlist =
MockMediaPlaylist video_playlist(kVodPlaylist, "video.m3u8", "somename", CreateVideoPlaylist("video.m3u8", "videocodec", 300000);
"somegroupid"); master_playlist_.AddMediaPlaylist(video_playlist.get());
video_playlist.SetStreamTypeForTesting(
MediaPlaylist::MediaPlaylistStreamType::kVideo);
video_playlist.SetCodecForTesting(video_codec);
EXPECT_CALL(video_playlist, Bitrate())
.Times(AtLeast(1))
.WillRepeatedly(Return(300000));
EXPECT_CALL(video_playlist, GetDisplayResolution(NotNull(), NotNull()))
.WillRepeatedly(DoAll(SetArgPointee<0>(kWidth), SetArgPointee<1>(kHeight),
Return(true)));
master_playlist_.AddMediaPlaylist(&video_playlist);
// First audio, eng_lo.m3u8. // First audio, eng_lo.m3u8.
const std::string audio_codec = "audiocodec"; std::unique_ptr<MockMediaPlaylist> eng_lo_playlist = CreateAudioPlaylist(
MockMediaPlaylist eng_lo_playlist(kVodPlaylist, "eng_lo.m3u8", "english", "eng_lo.m3u8", "english", "audio", "audiocodec", "en", 1, 50000);
"audio"); master_playlist_.AddMediaPlaylist(eng_lo_playlist.get());
EXPECT_CALL(eng_lo_playlist, GetLanguage()).WillRepeatedly(Return("en"));
EXPECT_CALL(eng_lo_playlist, GetNumChannels()).WillRepeatedly(Return(1));
eng_lo_playlist.SetStreamTypeForTesting(
MediaPlaylist::MediaPlaylistStreamType::kAudio);
eng_lo_playlist.SetCodecForTesting(audio_codec);
EXPECT_CALL(eng_lo_playlist, Bitrate())
.Times(AtLeast(1))
.WillRepeatedly(Return(50000));
EXPECT_CALL(eng_lo_playlist, GetDisplayResolution(NotNull(), NotNull()))
.Times(0);
master_playlist_.AddMediaPlaylist(&eng_lo_playlist);
// Second audio, eng_hi.m3u8. std::unique_ptr<MockMediaPlaylist> eng_hi_playlist = CreateAudioPlaylist(
MockMediaPlaylist eng_hi_playlist(kVodPlaylist, "eng_hi.m3u8", "english", "eng_hi.m3u8", "english", "audio", "audiocodec", "en", 8, 100000);
"audio"); master_playlist_.AddMediaPlaylist(eng_hi_playlist.get());
EXPECT_CALL(eng_hi_playlist, GetLanguage()).WillRepeatedly(Return("en"));
EXPECT_CALL(eng_hi_playlist, GetNumChannels()).WillRepeatedly(Return(8));
eng_hi_playlist.SetStreamTypeForTesting(
MediaPlaylist::MediaPlaylistStreamType::kAudio);
eng_hi_playlist.SetCodecForTesting(audio_codec);
EXPECT_CALL(eng_hi_playlist, Bitrate())
.Times(AtLeast(1))
.WillRepeatedly(Return(100000));
EXPECT_CALL(eng_hi_playlist, GetDisplayResolution(NotNull(), NotNull()))
.Times(0);
master_playlist_.AddMediaPlaylist(&eng_hi_playlist);
const char kBaseUrl[] = "http://anydomain.com/"; const char kBaseUrl[] = "http://anydomain.com/";
EXPECT_TRUE(master_playlist_.WriteMasterPlaylist(kBaseUrl, test_output_dir_)); EXPECT_TRUE(master_playlist_.WriteMasterPlaylist(kBaseUrl, test_output_dir_));
@ -325,6 +279,5 @@ TEST_F(MasterPlaylistTest, WriteMasterPlaylistSameAudioGroupSameLanguage) {
ASSERT_EQ(expected, actual); ASSERT_EQ(expected, actual);
} }
} // namespace hls } // namespace hls
} // namespace shaka } // namespace shaka