test_util.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_TEST_UTIL_H_ 6#define CHROME_BROWSER_CHROMEOS_DRIVE_TEST_UTIL_H_ 7 8#include <string> 9 10#include "chrome/browser/chromeos/drive/file_cache.h" 11#include "google_apis/drive/test_util.h" 12#include "net/base/completion_callback.h" 13#include "net/base/io_buffer.h" 14#include "net/base/network_change_notifier.h" 15#include "net/base/test_completion_callback.h" 16 17class PrefRegistrySimple; 18 19namespace net { 20class IOBuffer; 21} // namespace net 22 23namespace drive { 24 25namespace test_util { 26 27// Disk space size used by FakeFreeDiskSpaceGetter. 28const int64 kLotsOfSpace = internal::kMinFreeSpace * 10; 29 30// Runs a task posted to the blocking pool, including subsequent tasks posted 31// to the UI message loop and the blocking pool. 32// 33// A task is often posted to the blocking pool with PostTaskAndReply(). In 34// that case, a task is posted back to the UI message loop, which can again 35// post a task to the blocking pool. This function processes these tasks 36// repeatedly. 37void RunBlockingPoolTask(); 38 39// Helper to destroy objects which needs Destroy() to be called on destruction. 40// Note: When using this helper, you should destruct objects before 41// BrowserThread. 42struct DestroyHelperForTests { 43 template<typename T> 44 void operator()(T* object) const { 45 if (object) { 46 object->Destroy(); 47 test_util::RunBlockingPoolTask(); // Finish destruction. 48 } 49 } 50}; 51 52// Reads all the data from |reader| and copies to |content|. Returns net::Error 53// code. 54template<typename Reader> 55int ReadAllData(Reader* reader, std::string* content) { 56 const int kBufferSize = 10; 57 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); 58 while (true) { 59 net::TestCompletionCallback callback; 60 int result = reader->Read(buffer.get(), kBufferSize, callback.callback()); 61 result = callback.GetResult(result); 62 if (result <= 0) { 63 // Found an error or EOF. Return it. Note: net::OK is 0. 64 return result; 65 } 66 content->append(buffer->data(), result); 67 } 68} 69 70// Registers Drive related preferences in |pref_registry|. Drive related 71// preferences should be registered as TestingPrefServiceSimple will crash if 72// unregistered preference is referenced. 73void RegisterDrivePrefs(PrefRegistrySimple* pref_registry); 74 75// Fake NetworkChangeNotifier implementation. 76class FakeNetworkChangeNotifier : public net::NetworkChangeNotifier { 77 public: 78 FakeNetworkChangeNotifier(); 79 80 void SetConnectionType(ConnectionType type); 81 82 // NetworkChangeNotifier override. 83 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE; 84 85 private: 86 net::NetworkChangeNotifier::ConnectionType type_; 87}; 88 89} // namespace test_util 90} // namespace drive 91 92#endif // CHROME_BROWSER_CHROMEOS_DRIVE_TEST_UTIL_H_ 93