- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / extension_creator_filter_unittest.cc
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 "base/file_util.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/extensions/extension_creator_filter.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/platform_test.h"
11
12 namespace {
13
14 class ExtensionCreatorFilterTest : public PlatformTest {
15  protected:
16   virtual void SetUp() {
17     PlatformTest::SetUp();
18
19     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
20     test_dir_ = temp_dir_.path();
21
22     filter_ = new extensions::ExtensionCreatorFilter();
23   }
24
25   base::FilePath CreateEmptyTestFile(const base::FilePath& file_path) {
26     base::FilePath test_file(test_dir_.Append(file_path));
27     base::FilePath temp_file;
28     EXPECT_TRUE(file_util::CreateTemporaryFileInDir(test_dir_, &temp_file));
29     EXPECT_TRUE(base::Move(temp_file, test_file));
30     return test_file;
31   }
32
33   scoped_refptr<extensions::ExtensionCreatorFilter> filter_;
34
35   base::ScopedTempDir temp_dir_;
36
37   base::FilePath test_dir_;
38 };
39
40 struct UnaryBooleanTestData {
41   const base::FilePath::CharType* input;
42   bool expected;
43 };
44
45 TEST_F(ExtensionCreatorFilterTest, NormalCases) {
46   const struct UnaryBooleanTestData cases[] = {
47     { FILE_PATH_LITERAL("foo"), true },
48     { FILE_PATH_LITERAL(".foo"), false },
49     { FILE_PATH_LITERAL("~foo"), true },
50     { FILE_PATH_LITERAL("foo~"), false },
51     { FILE_PATH_LITERAL("#foo"), true },
52     { FILE_PATH_LITERAL("foo#"), true },
53     { FILE_PATH_LITERAL("#foo#"), false },
54     { FILE_PATH_LITERAL(".svn"), false },
55     { FILE_PATH_LITERAL("__MACOSX"), false },
56   };
57
58   for (size_t i = 0; i < arraysize(cases); ++i) {
59     base::FilePath input(cases[i].input);
60     base::FilePath test_file(CreateEmptyTestFile(input));
61     bool observed = filter_->ShouldPackageFile(test_file);
62
63     EXPECT_EQ(cases[i].expected, observed) <<
64       "i: " << i << ", input: " << test_file.value();
65   }
66 }
67
68 #if defined(OS_WIN)
69 struct StringBooleanWithBooleanTestData {
70   const base::FilePath::CharType* input_char;
71   bool input_bool;
72   bool expected;
73 };
74
75 TEST_F(ExtensionCreatorFilterTest, WindowsHiddenFiles) {
76   const struct StringBooleanWithBooleanTestData cases[] = {
77     { FILE_PATH_LITERAL("a-normal-file"), false, true },
78     { FILE_PATH_LITERAL(".a-dot-file"), false, false },
79     { FILE_PATH_LITERAL(".a-dot-file-that-we-have-set-to-hidden"),
80       true, false },
81     { FILE_PATH_LITERAL("a-file-that-we-have-set-to-hidden"), true, false },
82     { FILE_PATH_LITERAL("a-file-that-we-have-not-set-to-hidden"),
83       false, true },
84   };
85
86   for (size_t i = 0; i < arraysize(cases); ++i) {
87     base::FilePath input(cases[i].input_char);
88     bool should_hide = cases[i].input_bool;
89     base::FilePath test_file(CreateEmptyTestFile(input));
90
91     if (should_hide) {
92       SetFileAttributes(test_file.value().c_str(), FILE_ATTRIBUTE_HIDDEN);
93     }
94     bool observed = filter_->ShouldPackageFile(test_file);
95     EXPECT_EQ(cases[i].expected, observed) <<
96       "i: " << i << ", input: " << test_file.value();
97   }
98 }
99 #endif
100
101 }  // namespace