• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Embedded Framework Authors. Portions copyright
2 // 2011 The Chromium Authors. All rights reserved. Use of this source code is
3 // governed by a BSD-style license that can be found in the LICENSE file.
4 
5 #include <string>
6 
7 #include "include/cef_file_util.h"
8 #include "include/wrapper/cef_scoped_temp_dir.h"
9 #include "tests/gtest/include/gtest/gtest.h"
10 
TEST(ScopedTempDir,FullPath)11 TEST(ScopedTempDir, FullPath) {
12   CefString test_path;
13   CefCreateNewTempDirectory("scoped_temp_dir", test_path);
14 
15   // Against an existing dir, it should get destroyed when leaving scope.
16   EXPECT_TRUE(CefDirectoryExists(test_path));
17   {
18     CefScopedTempDir dir;
19     EXPECT_TRUE(dir.Set(test_path));
20     EXPECT_TRUE(dir.IsValid());
21   }
22   EXPECT_FALSE(CefDirectoryExists(test_path));
23 
24   {
25     CefScopedTempDir dir;
26     EXPECT_TRUE(dir.Set(test_path));
27     // Now the dir doesn't exist, so ensure that it gets created.
28     EXPECT_TRUE(CefDirectoryExists(test_path));
29     // When we call Take(), it shouldn't get destroyed when leaving scope.
30     CefString path = dir.Take();
31     EXPECT_STREQ(path.ToString().c_str(), test_path.ToString().c_str());
32     EXPECT_FALSE(dir.IsValid());
33   }
34   EXPECT_TRUE(CefDirectoryExists(test_path));
35 
36   // Clean up.
37   {
38     CefScopedTempDir dir;
39     EXPECT_TRUE(dir.Set(test_path));
40   }
41   EXPECT_FALSE(CefDirectoryExists(test_path));
42 }
43 
TEST(ScopedTempDir,TempDir)44 TEST(ScopedTempDir, TempDir) {
45   // In this case, just verify that a directory was created and that it's a
46   // child of TempDir.
47   CefString test_path;
48   {
49     CefScopedTempDir dir;
50     EXPECT_TRUE(dir.CreateUniqueTempDir());
51     test_path = dir.GetPath();
52     EXPECT_TRUE(CefDirectoryExists(test_path));
53     CefString tmp_dir;
54     EXPECT_TRUE(CefGetTempDirectory(tmp_dir));
55     EXPECT_TRUE(test_path.ToString().find(tmp_dir.ToString()) !=
56                 std::string::npos);
57   }
58   EXPECT_FALSE(CefDirectoryExists(test_path));
59 }
60 
TEST(ScopedTempDir,UniqueTempDirUnderPath)61 TEST(ScopedTempDir, UniqueTempDirUnderPath) {
62   // Create a path which will contain a unique temp path.
63   CefString base_path;
64   ASSERT_TRUE(CefCreateNewTempDirectory("base_dir", base_path));
65 
66   CefString test_path;
67   {
68     CefScopedTempDir dir;
69     EXPECT_TRUE(dir.CreateUniqueTempDirUnderPath(base_path));
70     test_path = dir.GetPath();
71     EXPECT_TRUE(CefDirectoryExists(test_path));
72     EXPECT_TRUE(test_path.ToString().find(base_path.ToString()) == 0);
73   }
74   EXPECT_FALSE(CefDirectoryExists(test_path));
75   CefDeleteFile(base_path, true);
76 }
77 
TEST(ScopedTempDir,MultipleInvocations)78 TEST(ScopedTempDir, MultipleInvocations) {
79   CefScopedTempDir dir;
80   EXPECT_TRUE(dir.CreateUniqueTempDir());
81   EXPECT_FALSE(dir.CreateUniqueTempDir());
82   EXPECT_TRUE(dir.Delete());
83   EXPECT_TRUE(dir.CreateUniqueTempDir());
84   EXPECT_FALSE(dir.CreateUniqueTempDir());
85   CefScopedTempDir other_dir;
86   EXPECT_TRUE(other_dir.Set(dir.Take()));
87   EXPECT_TRUE(dir.CreateUniqueTempDir());
88   EXPECT_FALSE(dir.CreateUniqueTempDir());
89   EXPECT_FALSE(other_dir.CreateUniqueTempDir());
90 }
91