blob: 3b89e1f00e521e21571c9e8dbc319fe179b3ead4 [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]1ec27eb52011-11-03 21:59:095#include "base/bind.h"
6#include "base/bind_helpers.h"
[email protected]ecabe6ee2009-10-07 22:49:107#include "base/file_path.h"
[email protected]1683aedf2009-09-29 23:06:138#include "base/file_util.h"
9#include "base/message_loop.h"
10#include "base/path_service.h"
[email protected]ecabe6ee2009-10-07 22:49:1011#include "chrome/browser/extensions/file_reader.h"
[email protected]1683aedf2009-09-29 23:06:1312#include "chrome/common/chrome_paths.h"
[email protected]052c92702010-06-25 07:25:5213#include "chrome/common/extensions/extension.h"
[email protected]ecabe6ee2009-10-07 22:49:1014#include "chrome/common/extensions/extension_resource.h"
[email protected]af9db5f2011-10-05 05:13:1515#include "chrome/common/extensions/extension_test_util.h"
[email protected]e97882f2012-06-04 02:23:1716#include "content/public/test/test_browser_thread.h"
[email protected]1683aedf2009-09-29 23:06:1317#include "testing/gtest/include/gtest/gtest.h"
18
[email protected]631bb742011-11-02 11:29:3919using content::BrowserThread;
20
[email protected]1683aedf2009-09-29 23:06:1321namespace {
22
23class FileReaderTest : public testing::Test {
24 public:
[email protected]ca4b5fa32010-10-09 12:42:1825 FileReaderTest() : file_thread_(BrowserThread::FILE) {
[email protected]1683aedf2009-09-29 23:06:1326 file_thread_.Start();
27 }
28 private:
29 MessageLoop message_loop_;
[email protected]c38831a12011-10-28 12:44:4930 content::TestBrowserThread file_thread_;
[email protected]1683aedf2009-09-29 23:06:1331};
32
33class Receiver {
34 public:
35 Receiver() : succeeded_(false) {
36 }
37
[email protected]1ec27eb52011-11-03 21:59:0938 FileReader::Callback NewCallback() {
39 return base::Bind(&Receiver::DidReadFile, base::Unretained(this));
[email protected]1683aedf2009-09-29 23:06:1340 }
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
56void RunBasicTest(const char* filename) {
[email protected]650b2d52013-02-10 03:41:4557 base::FilePath path;
[email protected]1683aedf2009-09-29 23:06:1358 PathService::Get(chrome::DIR_TEST_DATA, &path);
[email protected]af9db5f2011-10-05 05:13:1559 std::string extension_id = extension_test_util::MakeId("test");
[email protected]052c92702010-06-25 07:25:5260 ExtensionResource resource(extension_id, path,
[email protected]650b2d52013-02-10 03:41:4561 base::FilePath().AppendASCII(filename));
[email protected]1683aedf2009-09-29 23:06:1362 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(
[email protected]ecabe6ee2009-10-07 22:49:1070 new FileReader(resource, receiver.NewCallback()));
[email protected]1683aedf2009-09-29 23:06:1371 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
79TEST_F(FileReaderTest, SmallFile) {
80 RunBasicTest("title1.html");
81}
82
83TEST_F(FileReaderTest, BiggerFile) {
84 RunBasicTest("download-test1.lib");
85}
86
87TEST_F(FileReaderTest, NonExistantFile) {
[email protected]650b2d52013-02-10 03:41:4588 base::FilePath path;
[email protected]1683aedf2009-09-29 23:06:1389 PathService::Get(chrome::DIR_TEST_DATA, &path);
[email protected]af9db5f2011-10-05 05:13:1590 std::string extension_id = extension_test_util::MakeId("test");
[email protected]650b2d52013-02-10 03:41:4591 ExtensionResource resource(extension_id, path, base::FilePath(
[email protected]ecabe6ee2009-10-07 22:49:1092 FILE_PATH_LITERAL("file_that_does_not_exist")));
[email protected]1683aedf2009-09-29 23:06:1393 path = path.AppendASCII("file_that_does_not_exist");
94
95 Receiver receiver;
96
97 scoped_refptr<FileReader> file_reader(
[email protected]ecabe6ee2009-10-07 22:49:1098 new FileReader(resource, receiver.NewCallback()));
[email protected]1683aedf2009-09-29 23:06:1399 file_reader->Start();
100
101 MessageLoop::current()->Run();
102
103 EXPECT_FALSE(receiver.succeeded());
104}
105
106} // namespace