Commit ad9edca8 authored by satorux@chromium.org's avatar satorux@chromium.org

google_apis: Implement some functions in FakeDriveService

Implement GetResourceList, GetAccountMetadata, GetResourceEntry,
and DeleteResource.

BUG=162350
TEST=none; no changes in production code.

Review URL: https://codereview.chromium.org/11787038

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@175716 0039d316-1c4b-4281-b951-d872f2087c98
parent 101b8786
......@@ -5,6 +5,9 @@
#include "chrome/browser/google_apis/fake_drive_service.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "chrome/browser/google_apis/gdata_wapi_parser.h"
#include "chrome/browser/google_apis/test_util.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
......@@ -19,6 +22,32 @@ FakeDriveService::~FakeDriveService() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}
bool FakeDriveService::LoadResourceListForWapi(
const std::string& relative_path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
scoped_ptr<Value> raw_value = test_util::LoadJSONFile(relative_path);
base::DictionaryValue* as_dict = NULL;
base::Value* feed = NULL;
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)) {
resource_list_value_.reset(feed_as_dict);
}
return resource_list_value_;
}
bool FakeDriveService::LoadAccountMetadataForWapi(
const std::string& relative_path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
account_metadata_value_ = test_util::LoadJSONFile(relative_path);
return account_metadata_value_;
}
void FakeDriveService::Initialize(Profile* profile) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}
......@@ -68,6 +97,14 @@ void FakeDriveService::GetResourceList(
const GetResourceListCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
scoped_ptr<ResourceList> resource_list =
ResourceList::CreateFrom(*resource_list_value_);
MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(callback,
HTTP_SUCCESS,
base::Passed(&resource_list)));
}
void FakeDriveService::GetResourceEntry(
......@@ -75,12 +112,48 @@ void FakeDriveService::GetResourceEntry(
const GetResourceEntryCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
base::DictionaryValue* resource_list_dict = NULL;
base::ListValue* entries = NULL;
// Go through entries and return the one that matches |resource_id|.
if (resource_list_value_->GetAsDictionary(&resource_list_dict) &&
resource_list_dict->GetList("entry", &entries)) {
for (size_t i = 0; i < entries->GetSize(); ++i) {
base::DictionaryValue* entry = NULL;
base::DictionaryValue* resource_id_dict = NULL;
std::string current_resource_id;
if (entries->GetDictionary(i, &entry) &&
entry->GetDictionary("gd$resourceId", &resource_id_dict) &&
resource_id_dict->GetString("$t", &current_resource_id) &&
resource_id == current_resource_id) {
scoped_ptr<ResourceEntry> resource_entry =
ResourceEntry::CreateFrom(*entry);
MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(callback, HTTP_SUCCESS, base::Passed(&resource_entry)));
return;
}
}
}
scoped_ptr<ResourceEntry> null;
MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(callback, HTTP_NOT_FOUND, base::Passed(&null)));
}
void FakeDriveService::GetAccountMetadata(
const GetAccountMetadataCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
scoped_ptr<AccountMetadataFeed> account_metadata =
AccountMetadataFeed::CreateFrom(*account_metadata_value_);
MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(callback,
HTTP_SUCCESS,
base::Passed(&account_metadata)));
}
void FakeDriveService::GetApplicationInfo(
......@@ -94,6 +167,38 @@ void FakeDriveService::DeleteResource(
const EntryActionCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
base::DictionaryValue* resource_list_dict = NULL;
base::ListValue* entries = NULL;
// Go through entries and remove the one that matches |edit_url|.
if (resource_list_value_->GetAsDictionary(&resource_list_dict) &&
resource_list_dict->GetList("entry", &entries)) {
for (size_t i = 0; i < entries->GetSize(); ++i) {
base::DictionaryValue* entry = NULL;
base::ListValue* links = NULL;
if (entries->GetDictionary(i, &entry) &&
entry->GetList("link", &links)) {
for (size_t j = 0; j < links->GetSize(); ++j) {
base::DictionaryValue* link = NULL;
std::string rel;
std::string href;
if (links->GetDictionary(j, &link) &&
link->GetString("rel", &rel) &&
link->GetString("href", &href) &&
rel == "edit" &&
GURL(href) == edit_url) {
entries->Remove(i, NULL);
MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(callback, HTTP_SUCCESS));
return;
}
}
}
}
}
MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND));
}
void FakeDriveService::DownloadHostedDocument(
......
......@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_GOOGLE_APIS_FAKE_DRIVE_SERVICE_H_
#define CHROME_BROWSER_GOOGLE_APIS_FAKE_DRIVE_SERVICE_H_
#include "base/values.h"
#include "chrome/browser/google_apis/drive_service_interface.h"
namespace google_apis {
......@@ -22,6 +23,12 @@ class FakeDriveService : public DriveServiceInterface {
FakeDriveService();
virtual ~FakeDriveService();
// Loads the resource list for WAPI. Returns true on success.
bool LoadResourceListForWapi(const std::string& relative_path);
// Loads the account metadata for WAPI. Returns true on success.
bool LoadAccountMetadataForWapi(const std::string& relative_path);
// DriveServiceInterface Overrides
virtual void Initialize(Profile* profile) OVERRIDE;
virtual void AddObserver(DriveServiceObserver* observer) OVERRIDE;
......@@ -85,6 +92,11 @@ class FakeDriveService : public DriveServiceInterface {
virtual void AuthorizeApp(const GURL& edit_url,
const std::string& app_id,
const AuthorizeAppCallback& callback) OVERRIDE;
private:
scoped_ptr<base::Value> resource_list_value_;
scoped_ptr<base::Value> account_metadata_value_;
DISALLOW_COPY_AND_ASSIGN(FakeDriveService);
};
} // namespace google_apis
......
// Copyright (c) 2012 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 "chrome/browser/google_apis/fake_drive_service.h"
#include "base/message_loop.h"
#include "chrome/browser/google_apis/gdata_wapi_parser.h"
#include "chrome/browser/google_apis/test_util.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace google_apis {
class FakeDriveServiceTest : public testing::Test {
protected:
FakeDriveServiceTest()
: ui_thread_(content::BrowserThread::UI, &message_loop_) {
}
// Returns true if the resource identified by |resource_id| exists.
bool Exists(const std::string& resource_id) {
GDataErrorCode error = GDATA_OTHER_ERROR;
scoped_ptr<ResourceEntry> resource_entry;
fake_service_.GetResourceEntry(
resource_id,
base::Bind(&test_util::CopyResultsFromGetResourceEntryCallback,
&error,
&resource_entry));
message_loop_.RunUntilIdle();
return error == HTTP_SUCCESS;
}
MessageLoopForUI message_loop_;
content::TestBrowserThread ui_thread_;
FakeDriveService fake_service_;
};
TEST_F(FakeDriveServiceTest, GetResourceList) {
ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
GDataErrorCode error = GDATA_OTHER_ERROR;
scoped_ptr<ResourceList> resource_list;
fake_service_.GetResourceList(
GURL(),
0, // start_changestamp
"", // search_query
false, // shared_with_me
"", // directory_resource_id
base::Bind(&test_util::CopyResultsFromGetResourceListCallback,
&error,
&resource_list));
message_loop_.RunUntilIdle();
EXPECT_EQ(HTTP_SUCCESS, error);
ASSERT_TRUE(resource_list);
// Do some sanity check.
EXPECT_EQ(12U, resource_list->entries().size());
}
TEST_F(FakeDriveServiceTest, GetAccountMetadata) {
ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
"gdata/account_metadata.json"));
GDataErrorCode error = GDATA_OTHER_ERROR;
scoped_ptr<AccountMetadataFeed> account_metadata;
fake_service_.GetAccountMetadata(
base::Bind(&test_util::CopyResultsFromGetAccountMetadataCallback,
&error,
&account_metadata));
message_loop_.RunUntilIdle();
EXPECT_EQ(HTTP_SUCCESS, error);
ASSERT_TRUE(account_metadata);
// Do some sanity check.
EXPECT_EQ(2U, account_metadata->installed_apps().size());
}
TEST_F(FakeDriveServiceTest, GetResourceEntry_ExistingFile) {
ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
const std::string kResourceId = "file:2_file_resource_id";
GDataErrorCode error = GDATA_OTHER_ERROR;
scoped_ptr<ResourceEntry> resource_entry;
fake_service_.GetResourceEntry(
kResourceId,
base::Bind(&test_util::CopyResultsFromGetResourceEntryCallback,
&error,
&resource_entry));
message_loop_.RunUntilIdle();
EXPECT_EQ(HTTP_SUCCESS, error);
ASSERT_TRUE(resource_entry);
// Do some sanity check.
EXPECT_EQ(kResourceId, resource_entry->resource_id());
}
TEST_F(FakeDriveServiceTest, GetResourceEntry_NonexistingFile) {
ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
const std::string kResourceId = "file:nonexisting_resource_id";
GDataErrorCode error = GDATA_OTHER_ERROR;
scoped_ptr<ResourceEntry> resource_entry;
fake_service_.GetResourceEntry(
kResourceId,
base::Bind(&test_util::CopyResultsFromGetResourceEntryCallback,
&error,
&resource_entry));
message_loop_.RunUntilIdle();
EXPECT_EQ(HTTP_NOT_FOUND, error);
ASSERT_FALSE(resource_entry);
}
TEST_F(FakeDriveServiceTest, DeleteResource_ExistingFile) {
ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
// Resource "file:2_file_resource_id" should now exist.
ASSERT_TRUE(Exists("file:2_file_resource_id"));
GDataErrorCode error = GDATA_OTHER_ERROR;
fake_service_.DeleteResource(
GURL("https://file1_link_self/file:2_file_resource_id"),
base::Bind(&test_util::CopyResultsFromEntryActionCallback,
&error));
message_loop_.RunUntilIdle();
EXPECT_EQ(HTTP_SUCCESS, error);
// Resource "file:2_file_resource_id" should be gone now.
EXPECT_FALSE(Exists("file:2_file_resource_id"));
}
TEST_F(FakeDriveServiceTest, DeleteResource_NonexistingFile) {
ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
GDataErrorCode error = GDATA_OTHER_ERROR;
fake_service_.DeleteResource(
GURL("https://file1_link_self/file:nonexisting_resource_id"),
base::Bind(&test_util::CopyResultsFromEntryActionCallback,
&error));
message_loop_.RunUntilIdle();
EXPECT_EQ(HTTP_NOT_FOUND, error);
}
} // namespace google_apis
......@@ -769,6 +769,7 @@
'browser/google_apis/gdata_wapi_url_generator_unittest.cc',
'browser/google_apis/fake_drive_service.cc',
'browser/google_apis/fake_drive_service.h',
'browser/google_apis/fake_drive_service_unittest.cc',
'browser/google_apis/mock_drive_service.cc',
'browser/google_apis/mock_drive_service.h',
'browser/google_apis/operation_registry_unittest.cc',
......@@ -2133,6 +2134,7 @@
'browser/sessions/session_backend_unittest.cc',
# Test files cannot be opened on Android.
'browser/google_apis/fake_drive_service_unittest.cc',
'browser/google_apis/gdata_wapi_operations_unittest.cc',
'browser/google_apis/gdata_wapi_parser_unittest.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment