local_file_reader_unittest.cc revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
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.
4
5#include "chrome/browser/chromeos/drive/local_file_reader.h"
6
7#include <string>
8
9#include "base/file_util.h"
10#include "base/files/file_path.h"
11#include "base/files/scoped_temp_dir.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/message_loop.h"
15#include "base/rand_util.h"
16#include "base/threading/thread.h"
17#include "chrome/browser/chromeos/drive/test_util.h"
18#include "chrome/browser/google_apis/test_util.h"
19#include "net/base/io_buffer.h"
20#include "net/base/test_completion_callback.h"
21#include "testing/gtest/include/gtest/gtest.h"
22
23namespace drive {
24namespace util {
25namespace {
26
27// An adapter to use test_util::ReadAllData.
28class LocalFileReaderAdapter {
29 public:
30  explicit LocalFileReaderAdapter(LocalFileReader* reader) : reader_(reader) {}
31  int Read(net::IOBuffer* buffer,
32           int buffer_length,
33           const net::CompletionCallback& callback) {
34    reader_->Read(buffer, buffer_length, callback);
35    // As LocalFileReader::Read always works asynchronously,
36    // return ERR_IO_PENDING.
37    return net::ERR_IO_PENDING;
38  }
39
40 private:
41  LocalFileReader* reader_;
42};
43
44}  // namespace
45
46class LocalFileReaderTest : public ::testing::Test {
47 protected:
48  LocalFileReaderTest() {
49  }
50
51  virtual void SetUp() OVERRIDE {
52    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
53    worker_thread_.reset(new base::Thread("LocalFileReaderTest"));
54    ASSERT_TRUE(worker_thread_->Start());
55    file_reader_.reset(
56        new LocalFileReader(worker_thread_->message_loop_proxy()));
57  }
58
59  virtual void TearDown() OVERRIDE {
60    file_reader_.reset();
61    worker_thread_.reset();
62  }
63
64  MessageLoop message_loop_;
65  base::ScopedTempDir temp_dir_;
66  scoped_ptr<base::Thread> worker_thread_;
67  scoped_ptr<LocalFileReader> file_reader_;
68};
69
70TEST_F(LocalFileReaderTest, NonExistingFile) {
71  const base::FilePath kTestFile =
72      temp_dir_.path().AppendASCII("non-existing");
73
74  net::TestCompletionCallback callback;
75  file_reader_->Open(kTestFile, 0, callback.callback());
76  EXPECT_EQ(net::ERR_FILE_NOT_FOUND, callback.WaitForResult());
77}
78
79TEST_F(LocalFileReaderTest, FullRead) {
80  base::FilePath test_file;
81  std::string expected_content;
82  ASSERT_TRUE(google_apis::test_util::CreateFileOfSpecifiedSize(
83      temp_dir_.path(), 1024, &test_file, &expected_content));
84
85  net::TestCompletionCallback callback;
86  file_reader_->Open(test_file, 0, callback.callback());
87  ASSERT_EQ(net::OK, callback.WaitForResult());
88
89  LocalFileReaderAdapter adapter(file_reader_.get());
90  std::string content;
91  ASSERT_EQ(net::OK, test_util::ReadAllData(&adapter, &content));
92  EXPECT_EQ(expected_content, content);
93}
94
95TEST_F(LocalFileReaderTest, OpenWithOffset) {
96  base::FilePath test_file;
97  std::string expected_content;
98  ASSERT_TRUE(google_apis::test_util::CreateFileOfSpecifiedSize(
99      temp_dir_.path(), 1024, &test_file, &expected_content));
100
101  size_t offset = expected_content.size() / 2;
102  expected_content.erase(0, offset);
103
104  net::TestCompletionCallback callback;
105  file_reader_->Open(
106      test_file, static_cast<int64>(offset), callback.callback());
107  ASSERT_EQ(net::OK, callback.WaitForResult());
108
109  LocalFileReaderAdapter adapter(file_reader_.get());
110  std::string content;
111  ASSERT_EQ(net::OK, test_util::ReadAllData(&adapter, &content));
112  EXPECT_EQ(expected_content, content);
113}
114
115}  // namespace util
116}  // namespace drive
117