OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/bind.h" | |
6 #include "base/bind_helpers.h" | |
7 #include "base/file_util.h" | |
8 #include "base/files/file_path.h" | |
9 #include "base/message_loop.h" | |
10 #include "base/path_service.h" | |
11 #include "chrome/browser/extensions/file_reader.h" | |
12 #include "chrome/common/chrome_paths.h" | |
13 #include "chrome/common/extensions/extension.h" | |
14 #include "chrome/common/extensions/extension_resource.h" | |
15 #include "chrome/common/extensions/extension_test_util.h" | |
16 #include "content/public/test/test_browser_thread.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 using content::BrowserThread; | |
20 | |
21 namespace { | |
22 | |
23 class FileReaderTest : public testing::Test { | |
24 public: | |
25 FileReaderTest() : file_thread_(BrowserThread::FILE) { | |
26 file_thread_.Start(); | |
27 } | |
28 private: | |
29 MessageLoop message_loop_; | |
30 content::TestBrowserThread file_thread_; | |
31 }; | |
32 | |
33 class Receiver { | |
34 public: | |
35 Receiver() : succeeded_(false) { | |
36 } | |
37 | |
38 FileReader::Callback NewCallback() { | |
39 return base::Bind(&Receiver::DidReadFile, base::Unretained(this)); | |
40 } | |
41 | |
42 bool succeeded() const { return succeeded_; } | |
43 const std::string& data() const { return data_; } | |
44 | |
45 private: | |
46 void DidReadFile(bool success, const std::string& data) { | |
47 succeeded_ = success; | |
48 data_ = data; | |
49 MessageLoop::current()->Quit(); | |
50 } | |
51 | |
52 bool succeeded_; | |
53 std::string data_; | |
54 }; | |
55 | |
56 void RunBasicTest(const char* filename) { | |
57 base::FilePath path; | |
58 PathService::Get(chrome::DIR_TEST_DATA, &path); | |
59 std::string extension_id = extension_test_util::MakeId("test"); | |
60 ExtensionResource resource(extension_id, path, | |
61 base::FilePath().AppendASCII(filename)); | |
62 path = path.AppendASCII(filename); | |
63 | |
64 std::string file_contents; | |
65 bool file_exists = file_util::ReadFileToString(path, &file_contents); | |
66 | |
67 Receiver receiver; | |
68 | |
69 scoped_refptr<FileReader> file_reader( | |
70 new FileReader(resource, receiver.NewCallback())); | |
71 file_reader->Start(); | |
72 | |
73 MessageLoop::current()->Run(); | |
74 | |
75 EXPECT_EQ(file_exists, receiver.succeeded()); | |
76 EXPECT_EQ(file_contents, receiver.data()); | |
77 } | |
78 | |
79 TEST_F(FileReaderTest, SmallFile) { | |
80 RunBasicTest("title1.html"); | |
81 } | |
82 | |
83 TEST_F(FileReaderTest, BiggerFile) { | |
84 RunBasicTest("download-test1.lib"); | |
85 } | |
86 | |
87 TEST_F(FileReaderTest, NonExistantFile) { | |
88 base::FilePath path; | |
89 PathService::Get(chrome::DIR_TEST_DATA, &path); | |
90 std::string extension_id = extension_test_util::MakeId("test"); | |
91 ExtensionResource resource(extension_id, path, base::FilePath( | |
92 FILE_PATH_LITERAL("file_that_does_not_exist"))); | |
93 path = path.AppendASCII("file_that_does_not_exist"); | |
94 | |
95 Receiver receiver; | |
96 | |
97 scoped_refptr<FileReader> file_reader( | |
98 new FileReader(resource, receiver.NewCallback())); | |
99 file_reader->Start(); | |
100 | |
101 MessageLoop::current()->Run(); | |
102 | |
103 EXPECT_FALSE(receiver.succeeded()); | |
104 } | |
105 | |
106 } // namespace | |
OLD | NEW |