Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / drive / fake_drive_service.cc
index 3931727..971b7c4 100644 (file)
 #include "base/strings/string_util.h"
 #include "base/strings/stringprintf.h"
 #include "base/strings/utf_string_conversions.h"
+#include "base/values.h"
 #include "chrome/browser/drive/drive_api_util.h"
 #include "content/public/browser/browser_thread.h"
 #include "google_apis/drive/drive_api_parser.h"
 #include "google_apis/drive/gdata_wapi_parser.h"
 #include "google_apis/drive/test_util.h"
-#include "google_apis/drive/time_util.h"
 #include "net/base/escape.h"
 #include "net/base/url_util.h"
 
 using content::BrowserThread;
 using google_apis::AboutResource;
 using google_apis::AboutResourceCallback;
-using google_apis::AccountMetadata;
 using google_apis::AppList;
 using google_apis::AppListCallback;
 using google_apis::AuthStatusCallback;
@@ -66,9 +65,6 @@ namespace test_util = google_apis::test_util;
 namespace drive {
 namespace {
 
-// Mime type of directories.
-const char kDriveFolderMimeType[] = "application/vnd.google-apps.folder";
-
 // Returns true if a resource entry matches with the search query.
 // Supports queries consist of following format.
 // - Phrases quoted by double/single quotes
@@ -176,8 +172,14 @@ FakeDriveService::FakeDriveService()
       app_list_load_count_(0),
       blocked_resource_list_load_count_(0),
       offline_(false),
-      never_return_all_resource_list_(false) {
+      never_return_all_resource_list_(false),
+      share_url_base_("https://share_url/") {
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+
+  about_resource_->set_largest_change_id(654321);
+  about_resource_->set_quota_bytes_total(9876543210);
+  about_resource_->set_quota_bytes_used(6789012345);
+  about_resource_->set_root_folder_id(GetRootResourceId());
 }
 
 FakeDriveService::~FakeDriveService() {
@@ -185,78 +187,6 @@ FakeDriveService::~FakeDriveService() {
   STLDeleteValues(&entries_);
 }
 
-bool FakeDriveService::LoadResourceListForWapi(
-    const std::string& relative_path) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
-  scoped_ptr<base::Value> raw_value = test_util::LoadJSONFile(relative_path);
-  base::DictionaryValue* as_dict = NULL;
-  scoped_ptr<base::Value> feed;
-  base::DictionaryValue* feed_as_dict = NULL;
-
-  // Extract the "feed" from the raw value and take the ownership.
-  // Note that Remove() transfers the ownership to |feed|.
-  if (raw_value->GetAsDictionary(&as_dict) &&
-      as_dict->Remove("feed", &feed) &&
-      feed->GetAsDictionary(&feed_as_dict)) {
-    base::ListValue* entries = NULL;
-    if (feed_as_dict->GetList("entry", &entries)) {
-      for (size_t i = 0; i < entries->GetSize(); ++i) {
-        base::DictionaryValue* entry = NULL;
-        if (entries->GetDictionary(i, &entry)) {
-          scoped_ptr<ResourceEntry> resource_entry =
-              ResourceEntry::CreateFrom(*entry);
-
-          const std::string resource_id = resource_entry->resource_id();
-          EntryInfoMap::iterator it = entries_.find(resource_id);
-          if (it == entries_.end()) {
-            it = entries_.insert(
-                std::make_pair(resource_id, new EntryInfo)).first;
-          }
-          EntryInfo* new_entry = it->second;
-
-          ChangeResource* change = &new_entry->change_resource;
-          change->set_change_id(resource_entry->changestamp());
-          change->set_file_id(resource_id);
-          change->set_file(
-              util::ConvertResourceEntryToFileResource(*resource_entry));
-
-          const Link* share_url =
-              resource_entry->GetLinkByType(Link::LINK_SHARE);
-          if (share_url)
-            new_entry->share_url = share_url->href();
-
-          entry->GetString("test$data", &new_entry->content_data);
-        }
-      }
-    }
-  }
-
-  return feed_as_dict;
-}
-
-bool FakeDriveService::LoadAccountMetadataForWapi(
-    const std::string& relative_path) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
-
-  scoped_ptr<base::Value> value = test_util::LoadJSONFile(relative_path);
-  if (!value)
-    return false;
-
-  about_resource_ = util::ConvertAccountMetadataToAboutResource(
-      *AccountMetadata::CreateFrom(*value), GetRootResourceId());
-  if (!about_resource_)
-    return false;
-
-  // Add the largest changestamp to the existing entries.
-  // This will be used to generate change lists in GetResourceList().
-  for (EntryInfoMap::iterator it = entries_.begin(); it != entries_.end();
-       ++it) {
-    it->second->change_resource.set_change_id(
-        about_resource_->largest_change_id());
-  }
-  return true;
-}
-
 bool FakeDriveService::LoadAppListForDriveApi(
     const std::string& relative_path) {
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
@@ -426,7 +356,45 @@ CancelCallback FakeDriveService::GetRemainingChangeList(
   DCHECK(!next_link.is_empty());
   DCHECK(!callback.is_null());
 
-  return GetRemainingResourceList(next_link, callback);
+  // "changestamp", "q", "parent" and "start-offset" are parameters to
+  // implement "paging" of the result on FakeDriveService.
+  // The URL should be the one filled in GetResourceListInternal of the
+  // previous method invocation, so it should start with "http://localhost/?".
+  // See also GetResourceListInternal.
+  DCHECK_EQ(next_link.host(), "localhost");
+  DCHECK_EQ(next_link.path(), "/");
+
+  int64 start_changestamp = 0;
+  std::string search_query;
+  std::string directory_resource_id;
+  int start_offset = 0;
+  int max_results = default_max_results_;
+  std::vector<std::pair<std::string, std::string> > parameters;
+  if (base::SplitStringIntoKeyValuePairs(
+          next_link.query(), '=', '&', &parameters)) {
+    for (size_t i = 0; i < parameters.size(); ++i) {
+      if (parameters[i].first == "changestamp") {
+        base::StringToInt64(parameters[i].second, &start_changestamp);
+      } else if (parameters[i].first == "q") {
+        search_query =
+            net::UnescapeURLComponent(parameters[i].second,
+                                      net::UnescapeRule::URL_SPECIAL_CHARS);
+      } else if (parameters[i].first == "parent") {
+        directory_resource_id =
+            net::UnescapeURLComponent(parameters[i].second,
+                                      net::UnescapeRule::URL_SPECIAL_CHARS);
+      } else if (parameters[i].first == "start-offset") {
+        base::StringToInt(parameters[i].second, &start_offset);
+      } else if (parameters[i].first == "max-results") {
+        base::StringToInt(parameters[i].second, &max_results);
+      }
+    }
+  }
+
+  GetResourceListInternal(
+      start_changestamp, search_query, directory_resource_id,
+      start_offset, max_results, NULL, callback);
+  return CancelCallback();
 }
 
 CancelCallback FakeDriveService::GetRemainingFileList(
@@ -436,7 +404,7 @@ CancelCallback FakeDriveService::GetRemainingFileList(
   DCHECK(!next_link.is_empty());
   DCHECK(!callback.is_null());
 
-  return GetRemainingResourceList(next_link, callback);
+  return GetRemainingChangeList(next_link, callback);
 }
 
 CancelCallback FakeDriveService::GetResourceEntry(
@@ -737,7 +705,6 @@ CancelCallback FakeDriveService::CopyResource(
     ParentReference parent;
     parent.set_file_id(parent_resource_id);
     parent.set_parent_link(GetFakeLinkUrl(parent_resource_id));
-    parent.set_is_root(parent_resource_id == GetRootResourceId());
     std::vector<ParentReference> parents;
     parents.push_back(parent);
     *new_file->mutable_parents() = parents;
@@ -789,14 +756,15 @@ CancelCallback FakeDriveService::UpdateResource(
   if (entry) {
     ChangeResource* change = &entry->change_resource;
     FileResource* file = change->mutable_file();
-    file->set_title(new_title);
+
+    if (!new_title.empty())
+      file->set_title(new_title);
 
     // Set parent if necessary.
     if (!parent_resource_id.empty()) {
       ParentReference parent;
       parent.set_file_id(parent_resource_id);
       parent.set_parent_link(GetFakeLinkUrl(parent_resource_id));
-      parent.set_is_root(parent_resource_id == GetRootResourceId());
 
       std::vector<ParentReference> parents;
       parents.push_back(parent);
@@ -862,7 +830,6 @@ CancelCallback FakeDriveService::AddResourceToDirectory(
     ParentReference parent;
     parent.set_file_id(parent_resource_id);
     parent.set_parent_link(GetFakeLinkUrl(parent_resource_id));
-    parent.set_is_root(parent_resource_id == GetRootResourceId());
     change->mutable_file()->mutable_parents()->push_back(parent);
 
     AddNewChangestamp(change);
@@ -915,39 +882,12 @@ CancelCallback FakeDriveService::AddNewDirectory(
     const std::string& directory_title,
     const AddNewDirectoryOptions& options,
     const GetResourceEntryCallback& callback) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
-  DCHECK(!callback.is_null());
-
-  if (offline_) {
-    scoped_ptr<ResourceEntry> null;
-    base::MessageLoop::current()->PostTask(
-        FROM_HERE,
-        base::Bind(callback,
-                   GDATA_NO_CONNECTION,
-                   base::Passed(&null)));
-    return CancelCallback();
-  }
-
-  const EntryInfo* new_entry = AddNewEntry("",  // resource_id,
-                                           kDriveFolderMimeType,
-                                           "",  // content_data
-                                           parent_resource_id,
-                                           directory_title,
-                                           false);  // shared_with_me
-  if (!new_entry) {
-    scoped_ptr<ResourceEntry> null;
-    base::MessageLoop::current()->PostTask(
-        FROM_HERE,
-        base::Bind(callback, HTTP_NOT_FOUND, base::Passed(&null)));
-    return CancelCallback();
-  }
-
-  scoped_ptr<ResourceEntry> parsed_entry(
-      util::ConvertChangeResourceToResourceEntry(new_entry->change_resource));
-  base::MessageLoop::current()->PostTask(
-      FROM_HERE,
-      base::Bind(callback, HTTP_CREATED, base::Passed(&parsed_entry)));
-  return CancelCallback();
+  return AddNewDirectoryWithResourceId(
+      "",
+      parent_resource_id.empty() ? GetRootResourceId() : parent_resource_id,
+      directory_title,
+      options,
+      callback);
 }
 
 CancelCallback FakeDriveService::InitiateUploadNewFile(
@@ -1195,64 +1135,6 @@ CancelCallback FakeDriveService::UninstallApp(
   return CancelCallback();
 }
 
-CancelCallback FakeDriveService::GetResourceListInDirectoryByWapi(
-    const std::string& directory_resource_id,
-    const google_apis::GetResourceListCallback& callback) {
-  return GetResourceListInDirectory(
-      directory_resource_id == util::kWapiRootDirectoryResourceId ?
-          GetRootResourceId() :
-          directory_resource_id,
-      callback);
-}
-
-CancelCallback FakeDriveService::GetRemainingResourceList(
-    const GURL& next_link,
-    const google_apis::GetResourceListCallback& callback) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
-  DCHECK(!next_link.is_empty());
-  DCHECK(!callback.is_null());
-
-  // "changestamp", "q", "parent" and "start-offset" are parameters to
-  // implement "paging" of the result on FakeDriveService.
-  // The URL should be the one filled in GetResourceListInternal of the
-  // previous method invocation, so it should start with "http://localhost/?".
-  // See also GetResourceListInternal.
-  DCHECK_EQ(next_link.host(), "localhost");
-  DCHECK_EQ(next_link.path(), "/");
-
-  int64 start_changestamp = 0;
-  std::string search_query;
-  std::string directory_resource_id;
-  int start_offset = 0;
-  int max_results = default_max_results_;
-  std::vector<std::pair<std::string, std::string> > parameters;
-  if (base::SplitStringIntoKeyValuePairs(
-          next_link.query(), '=', '&', &parameters)) {
-    for (size_t i = 0; i < parameters.size(); ++i) {
-      if (parameters[i].first == "changestamp") {
-        base::StringToInt64(parameters[i].second, &start_changestamp);
-      } else if (parameters[i].first == "q") {
-        search_query =
-            net::UnescapeURLComponent(parameters[i].second,
-                                      net::UnescapeRule::URL_SPECIAL_CHARS);
-      } else if (parameters[i].first == "parent") {
-        directory_resource_id =
-            net::UnescapeURLComponent(parameters[i].second,
-                                      net::UnescapeRule::URL_SPECIAL_CHARS);
-      } else if (parameters[i].first == "start-offset") {
-        base::StringToInt(parameters[i].second, &start_offset);
-      } else if (parameters[i].first == "max-results") {
-        base::StringToInt(parameters[i].second, &max_results);
-      }
-    }
-  }
-
-  GetResourceListInternal(
-      start_changestamp, search_query, directory_resource_id,
-      start_offset, max_results, NULL, callback);
-  return CancelCallback();
-}
-
 void FakeDriveService::AddNewFile(const std::string& content_type,
                                   const std::string& content_data,
                                   const std::string& parent_resource_id,
@@ -1305,6 +1187,47 @@ void FakeDriveService::AddNewFileWithResourceId(
       base::Bind(callback, HTTP_CREATED, base::Passed(&parsed_entry)));
 }
 
+CancelCallback FakeDriveService::AddNewDirectoryWithResourceId(
+    const std::string& resource_id,
+    const std::string& parent_resource_id,
+    const std::string& directory_title,
+    const AddNewDirectoryOptions& options,
+    const GetResourceEntryCallback& callback) {
+  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(!callback.is_null());
+
+  if (offline_) {
+    scoped_ptr<ResourceEntry> null;
+    base::MessageLoop::current()->PostTask(
+        FROM_HERE,
+        base::Bind(callback,
+                   GDATA_NO_CONNECTION,
+                   base::Passed(&null)));
+    return CancelCallback();
+  }
+
+  const EntryInfo* new_entry = AddNewEntry(resource_id,
+                                           util::kDriveFolderMimeType,
+                                           "",  // content_data
+                                           parent_resource_id,
+                                           directory_title,
+                                           false);  // shared_with_me
+  if (!new_entry) {
+    scoped_ptr<ResourceEntry> null;
+    base::MessageLoop::current()->PostTask(
+        FROM_HERE,
+        base::Bind(callback, HTTP_NOT_FOUND, base::Passed(&null)));
+    return CancelCallback();
+  }
+
+  scoped_ptr<ResourceEntry> parsed_entry(
+      util::ConvertChangeResourceToResourceEntry(new_entry->change_resource));
+  base::MessageLoop::current()->PostTask(
+      FROM_HERE,
+      base::Bind(callback, HTTP_CREATED, base::Passed(&parsed_entry)));
+  return CancelCallback();
+}
+
 void FakeDriveService::SetLastModifiedTime(
     const std::string& resource_id,
     const base::Time& last_modified_time,
@@ -1401,7 +1324,7 @@ const FakeDriveService::EntryInfo* FakeDriveService::AddNewEntry(
   new_file->set_file_id(resource_id);
   new_file->set_title(title);
   // Set the contents, size and MD5 for a file.
-  if (content_type != kDriveFolderMimeType) {
+  if (content_type != util::kDriveFolderMimeType) {
     new_entry->content_data = content_data;
     new_file->set_file_size(content_data.size());
     new_file->set_md5_checksum(base::MD5String(content_data));
@@ -1417,17 +1340,19 @@ const FakeDriveService::EntryInfo* FakeDriveService::AddNewEntry(
   // Set mime type.
   new_file->set_mime_type(content_type);
 
+  // Set alternate link if needed.
+  if (content_type == util::kGoogleDocumentMimeType)
+    new_file->set_alternate_link(GURL("https://document_alternate_link"));
+
   // Set parents.
-  ParentReference parent;
-  if (parent_resource_id.empty())
-    parent.set_file_id(GetRootResourceId());
-  else
+  if (!parent_resource_id.empty()) {
+    ParentReference parent;
     parent.set_file_id(parent_resource_id);
-  parent.set_parent_link(GetFakeLinkUrl(parent.file_id()));
-  parent.set_is_root(parent.file_id() == GetRootResourceId());
-  std::vector<ParentReference> parents;
-  parents.push_back(parent);
-  *new_file->mutable_parents() = parents;
+    parent.set_parent_link(GetFakeLinkUrl(parent.file_id()));
+    std::vector<ParentReference> parents;
+    parents.push_back(parent);
+    *new_file->mutable_parents() = parents;
+  }
 
   new_entry->share_url = net::AppendOrReplaceQueryParameter(
       share_url_base_, "name", title);