Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / extensions / browser / file_reader_unittest.cc
blobefd4367525954262a83f45c1a0168835ca0977db
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/bind_helpers.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/path_service.h"
11 #include "components/crx_file/id_util.h"
12 #include "content/public/test/test_browser_thread.h"
13 #include "extensions/browser/file_reader.h"
14 #include "extensions/common/extension_paths.h"
15 #include "extensions/common/extension_resource.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using content::BrowserThread;
20 namespace extensions {
22 class FileReaderTest : public testing::Test {
23 public:
24 FileReaderTest() : file_thread_(BrowserThread::FILE) {
25 file_thread_.Start();
27 private:
28 base::MessageLoop message_loop_;
29 content::TestBrowserThread file_thread_;
32 class Receiver {
33 public:
34 Receiver() : succeeded_(false) {
37 FileReader::Callback NewCallback() {
38 return base::Bind(&Receiver::DidReadFile, base::Unretained(this));
41 bool succeeded() const { return succeeded_; }
42 const std::string& data() const { return data_; }
44 private:
45 void DidReadFile(bool success, const std::string& data) {
46 succeeded_ = success;
47 data_ = data;
48 base::MessageLoop::current()->Quit();
51 bool succeeded_;
52 std::string data_;
55 void RunBasicTest(const char* filename) {
56 base::FilePath path;
57 PathService::Get(DIR_TEST_DATA, &path);
58 std::string extension_id = crx_file::id_util::GenerateId("test");
59 ExtensionResource resource(
60 extension_id, path, base::FilePath().AppendASCII(filename));
61 path = path.AppendASCII(filename);
63 std::string file_contents;
64 ASSERT_TRUE(base::ReadFileToString(path, &file_contents));
66 Receiver receiver;
68 scoped_refptr<FileReader> file_reader(
69 new FileReader(resource, receiver.NewCallback()));
70 file_reader->Start();
72 base::MessageLoop::current()->Run();
74 EXPECT_TRUE(receiver.succeeded());
75 EXPECT_EQ(file_contents, receiver.data());
78 TEST_F(FileReaderTest, SmallFile) {
79 RunBasicTest("smallfile");
82 TEST_F(FileReaderTest, BiggerFile) {
83 RunBasicTest("bigfile");
86 TEST_F(FileReaderTest, NonExistantFile) {
87 base::FilePath path;
88 PathService::Get(DIR_TEST_DATA, &path);
89 std::string extension_id = crx_file::id_util::GenerateId("test");
90 ExtensionResource resource(extension_id, path, base::FilePath(
91 FILE_PATH_LITERAL("file_that_does_not_exist")));
92 path = path.AppendASCII("file_that_does_not_exist");
94 Receiver receiver;
96 scoped_refptr<FileReader> file_reader(
97 new FileReader(resource, receiver.NewCallback()));
98 file_reader->Start();
100 base::MessageLoop::current()->Run();
102 EXPECT_FALSE(receiver.succeeded());
105 } // namespace extensions