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.
4
5#include "chrome/browser/sync_file_system/drive_backend_v1/fake_api_util.h"
6
7#include <string>
8
9#include "base/bind.h"
10#include "base/files/file_path.h"
11#include "base/message_loop/message_loop.h"
12#include "google_apis/drive/gdata_errorcode.h"
13#include "testing/gtest/include/gtest/gtest.h"
14#include "webkit/common/blob/scoped_file.h"
15
16namespace sync_file_system {
17namespace drive_backend {
18
19namespace {
20
21void DidDownloadFile(google_apis::GDataErrorCode* error_out,
22                     std::string* file_md5_out,
23                     google_apis::GDataErrorCode error,
24                     const std::string& file_md5,
25                     int64 file_size,
26                     const base::Time& updated_time,
27                     webkit_blob::ScopedFile downloaded_file) {
28  *error_out = error;
29  *file_md5_out = file_md5;
30}
31
32void DidGetChangeList(google_apis::GDataErrorCode* error_out,
33                      scoped_ptr<google_apis::ResourceList>* change_list_out,
34                      google_apis::GDataErrorCode error,
35                      scoped_ptr<google_apis::ResourceList> change_list) {
36  *error_out = error;
37  *change_list_out = change_list.Pass();
38}
39
40void DidDeleteFile(google_apis::GDataErrorCode* error_out,
41                   google_apis::GDataErrorCode error) {
42  *error_out = error;
43}
44
45}  // namespace
46
47TEST(FakeAPIUtilTest, ChangeSquashTest) {
48  base::MessageLoop message_loop;
49  FakeAPIUtil api_util;
50  std::string kParentResourceId("parent resource id");
51  std::string kParentTitle("app-id");
52  std::string kTitle1("title 1");
53  std::string kTitle2("title 2");
54  std::string kTitle3("title 3");
55  std::string kResourceId1("resource id 1");
56  std::string kResourceId2("resource id 2");
57  std::string kMD5_1("md5 1");
58  std::string kMD5_2("md5 2");
59  std::string kMD5_3("md5 3");
60
61  api_util.PushRemoteChange(kParentResourceId,
62                            kParentTitle,
63                            kTitle1,
64                            kResourceId1,
65                            kMD5_1,
66                            SYNC_FILE_TYPE_FILE,
67                            false /* deleted */);
68  api_util.PushRemoteChange(kParentResourceId,
69                            kParentTitle,
70                            kTitle1,
71                            kResourceId1,
72                            kMD5_1,
73                            SYNC_FILE_TYPE_FILE,
74                            false /* deleted */);
75  api_util.PushRemoteChange(kParentResourceId,
76                            kParentTitle,
77                            kTitle1,
78                            kResourceId1,
79                            kMD5_1,
80                            SYNC_FILE_TYPE_FILE,
81                            true /* deleted */);
82  api_util.PushRemoteChange(kParentResourceId,
83                            kParentTitle,
84                            kTitle2,
85                            kResourceId2,
86                            kMD5_2,
87                            SYNC_FILE_TYPE_FILE,
88                            false /* deleted */);
89  api_util.PushRemoteChange(kParentResourceId,
90                            kParentTitle,
91                            kTitle3,
92                            kResourceId2,
93                            kMD5_3,
94                            SYNC_FILE_TYPE_FILE,
95                            false /* deleted */);
96
97  google_apis::GDataErrorCode error;
98  std::string md5;
99  api_util.DownloadFile(kResourceId1,
100                        kMD5_1,
101                        base::Bind(DidDownloadFile, &error, &md5));
102  message_loop.RunUntilIdle();
103  EXPECT_EQ(google_apis::HTTP_NOT_FOUND, error);
104  EXPECT_TRUE(md5.empty());
105
106  scoped_ptr<google_apis::ResourceList> change_list;
107  api_util.ListChanges(0, base::Bind(&DidGetChangeList, &error, &change_list));
108  message_loop.RunUntilIdle();
109  EXPECT_EQ(2u, change_list->entries().size());
110
111  EXPECT_EQ(kResourceId1, change_list->entries()[0]->resource_id());
112  EXPECT_TRUE(change_list->entries()[0]->deleted());
113
114  EXPECT_EQ(kResourceId2, change_list->entries()[1]->resource_id());
115  EXPECT_EQ(kMD5_3, change_list->entries()[1]->file_md5());
116  EXPECT_FALSE(change_list->entries()[1]->deleted());
117}
118
119TEST(FakeAPIUtilTest, DeleteFile) {
120  base::MessageLoop message_loop;
121  FakeAPIUtil api_util;
122  std::string resource_id = "resource_id_to_be_deleted";
123  api_util.PushRemoteChange("parent_id",
124                            "parent_title",
125                            "resource_title",
126                            resource_id,
127                            "resource_md5",
128                            SYNC_FILE_TYPE_FILE,
129                            false /* deleted */);
130
131  google_apis::GDataErrorCode error = google_apis::HTTP_NOT_FOUND;
132  api_util.DeleteFile(
133      resource_id, std::string(), base::Bind(&DidDeleteFile, &error));
134  message_loop.RunUntilIdle();
135
136  EXPECT_EQ(google_apis::HTTP_SUCCESS, error);
137  EXPECT_TRUE(api_util.remote_resources().find(resource_id)->second.deleted);
138}
139
140}  // namespace drive_backend
141}  // namespace sync_file_system
142