1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MEDIA_CDM_PPAPI_CDM_FILE_IO_TEST_H_
6 #define MEDIA_CDM_PPAPI_CDM_FILE_IO_TEST_H_
13 #include "base/callback.h"
14 #include "base/compiler_specific.h"
15 #include "media/cdm/ppapi/api/content_decryption_module.h"
19 typedef base::Callback
<void(bool success
)> CompletionCB
;
20 typedef base::Callback
<cdm::FileIO
*(cdm::FileIOClient
* client
)> CreateFileIOCB
;
22 // A customizable test class that tests cdm::FileIO implementation.
23 // - To create a test, call AddTestStep() to add a test step. A test step can be
24 // either an action to make (use ACTION_* types), or a result to verify (use
26 // - To run the test, simply call Run() with a completion callback. The result
27 // will be reported in the completion callback when the test is finished.
29 // The following rules apply to the test steps:
30 // - Test steps are ordered (with the exception that results in a result group
32 // - Consecutive action steps form an "action group". Consecutively result
33 // steps form a "result group". An action group is followed by a result
34 // group and vice versa.
35 // - A test must start with an action group.
36 // - To process an action group, the test runner runs (and clears) all steps
37 // in the group in the order they were added. Then it waits for test
39 // - When a cdm::FileIOClient method is called, the test runner compares the
40 // result with all results in the current result group. If no result in that
41 // group matches the test result, the test fails. Otherwise, the matching
42 // result is cleared from the group. If the group is empty, the test runner
43 // starts to process the next action group. Otherwise, the test runner keeps
44 // waiting for the next test result.
45 // - After all steps are cleared, the test passes.
46 class FileIOTest
: public cdm::FileIOClient
{
48 // Types of allowed test steps:
49 // - ACTION_* specifies the next step to test.
50 // - RESULT_* specifies the expected result of the previous step(s).
53 ACTION_OPEN
, // |test_name_| will be used used as the file name to open.
59 ACTION_CLOSE
// If ACTION_CLOSE is not specified, FileIO::Close() will be
60 // automatically called at the end of the test.
63 FileIOTest(const CreateFileIOCB
& create_file_io_cb
,
64 const std::string
& test_name
);
67 // Adds a test step in this test. |this| object doesn't take the ownership of
68 // |data|, which should be valid throughout the lifetime of |this| object.
70 StepType type
, Status status
, const uint8
* data
, uint32 data_size
);
72 // Runs this test case and returns the test result through |completion_cb|.
73 void Run(const CompletionCB
& completion_cb
);
77 // |this| object doesn't take the ownership of |data|, which should be valid
78 // throughout the lifetime of |this| object.
79 TestStep(StepType type
, Status status
, const uint8
* data
, uint32 data_size
)
80 : type(type
), status(status
), data(data
), data_size(data_size
) {}
84 // Expected status for RESULT* steps.
87 // Data to write in ACTION_WRITE, or read data in RESULT_READ.
92 // Returns whether |test_step| is a RESULT_* step.
93 static bool IsResult(const TestStep
& test_step
);
95 // Returns whether two results match.
96 static bool MatchesResult(const TestStep
& a
, const TestStep
& b
);
98 // cdm::FileIOClient implementation.
99 virtual void OnOpenComplete(Status status
) override
;
100 virtual void OnReadComplete(Status status
,
102 uint32_t data_size
) override
;
103 virtual void OnWriteComplete(Status status
) override
;
105 // Runs the next step in this test case.
108 void OnResult(const TestStep
& result
);
110 // Checks whether the test result matches this step. This can only be called
111 // when this step is a RESULT_* step.
112 bool CheckResult(const TestStep
& result
);
114 void OnTestComplete(bool success
);
116 CreateFileIOCB create_file_io_cb_
;
117 CompletionCB completion_cb_
;
119 std::string test_name_
;
120 std::list
<TestStep
> test_steps_
;
122 // All opened cdm::FileIO objects. We keep multiple cdm::FileIO objects open
123 // so that we can test multiple cdm::FileIO objects accessing the same file.
124 // In the current implementation, all ACTION_* are performed on the latest
125 // opened cdm::FileIO object, hence the stack.
126 std::stack
<cdm::FileIO
*> file_io_stack_
;
129 // Tests cdm::FileIO implementation.
130 class FileIOTestRunner
{
132 explicit FileIOTestRunner(const CreateFileIOCB
& create_file_io_cb
);
137 // Run all tests. When tests are completed, the result will be reported in the
139 void RunAllTests(const CompletionCB
& completion_cb
);
142 void OnTestComplete(bool success
);
145 CreateFileIOCB create_file_io_cb_
;
146 CompletionCB completion_cb_
;
147 std::list
<FileIOTest
> remaining_tests_
;
148 std::vector
<uint8
> large_data_
;
149 size_t total_num_tests_
; // Total number of tests.
150 size_t num_passed_tests_
; // Number of passed tests.
152 DISALLOW_COPY_AND_ASSIGN (FileIOTestRunner
);
157 #endif // MEDIA_CDM_PPAPI_CDM_FILE_IO_TEST_H_