1 // Copyright 2012 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.
9 #include "base/base_paths.h"
10 #include "base/files/file_path.h"
11 #include "base/functional/bind.h"
12 #include "base/path_service.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "media/base/test_helpers.h"
15 #include "media/filters/file_data_source.h"
17 using ::testing::NiceMock;
18 using ::testing::StrictMock;
24 ReadCBHandler() = default;
26 ReadCBHandler(const ReadCBHandler&) = delete;
27 ReadCBHandler& operator=(const ReadCBHandler&) = delete;
29 MOCK_METHOD1(ReadCB, void(int size));
32 // Returns a path to the test file which contains the string "0123456789"
33 // without the quotes or any trailing space or null termination. The file lives
34 // under the media/test/data directory. Under Windows, strings for the
35 // FilePath class are unicode, and the pipeline wants char strings. Convert
36 // the string to UTF8 under Windows. For Mac and Linux, file paths are already
37 // chars so just return the string from the base::FilePath.
38 base::FilePath TestFileURL() {
39 base::FilePath data_dir;
40 EXPECT_TRUE(base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &data_dir));
41 data_dir = data_dir.Append(FILE_PATH_LITERAL("media"))
42 .Append(FILE_PATH_LITERAL("test"))
43 .Append(FILE_PATH_LITERAL("data"))
44 .Append(FILE_PATH_LITERAL("ten_byte_file"));
48 // Use the mock filter host to directly call the Read and GetPosition methods.
49 TEST(FileDataSourceTest, ReadData) {
51 uint8_t ten_bytes[10];
53 // Create our mock filter host and initialize the data source.
54 FileDataSource data_source;
56 EXPECT_TRUE(data_source.Initialize(TestFileURL()));
57 EXPECT_TRUE(data_source.GetSize(&size));
60 ReadCBHandler handler;
61 EXPECT_CALL(handler, ReadCB(10));
64 base::BindOnce(&ReadCBHandler::ReadCB, base::Unretained(&handler)));
65 EXPECT_EQ('0', ten_bytes[0]);
66 EXPECT_EQ('5', ten_bytes[5]);
67 EXPECT_EQ('9', ten_bytes[9]);
69 EXPECT_CALL(handler, ReadCB(1));
72 base::BindOnce(&ReadCBHandler::ReadCB, base::Unretained(&handler)));
73 EXPECT_EQ('9', ten_bytes[0]);
75 EXPECT_CALL(handler, ReadCB(0));
78 base::BindOnce(&ReadCBHandler::ReadCB, base::Unretained(&handler)));
80 EXPECT_CALL(handler, ReadCB(5));
83 base::BindOnce(&ReadCBHandler::ReadCB, base::Unretained(&handler)));
84 EXPECT_EQ('5', ten_bytes[0]);