test_util.h revision 868fa2fe829687343ffae624259930155e16dbd8
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#include <vector>
10
11#include "chrome/browser/chromeos/drive/file_cache.h"
12#include "chrome/browser/google_apis/test_util.h"
13#include "net/base/completion_callback.h"
14#include "net/base/io_buffer.h"
15#include "net/base/test_completion_callback.h"
16
17namespace net {
18class IOBuffer;
19}  // namespace net
20
21namespace drive {
22
23namespace test_util {
24
25// Disk space size used by FakeFreeDiskSpaceGetter.
26const int64 kLotsOfSpace = internal::kMinFreeSpace * 10;
27
28// This is a bitmask of cache states in FileCacheEntry. Used only in tests.
29enum TestFileCacheState {
30  TEST_CACHE_STATE_NONE       = 0,
31  TEST_CACHE_STATE_PINNED     = 1 << 0,
32  TEST_CACHE_STATE_PRESENT    = 1 << 1,
33  TEST_CACHE_STATE_DIRTY      = 1 << 2,
34  TEST_CACHE_STATE_MOUNTED    = 1 << 3,
35  TEST_CACHE_STATE_PERSISTENT = 1 << 4,
36};
37
38// Test data type of file cache
39struct TestCacheResource {
40  TestCacheResource(const std::string& source_file,
41                    const std::string& resource_id,
42                    const std::string& md5,
43                    bool is_pinned,
44                    bool is_dirty);
45  ~TestCacheResource(){}
46
47  std::string source_file;
48  std::string resource_id;
49  std::string md5;
50  bool is_pinned;
51  bool is_dirty;
52};
53
54// Obtains default test data for FileCacheEntry.
55std::vector<TestCacheResource> GetDefaultTestCacheResources();
56
57// Converts |cache_state| which is a bit mask of TestFileCacheState, to a
58// FileCacheEntry.
59FileCacheEntry ToCacheEntry(int cache_state);
60
61// Returns true if the cache state of the given two cache entries are equal.
62bool CacheStatesEqual(const FileCacheEntry& a, const FileCacheEntry& b);
63
64// Helper to destroy objects which needs Destroy() to be called on destruction.
65// Note: When using this helper, you should destruct objects before
66// BrowserThread.
67struct DestroyHelperForTests {
68  template<typename T>
69  void operator()(T* object) const {
70    if (object) {
71      object->Destroy();
72      google_apis::test_util::RunBlockingPoolTask();  // Finish destruction.
73    }
74  }
75};
76
77// Reads all the data from |reader| and copies to |content|. Returns net::Error
78// code.
79template<typename Reader>
80int ReadAllData(Reader* reader, std::string* content) {
81  const int kBufferSize = 10;
82  scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize));
83  while (true) {
84    net::TestCompletionCallback callback;
85    int result = reader->Read(buffer.get(), kBufferSize, callback.callback());
86    result = callback.GetResult(result);
87    if (result <= 0) {
88      // Found an error or EOF. Return it. Note: net::OK is 0.
89      return result;
90    }
91    content->append(buffer->data(), result);
92  }
93}
94
95// Adds test cache |resources| to |cache|. Returns whether the operation
96// succeeded or not.
97bool PrepareTestCacheResources(
98    internal::FileCache* cache,
99    const std::vector<TestCacheResource>& resources);
100
101}  // namespace test_util
102}  // namespace drive
103
104#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_TEST_UTIL_H_
105