This source file includes following definitions.
- error
- file_stream
- deletable_file
- OnFileStreamCreated
- Wait
- Release
- TEST
#include "content/browser/loader/temporary_file_stream.h"
#include <string.h>
#include <string>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/run_loop.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "net/base/file_stream.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/common/blob/shareable_file_reference.h"
using webkit_blob::ShareableFileReference;
namespace content {
namespace {
const char kTestData[] = "0123456789";
const int kTestDataSize = arraysize(kTestData) - 1;
class WaitForFileStream {
public:
base::File::Error error() const { return error_; }
net::FileStream* file_stream() const { return file_stream_.get(); }
ShareableFileReference* deletable_file() const {
return deletable_file_.get();
}
void OnFileStreamCreated(base::File::Error error,
scoped_ptr<net::FileStream> file_stream,
ShareableFileReference* deletable_file) {
error_ = error;
file_stream_ = file_stream.Pass();
deletable_file_ = deletable_file;
loop_.Quit();
}
void Wait() {
loop_.Run();
}
void Release() {
file_stream_.reset(NULL);
deletable_file_ = NULL;
}
private:
base::RunLoop loop_;
base::File::Error error_;
scoped_ptr<net::FileStream> file_stream_;
scoped_refptr<ShareableFileReference> deletable_file_;
};
}
TEST(TemporaryFileStreamTest, Basic) {
TestBrowserThreadBundle thread_bundle(TestBrowserThreadBundle::IO_MAINLOOP);
WaitForFileStream file_stream_waiter;
CreateTemporaryFileStream(base::Bind(&WaitForFileStream::OnFileStreamCreated,
base::Unretained(&file_stream_waiter)));
file_stream_waiter.Wait();
EXPECT_EQ(base::File::FILE_OK, file_stream_waiter.error());
base::FilePath file_path = file_stream_waiter.deletable_file()->path();
EXPECT_TRUE(base::PathExists(file_path));
int bytes_written = 0;
scoped_refptr<net::IOBufferWithSize> buf =
new net::IOBufferWithSize(kTestDataSize);
memcpy(buf->data(), kTestData, kTestDataSize);
scoped_refptr<net::DrainableIOBuffer> drainable =
new net::DrainableIOBuffer(buf.get(), buf->size());
while (bytes_written != kTestDataSize) {
net::TestCompletionCallback write_callback;
int rv = file_stream_waiter.file_stream()->Write(
drainable.get(), drainable->BytesRemaining(),
write_callback.callback());
if (rv == net::ERR_IO_PENDING)
rv = write_callback.WaitForResult();
ASSERT_LT(0, rv);
drainable->DidConsume(rv);
bytes_written += rv;
}
std::string contents;
ASSERT_TRUE(base::ReadFileToString(file_path, &contents));
EXPECT_EQ(kTestData, contents);
net::TestCompletionCallback close_callback;
int rv = file_stream_waiter.file_stream()->Close(close_callback.callback());
if (rv == net::ERR_IO_PENDING)
rv = close_callback.WaitForResult();
EXPECT_EQ(net::OK, rv);
file_stream_waiter.Release();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_FALSE(base::PathExists(file_path));
}
}