Test breaking the mojo connection to MojoCdm
Improve the testing on MojoCdm by adding a MockCdm. This allows for
testing that:
- resolving the promise works
- rejecting the promise works
- closing the connection before a call works
- closing the connection while executing the call works.
BUG=671362
TEST=new tests pass
Review-Url: https://codereview.chromium.org/2592913002
Cr-Commit-Position: refs/heads/master@{#442104}
diff --git a/media/base/mock_filters.cc b/media/base/mock_filters.cc
index 1292c8d..c088b57 100644
--- a/media/base/mock_filters.cc
+++ b/media/base/mock_filters.cc
@@ -9,6 +9,7 @@
using ::testing::_;
using ::testing::Return;
using ::testing::SaveArg;
+using ::testing::StrictMock;
MATCHER(NotEmpty, "") {
return !arg.empty();
@@ -180,6 +181,118 @@
MarkPromiseSettled();
}
+MockCdm::MockCdm(const SessionMessageCB& session_message_cb,
+ const SessionClosedCB& session_closed_cb,
+ const SessionKeysChangeCB& session_keys_change_cb,
+ const SessionExpirationUpdateCB& session_expiration_update_cb)
+ : session_message_cb_(session_message_cb),
+ session_closed_cb_(session_closed_cb),
+ session_keys_change_cb_(session_keys_change_cb),
+ session_expiration_update_cb_(session_expiration_update_cb) {}
+
+MockCdm::~MockCdm() {}
+
+void MockCdm::SetServerCertificate(const std::vector<uint8_t>& certificate,
+ std::unique_ptr<SimpleCdmPromise> promise) {
+ OnSetServerCertificate(certificate, promise);
+}
+
+void MockCdm::CreateSessionAndGenerateRequest(
+ CdmSessionType session_type,
+ EmeInitDataType init_data_type,
+ const std::vector<uint8_t>& init_data,
+ std::unique_ptr<NewSessionCdmPromise> promise) {
+ OnCreateSessionAndGenerateRequest(session_type, init_data_type, init_data,
+ promise);
+}
+
+void MockCdm::LoadSession(CdmSessionType session_type,
+ const std::string& session_id,
+ std::unique_ptr<NewSessionCdmPromise> promise) {
+ OnLoadSession(session_type, session_id, promise);
+}
+
+void MockCdm::UpdateSession(const std::string& session_id,
+ const std::vector<uint8_t>& response,
+ std::unique_ptr<SimpleCdmPromise> promise) {
+ OnUpdateSession(session_id, response, promise);
+}
+
+void MockCdm::CloseSession(const std::string& session_id,
+ std::unique_ptr<SimpleCdmPromise> promise) {
+ OnCloseSession(session_id, promise);
+}
+
+void MockCdm::RemoveSession(const std::string& session_id,
+ std::unique_ptr<SimpleCdmPromise> promise) {
+ OnRemoveSession(session_id, promise);
+}
+
+void MockCdm::CallSessionMessageCB(
+ const std::string& session_id,
+ ContentDecryptionModule::MessageType message_type,
+ const std::vector<uint8_t>& message) {
+ session_message_cb_.Run(session_id, message_type, message);
+}
+
+void MockCdm::CallSessionClosedCB(const std::string& session_id) {
+ session_closed_cb_.Run(session_id);
+}
+
+void MockCdm::CallSessionKeysChangeCB(const std::string& session_id,
+ bool has_additional_usable_key,
+ CdmKeysInfo keys_info) {
+ session_keys_change_cb_.Run(session_id, has_additional_usable_key,
+ std::move(keys_info));
+}
+
+void MockCdm::CallSessionExpirationUpdateCB(const std::string& session_id,
+ base::Time new_expiry_time) {
+ session_expiration_update_cb_.Run(session_id, new_expiry_time);
+}
+
+MockCdmFactory::MockCdmFactory() {}
+
+MockCdmFactory::~MockCdmFactory() {}
+
+void MockCdmFactory::Create(
+ const std::string& key_system,
+ const GURL& security_origin,
+ const CdmConfig& cdm_config,
+ const SessionMessageCB& session_message_cb,
+ const SessionClosedCB& session_closed_cb,
+ const SessionKeysChangeCB& session_keys_change_cb,
+ const SessionExpirationUpdateCB& session_expiration_update_cb,
+ const CdmCreatedCB& cdm_created_cb) {
+ // If no key system specified, notify that Create() failed.
+ if (key_system.empty()) {
+ cdm_created_cb.Run(nullptr, "CDM creation failed");
+ return;
+ }
+
+ // Since there is a CDM, call |before_creation_cb_| first.
+ if (!before_creation_cb_.is_null())
+ before_creation_cb_.Run();
+
+ // Create and return a new MockCdm. Keep a pointer to the created CDM so
+ // that tests can access it. Calls to GetCdmContext() can be ignored.
+ scoped_refptr<MockCdm> cdm = new StrictMock<MockCdm>(
+ session_message_cb, session_closed_cb, session_keys_change_cb,
+ session_expiration_update_cb);
+ created_cdm_ = cdm.get();
+ EXPECT_CALL(*created_cdm_.get(), GetCdmContext());
+ cdm_created_cb.Run(std::move(cdm), "");
+}
+
+MockCdm* MockCdmFactory::GetCreatedCdm() {
+ return created_cdm_.get();
+}
+
+void MockCdmFactory::SetBeforeCreationCB(
+ const base::Closure& before_creation_cb) {
+ before_creation_cb_ = before_creation_cb;
+}
+
MockStreamParser::MockStreamParser() {}
MockStreamParser::~MockStreamParser() {}
diff --git a/media/base/mock_filters.h b/media/base/mock_filters.h
index 3e6f8be44..42345e5 100644
--- a/media/base/mock_filters.h
+++ b/media/base/mock_filters.h
@@ -15,9 +15,12 @@
#include "media/base/audio_decoder.h"
#include "media/base/audio_decoder_config.h"
#include "media/base/audio_renderer.h"
+#include "media/base/cdm_config.h"
#include "media/base/cdm_context.h"
+#include "media/base/cdm_factory.h"
#include "media/base/cdm_key_information.h"
#include "media/base/cdm_promise.h"
+#include "media/base/cdm_promise_adapter.h"
#include "media/base/content_decryption_module.h"
#include "media/base/decoder_buffer.h"
#include "media/base/decryptor.h"
@@ -446,6 +449,120 @@
DISALLOW_COPY_AND_ASSIGN(MockCdmSessionPromise);
};
+class MockCdm : public ContentDecryptionModule {
+ public:
+ MockCdm(const SessionMessageCB& session_message_cb,
+ const SessionClosedCB& session_closed_cb,
+ const SessionKeysChangeCB& session_keys_change_cb,
+ const SessionExpirationUpdateCB& session_expiration_update_cb);
+
+ // ContentDecryptionModule implementation.
+ // As move-only parameters aren't supported by mock methods, convert promises
+ // into IDs and pass them to On... methods.
+ void SetServerCertificate(const std::vector<uint8_t>& certificate,
+ std::unique_ptr<SimpleCdmPromise> promise) override;
+ MOCK_METHOD2(OnSetServerCertificate,
+ void(const std::vector<uint8_t>& certificate,
+ std::unique_ptr<SimpleCdmPromise>& promise));
+
+ void CreateSessionAndGenerateRequest(
+ CdmSessionType session_type,
+ EmeInitDataType init_data_type,
+ const std::vector<uint8_t>& init_data,
+ std::unique_ptr<NewSessionCdmPromise> promise) override;
+ MOCK_METHOD4(OnCreateSessionAndGenerateRequest,
+ void(CdmSessionType session_type,
+ EmeInitDataType init_data_type,
+ const std::vector<uint8_t>& init_data,
+ std::unique_ptr<NewSessionCdmPromise>& promise));
+
+ void LoadSession(CdmSessionType session_type,
+ const std::string& session_id,
+ std::unique_ptr<NewSessionCdmPromise> promise) override;
+ MOCK_METHOD3(OnLoadSession,
+ void(CdmSessionType session_type,
+ const std::string& session_id,
+ std::unique_ptr<NewSessionCdmPromise>& promise));
+
+ void UpdateSession(const std::string& session_id,
+ const std::vector<uint8_t>& response,
+ std::unique_ptr<SimpleCdmPromise> promise) override;
+ MOCK_METHOD3(OnUpdateSession,
+ void(const std::string& session_id,
+ const std::vector<uint8_t>& response,
+ std::unique_ptr<SimpleCdmPromise>& promise));
+
+ void CloseSession(const std::string& session_id,
+ std::unique_ptr<SimpleCdmPromise> promise) override;
+ MOCK_METHOD2(OnCloseSession,
+ void(const std::string& session_id,
+ std::unique_ptr<SimpleCdmPromise>& promise));
+
+ void RemoveSession(const std::string& session_id,
+ std::unique_ptr<SimpleCdmPromise> promise) override;
+ MOCK_METHOD2(OnRemoveSession,
+ void(const std::string& session_id,
+ std::unique_ptr<SimpleCdmPromise>& promise));
+
+ MOCK_METHOD0(GetCdmContext, CdmContext*());
+
+ void CallSessionMessageCB(const std::string& session_id,
+ ContentDecryptionModule::MessageType message_type,
+ const std::vector<uint8_t>& message);
+ void CallSessionClosedCB(const std::string& session_id);
+ void CallSessionKeysChangeCB(const std::string& session_id,
+ bool has_additional_usable_key,
+ CdmKeysInfo keys_info);
+ void CallSessionExpirationUpdateCB(const std::string& session_id,
+ base::Time new_expiry_time);
+
+ protected:
+ ~MockCdm() override;
+
+ private:
+ // Callbacks.
+ SessionMessageCB session_message_cb_;
+ SessionClosedCB session_closed_cb_;
+ SessionKeysChangeCB session_keys_change_cb_;
+ SessionExpirationUpdateCB session_expiration_update_cb_;
+
+ DISALLOW_COPY_AND_ASSIGN(MockCdm);
+};
+
+class MockCdmFactory : public CdmFactory {
+ public:
+ MockCdmFactory();
+ ~MockCdmFactory() override;
+
+ // CdmFactory implementation.
+ // This creates a StrictMock<MockCdm> when called. Although ownership of the
+ // created CDM is passed to |cdm_created_cb|, a copy is kept (and available
+ // using Cdm()). If |key_system| is empty, no CDM will be created.
+ void Create(const std::string& key_system,
+ const GURL& security_origin,
+ const CdmConfig& cdm_config,
+ const SessionMessageCB& session_message_cb,
+ const SessionClosedCB& session_closed_cb,
+ const SessionKeysChangeCB& session_keys_change_cb,
+ const SessionExpirationUpdateCB& session_expiration_update_cb,
+ const CdmCreatedCB& cdm_created_cb) override;
+
+ // Return a pointer to the created CDM.
+ MockCdm* GetCreatedCdm();
+
+ // Provide a callback to be called before the CDM is created and returned.
+ void SetBeforeCreationCB(const base::Closure& before_creation_cb);
+
+ private:
+ // Reference to the created CDM.
+ scoped_refptr<MockCdm> created_cdm_;
+
+ // Callback to be used before Create() successfully calls |cdm_created_cb|.
+ base::Closure before_creation_cb_;
+
+ DISALLOW_COPY_AND_ASSIGN(MockCdmFactory);
+};
+
class MockStreamParser : public StreamParser {
public:
MockStreamParser();
diff --git a/media/mojo/clients/mojo_cdm.cc b/media/mojo/clients/mojo_cdm.cc
index 471647b..1033174 100644
--- a/media/mojo/clients/mojo_cdm.cc
+++ b/media/mojo/clients/mojo_cdm.cc
@@ -25,13 +25,6 @@
namespace media {
-template <typename PromiseType>
-static void RejectPromise(std::unique_ptr<PromiseType> promise,
- mojom::CdmPromiseResultPtr result) {
- promise->reject(result->exception, result->system_code,
- result->error_message);
-}
-
// static
void MojoCdm::Create(
const std::string& key_system,
@@ -140,6 +133,9 @@
return;
}
+ // As communication with the remote CDM is broken, reject any outstanding
+ // promises and close all the existing sessions.
+ cdm_promise_adapter_.Clear();
cdm_session_tracker_.CloseRemainingSessions(session_closed_cb_);
}
@@ -154,9 +150,10 @@
return;
}
+ uint32_t promise_id = cdm_promise_adapter_.SavePromise(std::move(promise));
remote_cdm_->SetServerCertificate(
certificate, base::Bind(&MojoCdm::OnSimpleCdmPromiseResult,
- base::Unretained(this), base::Passed(&promise)));
+ base::Unretained(this), promise_id));
}
void MojoCdm::CreateSessionAndGenerateRequest(
@@ -173,10 +170,11 @@
return;
}
+ uint32_t promise_id = cdm_promise_adapter_.SavePromise(std::move(promise));
remote_cdm_->CreateSessionAndGenerateRequest(
session_type, init_data_type, init_data,
base::Bind(&MojoCdm::OnNewSessionCdmPromiseResult, base::Unretained(this),
- base::Passed(&promise)));
+ promise_id));
}
void MojoCdm::LoadSession(CdmSessionType session_type,
@@ -191,10 +189,10 @@
return;
}
- remote_cdm_->LoadSession(
- session_type, session_id,
- base::Bind(&MojoCdm::OnNewSessionCdmPromiseResult, base::Unretained(this),
- base::Passed(&promise)));
+ uint32_t promise_id = cdm_promise_adapter_.SavePromise(std::move(promise));
+ remote_cdm_->LoadSession(session_type, session_id,
+ base::Bind(&MojoCdm::OnNewSessionCdmPromiseResult,
+ base::Unretained(this), promise_id));
}
void MojoCdm::UpdateSession(const std::string& session_id,
@@ -209,10 +207,10 @@
return;
}
- remote_cdm_->UpdateSession(
- session_id, response,
- base::Bind(&MojoCdm::OnSimpleCdmPromiseResult, base::Unretained(this),
- base::Passed(&promise)));
+ uint32_t promise_id = cdm_promise_adapter_.SavePromise(std::move(promise));
+ remote_cdm_->UpdateSession(session_id, response,
+ base::Bind(&MojoCdm::OnSimpleCdmPromiseResult,
+ base::Unretained(this), promise_id));
}
void MojoCdm::CloseSession(const std::string& session_id,
@@ -226,9 +224,10 @@
return;
}
- remote_cdm_->CloseSession(
- session_id, base::Bind(&MojoCdm::OnSimpleCdmPromiseResult,
- base::Unretained(this), base::Passed(&promise)));
+ uint32_t promise_id = cdm_promise_adapter_.SavePromise(std::move(promise));
+ remote_cdm_->CloseSession(session_id,
+ base::Bind(&MojoCdm::OnSimpleCdmPromiseResult,
+ base::Unretained(this), promise_id));
}
void MojoCdm::RemoveSession(const std::string& session_id,
@@ -242,9 +241,10 @@
return;
}
- remote_cdm_->RemoveSession(
- session_id, base::Bind(&MojoCdm::OnSimpleCdmPromiseResult,
- base::Unretained(this), base::Passed(&promise)));
+ uint32_t promise_id = cdm_promise_adapter_.SavePromise(std::move(promise));
+ remote_cdm_->RemoveSession(session_id,
+ base::Bind(&MojoCdm::OnSimpleCdmPromiseResult,
+ base::Unretained(this), promise_id));
}
CdmContext* MojoCdm::GetCdmContext() {
@@ -341,7 +341,9 @@
DCHECK(pending_init_promise_);
if (!result->success) {
- RejectPromise(std::move(pending_init_promise_), std::move(result));
+ pending_init_promise_->reject(result->exception, result->system_code,
+ result->error_message);
+ pending_init_promise_.reset();
return;
}
@@ -366,24 +368,28 @@
decryptor_->OnKeyAdded();
}
-void MojoCdm::OnSimpleCdmPromiseResult(
- std::unique_ptr<SimpleCdmPromise> promise,
- mojom::CdmPromiseResultPtr result) {
+void MojoCdm::OnSimpleCdmPromiseResult(uint32_t promise_id,
+ mojom::CdmPromiseResultPtr result) {
if (result->success)
- promise->resolve();
- else
- RejectPromise(std::move(promise), std::move(result));
+ cdm_promise_adapter_.ResolvePromise(promise_id);
+ else {
+ cdm_promise_adapter_.RejectPromise(promise_id, result->exception,
+ result->system_code,
+ result->error_message);
+ }
}
-void MojoCdm::OnNewSessionCdmPromiseResult(
- std::unique_ptr<NewSessionCdmPromise> promise,
- mojom::CdmPromiseResultPtr result,
- const std::string& session_id) {
+void MojoCdm::OnNewSessionCdmPromiseResult(uint32_t promise_id,
+ mojom::CdmPromiseResultPtr result,
+ const std::string& session_id) {
if (result->success) {
cdm_session_tracker_.AddSession(session_id);
- promise->resolve(session_id);
- } else
- RejectPromise(std::move(promise), std::move(result));
+ cdm_promise_adapter_.ResolvePromise(promise_id, session_id);
+ } else {
+ cdm_promise_adapter_.RejectPromise(promise_id, result->exception,
+ result->system_code,
+ result->error_message);
+ }
}
} // namespace media
diff --git a/media/mojo/clients/mojo_cdm.h b/media/mojo/clients/mojo_cdm.h
index e72b5352..425e901 100644
--- a/media/mojo/clients/mojo_cdm.h
+++ b/media/mojo/clients/mojo_cdm.h
@@ -17,6 +17,7 @@
#include "base/threading/thread_checker.h"
#include "media/base/cdm_context.h"
#include "media/base/cdm_initialized_promise.h"
+#include "media/base/cdm_promise_adapter.h"
#include "media/base/cdm_session_tracker.h"
#include "media/base/content_decryption_module.h"
#include "media/mojo/interfaces/content_decryption_module.mojom.h"
@@ -113,12 +114,11 @@
void OnKeyAdded();
// Callbacks to handle CDM promises.
- void OnSimpleCdmPromiseResult(std::unique_ptr<SimpleCdmPromise> promise,
+ void OnSimpleCdmPromiseResult(uint32_t promise_id,
mojom::CdmPromiseResultPtr result);
- void OnNewSessionCdmPromiseResult(
- std::unique_ptr<NewSessionCdmPromise> promise,
- mojom::CdmPromiseResultPtr result,
- const std::string& session_id);
+ void OnNewSessionCdmPromiseResult(uint32_t promise_id,
+ mojom::CdmPromiseResultPtr result,
+ const std::string& session_id);
base::ThreadChecker thread_checker_;
@@ -157,6 +157,9 @@
// Keep track of current sessions.
CdmSessionTracker cdm_session_tracker_;
+ // Keep track of outstanding promises.
+ CdmPromiseAdapter cdm_promise_adapter_;
+
// This must be the last member.
base::WeakPtrFactory<MojoCdm> weak_factory_;
diff --git a/media/mojo/clients/mojo_cdm_unittest.cc b/media/mojo/clients/mojo_cdm_unittest.cc
index 43c7da4..2ddd066 100644
--- a/media/mojo/clients/mojo_cdm_unittest.cc
+++ b/media/mojo/clients/mojo_cdm_unittest.cc
@@ -7,8 +7,10 @@
#include "base/bind.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
+#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/test/test_message_loop.h"
+#include "base/time/time.h"
#include "media/base/cdm_config.h"
#include "media/base/content_decryption_module.h"
#include "media/base/mock_filters.h"
@@ -21,12 +23,30 @@
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
+using ::testing::DoAll;
+using ::testing::Invoke;
+using ::testing::ReturnNull;
using ::testing::StrictMock;
+using ::testing::WithArg;
+using ::testing::WithArgs;
+using ::testing::WithoutArgs;
MATCHER(NotEmpty, "") {
return !arg.empty();
}
+ACTION_P2(CdmCreated, cdm, error_message) {
+ arg0.Run(cdm, error_message);
+}
+
+ACTION_P3(InvokeFunction, classPointer, memberFunc, p1) {
+ (classPointer->*memberFunc)(arg0, p1);
+}
+
+ACTION_P4(InvokeFunction2, classPointer, memberFunc, p1, p2) {
+ (classPointer->*memberFunc)(arg0, p1, p2);
+}
+
namespace media {
namespace {
@@ -45,7 +65,12 @@
class MojoCdmTest : public ::testing::Test {
public:
- enum ExpectedResult { SUCCESS, CONNECTION_ERROR, FAILURE };
+ enum ExpectedResult {
+ SUCCESS,
+ CONNECTION_ERROR_BEFORE,
+ CONNECTION_ERROR_DURING,
+ FAILURE
+ };
MojoCdmTest()
: mojo_cdm_service_(base::MakeUnique<MojoCdmService>(
@@ -55,19 +80,26 @@
virtual ~MojoCdmTest() {}
- void Initialize(const std::string& key_system,
- ExpectedResult expected_result) {
+ void Initialize(ExpectedResult expected_result) {
mojom::ContentDecryptionModulePtr remote_cdm;
auto cdm_request = mojo::MakeRequest(&remote_cdm);
- switch (expected_result) {
- case SUCCESS:
- case FAILURE:
- cdm_binding_.Bind(std::move(cdm_request));
- break;
- case CONNECTION_ERROR:
- cdm_request.ResetWithReason(0, "Request dropped for testing.");
- break;
+ cdm_binding_.Bind(std::move(cdm_request));
+
+ std::string key_system;
+ if (expected_result == CONNECTION_ERROR_BEFORE) {
+ // Break the connection before the call.
+ ForceConnectionError();
+ } else if (expected_result != FAILURE) {
+ // In the remaining cases, CDM is expected, so provide a key system.
+ key_system = kClearKeyKeySystem;
+
+ if (expected_result == CONNECTION_ERROR_DURING) {
+ // Create() will be successful, so provide a callback that will break
+ // the connection before returning the CDM.
+ cdm_factory_.SetBeforeCreationCB(base::Bind(
+ &MojoCdmTest::ForceConnectionError, base::Unretained(this)));
+ }
}
MojoCdm::Create(key_system, GURL(kTestSecurityOrigin), CdmConfig(),
@@ -82,7 +114,6 @@
base::Unretained(&cdm_client_)),
base::Bind(&MojoCdmTest::OnCdmCreated,
base::Unretained(this), expected_result));
-
base::RunLoop().RunUntilIdle();
}
@@ -97,122 +128,436 @@
EXPECT_EQ(SUCCESS, expected_result);
mojo_cdm_ = cdm;
+ remote_cdm_ = cdm_factory_.GetCreatedCdm();
}
void ForceConnectionError() {
- // If there is an existing session it will get closed when the connection
- // is broken.
- if (!session_id_.empty()) {
- EXPECT_CALL(cdm_client_, OnSessionClosed(session_id_));
- }
-
cdm_binding_.CloseWithReason(2, "Test closed connection.");
-
base::RunLoop().RunUntilIdle();
}
void SetServerCertificateAndExpect(const std::vector<uint8_t>& certificate,
ExpectedResult expected_result) {
+ if (expected_result == CONNECTION_ERROR_BEFORE) {
+ // Break the connection before the call, so SetServerCertificate() is
+ // never called.
+ ForceConnectionError();
+ } else {
+ EXPECT_CALL(*remote_cdm_, OnSetServerCertificate(certificate, _))
+ .WillOnce(WithArg<1>(InvokeFunction(this, &MojoCdmTest::HandlePromise,
+ expected_result)));
+ }
+
mojo_cdm_->SetServerCertificate(
certificate,
base::MakeUnique<MockCdmPromise>(expected_result == SUCCESS));
-
base::RunLoop().RunUntilIdle();
}
- void CreateSessionAndExpect(EmeInitDataType data_type,
- const std::vector<uint8_t>& key_id,
+ void CreateSessionAndExpect(const std::string& session_id,
ExpectedResult expected_result) {
- if (expected_result == SUCCESS) {
- EXPECT_CALL(cdm_client_, OnSessionMessage(NotEmpty(), _, _));
+ // Specify parameters needed to call CreateSessionAndGenerateRequest() in
+ // order to verify that the data is passed properly.
+ const CdmSessionType session_type = CdmSessionType::TEMPORARY_SESSION;
+ const EmeInitDataType data_type = EmeInitDataType::WEBM;
+ const std::vector<uint8_t> key_id(kKeyId, kKeyId + arraysize(kKeyId));
+ std::string created_session_id;
+
+ if (expected_result == CONNECTION_ERROR_BEFORE) {
+ // Break the connection before the call, so
+ // CreateSessionAndGenerateRequest() is never called.
+ ForceConnectionError();
+ } else {
+ EXPECT_CALL(*remote_cdm_, OnCreateSessionAndGenerateRequest(
+ session_type, data_type, key_id, _))
+ .WillOnce(WithArg<3>(
+ InvokeFunction2(this, &MojoCdmTest::HandleSessionPromise,
+ session_id, expected_result)));
}
+ // Note that although it's called CreateSessionAndGenerateRequest, no
+ // request is generated.
mojo_cdm_->CreateSessionAndGenerateRequest(
- CdmSessionType::TEMPORARY_SESSION, data_type, key_id,
+ session_type, data_type, key_id,
base::MakeUnique<MockCdmSessionPromise>(expected_result == SUCCESS,
- &session_id_));
+ &created_session_id));
+ base::RunLoop().RunUntilIdle();
+ // If the session was "created" ...
+ if (expected_result == SUCCESS) {
+ // Returned session ID must match the session ID provided.
+ EXPECT_EQ(session_id, created_session_id);
+
+ // MojoCdm expects the session to be closed, so invoke SessionClosedCB
+ // to "close" it.
+ EXPECT_CALL(cdm_client_, OnSessionClosed(session_id));
+ remote_cdm_->CallSessionClosedCB(session_id);
+ base::RunLoop().RunUntilIdle();
+ }
+ }
+
+ void LoadSessionAndExpect(const std::string& session_id,
+ ExpectedResult expected_result) {
+ const CdmSessionType session_type =
+ CdmSessionType::PERSISTENT_LICENSE_SESSION;
+ std::string loaded_session_id;
+
+ if (expected_result == CONNECTION_ERROR_BEFORE) {
+ // Break the connection before the call, so LoadSession() is never called.
+ ForceConnectionError();
+ } else {
+ EXPECT_CALL(*remote_cdm_, OnLoadSession(session_type, session_id, _))
+ .WillOnce(WithArg<2>(
+ InvokeFunction2(this, &MojoCdmTest::HandleSessionPromise,
+ session_id, expected_result)));
+ }
+
+ mojo_cdm_->LoadSession(session_type, session_id,
+ base::MakeUnique<MockCdmSessionPromise>(
+ expected_result == SUCCESS, &loaded_session_id));
+ base::RunLoop().RunUntilIdle();
+
+ // If the session was "loaded" ...
+ if (expected_result == SUCCESS) {
+ // Returned session ID must match the session ID provided.
+ EXPECT_EQ(session_id, loaded_session_id);
+
+ // MojoCdm expects the session to be closed, so invoke SessionClosedCB
+ // to "close" it.
+ EXPECT_CALL(cdm_client_, OnSessionClosed(session_id));
+ remote_cdm_->CallSessionClosedCB(session_id);
+ base::RunLoop().RunUntilIdle();
+ }
+ }
+
+ void UpdateSessionAndExpect(const std::string& session_id,
+ ExpectedResult expected_result) {
+ // Specify parameters to UpdateSession() in order to verify that
+ // the data is passed properly.
+ const std::vector<uint8_t> response = {1, 2, 3, 4, 5, 6};
+
+ if (expected_result == CONNECTION_ERROR_BEFORE) {
+ // Break the connection before the call, so UpdateSession() is never
+ // called.
+ ForceConnectionError();
+ } else {
+ EXPECT_CALL(*remote_cdm_, OnUpdateSession(session_id, response, _))
+ .WillOnce(WithArg<2>(InvokeFunction(this, &MojoCdmTest::HandlePromise,
+ expected_result)));
+ }
+
+ mojo_cdm_->UpdateSession(
+ session_id, response,
+ base::MakeUnique<MockCdmPromise>(expected_result == SUCCESS));
base::RunLoop().RunUntilIdle();
}
- void CloseSessionAndExpect(ExpectedResult expected_result) {
- DCHECK(!session_id_.empty()) << "CloseSessionAndExpect() must be called "
- "after a successful "
- "CreateSessionAndExpect()";
-
- if (expected_result == SUCCESS) {
- EXPECT_CALL(cdm_client_, OnSessionClosed(session_id_));
+ void CloseSessionAndExpect(const std::string& session_id,
+ ExpectedResult expected_result) {
+ if (expected_result == CONNECTION_ERROR_BEFORE) {
+ // Break the connection before the call, so CloseSession() is never
+ // called.
+ ForceConnectionError();
+ } else {
+ EXPECT_CALL(*remote_cdm_, OnCloseSession(session_id, _))
+ .WillOnce(WithArg<1>(InvokeFunction(this, &MojoCdmTest::HandlePromise,
+ expected_result)));
}
- mojo_cdm_->CloseSession(session_id_, base::MakeUnique<MockCdmPromise>(
- expected_result == SUCCESS));
-
+ mojo_cdm_->CloseSession(session_id, base::MakeUnique<MockCdmPromise>(
+ expected_result == SUCCESS));
base::RunLoop().RunUntilIdle();
}
+ void RemoveSessionAndExpect(const std::string& session_id,
+ ExpectedResult expected_result) {
+ if (expected_result == CONNECTION_ERROR_BEFORE) {
+ // Break the connection before the call, so RemoveSession() is never
+ // called.
+ ForceConnectionError();
+ } else {
+ EXPECT_CALL(*remote_cdm_, OnRemoveSession(session_id, _))
+ .WillOnce(WithArg<1>(InvokeFunction(this, &MojoCdmTest::HandlePromise,
+ expected_result)));
+ }
+
+ mojo_cdm_->RemoveSession(session_id, base::MakeUnique<MockCdmPromise>(
+ expected_result == SUCCESS));
+ base::RunLoop().RunUntilIdle();
+ }
+
+ void HandlePromise(std::unique_ptr<SimpleCdmPromise>& promise,
+ ExpectedResult expected_result) {
+ switch (expected_result) {
+ case SUCCESS:
+ promise->resolve();
+ break;
+
+ case FAILURE:
+ promise->reject(media::CdmPromise::UNKNOWN_ERROR, 0,
+ "Promise rejected");
+ break;
+
+ case CONNECTION_ERROR_BEFORE:
+ // Connection should be broken before this is called.
+ NOTREACHED();
+ break;
+
+ case CONNECTION_ERROR_DURING:
+ ForceConnectionError();
+
+ // Now that the connection is broken the promise result won't be passed
+ // back. However, since we check that every promise is fulfilled, we
+ // need to do something with this promise. Resolve the promise to
+ // fulfill it, but note that the original caller will get back a
+ // failed promise due to the connection being broken.
+ promise->resolve();
+ break;
+ }
+ }
+
+ void HandleSessionPromise(std::unique_ptr<NewSessionCdmPromise>& promise,
+ const std::string& session_id,
+ ExpectedResult expected_result) {
+ switch (expected_result) {
+ case SUCCESS:
+ promise->resolve(session_id);
+ break;
+
+ case FAILURE:
+ promise->reject(media::CdmPromise::UNKNOWN_ERROR, 0,
+ "Promise rejected");
+ break;
+
+ case CONNECTION_ERROR_BEFORE:
+ // Connection should be broken before this is called.
+ NOTREACHED();
+ break;
+
+ case CONNECTION_ERROR_DURING:
+ ForceConnectionError();
+
+ // Now that the connection is broken the promise result won't be passed
+ // back. However, since we check that every promise is fulfilled, we
+ // need to do something with this promise. Resolve the promise to
+ // fulfill it, but note that the original caller will get back a
+ // failed promise due to the connection being broken.
+ promise->resolve(session_id);
+ break;
+ }
+ }
+
// Fixture members.
base::TestMessageLoop message_loop_;
+ // |remote_cdm_| represents the CDM at the end of the mojo message pipe.
+ MockCdm* remote_cdm_;
+ MockCdmFactory cdm_factory_;
+
MojoCdmServiceContext mojo_cdm_service_context_;
StrictMock<MockCdmClient> cdm_client_;
- // TODO(jrummell): Use a MockCdmFactory to create a MockCdm here for more test
- // coverage.
- DefaultCdmFactory cdm_factory_;
-
std::unique_ptr<MojoCdmService> mojo_cdm_service_;
mojo::Binding<mojom::ContentDecryptionModule> cdm_binding_;
scoped_refptr<ContentDecryptionModule> mojo_cdm_;
- // |session_id_| is the latest successful result of calling CreateSession().
- std::string session_id_;
-
private:
DISALLOW_COPY_AND_ASSIGN(MojoCdmTest);
};
TEST_F(MojoCdmTest, Create_Success) {
- Initialize(kClearKeyKeySystem, SUCCESS);
-}
-
-TEST_F(MojoCdmTest, Create_ConnectionError) {
- Initialize(kClearKeyKeySystem, CONNECTION_ERROR);
+ Initialize(SUCCESS);
}
TEST_F(MojoCdmTest, Create_Failure) {
- // This fails as DefaultCdmFactory only supports Clear Key.
- Initialize("org.random.cdm", FAILURE);
+ Initialize(FAILURE);
}
-TEST_F(MojoCdmTest, SetServerCertificate_AfterConnectionError) {
- Initialize(kClearKeyKeySystem, SUCCESS);
- ForceConnectionError();
- SetServerCertificateAndExpect({0, 1, 2}, FAILURE);
+TEST_F(MojoCdmTest, Create_ConnectionErrorBefore) {
+ Initialize(CONNECTION_ERROR_BEFORE);
}
-TEST_F(MojoCdmTest, CreateSessionAndGenerateRequest_AfterConnectionError) {
- std::vector<uint8_t> key_id(kKeyId, kKeyId + arraysize(kKeyId));
+TEST_F(MojoCdmTest, Create_ConnectionErrorDuring) {
+ Initialize(CONNECTION_ERROR_DURING);
+}
- Initialize(kClearKeyKeySystem, SUCCESS);
- ForceConnectionError();
- CreateSessionAndExpect(EmeInitDataType::WEBM, key_id, FAILURE);
+TEST_F(MojoCdmTest, SetServerCertificate_Success) {
+ const std::vector<uint8_t> certificate = {0, 1, 2};
+ Initialize(SUCCESS);
+ SetServerCertificateAndExpect(certificate, SUCCESS);
+}
+
+TEST_F(MojoCdmTest, SetServerCertificate_Failure) {
+ const std::vector<uint8_t> certificate = {1, 2, 3, 4, 5};
+ Initialize(SUCCESS);
+ SetServerCertificateAndExpect(certificate, FAILURE);
+}
+
+TEST_F(MojoCdmTest, SetServerCertificate_ConnectionErrorBefore) {
+ const std::vector<uint8_t> certificate = {3, 4};
+ Initialize(SUCCESS);
+ SetServerCertificateAndExpect(certificate, CONNECTION_ERROR_BEFORE);
+}
+
+TEST_F(MojoCdmTest, SetServerCertificate_ConnectionErrorDuring) {
+ const std::vector<uint8_t> certificate = {10, 11, 12};
+ Initialize(SUCCESS);
+ SetServerCertificateAndExpect(certificate, CONNECTION_ERROR_DURING);
+}
+
+TEST_F(MojoCdmTest, CreateSession_Success) {
+ const std::string session_id = "create1";
+ Initialize(SUCCESS);
+ CreateSessionAndExpect(session_id, SUCCESS);
+}
+
+TEST_F(MojoCdmTest, CreateSession_Failure) {
+ const std::string session_id = "create2";
+ Initialize(SUCCESS);
+ CreateSessionAndExpect(session_id, FAILURE);
+}
+
+TEST_F(MojoCdmTest, CreateSession_ConnectionErrorBefore) {
+ const std::string session_id = "create3";
+ Initialize(SUCCESS);
+ CreateSessionAndExpect(session_id, CONNECTION_ERROR_BEFORE);
+}
+
+TEST_F(MojoCdmTest, CreateSession_ConnectionErrorDuring) {
+ const std::string session_id = "create4";
+ Initialize(SUCCESS);
+ CreateSessionAndExpect(session_id, CONNECTION_ERROR_DURING);
+}
+
+TEST_F(MojoCdmTest, LoadSession_Success) {
+ const std::string session_id = "load1";
+ Initialize(SUCCESS);
+ LoadSessionAndExpect(session_id, SUCCESS);
+}
+
+TEST_F(MojoCdmTest, LoadSession_Failure) {
+ const std::string session_id = "load2";
+ Initialize(SUCCESS);
+ LoadSessionAndExpect(session_id, FAILURE);
+}
+
+TEST_F(MojoCdmTest, LoadSession_ConnectionErrorBefore) {
+ const std::string session_id = "load3";
+ Initialize(SUCCESS);
+ LoadSessionAndExpect(session_id, CONNECTION_ERROR_BEFORE);
+}
+
+TEST_F(MojoCdmTest, LoadSession_ConnectionErrorDuring) {
+ const std::string session_id = "load4";
+ Initialize(SUCCESS);
+ LoadSessionAndExpect(session_id, CONNECTION_ERROR_DURING);
+}
+
+TEST_F(MojoCdmTest, UpdateSession_Success) {
+ const std::string session_id = "update1";
+ Initialize(SUCCESS);
+ UpdateSessionAndExpect(session_id, SUCCESS);
+}
+
+TEST_F(MojoCdmTest, UpdateSession_Failure) {
+ const std::string session_id = "update2";
+ Initialize(SUCCESS);
+ UpdateSessionAndExpect(session_id, FAILURE);
+}
+
+TEST_F(MojoCdmTest, UpdateSession_ConnectionErrorBefore) {
+ const std::string session_id = "update3";
+ Initialize(SUCCESS);
+ UpdateSessionAndExpect(session_id, CONNECTION_ERROR_BEFORE);
+}
+
+TEST_F(MojoCdmTest, UpdateSession_ConnectionErrorDuring) {
+ const std::string session_id = "update4";
+ Initialize(SUCCESS);
+ UpdateSessionAndExpect(session_id, CONNECTION_ERROR_DURING);
}
TEST_F(MojoCdmTest, CloseSession_Success) {
- std::vector<uint8_t> key_id(kKeyId, kKeyId + arraysize(kKeyId));
-
- Initialize(kClearKeyKeySystem, SUCCESS);
- CreateSessionAndExpect(EmeInitDataType::WEBM, key_id, SUCCESS);
- CloseSessionAndExpect(SUCCESS);
+ const std::string session_id = "close1";
+ Initialize(SUCCESS);
+ CloseSessionAndExpect(session_id, SUCCESS);
}
-TEST_F(MojoCdmTest, CloseSession_AfterConnectionError) {
- std::vector<uint8_t> key_id(kKeyId, kKeyId + arraysize(kKeyId));
+TEST_F(MojoCdmTest, CloseSession_Failure) {
+ const std::string session_id = "close2";
+ Initialize(SUCCESS);
+ CloseSessionAndExpect(session_id, FAILURE);
+}
- Initialize(kClearKeyKeySystem, SUCCESS);
- CreateSessionAndExpect(EmeInitDataType::WEBM, key_id, SUCCESS);
- ForceConnectionError();
- CloseSessionAndExpect(FAILURE);
+TEST_F(MojoCdmTest, CloseSession_ConnectionErrorBefore) {
+ const std::string session_id = "close3";
+ Initialize(SUCCESS);
+ CloseSessionAndExpect(session_id, CONNECTION_ERROR_BEFORE);
+}
+
+TEST_F(MojoCdmTest, CloseSession_ConnectionErrorDuring) {
+ const std::string session_id = "close4";
+ Initialize(SUCCESS);
+ CloseSessionAndExpect(session_id, CONNECTION_ERROR_DURING);
+}
+
+TEST_F(MojoCdmTest, RemoveSession_Success) {
+ const std::string session_id = "remove1";
+ Initialize(SUCCESS);
+ RemoveSessionAndExpect(session_id, SUCCESS);
+}
+
+TEST_F(MojoCdmTest, RemoveSession_Failure) {
+ const std::string session_id = "remove2";
+ Initialize(SUCCESS);
+ RemoveSessionAndExpect(session_id, FAILURE);
+}
+
+TEST_F(MojoCdmTest, RemoveSession_ConnectionErrorBefore) {
+ const std::string session_id = "remove3";
+ Initialize(SUCCESS);
+ RemoveSessionAndExpect(session_id, CONNECTION_ERROR_BEFORE);
+}
+
+TEST_F(MojoCdmTest, RemoveSession_ConnectionErrorDuring) {
+ const std::string session_id = "remove4";
+ Initialize(SUCCESS);
+ RemoveSessionAndExpect(session_id, CONNECTION_ERROR_DURING);
+}
+
+// Note that MojoCdm requires a session to exist when SessionClosedCB is called,
+// so it is currently tested in the success cases for CreateSession/LoadSession.
+
+TEST_F(MojoCdmTest, SessionMessageCB_Success) {
+ const std::string session_id = "message";
+ const ContentDecryptionModule::MessageType message_type =
+ ContentDecryptionModule::LICENSE_REQUEST;
+ const std::vector<uint8_t> message = {0, 1, 2};
+ Initialize(SUCCESS);
+ EXPECT_CALL(cdm_client_, OnSessionMessage(session_id, message_type, message));
+ remote_cdm_->CallSessionMessageCB(session_id, message_type, message);
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(MojoCdmTest, SessionExpirationChangeCB_Success) {
+ const std::string session_id = "expiration";
+ const base::Time time = base::Time::Now();
+ Initialize(SUCCESS);
+ EXPECT_CALL(cdm_client_, OnSessionExpirationUpdate(session_id, time));
+ remote_cdm_->CallSessionExpirationUpdateCB(session_id, time);
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(MojoCdmTest, SessionKeysChangeCB_Success) {
+ const std::string session_id = "change";
+ bool has_additional_usable_key = true;
+ CdmKeysInfo keys_info;
+ Initialize(SUCCESS);
+ EXPECT_CALL(cdm_client_,
+ OnSessionKeysChangeCalled(session_id, has_additional_usable_key));
+ remote_cdm_->CallSessionKeysChangeCB(session_id, has_additional_usable_key,
+ std::move(keys_info));
+ base::RunLoop().RunUntilIdle();
}
} // namespace media