15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef MEDIA_CDM_PPAPI_CDM_FILE_IO_TEST_H_
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define MEDIA_CDM_PPAPI_CDM_FILE_IO_TEST_H_
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <list>
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <stack>
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <string>
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <vector>
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/callback.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/compiler_specific.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "media/cdm/ppapi/api/content_decryption_module.h"
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace media {
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)typedef base::Callback<void(bool success)> CompletionCB;
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)typedef base::Callback<cdm::FileIO*(cdm::FileIOClient* client)> CreateFileIOCB;
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// A customizable test class that tests cdm::FileIO implementation.
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// - To create a test, call AddTestStep() to add a test step. A test step can be
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   either an action to make (use ACTION_* types), or a result to verify (use
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   RESULT_* types).
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// - To run the test, simply call Run() with a completion callback. The result
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   will be reported in the completion callback when the test is finished.
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The following rules apply to the test steps:
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// - Test steps are ordered (with the exception that results in a result group
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   is not ordered).
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// - Consecutive action steps form an "action group". Consecutively result
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   steps form a "result group". An action group is followed by a result
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   group and vice versa.
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// - A test must start with an action group.
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// - To process an action group, the test runner runs (and clears) all steps
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   in the group in the order they were added. Then it waits for test
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   results.
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// - When a cdm::FileIOClient method is called, the test runner compares the
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   result with all results in the current result group. If no result in that
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   group matches the test result, the test fails. Otherwise, the matching
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   result is cleared from the group. If the group is empty, the test runner
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   starts to process the next action group. Otherwise, the test runner keeps
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   waiting for the next test result.
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// - After all steps are cleared, the test passes.
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class FileIOTest : public cdm::FileIOClient {
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Types of allowed test steps:
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // - ACTION_* specifies the next step to test.
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // - RESULT_* specifies the expected result of the previous step(s).
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  enum StepType {
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ACTION_CREATE,
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ACTION_OPEN,  // |test_name_| will be used used as the file name to open.
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    RESULT_OPEN,
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ACTION_READ,
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    RESULT_READ,
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ACTION_WRITE,
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    RESULT_WRITE,
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ACTION_CLOSE  // If ACTION_CLOSE is not specified, FileIO::Close() will be
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                  // automatically called at the end of the test.
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FileIOTest(const CreateFileIOCB& create_file_io_cb,
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)             const std::string& test_name);
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ~FileIOTest();
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Adds a test step in this test. |this| object doesn't take the ownership of
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |data|, which should be valid throughout the lifetime of |this| object.
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void AddTestStep(
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      StepType type, Status status, const uint8* data, uint32 data_size);
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Runs this test case and returns the test result through |completion_cb|.
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void Run(const CompletionCB& completion_cb);
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  struct TestStep {
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // |this| object doesn't take the ownership of |data|, which should be valid
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // throughout the lifetime of |this| object.
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    TestStep(StepType type, Status status, const uint8* data, uint32 data_size)
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        : type(type), status(status), data(data), data_size(data_size) {}
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    StepType type;
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Expected status for RESULT* steps.
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    Status status;
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Data to write in ACTION_WRITE, or read data in RESULT_READ.
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const uint8* data;
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    uint32 data_size;
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns whether |test_step| is a RESULT_* step.
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static bool IsResult(const TestStep& test_step);
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns whether two results match.
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static bool MatchesResult(const TestStep& a, const TestStep& b);
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // cdm::FileIOClient implementation.
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnOpenComplete(Status status) OVERRIDE;
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnReadComplete(Status status,
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                              const uint8_t* data,
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                              uint32_t data_size) OVERRIDE;
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnWriteComplete(Status status) OVERRIDE;
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Runs the next step in this test case.
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void RunNextStep();
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void OnResult(const TestStep& result);
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Checks whether the test result matches this step. This can only be called
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // when this step is a RESULT_* step.
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool CheckResult(const TestStep& result);
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void OnTestComplete(bool success);
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CreateFileIOCB create_file_io_cb_;
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CompletionCB completion_cb_;
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string test_name_;
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::list<TestStep> test_steps_;
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // All opened cdm::FileIO objects. We keep multiple cdm::FileIO objects open
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // so that we can test multiple cdm::FileIO objects accessing the same file.
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // In the current implementation, all ACTION_* are performed on the latest
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // opened cdm::FileIO object, hence the stack.
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::stack<cdm::FileIO*> file_io_stack_;
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Tests cdm::FileIO implementation.
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class FileIOTestRunner {
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  explicit FileIOTestRunner(const CreateFileIOCB& create_file_io_cb);
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ~FileIOTestRunner();
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void AddTests();
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Run all tests. When tests are completed, the result will be reported in the
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |completion_cb|.
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void RunAllTests(const CompletionCB& completion_cb);
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void OnTestComplete(bool success);
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void RunNextTest();
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CreateFileIOCB create_file_io_cb_;
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CompletionCB completion_cb_;
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::list<FileIOTest> remaining_tests_;
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<uint8> large_data_;
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  size_t total_num_tests_;  // Total number of tests.
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  size_t num_passed_tests_;  // Number of passed tests.
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN (FileIOTestRunner);
1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace media
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // MEDIA_CDM_PPAPI_CDM_FILE_IO_TEST_H_
158