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
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138

media / cdm / library_cdm / mock_library_cdm.h [blame]

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

#ifndef MEDIA_CDM_LIBRARY_CDM_MOCK_LIBRARY_CDM_H_
#define MEDIA_CDM_LIBRARY_CDM_MOCK_LIBRARY_CDM_H_

#include <stdint.h>

#include <memory>
#include <string>

#include "media/cdm/api/content_decryption_module.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace media {

class CdmHostProxy;

// Mock implementation of the cdm::ContentDecryptionModule interfaces.
class MockLibraryCdm : public cdm::ContentDecryptionModule_10,
                       public cdm::ContentDecryptionModule_11 {
 public:
  // Provides easy access to the MockLibraryCdm instance for testing to avoid
  // going through multiple layers to get it (e.g. CdmAdapter -> CdmWrapper ->
  // CdmWrapperImpl). It does impose a limitation that we cannot have more than
  // one MockLibraryCdm instances at the same time, which is fine in most
  // testing cases.
  static MockLibraryCdm* GetInstance();

  template <typename HostInterface>
  MockLibraryCdm(HostInterface* host, const std::string& key_system);

  CdmHostProxy* GetCdmHostProxy();

  // cdm::ContentDecryptionModule_10 implementation.
  MOCK_METHOD1(
      InitializeVideoDecoder,
      cdm::Status(const cdm::VideoDecoderConfig_2& video_decoder_config));
  MOCK_METHOD2(DecryptAndDecodeFrame,
               cdm::Status(const cdm::InputBuffer_2& encrypted_buffer,
                           cdm::VideoFrame* video_frame));

  // cdm::ContentDecryptionModule_11 implementation.
  MOCK_METHOD1(
      InitializeVideoDecoder,
      cdm::Status(const cdm::VideoDecoderConfig_3& video_decoder_config));
  MOCK_METHOD2(DecryptAndDecodeFrame,
               cdm::Status(const cdm::InputBuffer_2& encrypted_buffer,
                           cdm::VideoFrame_2* video_frame));

  // cdm::ContentDecryptionModule_10/11 implementation.
  void Initialize(bool allow_distinctive_identifier,
                  bool allow_persistent_state,
                  bool use_hw_secure_codecs) override;
  MOCK_METHOD1(
      InitializeAudioDecoder,
      cdm::Status(const cdm::AudioDecoderConfig_2& audio_decoder_config));
  MOCK_METHOD2(Decrypt,
               cdm::Status(const cdm::InputBuffer_2& encrypted_buffer,
                           cdm::DecryptedBlock* decrypted_block));
  MOCK_METHOD2(DecryptAndDecodeSamples,
               cdm::Status(const cdm::InputBuffer_2& encrypted_buffer,
                           cdm::AudioFrames* audio_frames));

  // Common cdm::ContentDecryptionModule_* implementation.
  MOCK_METHOD2(GetStatusForPolicy,
               void(uint32_t promise_id, const cdm::Policy& policy));
  MOCK_METHOD5(CreateSessionAndGenerateRequest,
               void(uint32_t promise_id,
                    cdm::SessionType session_type,
                    cdm::InitDataType init_data_type,
                    const uint8_t* init_data,
                    uint32_t init_data_size));
  MOCK_METHOD4(LoadSession,
               void(uint32_t promise_id,
                    cdm::SessionType session_type,
                    const char* session_id,
                    uint32_t session_id_length));
  MOCK_METHOD5(UpdateSession,
               void(uint32_t promise_id,
                    const char* session_id,
                    uint32_t session_id_length,
                    const uint8_t* response,
                    uint32_t response_size));
  MOCK_METHOD3(CloseSession,
               void(uint32_t promise_id,
                    const char* session_id,
                    uint32_t session_id_length));
  MOCK_METHOD3(RemoveSession,
               void(uint32_t promise_id,
                    const char* session_id,
                    uint32_t session_id_length));
  MOCK_METHOD3(SetServerCertificate,
               void(uint32_t promise_id,
                    const uint8_t* server_certificate_data,
                    uint32_t server_certificate_data_size));
  MOCK_METHOD1(TimerExpired, void(void* context));
  MOCK_METHOD1(DeinitializeDecoder, void(cdm::StreamType decoder_type));
  MOCK_METHOD1(ResetDecoder, void(cdm::StreamType decoder_type));
  MOCK_METHOD1(OnPlatformChallengeResponse,
               void(const cdm::PlatformChallengeResponse& response));
  MOCK_METHOD3(OnQueryOutputProtectionStatus,
               void(cdm::QueryResult result,
                    uint32_t link_mask,
                    uint32_t output_protection_mask));
  MOCK_METHOD3(OnStorageId,
               void(uint32_t version,
                    const uint8_t* storage_id,
                    uint32_t storage_id_size));

  // It could be tricky to expect Destroy() to be called and then delete
  // MockLibraryCdm directly in the test. So call "delete this" in this class,
  // same as a normal CDM implementation would do, but also add DestroyCalled()
  // so that it's easy to ensure Destroy() is actually called.
  MOCK_METHOD0(DestroyCalled, void());
  void Destroy() override {
    DestroyCalled();
    delete this;
  }

 private:
  // Can only be destructed through Destroy().
  ~MockLibraryCdm() override;

  std::unique_ptr<CdmHostProxy> cdm_host_proxy_;
};

// Helper function to create MockLibraryCdm.
void* CreateMockLibraryCdm(int cdm_interface_version,
                           const char* key_system,
                           uint32_t key_system_size,
                           GetCdmHostFunc get_cdm_host_func,
                           void* user_data);

}  // namespace media

#endif  // MEDIA_CDM_LIBRARY_CDM_MOCK_LIBRARY_CDM_H_