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" | |
18 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
19 | 18 |
20 namespace base { | 19 namespace base { |
21 | 20 |
22 class FileProxyTest : public testing::Test { | 21 class FileProxyTest : public testing::Test { |
23 public: | 22 public: |
24 FileProxyTest() | 23 FileProxyTest() |
25 : file_thread_("FileProxyTestFileThread"), | 24 : file_thread_("FileProxyTestFileThread"), |
26 error_(File::FILE_OK), | 25 error_(File::FILE_OK), |
27 bytes_written_(-1), | 26 bytes_written_(-1), |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 test_path(), | 136 test_path(), |
138 File::FLAG_OPEN | File::FLAG_READ, | 137 File::FLAG_OPEN | File::FLAG_READ, |
139 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr())); | 138 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr())); |
140 MessageLoop::current()->Run(); | 139 MessageLoop::current()->Run(); |
141 EXPECT_EQ(File::FILE_ERROR_NOT_FOUND, error_); | 140 EXPECT_EQ(File::FILE_ERROR_NOT_FOUND, error_); |
142 EXPECT_FALSE(proxy.IsValid()); | 141 EXPECT_FALSE(proxy.IsValid()); |
143 EXPECT_FALSE(proxy.created()); | 142 EXPECT_FALSE(proxy.created()); |
144 EXPECT_FALSE(PathExists(test_path())); | 143 EXPECT_FALSE(PathExists(test_path())); |
145 } | 144 } |
146 | 145 |
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 | |
162 TEST_F(FileProxyTest, Close) { | 146 TEST_F(FileProxyTest, Close) { |
163 // Creates a file. | 147 // Creates a file. |
164 FileProxy proxy(file_task_runner()); | 148 FileProxy proxy(file_task_runner()); |
165 CreateProxy(File::FLAG_CREATE | File::FLAG_WRITE, &proxy); | 149 CreateProxy(File::FLAG_CREATE | File::FLAG_WRITE, &proxy); |
166 | 150 |
167 #if defined(OS_WIN) | 151 #if defined(OS_WIN) |
168 // This fails on Windows if the file is not closed. | 152 // This fails on Windows if the file is not closed. |
169 EXPECT_FALSE(base::Move(test_path(), test_dir_path().AppendASCII("new"))); | 153 EXPECT_FALSE(base::Move(test_path(), test_dir_path().AppendASCII("new"))); |
170 #endif | 154 #endif |
171 | 155 |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 char buffer[53]; | 334 char buffer[53]; |
351 EXPECT_EQ(53, base::ReadFile(test_path(), buffer, 53)); | 335 EXPECT_EQ(53, base::ReadFile(test_path(), buffer, 53)); |
352 int i = 0; | 336 int i = 0; |
353 for (; i < 10; ++i) | 337 for (; i < 10; ++i) |
354 EXPECT_EQ(kTestData[i], buffer[i]); | 338 EXPECT_EQ(kTestData[i], buffer[i]); |
355 for (; i < 53; ++i) | 339 for (; i < 53; ++i) |
356 EXPECT_EQ(0, buffer[i]); | 340 EXPECT_EQ(0, buffer[i]); |
357 } | 341 } |
358 | 342 |
359 } // namespace base | 343 } // namespace base |
OLD | NEW |