[M120 Migration][MM][CAPI] Fix the logic for media using capi player.
[platform/framework/web/chromium-efl.git] / media / mojo / services / mojo_cdm_file_io_unittest.cc
1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "media/mojo/services/mojo_cdm_file_io.h"
6
7 #include "base/run_loop.h"
8 #include "base/test/task_environment.h"
9 #include "media/cdm/api/content_decryption_module.h"
10 #include "mojo/public/cpp/bindings/associated_receiver.h"
11 #include "mojo/public/cpp/bindings/associated_remote.h"
12 #include "mojo/public/cpp/bindings/pending_receiver.h"
13 #include "mojo/public/cpp/bindings/receiver.h"
14 #include "mojo/public/cpp/bindings/remote.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 using ::testing::_;
19 using ::testing::Unused;
20 using Status = cdm::FileIOClient::Status;
21
22 namespace media {
23
24 namespace {
25
26 class MockFileIOClient : public cdm::FileIOClient {
27  public:
28   MockFileIOClient() = default;
29   ~MockFileIOClient() override = default;
30
31   MOCK_METHOD1(OnOpenComplete, void(Status));
32   MOCK_METHOD3(OnReadComplete, void(Status, const uint8_t*, uint32_t));
33   MOCK_METHOD1(OnWriteComplete, void(Status));
34 };
35
36 class MockCdmFile : public mojom::CdmFile {
37  public:
38   MockCdmFile() = default;
39   ~MockCdmFile() override = default;
40
41   MOCK_METHOD1(Read, void(ReadCallback));
42   MOCK_METHOD2(Write, void(const std::vector<uint8_t>&, WriteCallback));
43 };
44
45 class MockCdmStorage : public mojom::CdmStorage {
46  public:
47   MockCdmStorage(mojo::PendingReceiver<mojom::CdmStorage> receiver,
48                  MockCdmFile* cdm_file)
49       : receiver_(this, std::move(receiver)), client_receiver_(cdm_file) {}
50   ~MockCdmStorage() override = default;
51
52   // MojoCdmFileIO calls CdmStorage::Open() to open the file. Receivers always
53   // succeed.
54   void Open(const std::string& file_name, OpenCallback callback) override {
55     mojo::PendingAssociatedRemote<mojom::CdmFile> client_remote;
56     client_receiver_.Bind(client_remote.InitWithNewEndpointAndPassReceiver());
57     std::move(callback).Run(mojom::CdmStorage::Status::kSuccess,
58                             std::move(client_remote));
59
60     base::RunLoop().RunUntilIdle();
61   }
62
63  private:
64   mojo::Receiver<mojom::CdmStorage> receiver_;
65   mojo::AssociatedReceiver<mojom::CdmFile> client_receiver_;
66 };
67
68 }  // namespace
69
70 // Note that the current browser_test ECKEncryptedMediaTest.FileIOTest
71 // does test reading and writing files with real data.
72
73 class MojoCdmFileIOTest : public testing::Test, public MojoCdmFileIO::Delegate {
74  protected:
75   MojoCdmFileIOTest() = default;
76   ~MojoCdmFileIOTest() override = default;
77
78   // testing::Test implementation.
79   void SetUp() override {
80     client_ = std::make_unique<MockFileIOClient>();
81
82     mojo::Remote<mojom::CdmStorage> cdm_storage_remote;
83     cdm_storage_ = std::make_unique<MockCdmStorage>(
84         cdm_storage_remote.BindNewPipeAndPassReceiver(), &cdm_file_);
85
86     file_io_ = std::make_unique<MojoCdmFileIO>(this, client_.get(),
87                                                std::move(cdm_storage_remote));
88   }
89
90   // MojoCdmFileIO::Delegate implementation.
91   void CloseCdmFileIO(MojoCdmFileIO* cdm_file_io) override {
92     DCHECK_EQ(file_io_.get(), cdm_file_io);
93     file_io_.reset();
94   }
95
96   void ReportFileReadSize(int file_size_bytes) override {}
97
98   base::test::TaskEnvironment task_environment_;
99   std::unique_ptr<MojoCdmFileIO> file_io_;
100   std::unique_ptr<MockFileIOClient> client_;
101   std::unique_ptr<MockCdmStorage> cdm_storage_;
102   MockCdmFile cdm_file_;
103 };
104
105 TEST_F(MojoCdmFileIOTest, OpenFile) {
106   const std::string kFileName = "openfile";
107   EXPECT_CALL(*client_.get(), OnOpenComplete(Status::kSuccess));
108   file_io_->Open(kFileName.data(), kFileName.length());
109
110   base::RunLoop().RunUntilIdle();
111 }
112
113 TEST_F(MojoCdmFileIOTest, OpenFileTwice) {
114   const std::string kFileName = "openfile";
115   EXPECT_CALL(*client_.get(), OnOpenComplete(Status::kSuccess));
116   file_io_->Open(kFileName.data(), kFileName.length());
117
118   EXPECT_CALL(*client_.get(), OnOpenComplete(Status::kError));
119   file_io_->Open(kFileName.data(), kFileName.length());
120
121   base::RunLoop().RunUntilIdle();
122 }
123
124 TEST_F(MojoCdmFileIOTest, OpenFileAfterOpen) {
125   const std::string kFileName = "openfile";
126   EXPECT_CALL(*client_.get(), OnOpenComplete(Status::kSuccess));
127   file_io_->Open(kFileName.data(), kFileName.length());
128
129   // Run now so that the file is opened.
130   base::RunLoop().RunUntilIdle();
131
132   EXPECT_CALL(*client_.get(), OnOpenComplete(Status::kError));
133   file_io_->Open(kFileName.data(), kFileName.length());
134
135   // Run a second time so Open() tries after the file is already open.
136   base::RunLoop().RunUntilIdle();
137 }
138
139 TEST_F(MojoCdmFileIOTest, OpenDifferentFiles) {
140   const std::string kFileName1 = "openfile1";
141   EXPECT_CALL(*client_.get(), OnOpenComplete(Status::kSuccess));
142   file_io_->Open(kFileName1.data(), kFileName1.length());
143
144   const std::string kFileName2 = "openfile2";
145   EXPECT_CALL(*client_.get(), OnOpenComplete(Status::kError));
146   file_io_->Open(kFileName2.data(), kFileName2.length());
147
148   base::RunLoop().RunUntilIdle();
149 }
150
151 TEST_F(MojoCdmFileIOTest, Read) {
152   const std::string kFileName = "readfile";
153   EXPECT_CALL(*client_.get(), OnOpenComplete(Status::kSuccess));
154   file_io_->Open(kFileName.data(), kFileName.length());
155   base::RunLoop().RunUntilIdle();
156
157   // Successful reads always return a 3-byte buffer.
158   EXPECT_CALL(cdm_file_, Read(_))
159       .WillOnce([](mojom::CdmFile::ReadCallback callback) {
160         std::move(callback).Run(mojom::CdmFile::Status::kSuccess, {1, 2, 3});
161       });
162   EXPECT_CALL(*client_.get(), OnReadComplete(Status::kSuccess, _, 3));
163   file_io_->Read();
164   base::RunLoop().RunUntilIdle();
165 }
166
167 TEST_F(MojoCdmFileIOTest, ReadBeforeOpen) {
168   // File not open, so reading should fail.
169   EXPECT_CALL(*client_.get(), OnReadComplete(Status::kError, _, _));
170   file_io_->Read();
171   base::RunLoop().RunUntilIdle();
172 }
173
174 TEST_F(MojoCdmFileIOTest, TwoReads) {
175   const std::string kFileName = "readfile";
176   EXPECT_CALL(*client_.get(), OnOpenComplete(Status::kSuccess));
177   file_io_->Open(kFileName.data(), kFileName.length());
178   base::RunLoop().RunUntilIdle();
179
180   EXPECT_CALL(cdm_file_, Read(_))
181       .WillOnce([](mojom::CdmFile::ReadCallback callback) {
182         std::move(callback).Run(mojom::CdmFile::Status::kSuccess, {1, 2, 3, 4});
183       });
184   EXPECT_CALL(*client_.get(), OnReadComplete(Status::kSuccess, _, 4));
185   EXPECT_CALL(*client_.get(), OnReadComplete(Status::kInUse, _, 0));
186   file_io_->Read();
187   file_io_->Read();
188   base::RunLoop().RunUntilIdle();
189 }
190
191 TEST_F(MojoCdmFileIOTest, Write) {
192   const std::string kFileName = "writefile";
193   std::vector<uint8_t> data{1, 2, 3, 4, 5};
194
195   EXPECT_CALL(*client_.get(), OnOpenComplete(Status::kSuccess));
196   file_io_->Open(kFileName.data(), kFileName.length());
197   base::RunLoop().RunUntilIdle();
198
199   // Writing always succeeds.
200   EXPECT_CALL(cdm_file_, Write(_, _))
201       .WillOnce([](Unused, mojom::CdmFile::WriteCallback callback) {
202         std::move(callback).Run(mojom::CdmFile::Status::kSuccess);
203       });
204   EXPECT_CALL(*client_.get(), OnWriteComplete(Status::kSuccess));
205   file_io_->Write(data.data(), data.size());
206   base::RunLoop().RunUntilIdle();
207 }
208
209 TEST_F(MojoCdmFileIOTest, WriteBeforeOpen) {
210   std::vector<uint8_t> data{1, 2, 3, 4, 5};
211
212   // File not open, so writing should fail.
213   EXPECT_CALL(*client_.get(), OnWriteComplete(Status::kError));
214   file_io_->Write(data.data(), data.size());
215   base::RunLoop().RunUntilIdle();
216 }
217
218 TEST_F(MojoCdmFileIOTest, TwoWrites) {
219   const std::string kFileName = "writefile";
220   std::vector<uint8_t> data{1, 2, 3, 4, 5};
221
222   EXPECT_CALL(*client_.get(), OnOpenComplete(Status::kSuccess));
223   file_io_->Open(kFileName.data(), kFileName.length());
224   base::RunLoop().RunUntilIdle();
225
226   EXPECT_CALL(cdm_file_, Write(_, _))
227       .WillOnce([](Unused, mojom::CdmFile::WriteCallback callback) {
228         std::move(callback).Run(mojom::CdmFile::Status::kSuccess);
229       });
230   EXPECT_CALL(*client_.get(), OnWriteComplete(Status::kSuccess));
231   EXPECT_CALL(*client_.get(), OnWriteComplete(Status::kInUse));
232   file_io_->Write(data.data(), data.size());
233   file_io_->Write(data.data(), data.size());
234   base::RunLoop().RunUntilIdle();
235 }
236
237 }  // namespace media