test_util.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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/drive/test_util.h" 6 7#include <string> 8 9#include "base/bind.h" 10#include "base/bind_helpers.h" 11#include "base/message_loop.h" 12#include "base/threading/worker_pool.h" 13#include "base/values.h" 14#include "chrome/browser/chromeos/drive/change_list_loader.h" 15#include "chrome/browser/chromeos/drive/change_list_processor.h" 16#include "chrome/browser/chromeos/drive/drive.pb.h" 17#include "chrome/browser/google_apis/drive_api_parser.h" 18 19namespace drive { 20 21namespace test_util { 22 23TestCacheResource::TestCacheResource(const std::string& source_file, 24 const std::string& resource_id, 25 const std::string& md5, 26 bool is_pinned, 27 bool is_dirty) : 28 source_file(source_file), 29 resource_id(resource_id), 30 md5(md5), 31 is_pinned(is_pinned), 32 is_dirty(is_dirty) { 33} 34 35std::vector<TestCacheResource> GetDefaultTestCacheResources() { 36 const TestCacheResource resources[] = { 37 // Cache resource in tmp dir, i.e. not pinned or dirty. 38 TestCacheResource("gdata/root_feed.json", 39 "tmp:resource_id", 40 "md5_tmp_alphanumeric", 41 false, 42 false), 43 // Cache resource in tmp dir, i.e. not pinned or dirty, with resource_id 44 // containing non-alphanumeric characters. 45 TestCacheResource("gdata/empty_feed.json", 46 "tmp:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?", 47 "md5_tmp_non_alphanumeric", 48 false, 49 false), 50 // Cache resource that is pinned and persistent. 51 TestCacheResource("gdata/directory_entry.json", 52 "pinned:existing", 53 "md5_pinned_existing", 54 true, 55 false), 56 // Cache resource with a non-existent source file that is pinned. 57 TestCacheResource("", 58 "pinned:non-existent", 59 "md5_pinned_non_existent", 60 true, 61 false), 62 // Cache resource that is dirty. 63 TestCacheResource("gdata/account_metadata.json", 64 "dirty:existing", 65 "md5_dirty_existing", 66 false, 67 true), 68 // Cache resource that is pinned and dirty. 69 TestCacheResource("gdata/basic_feed.json", 70 "dirty_and_pinned:existing", 71 "md5_dirty_and_pinned_existing", 72 true, 73 true) 74 }; 75 return std::vector<TestCacheResource>( 76 resources, 77 resources + ARRAYSIZE_UNSAFE(resources)); 78} 79 80FileCacheEntry ToCacheEntry(int cache_state) { 81 FileCacheEntry cache_entry; 82 cache_entry.set_is_present(cache_state & TEST_CACHE_STATE_PRESENT); 83 cache_entry.set_is_pinned(cache_state & TEST_CACHE_STATE_PINNED); 84 cache_entry.set_is_dirty(cache_state & TEST_CACHE_STATE_DIRTY); 85 cache_entry.set_is_mounted(cache_state & TEST_CACHE_STATE_MOUNTED); 86 cache_entry.set_is_persistent(cache_state & TEST_CACHE_STATE_PERSISTENT); 87 return cache_entry; 88} 89 90bool CacheStatesEqual(const FileCacheEntry& a, const FileCacheEntry& b) { 91 return (a.is_present() == b.is_present() && 92 a.is_pinned() == b.is_pinned() && 93 a.is_dirty() == b.is_dirty() && 94 a.is_mounted() == b.is_mounted() && 95 a.is_persistent() == b.is_persistent()); 96} 97 98bool LoadChangeFeed(const std::string& relative_path, 99 internal::ChangeListLoader* change_list_loader, 100 bool is_delta_feed, 101 const std::string& root_resource_id, 102 int64 root_feed_changestamp) { 103 scoped_ptr<Value> document = 104 google_apis::test_util::LoadJSONFile(relative_path); 105 if (!document.get()) 106 return false; 107 if (document->GetType() != Value::TYPE_DICTIONARY) 108 return false; 109 110 scoped_ptr<google_apis::ResourceList> document_feed( 111 google_apis::ResourceList::ExtractAndParse(*document)); 112 if (!document_feed.get()) 113 return false; 114 115 ScopedVector<internal::ChangeList> change_lists; 116 change_lists.push_back(new internal::ChangeList(*document_feed)); 117 118 scoped_ptr<google_apis::AboutResource> about_resource( 119 new google_apis::AboutResource); 120 about_resource->set_largest_change_id(root_feed_changestamp); 121 about_resource->set_root_folder_id(root_resource_id); 122 123 change_list_loader->UpdateFromFeed( 124 about_resource.Pass(), 125 change_lists.Pass(), 126 is_delta_feed, 127 base::Bind(&base::DoNothing)); 128 // ChangeListLoader::UpdateFromFeed is asynchronous, so wait for it to finish. 129 google_apis::test_util::RunBlockingPoolTask(); 130 131 return true; 132} 133 134bool PrepareTestCacheResources( 135 internal::FileCache* cache, 136 const std::vector<TestCacheResource>& resources) { 137 for (size_t i = 0; i < resources.size(); ++i) { 138 // Copy file from data dir to cache. 139 if (!resources[i].source_file.empty()) { 140 base::FilePath source_path = 141 google_apis::test_util::GetTestFilePath( 142 std::string("chromeos/") + resources[i].source_file); 143 144 FileError error = FILE_ERROR_OK; 145 cache->StoreOnUIThread( 146 resources[i].resource_id, 147 resources[i].md5, 148 source_path, 149 internal::FileCache::FILE_OPERATION_COPY, 150 google_apis::test_util::CreateCopyResultCallback(&error)); 151 google_apis::test_util::RunBlockingPoolTask(); 152 if (error != FILE_ERROR_OK) 153 return false; 154 } 155 // Pin. 156 if (resources[i].is_pinned) { 157 FileError error = FILE_ERROR_OK; 158 cache->PinOnUIThread( 159 resources[i].resource_id, 160 resources[i].md5, 161 google_apis::test_util::CreateCopyResultCallback(&error)); 162 google_apis::test_util::RunBlockingPoolTask(); 163 if (error != FILE_ERROR_OK) 164 return false; 165 } 166 // Mark dirty. 167 if (resources[i].is_dirty) { 168 FileError error = FILE_ERROR_OK; 169 cache->MarkDirtyOnUIThread( 170 resources[i].resource_id, 171 resources[i].md5, 172 google_apis::test_util::CreateCopyResultCallback(&error)); 173 google_apis::test_util::RunBlockingPoolTask(); 174 if (error != FILE_ERROR_OK) 175 return false; 176 177 cache->CommitDirtyOnUIThread( 178 resources[i].resource_id, 179 resources[i].md5, 180 google_apis::test_util::CreateCopyResultCallback(&error)); 181 google_apis::test_util::RunBlockingPoolTask(); 182 if (error != FILE_ERROR_OK) 183 return false; 184 } 185 } 186 return true; 187} 188 189} // namespace test_util 190} // namespace drive 191