cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / components / drive / local_file_reader_unittest.cc
blobe7790e734439e9c7c3fa8ec08846241764ee0372
1 // Copyright 2013 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 "components/drive/local_file_reader.h"
7 #include <string>
9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/rand_util.h"
15 #include "base/threading/thread.h"
16 #include "components/drive/drive_test_util.h"
17 #include "google_apis/drive/test_util.h"
18 #include "net/base/io_buffer.h"
19 #include "net/base/test_completion_callback.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace drive {
23 namespace util {
24 namespace {
26 // An adapter to use test_util::ReadAllData.
27 class LocalFileReaderAdapter {
28 public:
29 explicit LocalFileReaderAdapter(LocalFileReader* reader) : reader_(reader) {}
30 int Read(net::IOBuffer* buffer,
31 int buffer_length,
32 const net::CompletionCallback& callback) {
33 reader_->Read(buffer, buffer_length, callback);
34 // As LocalFileReader::Read always works asynchronously,
35 // return ERR_IO_PENDING.
36 return net::ERR_IO_PENDING;
39 private:
40 LocalFileReader* reader_;
43 } // namespace
45 class LocalFileReaderTest : public ::testing::Test {
46 protected:
47 void SetUp() override {
48 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
49 worker_thread_.reset(new base::Thread("LocalFileReaderTest"));
50 ASSERT_TRUE(worker_thread_->Start());
51 file_reader_.reset(
52 new LocalFileReader(worker_thread_->task_runner().get()));
55 base::MessageLoop message_loop_;
56 base::ScopedTempDir temp_dir_;
57 scoped_ptr<base::Thread> worker_thread_;
58 scoped_ptr<LocalFileReader> file_reader_;
61 TEST_F(LocalFileReaderTest, NonExistingFile) {
62 const base::FilePath kTestFile =
63 temp_dir_.path().AppendASCII("non-existing");
65 net::TestCompletionCallback callback;
66 file_reader_->Open(kTestFile, 0, callback.callback());
67 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, callback.WaitForResult());
70 TEST_F(LocalFileReaderTest, FullRead) {
71 base::FilePath test_file;
72 std::string expected_content;
73 ASSERT_TRUE(google_apis::test_util::CreateFileOfSpecifiedSize(
74 temp_dir_.path(), 1024, &test_file, &expected_content));
76 net::TestCompletionCallback callback;
77 file_reader_->Open(test_file, 0, callback.callback());
78 ASSERT_EQ(net::OK, callback.WaitForResult());
80 LocalFileReaderAdapter adapter(file_reader_.get());
81 std::string content;
82 ASSERT_EQ(net::OK, test_util::ReadAllData(&adapter, &content));
83 EXPECT_EQ(expected_content, content);
86 TEST_F(LocalFileReaderTest, OpenWithOffset) {
87 base::FilePath test_file;
88 std::string expected_content;
89 ASSERT_TRUE(google_apis::test_util::CreateFileOfSpecifiedSize(
90 temp_dir_.path(), 1024, &test_file, &expected_content));
92 size_t offset = expected_content.size() / 2;
93 expected_content.erase(0, offset);
95 net::TestCompletionCallback callback;
96 file_reader_->Open(
97 test_file, static_cast<int64>(offset), callback.callback());
98 ASSERT_EQ(net::OK, callback.WaitForResult());
100 LocalFileReaderAdapter adapter(file_reader_.get());
101 std::string content;
102 ASSERT_EQ(net::OK, test_util::ReadAllData(&adapter, &content));
103 EXPECT_EQ(expected_content, content);
106 } // namespace util
107 } // namespace drive