Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / sync / entry_revert_performer_unittest.cc
blob37b04d29d76ebb887dee0605a408d46e95b44793
1 // Copyright 2013 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 "chrome/browser/chromeos/drive/sync/entry_revert_performer.h"
7 #include "base/task_runner_util.h"
8 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
9 #include "chrome/browser/chromeos/drive/file_system_util.h"
10 #include "chrome/browser/chromeos/drive/resource_metadata.h"
11 #include "chrome/browser/drive/fake_drive_service.h"
12 #include "google_apis/drive/test_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace drive {
16 namespace internal {
18 class EntryRevertPerformerTest : public file_system::OperationTestBase {
19 protected:
20 virtual void SetUp() OVERRIDE {
21 OperationTestBase::SetUp();
22 performer_.reset(new EntryRevertPerformer(blocking_task_runner(),
23 observer(),
24 scheduler(),
25 metadata()));
28 scoped_ptr<EntryRevertPerformer> performer_;
31 TEST_F(EntryRevertPerformerTest, RevertEntry) {
32 base::FilePath path(
33 FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt"));
35 ResourceEntry src_entry;
36 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(path, &src_entry));
38 // Update local entry.
39 ResourceEntry updated_entry = src_entry;
40 updated_entry.set_title("Updated" + src_entry.title());
41 updated_entry.set_metadata_edit_state(ResourceEntry::DIRTY);
43 FileError error = FILE_ERROR_FAILED;
44 base::PostTaskAndReplyWithResult(
45 blocking_task_runner(),
46 FROM_HERE,
47 base::Bind(&ResourceMetadata::RefreshEntry,
48 base::Unretained(metadata()), updated_entry),
49 google_apis::test_util::CreateCopyResultCallback(&error));
50 test_util::RunBlockingPoolTask();
51 EXPECT_EQ(FILE_ERROR_OK, error);
53 // Revert local change.
54 error = FILE_ERROR_FAILED;
55 performer_->RevertEntry(
56 src_entry.local_id(),
57 google_apis::test_util::CreateCopyResultCallback(&error));
58 test_util::RunBlockingPoolTask();
59 EXPECT_EQ(FILE_ERROR_OK, error);
61 // Verify local change is reverted.
62 ResourceEntry result_entry;
63 EXPECT_EQ(FILE_ERROR_OK,
64 GetLocalResourceEntryById(src_entry.local_id(), &result_entry));
65 EXPECT_EQ(src_entry.title(), result_entry.title());
66 EXPECT_EQ(ResourceEntry::CLEAN, result_entry.metadata_edit_state());
68 EXPECT_EQ(1U, observer()->get_changed_paths().size());
69 EXPECT_TRUE(observer()->get_changed_paths().count(path.DirName()));
72 TEST_F(EntryRevertPerformerTest, RevertEntry_NotFoundOnServer) {
73 ResourceEntry my_drive;
74 EXPECT_EQ(FILE_ERROR_OK,
75 GetLocalResourceEntry(util::GetDriveMyDriveRootPath(), &my_drive));
77 // Add a new entry with a nonexistent resource ID.
78 ResourceEntry entry;
79 entry.set_title("New File.txt");
80 entry.set_resource_id("non-existent-resource-id");
81 entry.set_parent_local_id(my_drive.local_id());
83 FileError error = FILE_ERROR_FAILED;
84 std::string local_id;
85 base::PostTaskAndReplyWithResult(
86 blocking_task_runner(),
87 FROM_HERE,
88 base::Bind(&ResourceMetadata::AddEntry,
89 base::Unretained(metadata()), entry, &local_id),
90 google_apis::test_util::CreateCopyResultCallback(&error));
91 test_util::RunBlockingPoolTask();
92 EXPECT_EQ(FILE_ERROR_OK, error);
94 // Revert local change. The added entry should be removed.
95 error = FILE_ERROR_FAILED;
96 performer_->RevertEntry(
97 local_id,
98 google_apis::test_util::CreateCopyResultCallback(&error));
99 test_util::RunBlockingPoolTask();
100 EXPECT_EQ(FILE_ERROR_OK, error);
102 // Verify the entry was deleted locally.
103 EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntryById(local_id, &entry));
105 EXPECT_EQ(1U, observer()->get_changed_paths().size());
106 EXPECT_TRUE(observer()->get_changed_paths().count(
107 util::GetDriveMyDriveRootPath()));
110 TEST_F(EntryRevertPerformerTest, RevertEntry_TrashedOnServer) {
111 base::FilePath path(
112 FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt"));
114 ResourceEntry entry;
115 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(path, &entry));
117 // Trash the entry on the server.
118 google_apis::GDataErrorCode gdata_error = google_apis::GDATA_OTHER_ERROR;
119 fake_service()->TrashResource(
120 entry.resource_id(),
121 google_apis::test_util::CreateCopyResultCallback(&gdata_error));
122 test_util::RunBlockingPoolTask();
123 EXPECT_EQ(google_apis::HTTP_SUCCESS, gdata_error);
125 // Revert local change. The entry should be removed.
126 FileError error = FILE_ERROR_FAILED;
127 performer_->RevertEntry(
128 entry.local_id(),
129 google_apis::test_util::CreateCopyResultCallback(&error));
130 test_util::RunBlockingPoolTask();
131 EXPECT_EQ(FILE_ERROR_OK, error);
133 // Verify the entry was deleted locally.
134 EXPECT_EQ(FILE_ERROR_NOT_FOUND,
135 GetLocalResourceEntryById(entry.local_id(), &entry));
137 EXPECT_EQ(1U, observer()->get_changed_paths().size());
138 EXPECT_TRUE(observer()->get_changed_paths().count(path.DirName()));
141 } // namespace internal
142 } // namespace drive