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