blob: b02e0509b4e7caae9eff1d9cffa4617fbe79ec14 [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]1683aedf2009-09-29 23:06:137#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:528#include "base/files/file_path.h"
[email protected]1683aedf2009-09-29 23:06:139#include "base/message_loop.h"
10#include "base/path_service.h"
[email protected]e97882f2012-06-04 02:23:1711#include "content/public/test/test_browser_thread.h"
[email protected]993da5e2013-03-23 21:25:1612#include "extensions/browser/file_reader.h"
[email protected]32efb042013-03-29 00:23:2113#include "extensions/common/extension_paths.h"
[email protected]993da5e2013-03-23 21:25:1614#include "extensions/common/extension_resource.h"
15#include "extensions/common/id_util.h"
[email protected]1683aedf2009-09-29 23:06:1316#include "testing/gtest/include/gtest/gtest.h"
17
[email protected]631bb742011-11-02 11:29:3918using content::BrowserThread;
19
[email protected]32efb042013-03-29 00:23:2120namespace extensions {
[email protected]1683aedf2009-09-29 23:06:1321
22class FileReaderTest : public testing::Test {
23 public:
[email protected]ca4b5fa32010-10-09 12:42:1824 FileReaderTest() : file_thread_(BrowserThread::FILE) {
[email protected]1683aedf2009-09-29 23:06:1325 file_thread_.Start();
26 }
27 private:
[email protected]3f627b42013-05-01 16:56:1428 base::MessageLoop message_loop_;
[email protected]c38831a12011-10-28 12:44:4929 content::TestBrowserThread file_thread_;
[email protected]1683aedf2009-09-29 23:06:1330};
31
32class Receiver {
33 public:
34 Receiver() : succeeded_(false) {
35 }
36
[email protected]1ec27eb52011-11-03 21:59:0937 FileReader::Callback NewCallback() {
38 return base::Bind(&Receiver::DidReadFile, base::Unretained(this));
[email protected]1683aedf2009-09-29 23:06:1339 }
40
41 bool succeeded() const { return succeeded_; }
42 const std::string& data() const { return data_; }
43
44 private:
45 void DidReadFile(bool success, const std::string& data) {
46 succeeded_ = success;
47 data_ = data;
[email protected]3f627b42013-05-01 16:56:1448 base::MessageLoop::current()->Quit();
[email protected]1683aedf2009-09-29 23:06:1349 }
50
51 bool succeeded_;
52 std::string data_;
53};
54
55void RunBasicTest(const char* filename) {
[email protected]650b2d52013-02-10 03:41:4556 base::FilePath path;
[email protected]32efb042013-03-29 00:23:2157 PathService::Get(DIR_TEST_DATA, &path);
58 std::string extension_id = id_util::GenerateId("test");
59 ExtensionResource resource(
[email protected]993da5e2013-03-23 21:25:1660 extension_id, path, base::FilePath().AppendASCII(filename));
[email protected]1683aedf2009-09-29 23:06:1361 path = path.AppendASCII(filename);
62
63 std::string file_contents;
[email protected]32efb042013-03-29 00:23:2164 ASSERT_TRUE(file_util::ReadFileToString(path, &file_contents));
[email protected]1683aedf2009-09-29 23:06:1365
66 Receiver receiver;
67
68 scoped_refptr<FileReader> file_reader(
[email protected]ecabe6ee2009-10-07 22:49:1069 new FileReader(resource, receiver.NewCallback()));
[email protected]1683aedf2009-09-29 23:06:1370 file_reader->Start();
71
[email protected]3f627b42013-05-01 16:56:1472 base::MessageLoop::current()->Run();
[email protected]1683aedf2009-09-29 23:06:1373
[email protected]32efb042013-03-29 00:23:2174 EXPECT_TRUE(receiver.succeeded());
[email protected]1683aedf2009-09-29 23:06:1375 EXPECT_EQ(file_contents, receiver.data());
76}
77
78TEST_F(FileReaderTest, SmallFile) {
[email protected]32efb042013-03-29 00:23:2179 RunBasicTest("smallfile");
[email protected]1683aedf2009-09-29 23:06:1380}
81
82TEST_F(FileReaderTest, BiggerFile) {
[email protected]32efb042013-03-29 00:23:2183 RunBasicTest("bigfile");
[email protected]1683aedf2009-09-29 23:06:1384}
85
86TEST_F(FileReaderTest, NonExistantFile) {
[email protected]650b2d52013-02-10 03:41:4587 base::FilePath path;
[email protected]32efb042013-03-29 00:23:2188 PathService::Get(DIR_TEST_DATA, &path);
89 std::string extension_id = id_util::GenerateId("test");
90 ExtensionResource resource(extension_id, path, base::FilePath(
[email protected]ecabe6ee2009-10-07 22:49:1091 FILE_PATH_LITERAL("file_that_does_not_exist")));
[email protected]1683aedf2009-09-29 23:06:1392 path = path.AppendASCII("file_that_does_not_exist");
93
94 Receiver receiver;
95
96 scoped_refptr<FileReader> file_reader(
[email protected]ecabe6ee2009-10-07 22:49:1097 new FileReader(resource, receiver.NewCallback()));
[email protected]1683aedf2009-09-29 23:06:1398 file_reader->Start();
99
[email protected]3f627b42013-05-01 16:56:14100 base::MessageLoop::current()->Run();
[email protected]1683aedf2009-09-29 23:06:13101
102 EXPECT_FALSE(receiver.succeeded());
103}
104
[email protected]32efb042013-03-29 00:23:21105} // namespace extensions