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