OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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/chromeos/gdata/mock_documents_service.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/file_path.h" | |
9 #include "base/file_util.h" | |
10 #include "base/location.h" | |
11 #include "base/json/json_file_value_serializer.h" | |
12 #include "base/message_loop_proxy.h" | |
13 #include "base/path_service.h" | |
14 #include "base/platform_file.h" | |
15 #include "chrome/common/chrome_paths.h" | |
16 #include "testing/gmock/include/gmock/gmock.h" | |
17 | |
18 using ::testing::_; | |
19 using ::testing::Invoke; | |
20 | |
21 namespace gdata { | |
22 | |
23 namespace { | |
24 | |
25 static Value* LoadJSONFile(const std::string& filename) { | |
26 FilePath path; | |
27 std::string error; | |
28 PathService::Get(chrome::DIR_TEST_DATA, &path); | |
29 path = path.AppendASCII("chromeos") | |
30 .AppendASCII("gdata") | |
31 .AppendASCII(filename.c_str()); | |
32 EXPECT_TRUE(file_util::PathExists(path)) << | |
33 "Couldn't find " << path.value(); | |
34 | |
35 JSONFileValueSerializer serializer(path); | |
36 Value* value = serializer.Deserialize(NULL, &error); | |
37 EXPECT_TRUE(value) << | |
38 "Parse error " << path.value() << ": " << error; | |
39 return value; | |
40 } | |
41 | |
42 } // namespace | |
43 | |
44 MockDocumentsService::MockDocumentsService() { | |
45 ON_CALL(*this, Authenticate(_)) | |
46 .WillByDefault(Invoke(this, &MockDocumentsService::AuthenticateStub)); | |
47 ON_CALL(*this, GetDocuments(_, _, _, _, _)) | |
48 .WillByDefault(Invoke(this, &MockDocumentsService::GetDocumentsStub)); | |
49 ON_CALL(*this, GetAccountMetadata(_)) | |
50 .WillByDefault(Invoke(this, | |
51 &MockDocumentsService::GetAccountMetadataStub)); | |
52 ON_CALL(*this, DeleteDocument(_, _)) | |
53 .WillByDefault(Invoke(this, &MockDocumentsService::DeleteDocumentStub)); | |
54 ON_CALL(*this, DownloadDocument(_, _, _, _, _)) | |
55 .WillByDefault(Invoke(this, &MockDocumentsService::DownloadDocumentStub)); | |
56 ON_CALL(*this, CopyDocument(_, _, _)) | |
57 .WillByDefault(Invoke(this, &MockDocumentsService::CopyDocumentStub)); | |
58 ON_CALL(*this, RenameResource(_, _, _)) | |
59 .WillByDefault(Invoke(this, &MockDocumentsService::RenameResourceStub)); | |
60 ON_CALL(*this, AddResourceToDirectory(_, _, _)) | |
61 .WillByDefault( | |
62 Invoke(this, &MockDocumentsService::AddResourceToDirectoryStub)); | |
63 ON_CALL(*this, RemoveResourceFromDirectory(_, _, _, _)) | |
64 .WillByDefault( | |
65 Invoke(this, &MockDocumentsService::RemoveResourceFromDirectoryStub)); | |
66 ON_CALL(*this, CreateDirectory(_, _, _)) | |
67 .WillByDefault(Invoke(this, &MockDocumentsService::CreateDirectoryStub)); | |
68 ON_CALL(*this, DownloadFile(_, _, _, _, _)) | |
69 .WillByDefault(Invoke(this, &MockDocumentsService::DownloadFileStub)); | |
70 | |
71 // Fill in the default values for mock feeds. | |
72 account_metadata_.reset(LoadJSONFile("account_metadata.json")); | |
73 feed_data_.reset(LoadJSONFile("basic_feed.json")); | |
74 directory_data_.reset(LoadJSONFile("new_folder_entry.json")); | |
75 } | |
76 | |
77 MockDocumentsService::~MockDocumentsService() {} | |
78 | |
79 void MockDocumentsService::set_search_result( | |
80 const std::string& search_result_feed) { | |
81 search_result_.reset(LoadJSONFile(search_result_feed)); | |
82 } | |
83 | |
84 void MockDocumentsService::AuthenticateStub( | |
85 const AuthStatusCallback& callback) { | |
86 base::MessageLoopProxy::current()->PostTask( | |
87 FROM_HERE, | |
88 base::Bind(callback, HTTP_SUCCESS, "my_auth_token")); | |
89 } | |
90 | |
91 void MockDocumentsService::GetDocumentsStub( | |
92 const GURL& feed_url, | |
93 int64 start_changestamp, | |
94 const std::string& search_string, | |
95 const std::string& directory_resource_id, | |
96 const GetDataCallback& callback) { | |
97 if (search_string.empty()) { | |
98 base::MessageLoopProxy::current()->PostTask( | |
99 FROM_HERE, | |
100 base::Bind(callback, HTTP_SUCCESS, base::Passed(&feed_data_))); | |
101 } else { | |
102 base::MessageLoopProxy::current()->PostTask( | |
103 FROM_HERE, | |
104 base::Bind(callback, HTTP_SUCCESS, base::Passed(&search_result_))); | |
105 } | |
106 } | |
107 | |
108 void MockDocumentsService::GetAccountMetadataStub( | |
109 const GetDataCallback& callback) { | |
110 base::MessageLoopProxy::current()->PostTask( | |
111 FROM_HERE, | |
112 base::Bind(callback, HTTP_SUCCESS, base::Passed(&account_metadata_))); | |
113 } | |
114 | |
115 void MockDocumentsService::DeleteDocumentStub( | |
116 const GURL& document_url, | |
117 const EntryActionCallback& callback) { | |
118 base::MessageLoopProxy::current()->PostTask( | |
119 FROM_HERE, | |
120 base::Bind(callback, HTTP_SUCCESS, document_url)); | |
121 } | |
122 | |
123 void MockDocumentsService::DownloadDocumentStub( | |
124 const FilePath& virtual_path, | |
125 const FilePath& local_tmp_path, | |
126 const GURL& content_url, | |
127 DocumentExportFormat format, | |
128 const DownloadActionCallback& callback) { | |
129 base::MessageLoopProxy::current()->PostTask( | |
130 FROM_HERE, | |
131 base::Bind(callback, HTTP_SUCCESS, content_url, local_tmp_path)); | |
132 } | |
133 | |
134 void MockDocumentsService::CopyDocumentStub( | |
135 const std::string& resource_id, | |
136 const FilePath::StringType& new_name, | |
137 const GetDataCallback& callback) { | |
138 base::MessageLoopProxy::current()->PostTask( | |
139 FROM_HERE, | |
140 base::Bind(callback, HTTP_SUCCESS, base::Passed(&document_data_))); | |
141 } | |
142 | |
143 void MockDocumentsService::RenameResourceStub( | |
144 const GURL& resource_url, | |
145 const FilePath::StringType& new_name, | |
146 const EntryActionCallback& callback) { | |
147 base::MessageLoopProxy::current()->PostTask( | |
148 FROM_HERE, | |
149 base::Bind(callback, HTTP_SUCCESS, resource_url)); | |
150 } | |
151 | |
152 void MockDocumentsService::AddResourceToDirectoryStub( | |
153 const GURL& parent_content_url, | |
154 const GURL& resource_url, | |
155 const EntryActionCallback& callback) { | |
156 base::MessageLoopProxy::current()->PostTask( | |
157 FROM_HERE, | |
158 base::Bind(callback, HTTP_SUCCESS, resource_url)); | |
159 } | |
160 | |
161 void MockDocumentsService::RemoveResourceFromDirectoryStub( | |
162 const GURL& parent_content_url, | |
163 const GURL& resource_url, | |
164 const std::string& resource_id, | |
165 const EntryActionCallback& callback) { | |
166 base::MessageLoopProxy::current()->PostTask( | |
167 FROM_HERE, | |
168 base::Bind(callback, HTTP_SUCCESS, resource_url)); | |
169 } | |
170 | |
171 void MockDocumentsService::CreateDirectoryStub( | |
172 const GURL& parent_content_url, | |
173 const FilePath::StringType& directory_name, | |
174 const GetDataCallback& callback) { | |
175 base::MessageLoopProxy::current()->PostTask( | |
176 FROM_HERE, | |
177 base::Bind(callback, HTTP_SUCCESS, base::Passed(&directory_data_))); | |
178 } | |
179 | |
180 void MockDocumentsService::DownloadFileStub( | |
181 const FilePath& virtual_path, | |
182 const FilePath& local_tmp_path, | |
183 const GURL& content_url, | |
184 const DownloadActionCallback& download_action_callback, | |
185 const GetContentCallback& get_content_callback) { | |
186 GDataErrorCode error = HTTP_SUCCESS; | |
187 if (file_data_.get()) { | |
188 int file_data_size = static_cast<int>(file_data_->size()); | |
189 ASSERT_EQ(file_data_size, | |
190 file_util::WriteFile(local_tmp_path, file_data_->data(), | |
191 file_data_size)); | |
192 } | |
193 base::MessageLoopProxy::current()->PostTask( | |
194 FROM_HERE, | |
195 base::Bind(download_action_callback, error, content_url, local_tmp_path)); | |
196 } | |
197 | |
198 } // namespace gdata | |
OLD | NEW |