OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 private: | 28 private: |
29 base::MessageLoop message_loop_; | 29 base::MessageLoop message_loop_; |
30 content::TestBrowserThread file_thread_; | 30 content::TestBrowserThread file_thread_; |
31 }; | 31 }; |
32 | 32 |
33 class Receiver { | 33 class Receiver { |
34 public: | 34 public: |
35 Receiver() : succeeded_(false) { | 35 Receiver() : succeeded_(false) { |
36 } | 36 } |
37 | 37 |
38 FileReader::Callback NewCallback() { | 38 FileReader::DoneCallback NewCallback() { |
39 return base::Bind(&Receiver::DidReadFile, base::Unretained(this)); | 39 return base::Bind(&Receiver::DidReadFile, base::Unretained(this)); |
40 } | 40 } |
41 | 41 |
42 bool succeeded() const { return succeeded_; } | 42 bool succeeded() const { return succeeded_; } |
43 const std::string& data() const { return *data_; } | 43 const std::string& data() const { return *data_; } |
44 | 44 |
45 private: | 45 private: |
46 void DidReadFile(bool success, std::unique_ptr<std::string> data) { | 46 void DidReadFile(bool success, std::unique_ptr<std::string> data) { |
47 succeeded_ = success; | 47 succeeded_ = success; |
48 data_ = std::move(data); | 48 data_ = std::move(data); |
(...skipping 11 matching lines...) Expand all Loading... |
60 ExtensionResource resource( | 60 ExtensionResource resource( |
61 extension_id, path, base::FilePath().AppendASCII(filename)); | 61 extension_id, path, base::FilePath().AppendASCII(filename)); |
62 path = path.AppendASCII(filename); | 62 path = path.AppendASCII(filename); |
63 | 63 |
64 std::string file_contents; | 64 std::string file_contents; |
65 ASSERT_TRUE(base::ReadFileToString(path, &file_contents)); | 65 ASSERT_TRUE(base::ReadFileToString(path, &file_contents)); |
66 | 66 |
67 Receiver receiver; | 67 Receiver receiver; |
68 | 68 |
69 scoped_refptr<FileReader> file_reader( | 69 scoped_refptr<FileReader> file_reader( |
70 new FileReader(resource, receiver.NewCallback())); | 70 new FileReader(resource, FileReader::OptionalFileThreadTaskCallback(), |
| 71 receiver.NewCallback())); |
71 file_reader->Start(); | 72 file_reader->Start(); |
72 | 73 |
73 base::RunLoop().Run(); | 74 base::RunLoop().Run(); |
74 | 75 |
75 EXPECT_TRUE(receiver.succeeded()); | 76 EXPECT_TRUE(receiver.succeeded()); |
76 EXPECT_EQ(file_contents, receiver.data()); | 77 EXPECT_EQ(file_contents, receiver.data()); |
77 } | 78 } |
78 | 79 |
79 TEST_F(FileReaderTest, SmallFile) { | 80 TEST_F(FileReaderTest, SmallFile) { |
80 RunBasicTest("smallfile"); | 81 RunBasicTest("smallfile"); |
81 } | 82 } |
82 | 83 |
83 TEST_F(FileReaderTest, BiggerFile) { | 84 TEST_F(FileReaderTest, BiggerFile) { |
84 RunBasicTest("bigfile"); | 85 RunBasicTest("bigfile"); |
85 } | 86 } |
86 | 87 |
87 TEST_F(FileReaderTest, NonExistantFile) { | 88 TEST_F(FileReaderTest, NonExistantFile) { |
88 base::FilePath path; | 89 base::FilePath path; |
89 PathService::Get(DIR_TEST_DATA, &path); | 90 PathService::Get(DIR_TEST_DATA, &path); |
90 std::string extension_id = crx_file::id_util::GenerateId("test"); | 91 std::string extension_id = crx_file::id_util::GenerateId("test"); |
91 ExtensionResource resource(extension_id, path, base::FilePath( | 92 ExtensionResource resource(extension_id, path, base::FilePath( |
92 FILE_PATH_LITERAL("file_that_does_not_exist"))); | 93 FILE_PATH_LITERAL("file_that_does_not_exist"))); |
93 path = path.AppendASCII("file_that_does_not_exist"); | 94 path = path.AppendASCII("file_that_does_not_exist"); |
94 | 95 |
95 Receiver receiver; | 96 Receiver receiver; |
96 | 97 |
97 scoped_refptr<FileReader> file_reader( | 98 scoped_refptr<FileReader> file_reader( |
98 new FileReader(resource, receiver.NewCallback())); | 99 new FileReader(resource, FileReader::OptionalFileThreadTaskCallback(), |
| 100 receiver.NewCallback())); |
99 file_reader->Start(); | 101 file_reader->Start(); |
100 | 102 |
101 base::RunLoop().Run(); | 103 base::RunLoop().Run(); |
102 | 104 |
103 EXPECT_FALSE(receiver.succeeded()); | 105 EXPECT_FALSE(receiver.succeeded()); |
104 } | 106 } |
105 | 107 |
106 } // namespace extensions | 108 } // namespace extensions |
OLD | NEW |