Fix some potential after free errors on TestCompletionCallback
[chromium-blink-merge.git] / media / filters / file_data_source_unittest.cc
blobf6c9b8988586c13f4a1db7bfe7ed3c4fc550840b
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.
5 #include <string>
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/mock_data_source_host.h"
13 #include "media/base/test_helpers.h"
14 #include "media/filters/file_data_source.h"
16 using ::testing::NiceMock;
17 using ::testing::StrictMock;
19 namespace media {
21 class ReadCBHandler {
22 public:
23 ReadCBHandler() {}
25 MOCK_METHOD1(ReadCB, void(int size));
27 private:
28 DISALLOW_COPY_AND_ASSIGN(ReadCBHandler);
31 // Returns a path to the test file which contains the string "0123456789"
32 // without the quotes or any trailing space or null termination. The file lives
33 // under the media/test/data directory. Under Windows, strings for the
34 // FilePath class are unicode, and the pipeline wants char strings. Convert
35 // the string to UTF8 under Windows. For Mac and Linux, file paths are already
36 // chars so just return the string from the base::FilePath.
37 base::FilePath TestFileURL() {
38 base::FilePath data_dir;
39 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
40 data_dir = data_dir.Append(FILE_PATH_LITERAL("media"))
41 .Append(FILE_PATH_LITERAL("test"))
42 .Append(FILE_PATH_LITERAL("data"))
43 .Append(FILE_PATH_LITERAL("ten_byte_file"));
44 return data_dir;
47 // Test that FileDataSource call the appropriate methods on its filter host.
48 TEST(FileDataSourceTest, OpenFile) {
49 StrictMock<MockDataSourceHost> host;
50 EXPECT_CALL(host, SetTotalBytes(10));
51 EXPECT_CALL(host, AddBufferedByteRange(0, 10));
53 FileDataSource data_source;
54 data_source.set_host(&host);
55 EXPECT_TRUE(data_source.Initialize(TestFileURL()));
57 data_source.Stop(NewExpectedClosure());
60 // Use the mock filter host to directly call the Read and GetPosition methods.
61 TEST(FileDataSourceTest, ReadData) {
62 int64 size;
63 uint8 ten_bytes[10];
65 // Create our mock filter host and initialize the data source.
66 NiceMock<MockDataSourceHost> host;
67 FileDataSource data_source;
69 data_source.set_host(&host);
70 EXPECT_TRUE(data_source.Initialize(TestFileURL()));
72 EXPECT_TRUE(data_source.GetSize(&size));
73 EXPECT_EQ(10, size);
75 ReadCBHandler handler;
76 EXPECT_CALL(handler, ReadCB(10));
77 data_source.Read(0, 10, ten_bytes, base::Bind(
78 &ReadCBHandler::ReadCB, base::Unretained(&handler)));
79 EXPECT_EQ('0', ten_bytes[0]);
80 EXPECT_EQ('5', ten_bytes[5]);
81 EXPECT_EQ('9', ten_bytes[9]);
83 EXPECT_CALL(handler, ReadCB(1));
84 data_source.Read(9, 1, ten_bytes, base::Bind(
85 &ReadCBHandler::ReadCB, base::Unretained(&handler)));
86 EXPECT_EQ('9', ten_bytes[0]);
88 EXPECT_CALL(handler, ReadCB(0));
89 data_source.Read(10, 10, ten_bytes, base::Bind(
90 &ReadCBHandler::ReadCB, base::Unretained(&handler)));
92 EXPECT_CALL(handler, ReadCB(5));
93 data_source.Read(5, 10, ten_bytes, base::Bind(
94 &ReadCBHandler::ReadCB, base::Unretained(&handler)));
95 EXPECT_EQ('5', ten_bytes[0]);
97 data_source.Stop(NewExpectedClosure());
100 } // namespace media