1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89

base / files / scoped_temp_file_unittest.cc [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/files/scoped_temp_file.h"

#include <utility>

#include "base/files/file_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {

TEST(ScopedTempFile, Basic) {
  ScopedTempFile temp_file;
  EXPECT_TRUE(temp_file.path().empty());

  EXPECT_TRUE(temp_file.Create());
  EXPECT_TRUE(PathExists(temp_file.path()));

  EXPECT_TRUE(temp_file.Delete());
  EXPECT_FALSE(PathExists(temp_file.path()));
}

TEST(ScopedTempFile, MoveConstruct) {
  ScopedTempFile temp1;
  EXPECT_TRUE(temp1.Create());
  FilePath file1 = temp1.path();
  EXPECT_TRUE(PathExists(file1));

  ScopedTempFile temp2(std::move(temp1));
  EXPECT_TRUE(temp1.path().empty());
  EXPECT_EQ(file1, temp2.path());
  EXPECT_TRUE(PathExists(file1));
}

TEST(ScopedTempFile, MoveAssign) {
  ScopedTempFile temp1;
  EXPECT_TRUE(temp1.Create());
  FilePath file1 = temp1.path();
  EXPECT_TRUE(PathExists(file1));

  ScopedTempFile temp2;
  EXPECT_TRUE(temp2.Create());
  FilePath file2 = temp2.path();
  EXPECT_TRUE(PathExists(file2));

  temp2 = std::move(temp1);
  EXPECT_TRUE(temp1.path().empty());
  EXPECT_EQ(temp2.path(), file1);
  EXPECT_TRUE(PathExists(file1));
  EXPECT_FALSE(PathExists(file2));
}

TEST(ScopedTempFile, Destruct) {
  FilePath file;
  {
    ScopedTempFile temp;
    EXPECT_TRUE(temp.Create());
    file = temp.path();
    EXPECT_TRUE(PathExists(file));
  }

  EXPECT_FALSE(PathExists(file));
}

TEST(ScopedTempFile, Reset) {
  ScopedTempFile temp_file;
  EXPECT_TRUE(temp_file.path().empty());

  EXPECT_TRUE(temp_file.Create());
  EXPECT_TRUE(PathExists(temp_file.path()));

  temp_file.Reset();
  EXPECT_FALSE(PathExists(temp_file.path()));
}

TEST(ScopedTempFile, OperatorBool) {
  ScopedTempFile temp_file;
  EXPECT_FALSE(temp_file);

  EXPECT_TRUE(temp_file.Create());
  EXPECT_TRUE(temp_file);

  EXPECT_TRUE(temp_file.Delete());
  EXPECT_FALSE(temp_file);
}

}  // namespace base