1 // Copyright (c) 2011 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 #include "base/basictypes.h"
6 #include "base/file_util.h"
7 #include "base/string_util.h"
8 #include "net/disk_cache/disk_cache_test_base.h"
9 #include "net/disk_cache/disk_cache_test_util.h"
10 #include "net/disk_cache/mapped_file.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 // Implementation of FileIOCallback for the tests.
16 class FileCallbackTest
: public disk_cache::FileIOCallback
{
18 FileCallbackTest(int id
, MessageLoopHelper
* helper
, int* max_id
)
23 virtual ~FileCallbackTest() {}
25 virtual void OnFileIOComplete(int bytes_copied
) OVERRIDE
;
29 MessageLoopHelper
* helper_
;
33 void FileCallbackTest::OnFileIOComplete(int bytes_copied
) {
36 helper_
->set_callback_reused_error(true);
39 helper_
->CallbackWasCalled();
44 TEST_F(DiskCacheTest
, MappedFile_SyncIO
) {
45 base::FilePath filename
= cache_path_
.AppendASCII("a_test");
46 scoped_refptr
<disk_cache::MappedFile
> file(new disk_cache::MappedFile
);
47 ASSERT_TRUE(CreateCacheTestFile(filename
));
48 ASSERT_TRUE(file
->Init(filename
, 8192));
52 CacheTestFillBuffer(buffer1
, sizeof(buffer1
), false);
53 base::strlcpy(buffer1
, "the data", arraysize(buffer1
));
54 EXPECT_TRUE(file
->Write(buffer1
, sizeof(buffer1
), 8192));
55 EXPECT_TRUE(file
->Read(buffer2
, sizeof(buffer2
), 8192));
56 EXPECT_STREQ(buffer1
, buffer2
);
59 TEST_F(DiskCacheTest
, MappedFile_AsyncIO
) {
60 base::FilePath filename
= cache_path_
.AppendASCII("a_test");
61 scoped_refptr
<disk_cache::MappedFile
> file(new disk_cache::MappedFile
);
62 ASSERT_TRUE(CreateCacheTestFile(filename
));
63 ASSERT_TRUE(file
->Init(filename
, 8192));
66 MessageLoopHelper helper
;
67 FileCallbackTest
callback(1, &helper
, &max_id
);
71 CacheTestFillBuffer(buffer1
, sizeof(buffer1
), false);
72 base::strlcpy(buffer1
, "the data", arraysize(buffer1
));
74 EXPECT_TRUE(file
->Write(buffer1
, sizeof(buffer1
), 1024 * 1024, &callback
,
76 int expected
= completed
? 0 : 1;
79 helper
.WaitUntilCacheIoFinished(expected
);
81 EXPECT_TRUE(file
->Read(buffer2
, sizeof(buffer2
), 1024 * 1024, &callback
,
86 helper
.WaitUntilCacheIoFinished(expected
);
88 EXPECT_EQ(expected
, helper
.callbacks_called());
89 EXPECT_FALSE(helper
.callback_reused_error());
90 EXPECT_STREQ(buffer1
, buffer2
);