1 // Copyright (c) 2012 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.
7 #include "base/base_paths.h"
9 #include "base/files/file_path.h"
10 #include "base/path_service.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "media/base/test_helpers.h"
13 #include "media/filters/file_data_source.h"
15 using ::testing::NiceMock;
16 using ::testing::StrictMock;
24 MOCK_METHOD1(ReadCB, void(int size));
27 DISALLOW_COPY_AND_ASSIGN(ReadCBHandler);
30 // Returns a path to the test file which contains the string "0123456789"
31 // without the quotes or any trailing space or null termination. The file lives
32 // under the media/test/data directory. Under Windows, strings for the
33 // FilePath class are unicode, and the pipeline wants char strings. Convert
34 // the string to UTF8 under Windows. For Mac and Linux, file paths are already
35 // chars so just return the string from the base::FilePath.
36 base::FilePath TestFileURL() {
37 base::FilePath data_dir;
38 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
39 data_dir = data_dir.Append(FILE_PATH_LITERAL("media"))
40 .Append(FILE_PATH_LITERAL("test"))
41 .Append(FILE_PATH_LITERAL("data"))
42 .Append(FILE_PATH_LITERAL("ten_byte_file"));
46 // Use the mock filter host to directly call the Read and GetPosition methods.
47 TEST(FileDataSourceTest, ReadData) {
51 // Create our mock filter host and initialize the data source.
52 FileDataSource data_source;
54 EXPECT_TRUE(data_source.Initialize(TestFileURL()));
55 EXPECT_TRUE(data_source.GetSize(&size));
58 ReadCBHandler handler;
59 EXPECT_CALL(handler, ReadCB(10));
60 data_source.Read(0, 10, ten_bytes, base::Bind(
61 &ReadCBHandler::ReadCB, base::Unretained(&handler)));
62 EXPECT_EQ('0', ten_bytes[0]);
63 EXPECT_EQ('5', ten_bytes[5]);
64 EXPECT_EQ('9', ten_bytes[9]);
66 EXPECT_CALL(handler, ReadCB(1));
67 data_source.Read(9, 1, ten_bytes, base::Bind(
68 &ReadCBHandler::ReadCB, base::Unretained(&handler)));
69 EXPECT_EQ('9', ten_bytes[0]);
71 EXPECT_CALL(handler, ReadCB(0));
72 data_source.Read(10, 10, ten_bytes, base::Bind(
73 &ReadCBHandler::ReadCB, base::Unretained(&handler)));
75 EXPECT_CALL(handler, ReadCB(5));
76 data_source.Read(5, 10, ten_bytes, base::Bind(
77 &ReadCBHandler::ReadCB, base::Unretained(&handler)));
78 EXPECT_EQ('5', ten_bytes[0]);