10
10
#include " base/callback.h"
11
11
#include " base/files/file.h"
12
12
#include " base/logging.h"
13
- #include " base/run_loop.h"
14
13
#include " base/test/test_future.h"
15
14
#include " base/test/with_feature_override.h"
16
15
#include " content/browser/media/media_license_manager.h"
@@ -50,36 +49,6 @@ void SimulateNavigation(RenderFrameHost** rfh, const GURL& url) {
50
49
*rfh = navigation_simulator->GetFinalRenderFrameHost ();
51
50
}
52
51
53
- // Helper that wraps a base::RunLoop and only quits the RunLoop
54
- // if the expected number of quit calls have happened.
55
- class RunLoopWithExpectedCount {
56
- public:
57
- RunLoopWithExpectedCount () = default ;
58
-
59
- RunLoopWithExpectedCount (const RunLoopWithExpectedCount&) = delete ;
60
- RunLoopWithExpectedCount& operator =(const RunLoopWithExpectedCount&) = delete ;
61
-
62
- ~RunLoopWithExpectedCount () { DCHECK_EQ (0 , remaining_quit_calls_); }
63
-
64
- void Run (int expected_quit_calls) {
65
- DCHECK_GT (expected_quit_calls, 0 );
66
- DCHECK_EQ (remaining_quit_calls_, 0 );
67
- remaining_quit_calls_ = expected_quit_calls;
68
- run_loop_ = std::make_unique<base::RunLoop>();
69
- run_loop_->Run ();
70
- }
71
-
72
- void Quit () {
73
- if (--remaining_quit_calls_ > 0 )
74
- return ;
75
- run_loop_->Quit ();
76
- }
77
-
78
- private:
79
- std::unique_ptr<base::RunLoop> run_loop_;
80
- int remaining_quit_calls_ = 0 ;
81
- };
82
-
83
52
} // namespace
84
53
85
54
class CdmStorageTest : public base ::test::WithFeatureOverride,
@@ -159,23 +128,6 @@ class CdmStorageTest : public base::test::WithFeatureOverride,
159
128
return status == CdmFile::Status::kSuccess ;
160
129
}
161
130
162
- // Attempts to reads the contents of the previously opened |cdm_file| twice.
163
- // We don't really care about the data, just that 1 read succeeds and the
164
- // other fails.
165
- void ReadTwice (CdmFile* cdm_file,
166
- CdmFile::Status* status1,
167
- CdmFile::Status* status2) {
168
- DVLOG (3 ) << __func__;
169
- std::vector<uint8_t > data1;
170
- std::vector<uint8_t > data2;
171
-
172
- cdm_file->Read (base::BindOnce (&CdmStorageTest::FileRead,
173
- base::Unretained (this ), status1, &data1));
174
- cdm_file->Read (base::BindOnce (&CdmStorageTest::FileRead,
175
- base::Unretained (this ), status2, &data2));
176
- RunAndWaitForResult (2 );
177
- }
178
-
179
131
// Writes |data| to the previously opened |cdm_file|, replacing the contents
180
132
// of the file. Returns true if successful, false otherwise.
181
133
bool Write (CdmFile* cdm_file, const std::vector<uint8_t >& data) {
@@ -188,47 +140,9 @@ class CdmStorageTest : public base::test::WithFeatureOverride,
188
140
return status == CdmFile::Status::kSuccess ;
189
141
}
190
142
191
- // Attempts to write the contents of the previously opened |cdm_file| twice.
192
- // We don't really care about the data, just that 1 read succeeds and the
193
- // other fails.
194
- void WriteTwice (CdmFile* cdm_file,
195
- CdmFile::Status* status1,
196
- CdmFile::Status* status2) {
197
- DVLOG (3 ) << __func__;
198
-
199
- cdm_file->Write ({1 , 2 , 3 }, base::BindOnce (&CdmStorageTest::FileWritten,
200
- base::Unretained (this ), status1));
201
- cdm_file->Write ({4 , 5 , 6 }, base::BindOnce (&CdmStorageTest::FileWritten,
202
- base::Unretained (this ), status2));
203
- RunAndWaitForResult (2 );
204
- }
205
-
206
143
private:
207
- void FileRead (CdmFile::Status* status,
208
- std::vector<uint8_t >* data,
209
- CdmFile::Status actual_status,
210
- const std::vector<uint8_t >& actual_data) {
211
- DVLOG (3 ) << __func__;
212
- *status = actual_status;
213
- *data = actual_data;
214
- run_loop_with_count_->Quit ();
215
- }
216
-
217
- void FileWritten (CdmFile::Status* status, CdmFile::Status actual_status) {
218
- DVLOG (3 ) << __func__;
219
- *status = actual_status;
220
- run_loop_with_count_->Quit ();
221
- }
222
-
223
- // Start running and allow the asynchronous IO operations to complete.
224
- void RunAndWaitForResult (int expected_quit_calls) {
225
- run_loop_with_count_ = std::make_unique<RunLoopWithExpectedCount>();
226
- run_loop_with_count_->Run (expected_quit_calls);
227
- }
228
-
229
144
RenderFrameHost* rfh_ = nullptr ;
230
145
mojo::Remote<CdmStorage> cdm_storage_;
231
- std::unique_ptr<RunLoopWithExpectedCount> run_loop_with_count_;
232
146
};
233
147
234
148
// TODO(crbug.com/1231162): Make this a non-parameterized test suite once we no
@@ -349,9 +263,22 @@ TEST_P(CdmStorageTest, ParallelRead) {
349
263
EXPECT_TRUE (Open (kFileName , cdm_file));
350
264
EXPECT_TRUE (cdm_file.is_bound ());
351
265
352
- CdmFile::Status status1;
353
- CdmFile::Status status2;
354
- ReadTwice (cdm_file.get (), &status1, &status2);
266
+ // Attempts to reads the contents of the previously opened |cdm_file| twice.
267
+ // We don't really care about the data, just that 1 read succeeds and the
268
+ // other fails.
269
+ base::test::TestFuture<CdmFile::Status, std::vector<uint8_t >> future1;
270
+ base::test::TestFuture<CdmFile::Status, std::vector<uint8_t >> future2;
271
+
272
+ cdm_file->Read (
273
+ future1.GetCallback <CdmFile::Status, const std::vector<uint8_t >&>());
274
+ cdm_file->Read (
275
+ future2.GetCallback <CdmFile::Status, const std::vector<uint8_t >&>());
276
+
277
+ EXPECT_TRUE (future1.Wait ());
278
+ EXPECT_TRUE (future2.Wait ());
279
+
280
+ CdmFile::Status status1 = future1.Get <0 >();
281
+ CdmFile::Status status2 = future2.Get <0 >();
355
282
356
283
// One call should succeed, one should fail.
357
284
EXPECT_TRUE ((status1 == CdmFile::Status::kSuccess &&
@@ -367,9 +294,20 @@ TEST_P(CdmStorageTest, ParallelWrite) {
367
294
EXPECT_TRUE (Open (kFileName , cdm_file));
368
295
EXPECT_TRUE (cdm_file.is_bound ());
369
296
370
- CdmFile::Status status1;
371
- CdmFile::Status status2;
372
- WriteTwice (cdm_file.get (), &status1, &status2);
297
+ // Attempts to write the contents of the previously opened |cdm_file| twice.
298
+ // We don't really care about the data, just that 1 write succeeds and the
299
+ // other fails.
300
+ base::test::TestFuture<CdmFile::Status> future1;
301
+ base::test::TestFuture<CdmFile::Status> future2;
302
+
303
+ cdm_file->Write ({1 , 2 , 3 }, future1.GetCallback ());
304
+ cdm_file->Write ({4 , 5 , 6 }, future2.GetCallback ());
305
+
306
+ EXPECT_TRUE (future1.Wait ());
307
+ EXPECT_TRUE (future2.Wait ());
308
+
309
+ CdmFile::Status status1 = future1.Get ();
310
+ CdmFile::Status status2 = future2.Get ();
373
311
374
312
// One call should succeed, one should fail.
375
313
EXPECT_TRUE ((status1 == CdmFile::Status::kSuccess &&
0 commit comments