Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / net / disk_cache / disk_cache_test_util.h
blob0f40f5e44d4fdba027eebe73af4ae9fe42f5a31c
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 NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_
6 #define NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_
8 #include <string>
10 #include "base/files/file_path.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/timer/timer.h"
13 #include "build/build_config.h"
15 // Re-creates a given test file inside the cache test folder.
16 bool CreateCacheTestFile(const base::FilePath& name);
18 // Deletes all file son the cache.
19 bool DeleteCache(const base::FilePath& path);
21 // Fills buffer with random values (may contain nulls unless no_nulls is true).
22 void CacheTestFillBuffer(char* buffer, size_t len, bool no_nulls);
24 // Generates a random key of up to 200 bytes.
25 std::string GenerateKey(bool same_length);
27 // Returns true if the cache is not corrupt.
28 bool CheckCacheIntegrity(const base::FilePath& path, bool new_eviction,
29 uint32 mask);
31 // -----------------------------------------------------------------------
33 // Simple helper to deal with the message loop on a test.
34 class MessageLoopHelper {
35 public:
36 MessageLoopHelper();
37 ~MessageLoopHelper();
39 // Run the message loop and wait for num_callbacks before returning. Returns
40 // false if we are waiting to long. Each callback that will be waited on is
41 // required to call CallbackWasCalled() to indicate when it was called.
42 bool WaitUntilCacheIoFinished(int num_callbacks);
44 // True if a given callback was called more times than it expected.
45 bool callback_reused_error() const { return callback_reused_error_; }
46 void set_callback_reused_error(bool error) {
47 callback_reused_error_ = error;
50 int callbacks_called() const { return callbacks_called_; }
51 // Report that a callback was called. Each callback that will be waited on
52 // via WaitUntilCacheIoFinished() is expected to call this method to
53 // indicate when it has been executed.
54 void CallbackWasCalled() { ++callbacks_called_; }
56 private:
57 // Sets the number of callbacks that can be received so far.
58 void ExpectCallbacks(int num_callbacks) {
59 num_callbacks_ = num_callbacks;
60 num_iterations_ = last_ = 0;
61 completed_ = false;
64 // Called periodically to test if WaitUntilCacheIoFinished should return.
65 void TimerExpired();
67 base::RepeatingTimer<MessageLoopHelper> timer_;
68 int num_callbacks_;
69 int num_iterations_;
70 int last_;
71 bool completed_;
73 // True if a callback was called/reused more than expected.
74 bool callback_reused_error_;
75 int callbacks_called_;
77 DISALLOW_COPY_AND_ASSIGN(MessageLoopHelper);
80 // -----------------------------------------------------------------------
82 // Simple callback to process IO completions from the cache. It allows tests
83 // with multiple simultaneous IO operations.
84 class CallbackTest {
85 public:
86 // Creates a new CallbackTest object. When the callback is called, it will
87 // update |helper|. If |reuse| is false and a callback is called more than
88 // once, or if |reuse| is true and a callback is called more than twice, an
89 // error will be reported to |helper|.
90 CallbackTest(MessageLoopHelper* helper, bool reuse);
91 ~CallbackTest();
93 void Run(int result);
95 int last_result() const { return last_result_; }
97 private:
98 MessageLoopHelper* helper_;
99 int reuse_;
100 int last_result_;
101 DISALLOW_COPY_AND_ASSIGN(CallbackTest);
104 #endif // NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_