// Copyright 2014 Google LLC. All rights reserved. // // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd #include #include #include #include #include #include #include #include namespace shaka { using ::testing::_; using ::testing::Invoke; using ::testing::Return; namespace { class TestMpdNotifierFactory : public MpdNotifierFactory { public: TestMpdNotifierFactory() {} ~TestMpdNotifierFactory() override {} // So sad that this method cannot be mocked (gmock errors at compile time). // Also (probably) this version of gmock does not support returning // std::unique_ptr. // For now we only need to return MockMpdNotifier() with these set of // expectations for all the tests. std::unique_ptr Create(const MpdOptions& mpd_options) override { EXPECT_EQ(expected_base_urls_, mpd_options.mpd_params.base_urls); std::unique_ptr mock_notifier( new MockMpdNotifier(mpd_options)); EXPECT_CALL(*mock_notifier, Init()).WillOnce(Return(true)); EXPECT_CALL(*mock_notifier, NotifyNewContainer(_, _)) .Times(2) .WillRepeatedly(Return(true)); EXPECT_CALL(*mock_notifier, Flush()).WillOnce(Return(true)); return mock_notifier; } void SetExpectedBaseUrls(const std::vector& base_urls) { expected_base_urls_ = base_urls; } std::vector expected_base_urls_; }; } // namespace class MpdWriterTest : public ::testing::Test { protected: // Note that MpdWriter::SetMpdNotifierFactoryForTest() is not called in SetUp. // Set expectations in the test and call it. void SetUp() override { notifier_factory_.reset(new TestMpdNotifierFactory()); } void SetMpdNotifierFactoryForTest() { mpd_writer_.SetMpdNotifierFactoryForTest(std::move(notifier_factory_)); } std::unique_ptr notifier_factory_; MpdWriter mpd_writer_; }; // Verify that writing mpd to a file works. // Also check that base URLs are passed correctly. TEST_F(MpdWriterTest, WriteMpdToFile) { const char kBaseUrl1[] = "http://cdn1.mydomain.com/"; const char kBaseUrl2[] = "http://cdn2.mydomain.com/"; std::vector base_urls_; base_urls_.emplace_back(kBaseUrl1); base_urls_.emplace_back(kBaseUrl2); notifier_factory_->SetExpectedBaseUrls(base_urls_); std::filesystem::path media_info_file1 = GetTestDataFilePath(kFileNameVideoMediaInfo1); std::filesystem::path media_info_file2 = GetTestDataFilePath(kFileNameVideoMediaInfo2); SetMpdNotifierFactoryForTest(); EXPECT_TRUE(mpd_writer_.AddFile(media_info_file1.string())); EXPECT_TRUE(mpd_writer_.AddFile(media_info_file2.string())); mpd_writer_.AddBaseUrl(kBaseUrl1); mpd_writer_.AddBaseUrl(kBaseUrl2); auto temp = new TempFile(); EXPECT_TRUE(mpd_writer_.WriteMpdToFile(temp->path().c_str())); } } // namespace shaka