1 // Copyright 2023 The Chromium Authors
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_temp_file.h"
6
7 #include <utility>
8
9 #include "base/files/file_util.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace base {
13
TEST(ScopedTempFile,Basic)14 TEST(ScopedTempFile, Basic) {
15 ScopedTempFile temp_file;
16 EXPECT_TRUE(temp_file.path().empty());
17
18 EXPECT_TRUE(temp_file.Create());
19 EXPECT_TRUE(PathExists(temp_file.path()));
20
21 EXPECT_TRUE(temp_file.Delete());
22 EXPECT_FALSE(PathExists(temp_file.path()));
23 }
24
TEST(ScopedTempFile,MoveConstruct)25 TEST(ScopedTempFile, MoveConstruct) {
26 ScopedTempFile temp1;
27 EXPECT_TRUE(temp1.Create());
28 FilePath file1 = temp1.path();
29 EXPECT_TRUE(PathExists(file1));
30
31 ScopedTempFile temp2(std::move(temp1));
32 EXPECT_TRUE(temp1.path().empty());
33 EXPECT_EQ(file1, temp2.path());
34 EXPECT_TRUE(PathExists(file1));
35 }
36
TEST(ScopedTempFile,MoveAssign)37 TEST(ScopedTempFile, MoveAssign) {
38 ScopedTempFile temp1;
39 EXPECT_TRUE(temp1.Create());
40 FilePath file1 = temp1.path();
41 EXPECT_TRUE(PathExists(file1));
42
43 ScopedTempFile temp2;
44 EXPECT_TRUE(temp2.Create());
45 FilePath file2 = temp2.path();
46 EXPECT_TRUE(PathExists(file2));
47
48 temp2 = std::move(temp1);
49 EXPECT_TRUE(temp1.path().empty());
50 EXPECT_EQ(temp2.path(), file1);
51 EXPECT_TRUE(PathExists(file1));
52 EXPECT_FALSE(PathExists(file2));
53 }
54
TEST(ScopedTempFile,Destruct)55 TEST(ScopedTempFile, Destruct) {
56 FilePath file;
57 {
58 ScopedTempFile temp;
59 EXPECT_TRUE(temp.Create());
60 file = temp.path();
61 EXPECT_TRUE(PathExists(file));
62 }
63
64 EXPECT_FALSE(PathExists(file));
65 }
66
TEST(ScopedTempFile,Reset)67 TEST(ScopedTempFile, Reset) {
68 ScopedTempFile temp_file;
69 EXPECT_TRUE(temp_file.path().empty());
70
71 EXPECT_TRUE(temp_file.Create());
72 EXPECT_TRUE(PathExists(temp_file.path()));
73
74 temp_file.Reset();
75 EXPECT_FALSE(PathExists(temp_file.path()));
76 }
77
TEST(ScopedTempFile,OperatorBool)78 TEST(ScopedTempFile, OperatorBool) {
79 ScopedTempFile temp_file;
80 EXPECT_FALSE(temp_file);
81
82 EXPECT_TRUE(temp_file.Create());
83 EXPECT_TRUE(temp_file);
84
85 EXPECT_TRUE(temp_file.Delete());
86 EXPECT_FALSE(temp_file);
87 }
88
89 } // namespace base
90