Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(555)

Side by Side Diff: chrome/browser/file_associations_win_unittest.cc

Issue 487693002: ShellUtil: Add generic methods to add/delete Windows file associations. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix include order (see crbug.com/404978) and lint errors. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 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 "chrome/browser/file_associations_win.h"
6
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/macros.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/win/registry.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace {
15
16 // These will be written directly into the registry, so clearly mark them as
17 // 'Chrome' and avoid clobbering common extensions. Note: By running this test,
18 // these classes and extensions will be deleted from the real registry!
19 const char kTestProgid[] = "Chrome.TestApp";
20 const wchar_t kTestOpenCommand[] = L"C:\\test.exe";
21 const char kTestFileTypeName[] = "Chrome Test File Type";
22 const wchar_t kTestIconPath[] = L"D:\\test.ico";
23 const char* kTestFileExtensions[] = {
24 "chrometest1", "chrometest2",
25 };
26
27 class FileAssociationsTest : public testing::Test {
28 public:
29 FileAssociationsTest() {}
30
31 virtual void SetUp() OVERRIDE {
robertshield 2014/08/25 02:45:22 Tests that write to the registry should use https
32 // .chrometest2 files already have a default application.
33 base::win::RegKey key;
34 ASSERT_EQ(ERROR_SUCCESS,
35 key.Create(HKEY_CURRENT_USER,
36 L"Software\\Classes\\.chrometest2",
37 KEY_ALL_ACCESS));
38 EXPECT_EQ(ERROR_SUCCESS, key.WriteValue(L"", L"SomeOtherApp"));
39 }
40
41 virtual void TearDown() OVERRIDE {
42 // Clean up all keys that might have been created by this test.
43 base::win::RegKey classes_key;
44 ASSERT_EQ(ERROR_SUCCESS,
45 classes_key.Open(
46 HKEY_CURRENT_USER, L"Software\\Classes", KEY_ALL_ACCESS));
47
48 // Delete the class key in the registry.
49 classes_key.DeleteKey(base::ASCIIToUTF16(kTestProgid).c_str());
50
51 // Delete the extension keys for our test extensions.
52 for (size_t i = 0; i < arraysize(kTestFileExtensions); ++i) {
53 base::string16 ext(L"." + base::ASCIIToUTF16(kTestFileExtensions[i]));
54 classes_key.DeleteKey(ext.c_str());
55 }
56 }
57
58 protected:
59 static base::CommandLine OpenCommand() {
60 base::FilePath open_command_path(kTestOpenCommand);
61 return base::CommandLine(open_command_path);
62 }
63
64 static std::set<std::string> FileExtensions() {
65 std::set<std::string> file_extensions;
66 for (size_t i = 0; i < arraysize(kTestFileExtensions); ++i)
67 file_extensions.insert(kTestFileExtensions[i]);
68 return file_extensions;
69 }
70
71 private:
72 DISALLOW_COPY_AND_ASSIGN(FileAssociationsTest);
73 };
74
75 TEST_F(FileAssociationsTest, AddWindowsFileAssociations) {
76 // Create file associations.
77 EXPECT_TRUE(AddWindowsFileAssociations(kTestProgid,
78 OpenCommand(),
79 kTestFileTypeName,
80 base::FilePath(kTestIconPath),
81 FileExtensions()));
82
83 // Ensure that the registry keys have been correctly set. We'll read from
84 // HKEY_CLASSES_ROOT (not HKEY_CURRENT_USER) to ensure that when we write to
85 // HKEY_CURRENT_USER\Software\Classes, it is correctly being mirrored to
86 // HKEY_CLASSES_ROOT.
87 base::win::RegKey key;
88 std::wstring value;
89 ASSERT_EQ(ERROR_SUCCESS,
90 key.Open(HKEY_CLASSES_ROOT, L"Chrome.TestApp", KEY_READ));
91 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
92 EXPECT_EQ(L"Chrome Test File Type", value);
93 ASSERT_EQ(
94 ERROR_SUCCESS,
95 key.Open(HKEY_CLASSES_ROOT, L"Chrome.TestApp\\DefaultIcon", KEY_READ));
96 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
97 EXPECT_EQ(L"D:\\test.ico,0", value);
98 ASSERT_EQ(ERROR_SUCCESS,
99 key.Open(HKEY_CLASSES_ROOT,
100 L"Chrome.TestApp\\shell\\open\\command",
101 KEY_READ));
102 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
103 EXPECT_EQ(L"\"C:\\test.exe\"", value);
104
105 // .chrometest1 should be default-associated with our test app.
106 ASSERT_EQ(ERROR_SUCCESS,
107 key.Open(HKEY_CLASSES_ROOT, L".chrometest1", KEY_READ));
108 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
109 EXPECT_EQ(L"Chrome.TestApp", value);
110 ASSERT_EQ(
111 ERROR_SUCCESS,
112 key.Open(HKEY_CLASSES_ROOT, L".chrometest1\\OpenWithProgids", KEY_READ));
113 EXPECT_TRUE(key.HasValue(L"Chrome.TestApp"));
114 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"Chrome.TestApp", &value));
115 EXPECT_EQ(L"", value);
116
117 // .chrometest2 should still be associated with the other app (should not have
118 // been overridden). But it should have our app in its Open With list.
119 ASSERT_EQ(ERROR_SUCCESS,
120 key.Open(HKEY_CLASSES_ROOT, L".chrometest2", KEY_READ));
121 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
122 EXPECT_EQ(L"SomeOtherApp", value);
123 ASSERT_EQ(
124 ERROR_SUCCESS,
125 key.Open(HKEY_CLASSES_ROOT, L".chrometest2\\OpenWithProgids", KEY_READ));
126 EXPECT_TRUE(key.HasValue(L"Chrome.TestApp"));
127 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"Chrome.TestApp", &value));
128 EXPECT_EQ(L"", value);
129 }
130
131 TEST_F(FileAssociationsTest, DeleteWindowsFileAssociations) {
132 // Create file associations.
133 EXPECT_TRUE(AddWindowsFileAssociations(kTestProgid,
134 OpenCommand(),
135 kTestFileTypeName,
136 base::FilePath(kTestIconPath),
137 FileExtensions()));
138
139 // Delete them again.
140 EXPECT_TRUE(DeleteWindowsFileAssociations(kTestProgid));
141
142 // The class key should have been completely deleted.
143 base::win::RegKey key;
144 std::wstring value;
145 ASSERT_NE(ERROR_SUCCESS,
146 key.Open(HKEY_CLASSES_ROOT, L"Chrome.TestApp", KEY_READ));
147
148 // We don't need to delete the associations with the particular extensions
149 // (Windows will ignore those). However, we must ensure that .chrometest2 is
150 // still associated with the other app.
151 ASSERT_EQ(ERROR_SUCCESS,
152 key.Open(HKEY_CLASSES_ROOT, L".chrometest2", KEY_READ));
153 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
154 EXPECT_EQ(L"SomeOtherApp", value);
155 }
156
157 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698