Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / loader / temporary_file_stream_unittest.cc
blobaf57f0824ec5581ef4e71ae12ed96e0e36c59e04
1 // Copyright 2014 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 "content/browser/loader/temporary_file_stream.h"
7 #include <string.h>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/bind.h"
13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h"
15 #include "base/run_loop.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "net/base/file_stream.h"
18 #include "net/base/io_buffer.h"
19 #include "net/base/net_errors.h"
20 #include "net/base/test_completion_callback.h"
21 #include "storage/browser/blob/shareable_file_reference.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 using storage::ShareableFileReference;
26 namespace content {
28 namespace {
30 const char kTestData[] = "0123456789";
31 const int kTestDataSize = arraysize(kTestData) - 1;
33 class WaitForFileStream {
34 public:
35 base::File::Error error() const { return error_; }
36 net::FileStream* file_stream() const { return file_stream_.get(); }
37 ShareableFileReference* deletable_file() const {
38 return deletable_file_.get();
41 void OnFileStreamCreated(base::File::Error error,
42 scoped_ptr<net::FileStream> file_stream,
43 ShareableFileReference* deletable_file) {
44 error_ = error;
45 file_stream_ = file_stream.Pass();
46 deletable_file_ = deletable_file;
47 loop_.Quit();
50 void Wait() {
51 loop_.Run();
54 void Release() {
55 file_stream_.reset(NULL);
56 deletable_file_ = NULL;
58 private:
59 base::RunLoop loop_;
60 base::File::Error error_;
61 scoped_ptr<net::FileStream> file_stream_;
62 scoped_refptr<ShareableFileReference> deletable_file_;
65 } // namespace
67 TEST(TemporaryFileStreamTest, Basic) {
68 TestBrowserThreadBundle thread_bundle(TestBrowserThreadBundle::IO_MAINLOOP);
70 // Create a temporary.
71 WaitForFileStream file_stream_waiter;
72 CreateTemporaryFileStream(base::Bind(&WaitForFileStream::OnFileStreamCreated,
73 base::Unretained(&file_stream_waiter)));
74 file_stream_waiter.Wait();
76 // The temporary should exist.
77 EXPECT_EQ(base::File::FILE_OK, file_stream_waiter.error());
78 base::FilePath file_path = file_stream_waiter.deletable_file()->path();
79 EXPECT_TRUE(base::PathExists(file_path));
81 // Write some data to the temporary.
82 int bytes_written = 0;
83 scoped_refptr<net::IOBufferWithSize> buf =
84 new net::IOBufferWithSize(kTestDataSize);
85 memcpy(buf->data(), kTestData, kTestDataSize);
86 scoped_refptr<net::DrainableIOBuffer> drainable =
87 new net::DrainableIOBuffer(buf.get(), buf->size());
88 while (bytes_written != kTestDataSize) {
89 net::TestCompletionCallback write_callback;
90 int rv = file_stream_waiter.file_stream()->Write(
91 drainable.get(), drainable->BytesRemaining(),
92 write_callback.callback());
93 if (rv == net::ERR_IO_PENDING)
94 rv = write_callback.WaitForResult();
95 ASSERT_LT(0, rv);
96 drainable->DidConsume(rv);
97 bytes_written += rv;
100 // Verify the data matches.
101 std::string contents;
102 ASSERT_TRUE(base::ReadFileToString(file_path, &contents));
103 EXPECT_EQ(kTestData, contents);
105 // Close the file.
106 net::TestCompletionCallback close_callback;
107 int rv = file_stream_waiter.file_stream()->Close(close_callback.callback());
108 if (rv == net::ERR_IO_PENDING)
109 rv = close_callback.WaitForResult();
110 EXPECT_EQ(net::OK, rv);
112 // Release everything. The file should be gone now.
113 file_stream_waiter.Release();
114 base::MessageLoop::current()->RunUntilIdle();
116 // The temporary should be gone now.
117 EXPECT_FALSE(base::PathExists(file_path));
120 } // content