Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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/fileapi/local_file_writer.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/callback.h" | |
| 10 #include "base/file_util.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/scoped_temp_dir.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "net/base/io_buffer.h" | |
| 15 #include "net/base/test_completion_callback.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 using fileapi::LocalFileWriter; | |
| 21 | |
| 22 class LocalFileWriterTest : public testing::Test { | |
| 23 public: | |
| 24 virtual void SetUp() OVERRIDE { | |
| 25 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 26 } | |
|
kinuko
2012/04/20 11:26:37
Everything below this could be protected methods.
kinaba
2012/04/23 08:56:41
Done.
| |
| 27 FilePath Path(const std::string& name) { | |
| 28 return temp_dir_.path().AppendASCII(name); | |
| 29 } | |
| 30 void WriteStringToWriter(LocalFileWriter* writer, const std::string& data) { | |
| 31 scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer(data)); | |
| 32 scoped_refptr<net::DrainableIOBuffer> drainable( | |
| 33 new net::DrainableIOBuffer(buffer, buffer->size())); | |
| 34 | |
| 35 while (drainable->BytesRemaining() > 0) { | |
| 36 net::TestCompletionCallback callback; | |
| 37 int result = writer->Write(drainable, drainable->BytesRemaining(), | |
| 38 callback.callback()); | |
| 39 if (result == net::ERR_IO_PENDING) | |
| 40 result = callback.WaitForResult(); | |
| 41 EXPECT_GE(result, 0); | |
| 42 drainable->DidConsume(result); | |
| 43 } | |
| 44 } | |
| 45 void SeekWriter(LocalFileWriter* writer, int64 offset) { | |
| 46 net::TestInt64CompletionCallback callback; | |
| 47 int result = writer->Seek(offset, callback.callback()); | |
| 48 EXPECT_EQ(net::ERR_IO_PENDING, result); | |
| 49 int64 after_seek = callback.WaitForResult(); | |
| 50 EXPECT_EQ(after_seek, offset); | |
| 51 } | |
| 52 std::string FileContent(const FilePath& path) { | |
|
kinuko
2012/04/20 11:26:37
naming-nit: GetFileContent
kinaba
2012/04/23 08:56:41
Done.
| |
| 53 std::string content; | |
| 54 file_util::ReadFileToString(path, &content); | |
| 55 return content; | |
| 56 } | |
| 57 FilePath CreateEmptyFile(const std::string& name, const std::string& data) { | |
| 58 FilePath path = Path(name); | |
| 59 file_util::WriteFile(path, data.c_str(), data.size()); | |
|
kinuko
2012/04/20 11:26:37
'Empty'? :)
kinaba
2012/04/23 08:56:41
Surely not!
| |
| 60 return path; | |
| 61 } | |
| 62 | |
| 63 private: | |
| 64 ScopedTempDir temp_dir_; | |
| 65 }; | |
| 66 | |
| 67 } // namespace | |
| 68 | |
| 69 TEST_F(LocalFileWriterTest, Write) { | |
| 70 FilePath path = CreateEmptyFile("file_a", ""); | |
| 71 scoped_ptr<LocalFileWriter> writer(new LocalFileWriter(path)); | |
| 72 WriteStringToWriter(writer.get(), "foo"); | |
| 73 WriteStringToWriter(writer.get(), "bar"); | |
| 74 writer.reset(); | |
| 75 EXPECT_TRUE(file_util::PathExists(path)); | |
| 76 EXPECT_EQ("foobar", FileContent(path)); | |
| 77 } | |
| 78 | |
| 79 TEST_F(LocalFileWriterTest, Seek) { | |
| 80 FilePath path = CreateEmptyFile("file_a", "foobar"); | |
| 81 scoped_ptr<LocalFileWriter> writer(new LocalFileWriter(path)); | |
| 82 SeekWriter(writer.get(), 2); | |
| 83 WriteStringToWriter(writer.get(), "xxx"); | |
| 84 writer.reset(); | |
| 85 EXPECT_TRUE(file_util::PathExists(path)); | |
| 86 EXPECT_EQ("foxxxr", FileContent(path)); | |
| 87 } | |
| 88 | |
| 89 TEST_F(LocalFileWriterTest, SeekEnd) { | |
| 90 FilePath path = CreateEmptyFile("file_a", "foobar"); | |
| 91 scoped_ptr<LocalFileWriter> writer(new LocalFileWriter(path)); | |
| 92 SeekWriter(writer.get(), 6); | |
| 93 WriteStringToWriter(writer.get(), "buz"); | |
| 94 writer.reset(); | |
| 95 EXPECT_TRUE(file_util::PathExists(path)); | |
| 96 EXPECT_EQ("foobarbuz", FileContent(path)); | |
| 97 } | |
| OLD | NEW |