1// Copyright (c) 2012 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 "content/browser/storage_partition_impl_map.h"
6
7#include "base/files/file_util.h"
8#include "base/run_loop.h"
9#include "content/public/test/test_browser_context.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12namespace content {
13
14// Test that the Less comparison function is implemented properly to uniquely
15// identify storage partitions used as keys in a std::map.
16TEST(StoragePartitionConfigTest, OperatorLess) {
17  StoragePartitionImplMap::StoragePartitionConfig c1(
18      std::string(), std::string(), false);
19  StoragePartitionImplMap::StoragePartitionConfig c2(
20      std::string(), std::string(), false);
21  StoragePartitionImplMap::StoragePartitionConfig c3(
22      std::string(), std::string(), true);
23  StoragePartitionImplMap::StoragePartitionConfig c4("a", std::string(), true);
24  StoragePartitionImplMap::StoragePartitionConfig c5("b", std::string(), true);
25  StoragePartitionImplMap::StoragePartitionConfig c6(
26      std::string(), "abc", false);
27  StoragePartitionImplMap::StoragePartitionConfig c7(
28      std::string(), "abc", true);
29  StoragePartitionImplMap::StoragePartitionConfig c8("a", "abc", false);
30  StoragePartitionImplMap::StoragePartitionConfig c9("a", "abc", true);
31
32  StoragePartitionImplMap::StoragePartitionConfigLess less;
33
34  // Let's ensure basic comparison works.
35  EXPECT_TRUE(less(c1, c3));
36  EXPECT_TRUE(less(c1, c4));
37  EXPECT_TRUE(less(c3, c4));
38  EXPECT_TRUE(less(c4, c5));
39  EXPECT_TRUE(less(c4, c8));
40  EXPECT_TRUE(less(c6, c4));
41  EXPECT_TRUE(less(c6, c7));
42  EXPECT_TRUE(less(c8, c9));
43
44  // Now, ensure antisymmetry for each pair we've tested.
45  EXPECT_FALSE(less(c3, c1));
46  EXPECT_FALSE(less(c4, c1));
47  EXPECT_FALSE(less(c4, c3));
48  EXPECT_FALSE(less(c5, c4));
49  EXPECT_FALSE(less(c8, c4));
50  EXPECT_FALSE(less(c4, c6));
51  EXPECT_FALSE(less(c7, c6));
52  EXPECT_FALSE(less(c9, c8));
53
54  // Check for irreflexivity.
55  EXPECT_FALSE(less(c1, c1));
56
57  // Check for transitivity.
58  EXPECT_TRUE(less(c1, c4));
59
60  // Let's enforce that two identical elements obey strict weak ordering.
61  EXPECT_TRUE(!less(c1, c2) && !less(c2, c1));
62}
63
64TEST(StoragePartitionImplMapTest, GarbageCollect) {
65  base::MessageLoop message_loop;
66  TestBrowserContext browser_context;
67  StoragePartitionImplMap storage_partition_impl_map(&browser_context);
68
69  scoped_ptr<base::hash_set<base::FilePath> > active_paths(
70      new base::hash_set<base::FilePath>);
71
72  base::FilePath active_path = browser_context.GetPath().Append(
73      StoragePartitionImplMap::GetStoragePartitionPath(
74          "active", std::string()));
75  ASSERT_TRUE(base::CreateDirectory(active_path));
76  active_paths->insert(active_path);
77
78  base::FilePath inactive_path = browser_context.GetPath().Append(
79      StoragePartitionImplMap::GetStoragePartitionPath(
80          "inactive", std::string()));
81  ASSERT_TRUE(base::CreateDirectory(inactive_path));
82
83  base::RunLoop run_loop;
84  storage_partition_impl_map.GarbageCollect(
85      active_paths.Pass(), run_loop.QuitClosure());
86  run_loop.Run();
87
88  EXPECT_TRUE(base::PathExists(active_path));
89  EXPECT_FALSE(base::PathExists(inactive_path));
90}
91
92}  // namespace content
93