Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / drive / drive_test_util.h
blob327dc157e0174d69f7f0568578308e8c4efd88fc
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.
5 #ifndef COMPONENTS_DRIVE_DRIVE_TEST_UTIL_H_
6 #define COMPONENTS_DRIVE_DRIVE_TEST_UTIL_H_
8 #include <string>
10 #include "components/drive/file_cache.h"
11 #include "content/public/test/test_utils.h"
12 #include "google_apis/drive/test_util.h"
13 #include "net/base/completion_callback.h"
14 #include "net/base/io_buffer.h"
15 #include "net/base/network_change_notifier.h"
16 #include "net/base/test_completion_callback.h"
18 class PrefRegistrySimple;
20 namespace net {
21 class IOBuffer;
22 } // namespace net
24 namespace drive {
26 namespace test_util {
28 // Disk space size used by FakeFreeDiskSpaceGetter.
29 const int64 kLotsOfSpace = drive::internal::kMinFreeSpaceInBytes * 10;
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.
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;
58 content->append(buffer->data(), result);
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);
67 // Fake NetworkChangeNotifier implementation.
68 class FakeNetworkChangeNotifier : public net::NetworkChangeNotifier {
69 public:
70 FakeNetworkChangeNotifier();
72 void SetConnectionType(ConnectionType type);
74 // NetworkChangeNotifier override.
75 ConnectionType GetCurrentConnectionType() const override;
77 private:
78 net::NetworkChangeNotifier::ConnectionType type_;
81 } // namespace test_util
82 } // namespace drive
84 #endif // COMPONENTS_DRIVE_DRIVE_TEST_UTIL_H_