This source file includes following definitions.
- SetUp
- TEST_F
- SetUp
- TEST_F
- TEST_F
- TEST_F
- SetUp
- TEST_F
#include "net/url_request/url_fetcher_response_writer.h"
#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/run_loop.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "testing/platform_test.h"
namespace net {
namespace {
const char kData[] = "Hello!";
}
class URLFetcherStringWriterTest : public PlatformTest {
protected:
virtual void SetUp() OVERRIDE {
writer_.reset(new URLFetcherStringWriter);
buf_ = new StringIOBuffer(kData);
}
scoped_ptr<URLFetcherStringWriter> writer_;
scoped_refptr<StringIOBuffer> buf_;
};
TEST_F(URLFetcherStringWriterTest, Basic) {
int rv = 0;
TestCompletionCallback callback;
rv = writer_->Initialize(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
rv = writer_->Write(buf_.get(), buf_->size(), callback.callback());
EXPECT_EQ(buf_->size(), callback.GetResult(rv));
rv = writer_->Finish(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
EXPECT_EQ(kData, writer_->data());
rv = writer_->Initialize(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
EXPECT_TRUE(writer_->data().empty());
}
class URLFetcherFileWriterTest : public PlatformTest {
protected:
virtual void SetUp() OVERRIDE {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
file_path_ = temp_dir_.path().AppendASCII("test.txt");
writer_.reset(new URLFetcherFileWriter(
base::MessageLoopProxy::current(), file_path_));
buf_ = new StringIOBuffer(kData);
}
base::ScopedTempDir temp_dir_;
base::FilePath file_path_;
scoped_ptr<URLFetcherFileWriter> writer_;
scoped_refptr<StringIOBuffer> buf_;
};
TEST_F(URLFetcherFileWriterTest, WriteToFile) {
int rv = 0;
TestCompletionCallback callback;
rv = writer_->Initialize(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
rv = writer_->Write(buf_.get(), buf_->size(), callback.callback());
EXPECT_EQ(buf_->size(), callback.GetResult(rv));
rv = writer_->Finish(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
EXPECT_EQ(file_path_.value(), writer_->file_path().value());
std::string file_contents;
EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents));
EXPECT_EQ(kData, file_contents);
writer_.reset();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(base::PathExists(file_path_));
}
TEST_F(URLFetcherFileWriterTest, InitializeAgain) {
int rv = 0;
TestCompletionCallback callback;
rv = writer_->Initialize(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
rv = writer_->Write(buf_.get(), buf_->size(), callback.callback());
EXPECT_EQ(buf_->size(), callback.GetResult(rv));
rv = writer_->Finish(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
std::string file_contents;
EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents));
EXPECT_EQ(kData, file_contents);
const std::string data2 = "Bye!";
scoped_refptr<StringIOBuffer> buf2(new StringIOBuffer(data2));
rv = writer_->Initialize(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
rv = writer_->Write(buf2.get(), buf2->size(), callback.callback());
EXPECT_EQ(buf2->size(), callback.GetResult(rv));
rv = writer_->Finish(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
file_contents.clear();
EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents));
EXPECT_EQ(data2, file_contents);
}
TEST_F(URLFetcherFileWriterTest, DisownFile) {
int rv = 0;
TestCompletionCallback callback;
rv = writer_->Initialize(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
rv = writer_->Finish(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
writer_->DisownFile();
writer_.reset();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(base::PathExists(file_path_));
}
class URLFetcherFileWriterTemporaryFileTest : public PlatformTest {
protected:
virtual void SetUp() OVERRIDE {
writer_.reset(new URLFetcherFileWriter(
base::MessageLoopProxy::current(), base::FilePath()));
buf_ = new StringIOBuffer(kData);
}
scoped_ptr<URLFetcherFileWriter> writer_;
scoped_refptr<StringIOBuffer> buf_;
};
TEST_F(URLFetcherFileWriterTemporaryFileTest, WriteToTemporaryFile) {
int rv = 0;
TestCompletionCallback callback;
rv = writer_->Initialize(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
rv = writer_->Write(buf_.get(), buf_->size(), callback.callback());
EXPECT_EQ(buf_->size(), callback.GetResult(rv));
rv = writer_->Finish(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
std::string file_contents;
EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents));
EXPECT_EQ(kData, file_contents);
const base::FilePath file_path = writer_->file_path();
writer_.reset();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(base::PathExists(file_path));
}
}