[email protected] | af9db5f | 2011-10-05 05:13:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 1ec27eb5 | 2011-11-03 21:59:09 | [diff] [blame] | 5 | #include "base/bind.h" |
| 6 | #include "base/bind_helpers.h" |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 7 | #include "base/file_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 8 | #include "base/files/file_path.h" |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 9 | #include "base/message_loop.h" |
| 10 | #include "base/path_service.h" |
[email protected] | e97882f | 2012-06-04 02:23:17 | [diff] [blame] | 11 | #include "content/public/test/test_browser_thread.h" |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 12 | #include "extensions/browser/file_reader.h" |
[email protected] | 32efb04 | 2013-03-29 00:23:21 | [diff] [blame] | 13 | #include "extensions/common/extension_paths.h" |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 14 | #include "extensions/common/extension_resource.h" |
| 15 | #include "extensions/common/id_util.h" |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 18 | using content::BrowserThread; |
| 19 | |
[email protected] | 32efb04 | 2013-03-29 00:23:21 | [diff] [blame] | 20 | namespace extensions { |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 21 | |
| 22 | class FileReaderTest : public testing::Test { |
| 23 | public: |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 24 | FileReaderTest() : file_thread_(BrowserThread::FILE) { |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 25 | file_thread_.Start(); |
| 26 | } |
| 27 | private: |
[email protected] | 3f627b4 | 2013-05-01 16:56:14 | [diff] [blame^] | 28 | base::MessageLoop message_loop_; |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 29 | content::TestBrowserThread file_thread_; |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | class Receiver { |
| 33 | public: |
| 34 | Receiver() : succeeded_(false) { |
| 35 | } |
| 36 | |
[email protected] | 1ec27eb5 | 2011-11-03 21:59:09 | [diff] [blame] | 37 | FileReader::Callback NewCallback() { |
| 38 | return base::Bind(&Receiver::DidReadFile, base::Unretained(this)); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 39 | } |
| 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] | 3f627b4 | 2013-05-01 16:56:14 | [diff] [blame^] | 48 | base::MessageLoop::current()->Quit(); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | bool succeeded_; |
| 52 | std::string data_; |
| 53 | }; |
| 54 | |
| 55 | void RunBasicTest(const char* filename) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 56 | base::FilePath path; |
[email protected] | 32efb04 | 2013-03-29 00:23:21 | [diff] [blame] | 57 | PathService::Get(DIR_TEST_DATA, &path); |
| 58 | std::string extension_id = id_util::GenerateId("test"); |
| 59 | ExtensionResource resource( |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 60 | extension_id, path, base::FilePath().AppendASCII(filename)); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 61 | path = path.AppendASCII(filename); |
| 62 | |
| 63 | std::string file_contents; |
[email protected] | 32efb04 | 2013-03-29 00:23:21 | [diff] [blame] | 64 | ASSERT_TRUE(file_util::ReadFileToString(path, &file_contents)); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 65 | |
| 66 | Receiver receiver; |
| 67 | |
| 68 | scoped_refptr<FileReader> file_reader( |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 69 | new FileReader(resource, receiver.NewCallback())); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 70 | file_reader->Start(); |
| 71 | |
[email protected] | 3f627b4 | 2013-05-01 16:56:14 | [diff] [blame^] | 72 | base::MessageLoop::current()->Run(); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 73 | |
[email protected] | 32efb04 | 2013-03-29 00:23:21 | [diff] [blame] | 74 | EXPECT_TRUE(receiver.succeeded()); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 75 | EXPECT_EQ(file_contents, receiver.data()); |
| 76 | } |
| 77 | |
| 78 | TEST_F(FileReaderTest, SmallFile) { |
[email protected] | 32efb04 | 2013-03-29 00:23:21 | [diff] [blame] | 79 | RunBasicTest("smallfile"); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | TEST_F(FileReaderTest, BiggerFile) { |
[email protected] | 32efb04 | 2013-03-29 00:23:21 | [diff] [blame] | 83 | RunBasicTest("bigfile"); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | TEST_F(FileReaderTest, NonExistantFile) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 87 | base::FilePath path; |
[email protected] | 32efb04 | 2013-03-29 00:23:21 | [diff] [blame] | 88 | 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] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 91 | FILE_PATH_LITERAL("file_that_does_not_exist"))); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 92 | path = path.AppendASCII("file_that_does_not_exist"); |
| 93 | |
| 94 | Receiver receiver; |
| 95 | |
| 96 | scoped_refptr<FileReader> file_reader( |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 97 | new FileReader(resource, receiver.NewCallback())); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 98 | file_reader->Start(); |
| 99 | |
[email protected] | 3f627b4 | 2013-05-01 16:56:14 | [diff] [blame^] | 100 | base::MessageLoop::current()->Run(); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 101 | |
| 102 | EXPECT_FALSE(receiver.succeeded()); |
| 103 | } |
| 104 | |
[email protected] | 32efb04 | 2013-03-29 00:23:21 | [diff] [blame] | 105 | } // namespace extensions |