OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/files/file_proxy.h" | 5 #include "base/files/file_proxy.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/files/file.h" | 11 #include "base/files/file.h" |
12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
| 17 #include "base/threading/thread_restrictions.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 | 21 |
21 class FileProxyTest : public testing::Test { | 22 class FileProxyTest : public testing::Test { |
22 public: | 23 public: |
23 FileProxyTest() | 24 FileProxyTest() |
24 : file_thread_("FileProxyTestFileThread"), | 25 : file_thread_("FileProxyTestFileThread"), |
25 error_(File::FILE_OK), | 26 error_(File::FILE_OK), |
26 bytes_written_(-1), | 27 bytes_written_(-1), |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 test_path(), | 137 test_path(), |
137 File::FLAG_OPEN | File::FLAG_READ, | 138 File::FLAG_OPEN | File::FLAG_READ, |
138 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr())); | 139 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr())); |
139 MessageLoop::current()->Run(); | 140 MessageLoop::current()->Run(); |
140 EXPECT_EQ(File::FILE_ERROR_NOT_FOUND, error_); | 141 EXPECT_EQ(File::FILE_ERROR_NOT_FOUND, error_); |
141 EXPECT_FALSE(proxy.IsValid()); | 142 EXPECT_FALSE(proxy.IsValid()); |
142 EXPECT_FALSE(proxy.created()); | 143 EXPECT_FALSE(proxy.created()); |
143 EXPECT_FALSE(PathExists(test_path())); | 144 EXPECT_FALSE(PathExists(test_path())); |
144 } | 145 } |
145 | 146 |
| 147 TEST_F(FileProxyTest, CreateOrOpen_AbandonedCreate) { |
| 148 bool prev = ThreadRestrictions::SetIOAllowed(false); |
| 149 { |
| 150 FileProxy proxy(file_task_runner()); |
| 151 proxy.CreateOrOpen( |
| 152 test_path(), |
| 153 File::FLAG_CREATE | File::FLAG_READ, |
| 154 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr())); |
| 155 } |
| 156 MessageLoop::current()->Run(); |
| 157 ThreadRestrictions::SetIOAllowed(prev); |
| 158 |
| 159 EXPECT_TRUE(PathExists(test_path())); |
| 160 } |
| 161 |
146 TEST_F(FileProxyTest, Close) { | 162 TEST_F(FileProxyTest, Close) { |
147 // Creates a file. | 163 // Creates a file. |
148 FileProxy proxy(file_task_runner()); | 164 FileProxy proxy(file_task_runner()); |
149 CreateProxy(File::FLAG_CREATE | File::FLAG_WRITE, &proxy); | 165 CreateProxy(File::FLAG_CREATE | File::FLAG_WRITE, &proxy); |
150 | 166 |
151 #if defined(OS_WIN) | 167 #if defined(OS_WIN) |
152 // This fails on Windows if the file is not closed. | 168 // This fails on Windows if the file is not closed. |
153 EXPECT_FALSE(base::Move(test_path(), test_dir_path().AppendASCII("new"))); | 169 EXPECT_FALSE(base::Move(test_path(), test_dir_path().AppendASCII("new"))); |
154 #endif | 170 #endif |
155 | 171 |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 char buffer[53]; | 350 char buffer[53]; |
335 EXPECT_EQ(53, base::ReadFile(test_path(), buffer, 53)); | 351 EXPECT_EQ(53, base::ReadFile(test_path(), buffer, 53)); |
336 int i = 0; | 352 int i = 0; |
337 for (; i < 10; ++i) | 353 for (; i < 10; ++i) |
338 EXPECT_EQ(kTestData[i], buffer[i]); | 354 EXPECT_EQ(kTestData[i], buffer[i]); |
339 for (; i < 53; ++i) | 355 for (; i < 53; ++i) |
340 EXPECT_EQ(0, buffer[i]); | 356 EXPECT_EQ(0, buffer[i]); |
341 } | 357 } |
342 | 358 |
343 } // namespace base | 359 } // namespace base |
OLD | NEW |