- add sources.
[platform/framework/web/crosswalk.git] / src / webkit / browser / fileapi / local_file_stream_writer_unittest.cc
1 // Copyright (c) 2012 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 "webkit/browser/fileapi/local_file_stream_writer.h"
6
7 #include <string>
8
9 #include "base/callback.h"
10 #include "base/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h"
16 #include "base/threading/thread.h"
17 #include "net/base/io_buffer.h"
18 #include "net/base/test_completion_callback.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace fileapi {
22
23 class LocalFileStreamWriterTest : public testing::Test {
24  public:
25   LocalFileStreamWriterTest()
26       : file_thread_("FileUtilProxyTestFileThread") {}
27
28   virtual void SetUp() OVERRIDE {
29     ASSERT_TRUE(file_thread_.Start());
30     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
31   }
32
33   virtual void TearDown() OVERRIDE {
34     // Give another chance for deleted streams to perform Close.
35     base::RunLoop().RunUntilIdle();
36     file_thread_.Stop();
37     base::RunLoop().RunUntilIdle();
38   }
39
40  protected:
41   base::FilePath Path(const std::string& name) {
42     return temp_dir_.path().AppendASCII(name);
43   }
44
45   int WriteStringToWriter(LocalFileStreamWriter* writer,
46                           const std::string& data) {
47     scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer(data));
48     scoped_refptr<net::DrainableIOBuffer> drainable(
49         new net::DrainableIOBuffer(buffer.get(), buffer->size()));
50
51     while (drainable->BytesRemaining() > 0) {
52       net::TestCompletionCallback callback;
53       int result = writer->Write(
54           drainable.get(), drainable->BytesRemaining(), callback.callback());
55       if (result == net::ERR_IO_PENDING)
56         result = callback.WaitForResult();
57       if (result <= 0)
58         return result;
59       drainable->DidConsume(result);
60     }
61     return net::OK;
62   }
63
64   std::string GetFileContent(const base::FilePath& path) {
65     std::string content;
66     base::ReadFileToString(path, &content);
67     return content;
68   }
69
70   base::FilePath CreateFileWithContent(const std::string& name,
71                                  const std::string& data) {
72     base::FilePath path = Path(name);
73     file_util::WriteFile(path, data.c_str(), data.size());
74     return path;
75   }
76
77   base::MessageLoopProxy* file_task_runner() const {
78     return file_thread_.message_loop_proxy().get();
79   }
80
81   LocalFileStreamWriter* CreateWriter(const base::FilePath& path,
82                                       int64 offset) {
83     return new LocalFileStreamWriter(file_task_runner(), path, offset);
84   }
85
86  private:
87   base::MessageLoopForIO message_loop_;
88   base::Thread file_thread_;
89   base::ScopedTempDir temp_dir_;
90 };
91
92 void NeverCalled(int unused) {
93   ADD_FAILURE();
94 }
95
96 TEST_F(LocalFileStreamWriterTest, Write) {
97   base::FilePath path = CreateFileWithContent("file_a", std::string());
98   scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0));
99   EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo"));
100   EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "bar"));
101   writer.reset();
102   base::RunLoop().RunUntilIdle();
103   EXPECT_TRUE(base::PathExists(path));
104   EXPECT_EQ("foobar", GetFileContent(path));
105 }
106
107 TEST_F(LocalFileStreamWriterTest, WriteMiddle) {
108   base::FilePath path = CreateFileWithContent("file_a", "foobar");
109   scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 2));
110   EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx"));
111   writer.reset();
112   base::RunLoop().RunUntilIdle();
113   EXPECT_TRUE(base::PathExists(path));
114   EXPECT_EQ("foxxxr", GetFileContent(path));
115 }
116
117 TEST_F(LocalFileStreamWriterTest, WriteEnd) {
118   base::FilePath path = CreateFileWithContent("file_a", "foobar");
119   scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 6));
120   EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx"));
121   writer.reset();
122   base::RunLoop().RunUntilIdle();
123   EXPECT_TRUE(base::PathExists(path));
124   EXPECT_EQ("foobarxxx", GetFileContent(path));
125 }
126
127 TEST_F(LocalFileStreamWriterTest, WriteFailForNonexistingFile) {
128   base::FilePath path = Path("file_a");
129   ASSERT_FALSE(base::PathExists(path));
130   scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0));
131   EXPECT_EQ(net::ERR_FILE_NOT_FOUND, WriteStringToWriter(writer.get(), "foo"));
132   writer.reset();
133   base::RunLoop().RunUntilIdle();
134   EXPECT_FALSE(base::PathExists(path));
135 }
136
137 TEST_F(LocalFileStreamWriterTest, CancelBeforeOperation) {
138   base::FilePath path = Path("file_a");
139   scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0));
140   // Cancel immediately fails when there's no in-flight operation.
141   int cancel_result = writer->Cancel(base::Bind(&NeverCalled));
142   EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result);
143 }
144
145 TEST_F(LocalFileStreamWriterTest, CancelAfterFinishedOperation) {
146   base::FilePath path = CreateFileWithContent("file_a", std::string());
147   scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0));
148   EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo"));
149
150   // Cancel immediately fails when there's no in-flight operation.
151   int cancel_result = writer->Cancel(base::Bind(&NeverCalled));
152   EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result);
153
154   writer.reset();
155   base::RunLoop().RunUntilIdle();
156   // Write operation is already completed.
157   EXPECT_TRUE(base::PathExists(path));
158   EXPECT_EQ("foo", GetFileContent(path));
159 }
160
161 TEST_F(LocalFileStreamWriterTest, CancelWrite) {
162   base::FilePath path = CreateFileWithContent("file_a", "foobar");
163   scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0));
164
165   scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer("xxx"));
166   int result =
167       writer->Write(buffer.get(), buffer->size(), base::Bind(&NeverCalled));
168   ASSERT_EQ(net::ERR_IO_PENDING, result);
169
170   net::TestCompletionCallback callback;
171   writer->Cancel(callback.callback());
172   int cancel_result = callback.WaitForResult();
173   EXPECT_EQ(net::OK, cancel_result);
174 }
175
176 }  // namespace fileapi