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

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: Added a TODO. Created 6 years, 3 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,
grt (UTC plus 2) 2014/09/15 13:38:37 personally, i'd go with Chromium here
Matt Giuca 2014/09/16 07:29:13 Just removed the prefix (no longer necessary since
grt (UTC plus 2) 2014/09/16 13:34:56 Acknowledged.
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 // TODO(mgiuca): Use RegistryOverrideManager instead of writing directly to the
28 // real registry. DO NOT SUBMIT.
29 class FileAssociationsTest : public testing::Test {
30 public:
31 FileAssociationsTest() {}
32
33 virtual void SetUp() OVERRIDE {
34 // .chrometest2 files already have a default application.
35 base::win::RegKey key;
36 ASSERT_EQ(ERROR_SUCCESS,
37 key.Create(HKEY_CURRENT_USER,
38 L"Software\\Classes\\.chrometest2",
39 KEY_ALL_ACCESS));
40 EXPECT_EQ(ERROR_SUCCESS, key.WriteValue(L"", L"SomeOtherApp"));
41 }
42
43 virtual void TearDown() OVERRIDE {
44 // Clean up all keys that might have been created by this test.
45 base::win::RegKey classes_key;
46 ASSERT_EQ(ERROR_SUCCESS,
47 classes_key.Open(
48 HKEY_CURRENT_USER, L"Software\\Classes", KEY_ALL_ACCESS));
49
50 // Delete the class key in the registry.
51 classes_key.DeleteKey(base::ASCIIToUTF16(kTestProgid).c_str());
52
53 // Delete the extension keys for our test extensions.
54 for (size_t i = 0; i < arraysize(kTestFileExtensions); ++i) {
55 base::string16 ext(L"." + base::ASCIIToUTF16(kTestFileExtensions[i]));
56 classes_key.DeleteKey(ext.c_str());
57 }
58 }
59
60 protected:
61 static base::CommandLine OpenCommand() {
62 base::FilePath open_command_path(kTestOpenCommand);
63 return base::CommandLine(open_command_path);
64 }
65
66 static std::set<std::string> FileExtensions() {
67 std::set<std::string> file_extensions;
68 for (size_t i = 0; i < arraysize(kTestFileExtensions); ++i)
69 file_extensions.insert(kTestFileExtensions[i]);
70 return file_extensions;
71 }
72
73 private:
74 DISALLOW_COPY_AND_ASSIGN(FileAssociationsTest);
75 };
76
77 TEST_F(FileAssociationsTest, AddWindowsFileAssociations) {
78 // Create file associations.
79 EXPECT_TRUE(AddWindowsFileAssociations(kTestProgid,
80 OpenCommand(),
81 kTestFileTypeName,
82 base::FilePath(kTestIconPath),
83 FileExtensions()));
84
85 // Ensure that the registry keys have been correctly set. We'll read from
86 // HKEY_CLASSES_ROOT (not HKEY_CURRENT_USER) to ensure that when we write to
87 // HKEY_CURRENT_USER\Software\Classes, it is correctly being mirrored to
88 // HKEY_CLASSES_ROOT.
89 base::win::RegKey key;
90 std::wstring value;
91 ASSERT_EQ(ERROR_SUCCESS,
92 key.Open(HKEY_CLASSES_ROOT, L"Chrome.TestApp", KEY_READ));
93 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
94 EXPECT_EQ(L"Chrome Test File Type", value);
95 ASSERT_EQ(
96 ERROR_SUCCESS,
97 key.Open(HKEY_CLASSES_ROOT, L"Chrome.TestApp\\DefaultIcon", KEY_READ));
98 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
99 EXPECT_EQ(L"D:\\test.ico,0", value);
100 ASSERT_EQ(ERROR_SUCCESS,
101 key.Open(HKEY_CLASSES_ROOT,
102 L"Chrome.TestApp\\shell\\open\\command",
103 KEY_READ));
104 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
105 EXPECT_EQ(L"\"C:\\test.exe\"", value);
106
107 // .chrometest1 should be default-associated with our test app.
108 ASSERT_EQ(ERROR_SUCCESS,
109 key.Open(HKEY_CLASSES_ROOT, L".chrometest1", KEY_READ));
110 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
111 EXPECT_EQ(L"Chrome.TestApp", value);
112 ASSERT_EQ(
113 ERROR_SUCCESS,
114 key.Open(HKEY_CLASSES_ROOT, L".chrometest1\\OpenWithProgids", KEY_READ));
115 EXPECT_TRUE(key.HasValue(L"Chrome.TestApp"));
116 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"Chrome.TestApp", &value));
117 EXPECT_EQ(L"", value);
118
119 // .chrometest2 should still be associated with the other app (should not have
120 // been overridden). But it should have our app in its Open With list.
121 ASSERT_EQ(ERROR_SUCCESS,
122 key.Open(HKEY_CLASSES_ROOT, L".chrometest2", KEY_READ));
123 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
124 EXPECT_EQ(L"SomeOtherApp", value);
125 ASSERT_EQ(
126 ERROR_SUCCESS,
127 key.Open(HKEY_CLASSES_ROOT, L".chrometest2\\OpenWithProgids", KEY_READ));
128 EXPECT_TRUE(key.HasValue(L"Chrome.TestApp"));
129 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"Chrome.TestApp", &value));
130 EXPECT_EQ(L"", value);
131 }
132
133 TEST_F(FileAssociationsTest, DeleteWindowsFileAssociations) {
134 // Create file associations.
135 EXPECT_TRUE(AddWindowsFileAssociations(kTestProgid,
136 OpenCommand(),
137 kTestFileTypeName,
138 base::FilePath(kTestIconPath),
139 FileExtensions()));
140
141 // Delete them again.
142 EXPECT_TRUE(DeleteWindowsFileAssociations(kTestProgid));
143
144 // The class key should have been completely deleted.
145 base::win::RegKey key;
146 std::wstring value;
147 ASSERT_NE(ERROR_SUCCESS,
148 key.Open(HKEY_CLASSES_ROOT, L"Chrome.TestApp", KEY_READ));
149
150 // We don't need to delete the associations with the particular extensions
151 // (Windows will ignore those). However, we must ensure that .chrometest2 is
152 // still associated with the other app.
153 ASSERT_EQ(ERROR_SUCCESS,
154 key.Open(HKEY_CLASSES_ROOT, L".chrometest2", KEY_READ));
155 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
156 EXPECT_EQ(L"SomeOtherApp", value);
157 }
158
159 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698