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 "content/public/test/test_utils.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 #include "third_party/cros_system_api/constants/cryptohome.h" 17 18 class PrefRegistrySimple; 19 20 namespace net { 21 class IOBuffer; 22 } // namespace net 23 24 namespace drive { 25 26 namespace test_util { 27 28 // Disk space size used by FakeFreeDiskSpaceGetter. 29 const int64 kLotsOfSpace = cryptohome::kMinFreeSpaceInBytes * 10; 30 31 // Helper to destroy objects which needs Destroy() to be called on destruction. 32 // Note: When using this helper, you should destruct objects before 33 // BrowserThread. 34 struct DestroyHelperForTests { 35 template<typename T> 36 void operator()(T* object) const { 37 if (object) { 38 object->Destroy(); 39 content::RunAllBlockingPoolTasksUntilIdle(); // Finish destruction. 40 } 41 } 42 }; 43 44 // Reads all the data from |reader| and copies to |content|. Returns net::Error 45 // code. 46 template<typename Reader> 47 int ReadAllData(Reader* reader, std::string* content) { 48 const int kBufferSize = 10; 49 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); 50 while (true) { 51 net::TestCompletionCallback callback; 52 int result = reader->Read(buffer.get(), kBufferSize, callback.callback()); 53 result = callback.GetResult(result); 54 if (result <= 0) { 55 // Found an error or EOF. Return it. Note: net::OK is 0. 56 return result; 57 } 58 content->append(buffer->data(), result); 59 } 60 } 61 62 // Registers Drive related preferences in |pref_registry|. Drive related 63 // preferences should be registered as TestingPrefServiceSimple will crash if 64 // unregistered preference is referenced. 65 void RegisterDrivePrefs(PrefRegistrySimple* pref_registry); 66 67 // Fake NetworkChangeNotifier implementation. 68 class FakeNetworkChangeNotifier : public net::NetworkChangeNotifier { 69 public: 70 FakeNetworkChangeNotifier(); 71 72 void SetConnectionType(ConnectionType type); 73 74 // NetworkChangeNotifier override. 75 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE; 76 77 private: 78 net::NetworkChangeNotifier::ConnectionType type_; 79 }; 80 81 } // namespace test_util 82 } // namespace drive 83 84 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_TEST_UTIL_H_ 85