blob: 08e5a08c319b4ca14576c4fa4d6dd217433cc864 [file] [log] [blame]
[email protected]af9db5f2011-10-05 05:13:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]1683aedf2009-09-29 23:06:132// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]2041cf342010-02-19 03:15:595#include "base/callback.h"
[email protected]ecabe6ee2009-10-07 22:49:106#include "base/file_path.h"
[email protected]1683aedf2009-09-29 23:06:137#include "base/file_util.h"
8#include "base/message_loop.h"
9#include "base/path_service.h"
[email protected]ecabe6ee2009-10-07 22:49:1010#include "chrome/browser/extensions/file_reader.h"
[email protected]1683aedf2009-09-29 23:06:1311#include "chrome/common/chrome_paths.h"
[email protected]052c92702010-06-25 07:25:5212#include "chrome/common/extensions/extension.h"
[email protected]ecabe6ee2009-10-07 22:49:1013#include "chrome/common/extensions/extension_resource.h"
[email protected]af9db5f2011-10-05 05:13:1514#include "chrome/common/extensions/extension_test_util.h"
[email protected]1bda97552011-03-01 20:11:5215#include "content/browser/browser_thread.h"
[email protected]1683aedf2009-09-29 23:06:1316#include "testing/gtest/include/gtest/gtest.h"
17
18namespace {
19
20class FileReaderTest : public testing::Test {
21 public:
[email protected]ca4b5fa32010-10-09 12:42:1822 FileReaderTest() : file_thread_(BrowserThread::FILE) {
[email protected]1683aedf2009-09-29 23:06:1323 file_thread_.Start();
24 }
25 private:
26 MessageLoop message_loop_;
[email protected]ca4b5fa32010-10-09 12:42:1827 BrowserThread file_thread_;
[email protected]1683aedf2009-09-29 23:06:1328};
29
30class Receiver {
31 public:
32 Receiver() : succeeded_(false) {
33 }
34
35 FileReader::Callback* NewCallback() {
36 return ::NewCallback(this, &Receiver::DidReadFile);
37 }
38
39 bool succeeded() const { return succeeded_; }
40 const std::string& data() const { return data_; }
41
42 private:
43 void DidReadFile(bool success, const std::string& data) {
44 succeeded_ = success;
45 data_ = data;
46 MessageLoop::current()->Quit();
47 }
48
49 bool succeeded_;
50 std::string data_;
51};
52
53void RunBasicTest(const char* filename) {
54 FilePath path;
55 PathService::Get(chrome::DIR_TEST_DATA, &path);
[email protected]af9db5f2011-10-05 05:13:1556 std::string extension_id = extension_test_util::MakeId("test");
[email protected]052c92702010-06-25 07:25:5257 ExtensionResource resource(extension_id, path,
58 FilePath().AppendASCII(filename));
[email protected]1683aedf2009-09-29 23:06:1359 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(
[email protected]ecabe6ee2009-10-07 22:49:1067 new FileReader(resource, receiver.NewCallback()));
[email protected]1683aedf2009-09-29 23:06:1368 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
76TEST_F(FileReaderTest, SmallFile) {
77 RunBasicTest("title1.html");
78}
79
80TEST_F(FileReaderTest, BiggerFile) {
81 RunBasicTest("download-test1.lib");
82}
83
84TEST_F(FileReaderTest, NonExistantFile) {
85 FilePath path;
86 PathService::Get(chrome::DIR_TEST_DATA, &path);
[email protected]af9db5f2011-10-05 05:13:1587 std::string extension_id = extension_test_util::MakeId("test");
[email protected]052c92702010-06-25 07:25:5288 ExtensionResource resource(extension_id, path, FilePath(
[email protected]ecabe6ee2009-10-07 22:49:1089 FILE_PATH_LITERAL("file_that_does_not_exist")));
[email protected]1683aedf2009-09-29 23:06:1390 path = path.AppendASCII("file_that_does_not_exist");
91
92 Receiver receiver;
93
94 scoped_refptr<FileReader> file_reader(
[email protected]ecabe6ee2009-10-07 22:49:1095 new FileReader(resource, receiver.NewCallback()));
[email protected]1683aedf2009-09-29 23:06:1396 file_reader->Start();
97
98 MessageLoop::current()->Run();
99
100 EXPECT_FALSE(receiver.succeeded());
101}
102
103} // namespace