Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(320)

Side by Side Diff: base/files/scoped_platform_handle_unittest.cc

Issue 2705743002: Introduce base::ScopedPlatformHandle (Closed)
Patch Set: . Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/files/scoped_platform_handle_posix.cc ('k') | base/files/scoped_platform_handle_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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.
4
5 #include "base/files/scoped_platform_handle.h"
6
7 #include "base/files/file.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/macros.h"
10 #include "base/strings/stringprintf.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 #if defined(OS_WIN)
14 #include <windows.h>
15
16 #include "base/win/scoped_handle.h"
17 #elif defined(OS_POSIX)
18 #include "base/files/scoped_file.h"
19 #endif
20
21 namespace base {
22 namespace {
23
24 class ScopedPlatformHandleTest : public testing::Test {
25 public:
26 ScopedPlatformHandleTest() { CHECK(temp_dir_.CreateUniqueTempDir()); }
27
28 protected:
29 ScopedPlatformHandle CreateValidHandle() {
30 return ScopedPlatformHandle(OpenTempFile().TakePlatformFile());
31 }
32
33 private:
34 base::File OpenTempFile() {
35 base::File temp_file(temp_dir_.GetPath().AppendASCII(
36 base::StringPrintf("file_%d", next_file_id_)),
37 base::File::FLAG_CREATE | base::File::FLAG_WRITE);
38 ++next_file_id_;
39 return temp_file;
40 }
41
42 ScopedTempDir temp_dir_;
43 int next_file_id_ = 1;
44
45 DISALLOW_COPY_AND_ASSIGN(ScopedPlatformHandleTest);
46 };
47
48 TEST_F(ScopedPlatformHandleTest, Invalid) {
49 ScopedPlatformHandle default_value;
50 EXPECT_TRUE(!default_value.is_valid());
51
52 ScopedPlatformHandle null_value(nullptr);
53 EXPECT_TRUE(!null_value.is_valid());
54
55 default_value.reset();
56 null_value.reset();
57 EXPECT_TRUE(!default_value.is_valid());
58 EXPECT_TRUE(!null_value.is_valid());
59 }
60
61 TEST_F(ScopedPlatformHandleTest, BasicUsage) {
62 ScopedPlatformHandle handle_a = CreateValidHandle();
63 ScopedPlatformHandle handle_b = CreateValidHandle();
64 EXPECT_TRUE(handle_a.is_valid());
65 EXPECT_TRUE(handle_b.is_valid());
66
67 ScopedPlatformHandle::HandleType handle_a_value = handle_a.get();
68 ScopedPlatformHandle::HandleType handle_b_value = handle_b.get();
69 EXPECT_TRUE(handle_a.is_valid());
70 EXPECT_TRUE(handle_b.is_valid());
71
72 ScopedPlatformHandle::ScopedHandleType scoped_handle = handle_a.Take();
73 ScopedPlatformHandle::HandleType raw_handle = handle_b.release();
74 EXPECT_FALSE(handle_a.is_valid());
75 EXPECT_FALSE(handle_b.is_valid());
76
77 handle_a = ScopedPlatformHandle(std::move(scoped_handle));
78 handle_b = ScopedPlatformHandle(raw_handle);
79 EXPECT_TRUE(handle_a.is_valid());
80 EXPECT_TRUE(handle_b.is_valid());
81 EXPECT_EQ(handle_a_value, handle_a.get());
82 EXPECT_EQ(handle_b_value, handle_b.get());
83
84 handle_b = std::move(handle_a);
85 EXPECT_FALSE(handle_a.is_valid());
86 EXPECT_TRUE(handle_b.is_valid());
87 EXPECT_EQ(handle_a_value, handle_b.get());
88
89 handle_b.reset();
90 EXPECT_FALSE(handle_b.is_valid());
91 }
92
93 } // namespace
94 } // namespace base
OLDNEW
« no previous file with comments | « base/files/scoped_platform_handle_posix.cc ('k') | base/files/scoped_platform_handle_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698