summaryrefslogtreecommitdiffstats
path: root/chrome/browser/download/base_file_unittest.cc
diff options
context:
space:
mode:
authorphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-27 16:27:15 +0000
committerphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-27 16:27:15 +0000
commitf15edeb24bd34ef09d6c62b734eca5765f5c2854 (patch)
tree24d4ef0375b0aab1486fb52d0ae9fa41766b262d /chrome/browser/download/base_file_unittest.cc
parenta0eca45b109d89137269cb9b5004e8a51309a6b2 (diff)
downloadchromium_src-f15edeb24bd34ef09d6c62b734eca5765f5c2854.zip
chromium_src-f15edeb24bd34ef09d6c62b734eca5765f5c2854.tar.gz
chromium_src-f15edeb24bd34ef09d6c62b734eca5765f5c2854.tar.bz2
Download code cleanup: share most of the code between DownloadFile and SaveFile.
I'm not really happy about BaseFile, but removing the remaining differences will require changes to DownloadFileManager and SaveFileManager. I prefer to do that in small steps. TEST=unit_tests, browser_tests, ui_tests BUG=48913 Review URL: http://codereview.chromium.org/3164039 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@57688 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/download/base_file_unittest.cc')
-rw-r--r--chrome/browser/download/base_file_unittest.cc154
1 files changed, 154 insertions, 0 deletions
diff --git a/chrome/browser/download/base_file_unittest.cc b/chrome/browser/download/base_file_unittest.cc
new file mode 100644
index 0000000..d1b93a9
--- /dev/null
+++ b/chrome/browser/download/base_file_unittest.cc
@@ -0,0 +1,154 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/file_util.h"
+#include "base/message_loop.h"
+#include "base/scoped_temp_dir.h"
+#include "chrome/browser/chrome_thread.h"
+#include "chrome/browser/download/base_file.h"
+#include "net/base/file_stream.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+const char kTestData1[] = "Let's write some data to the file!\n";
+const char kTestData2[] = "Writing more data.\n";
+const char kTestData3[] = "Final line.";
+
+class BaseFileTest : public testing::Test {
+ public:
+ BaseFileTest() : file_thread_(ChromeThread::FILE, &message_loop_) {
+ }
+
+ virtual void SetUp() {
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
+ base_file_.reset(new BaseFile(FilePath(), GURL(), GURL(), file_stream_));
+ }
+
+ virtual void TearDown() {
+ EXPECT_FALSE(base_file_->in_progress());
+ EXPECT_EQ(static_cast<int64>(expected_data_.size()),
+ base_file_->bytes_so_far());
+
+ if (!expected_data_.empty()) {
+ // Make sure the data has been properly written to disk.
+ std::string disk_data;
+ EXPECT_TRUE(file_util::ReadFileToString(base_file_->full_path(),
+ &disk_data));
+ EXPECT_EQ(expected_data_, disk_data);
+ }
+
+ // Make sure the mock ChromeThread outlives the BaseFile to satisfy
+ // thread checks inside it.
+ base_file_.reset();
+ }
+
+ void AppendDataToFile(const std::string& data) {
+ ASSERT_TRUE(base_file_->in_progress());
+ base_file_->AppendDataToFile(data.data(), data.size());
+ expected_data_ += data;
+ EXPECT_EQ(static_cast<int64>(expected_data_.size()),
+ base_file_->bytes_so_far());
+ }
+
+ protected:
+ linked_ptr<net::FileStream> file_stream_;
+
+ // BaseClass instance we are testing.
+ scoped_ptr<BaseFile> base_file_;
+
+ // Temporary directory for renamed downloads.
+ ScopedTempDir temp_dir_;
+
+ private:
+ // Keep track of what data should be saved to the disk file.
+ std::string expected_data_;
+
+ // Mock file thread to satisfy debug checks in BaseFile.
+ MessageLoop message_loop_;
+ ChromeThread file_thread_;
+};
+
+// Test the most basic scenario: just create the object and do a sanity check
+// on all its accessors. This is actually a case that rarely happens
+// in production, where we would at least Initialize it.
+TEST_F(BaseFileTest, CreateDestroy) {
+ EXPECT_EQ(FilePath().value(), base_file_->full_path().value());
+ EXPECT_FALSE(base_file_->path_renamed());
+}
+
+// Cancel the download explicitly.
+TEST_F(BaseFileTest, Cancel) {
+ ASSERT_TRUE(base_file_->Initialize());
+ EXPECT_TRUE(file_util::PathExists(base_file_->full_path()));
+ base_file_->Cancel();
+ EXPECT_FALSE(file_util::PathExists(base_file_->full_path()));
+ EXPECT_NE(FilePath().value(), base_file_->full_path().value());
+ EXPECT_FALSE(base_file_->path_renamed());
+}
+
+// Write data to the file once.
+TEST_F(BaseFileTest, SingleWrite) {
+ ASSERT_TRUE(base_file_->Initialize());
+ AppendDataToFile(kTestData1);
+ base_file_->Finish();
+
+ EXPECT_FALSE(base_file_->path_renamed());
+}
+
+// Write data to the file multiple times.
+TEST_F(BaseFileTest, MultipleWrites) {
+ ASSERT_TRUE(base_file_->Initialize());
+ AppendDataToFile(kTestData1);
+ AppendDataToFile(kTestData2);
+ AppendDataToFile(kTestData3);
+ base_file_->Finish();
+
+ EXPECT_FALSE(base_file_->path_renamed());
+}
+
+// Rename the file after all writes to it.
+TEST_F(BaseFileTest, WriteThenRename) {
+ ASSERT_TRUE(base_file_->Initialize());
+
+ FilePath initial_path(base_file_->full_path());
+ EXPECT_TRUE(file_util::PathExists(initial_path));
+ FilePath new_path(temp_dir_.path().AppendASCII("NewFile"));
+ EXPECT_FALSE(file_util::PathExists(new_path));
+
+ AppendDataToFile(kTestData1);
+
+ EXPECT_TRUE(base_file_->Rename(new_path, true));
+ EXPECT_FALSE(file_util::PathExists(initial_path));
+ EXPECT_TRUE(file_util::PathExists(new_path));
+
+ base_file_->Finish();
+
+ EXPECT_TRUE(base_file_->path_renamed());
+}
+
+// Rename the file while the download is still in progress.
+TEST_F(BaseFileTest, RenameWhileInProgress) {
+ ASSERT_TRUE(base_file_->Initialize());
+
+ FilePath initial_path(base_file_->full_path());
+ EXPECT_TRUE(file_util::PathExists(initial_path));
+ FilePath new_path(temp_dir_.path().AppendASCII("NewFile"));
+ EXPECT_FALSE(file_util::PathExists(new_path));
+
+ AppendDataToFile(kTestData1);
+
+ EXPECT_TRUE(base_file_->in_progress());
+ EXPECT_TRUE(base_file_->Rename(new_path, true));
+ EXPECT_FALSE(file_util::PathExists(initial_path));
+ EXPECT_TRUE(file_util::PathExists(new_path));
+
+ AppendDataToFile(kTestData2);
+
+ base_file_->Finish();
+
+ EXPECT_TRUE(base_file_->path_renamed());
+}
+
+} // namespace