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_
10 #include "base/files/file_path.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/timer/timer.h"
13 #include "base/tuple.h"
14 #include "build/build_config.h"
16 // Re-creates a given test file inside the cache test folder.
17 bool CreateCacheTestFile(const base::FilePath
& name
);
19 // Deletes all file son the cache.
20 bool DeleteCache(const base::FilePath
& path
);
22 // Fills buffer with random values (may contain nulls unless no_nulls is true).
23 void CacheTestFillBuffer(char* buffer
, size_t len
, bool no_nulls
);
25 // Generates a random key of up to 200 bytes.
26 std::string
GenerateKey(bool same_length
);
28 // Returns true if the cache is not corrupt.
29 bool CheckCacheIntegrity(const base::FilePath
& path
, bool new_eviction
,
32 // -----------------------------------------------------------------------
34 // Simple helper to deal with the message loop on a test.
35 class MessageLoopHelper
{
40 // Run the message loop and wait for num_callbacks before returning. Returns
41 // false if we are waiting to long. Each callback that will be waited on is
42 // required to call CallbackWasCalled() to indicate when it was called.
43 bool WaitUntilCacheIoFinished(int num_callbacks
);
45 // True if a given callback was called more times than it expected.
46 bool callback_reused_error() const { return callback_reused_error_
; }
47 void set_callback_reused_error(bool error
) {
48 callback_reused_error_
= error
;
51 int callbacks_called() const { return callbacks_called_
; }
52 // Report that a callback was called. Each callback that will be waited on
53 // via WaitUntilCacheIoFinished() is expected to call this method to
54 // indicate when it has been executed.
55 void CallbackWasCalled() { ++callbacks_called_
; }
58 // Sets the number of callbacks that can be received so far.
59 void ExpectCallbacks(int num_callbacks
) {
60 num_callbacks_
= num_callbacks
;
61 num_iterations_
= last_
= 0;
65 // Called periodically to test if WaitUntilCacheIoFinished should return.
68 base::RepeatingTimer
<MessageLoopHelper
> timer_
;
74 // True if a callback was called/reused more than expected.
75 bool callback_reused_error_
;
76 int callbacks_called_
;
78 DISALLOW_COPY_AND_ASSIGN(MessageLoopHelper
);
81 // -----------------------------------------------------------------------
83 // Simple callback to process IO completions from the cache. It allows tests
84 // with multiple simultaneous IO operations.
87 // Creates a new CallbackTest object. When the callback is called, it will
88 // update |helper|. If |reuse| is false and a callback is called more than
89 // once, or if |reuse| is true and a callback is called more than twice, an
90 // error will be reported to |helper|.
91 CallbackTest(MessageLoopHelper
* helper
, bool reuse
);
96 int last_result() const { return last_result_
; }
99 MessageLoopHelper
* helper_
;
102 DISALLOW_COPY_AND_ASSIGN(CallbackTest
);
105 #endif // NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_