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.
4
5#include <string>
6
7#include "base/base_paths.h"
8#include "base/bind.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"
14
15using ::testing::NiceMock;
16using ::testing::StrictMock;
17
18namespace media {
19
20class ReadCBHandler {
21 public:
22  ReadCBHandler() {}
23
24  MOCK_METHOD1(ReadCB, void(int size));
25
26 private:
27  DISALLOW_COPY_AND_ASSIGN(ReadCBHandler);
28};
29
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.
36base::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"));
43  return data_dir;
44}
45
46// Use the mock filter host to directly call the Read and GetPosition methods.
47TEST(FileDataSourceTest, ReadData) {
48  int64 size;
49  uint8 ten_bytes[10];
50
51  // Create our mock filter host and initialize the data source.
52  FileDataSource data_source;
53
54  EXPECT_TRUE(data_source.Initialize(TestFileURL()));
55  EXPECT_TRUE(data_source.GetSize(&size));
56  EXPECT_EQ(10, size);
57
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]);
65
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]);
70
71  EXPECT_CALL(handler, ReadCB(0));
72  data_source.Read(10, 10, ten_bytes, base::Bind(
73      &ReadCBHandler::ReadCB, base::Unretained(&handler)));
74
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]);
79
80  data_source.Stop();
81}
82
83}  // namespace media
84