Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / chrome / browser / download / download_prefs_unittest.cc
blob88674145ee0dd68fd22dffa0fac2f936fe5ae536
1 // Copyright 2015 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.
5 #include "chrome/browser/download/download_prefs.h"
7 #include "base/files/file_path.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/download/download_extensions.h"
10 #include "chrome/common/pref_names.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 TEST(DownloadPrefsTest, Prerequisites) {
16 // Most of the tests below are based on the assumption that .swf files are not
17 // allowed to open automatically, and that .txt files are allowed. If this
18 // assumption changes, then we need to update the tests to match.
19 ASSERT_FALSE(download_util::IsAllowedToOpenAutomatically(
20 base::FilePath(FILE_PATH_LITERAL("a.swf"))));
21 ASSERT_TRUE(download_util::IsAllowedToOpenAutomatically(
22 base::FilePath(FILE_PATH_LITERAL("a.txt"))));
25 TEST(DownloadPrefsTest, NoAutoOpenForDisallowedFileTypes) {
26 const base::FilePath kDangerousFilePath(FILE_PATH_LITERAL("/b/very-bad.swf"));
28 content::TestBrowserThreadBundle threads_are_required_for_testing_profile;
29 TestingProfile profile;
30 DownloadPrefs prefs(&profile);
32 EXPECT_FALSE(prefs.EnableAutoOpenBasedOnExtension(kDangerousFilePath));
33 EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension(kDangerousFilePath));
36 TEST(DownloadPrefsTest, NoAutoOpenForFilesWithNoExtension) {
37 const base::FilePath kFileWithNoExtension(FILE_PATH_LITERAL("abcd"));
39 content::TestBrowserThreadBundle threads_are_required_for_testing_profile;
40 TestingProfile profile;
41 DownloadPrefs prefs(&profile);
43 EXPECT_FALSE(prefs.EnableAutoOpenBasedOnExtension(kFileWithNoExtension));
44 EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension(kFileWithNoExtension));
47 TEST(DownloadPrefsTest, AutoOpenForSafeFiles) {
48 const base::FilePath kSafeFilePath(
49 FILE_PATH_LITERAL("/good/nothing-wrong.txt"));
50 const base::FilePath kAnotherSafeFilePath(
51 FILE_PATH_LITERAL("/ok/not-bad.txt"));
53 content::TestBrowserThreadBundle threads_are_required_for_testing_profile;
54 TestingProfile profile;
55 DownloadPrefs prefs(&profile);
57 EXPECT_TRUE(prefs.EnableAutoOpenBasedOnExtension(kSafeFilePath));
58 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(kSafeFilePath));
59 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(kAnotherSafeFilePath));
62 TEST(DownloadPrefsTest, AutoOpenPrefSkipsDangerousFileTypesInPrefs) {
63 const base::FilePath kDangerousFilePath(FILE_PATH_LITERAL("/b/very-bad.swf"));
64 const base::FilePath kSafeFilePath(
65 FILE_PATH_LITERAL("/good/nothing-wrong.txt"));
67 content::TestBrowserThreadBundle threads_are_required_for_testing_profile;
68 TestingProfile profile;
69 // This sets .swf files and .txt files as auto-open file types.
70 profile.GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, "swf:txt");
71 DownloadPrefs prefs(&profile);
73 EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension(kDangerousFilePath));
74 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(kSafeFilePath));
77 TEST(DownloadPrefsTest, PrefsInitializationSkipsInvalidFileTypes) {
78 content::TestBrowserThreadBundle threads_are_required_for_testing_profile;
79 TestingProfile profile;
80 profile.GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen,
81 "swf:txt::.foo:baz");
82 DownloadPrefs prefs(&profile);
83 prefs.DisableAutoOpenBasedOnExtension(
84 base::FilePath(FILE_PATH_LITERAL("x.baz")));
86 EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension(
87 base::FilePath(FILE_PATH_LITERAL("x.swf"))));
88 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(
89 base::FilePath(FILE_PATH_LITERAL("x.txt"))));
90 EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension(
91 base::FilePath(FILE_PATH_LITERAL("x.foo"))));
93 // .swf is skipped because it's not an allowed auto-open file type.
94 // The empty entry and .foo are skipped because they are malformed.
95 // "baz" is removed by the DisableAutoOpenBasedOnExtension() call.
96 // The only entry that should be remaining is 'txt'.
97 EXPECT_STREQ(
98 "txt",
99 profile.GetPrefs()->GetString(prefs::kDownloadExtensionsToOpen).c_str());
102 TEST(DownloadPrefsTest, AutoOpenCheckIsCaseInsensitive) {
103 content::TestBrowserThreadBundle threads_are_required_for_testing_profile;
104 TestingProfile profile;
105 profile.GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen,
106 "txt:Foo:BAR");
107 DownloadPrefs prefs(&profile);
109 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(
110 base::FilePath(FILE_PATH_LITERAL("x.txt"))));
111 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(
112 base::FilePath(FILE_PATH_LITERAL("x.TXT"))));
113 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(
114 base::FilePath(FILE_PATH_LITERAL("x.foo"))));
115 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(
116 base::FilePath(FILE_PATH_LITERAL("x.Bar"))));