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

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: Respond to grt's comments. 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/test/test_reg_util_win.h"
12 #include "base/win/registry.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace {
16
17 const char kTestProgid[] = "TestApp";
18 const wchar_t kTestOpenCommand[] = L"C:\\test.exe";
19 const char kTestFileTypeName[] = "Test File Type";
20 const wchar_t kTestIconPath[] = L"D:\\test.ico";
21 const char* kTestFileExtensions[] = {
22 "test1", "test2",
23 };
24
25 class FileAssociationsTest : public testing::Test {
26 public:
27 FileAssociationsTest() {}
28
29 protected:
30 virtual void SetUp() OVERRIDE {
31 registry_overrides_.OverrideRegistry(HKEY_CURRENT_USER, base::string16());
32
33 // .test2 files already have a default application.
34 base::win::RegKey key;
35 ASSERT_EQ(
36 ERROR_SUCCESS,
37 key.Create(
38 HKEY_CURRENT_USER, L"Software\\Classes\\.test2", KEY_ALL_ACCESS));
39 EXPECT_EQ(ERROR_SUCCESS, key.WriteValue(L"", L"SomeOtherApp"));
40 }
41
42 static base::CommandLine OpenCommand() {
43 base::FilePath open_command_path(kTestOpenCommand);
44 return base::CommandLine(open_command_path);
45 }
46
47 static std::set<std::string> FileExtensions() {
48 std::set<std::string> file_extensions;
49 for (size_t i = 0; i < arraysize(kTestFileExtensions); ++i)
50 file_extensions.insert(kTestFileExtensions[i]);
51 return file_extensions;
52 }
53
54 private:
55 registry_util::RegistryOverrideManager registry_overrides_;
56
57 DISALLOW_COPY_AND_ASSIGN(FileAssociationsTest);
58 };
59
60 TEST_F(FileAssociationsTest, AddWindowsFileAssociations) {
61 // Create file associations.
62 EXPECT_TRUE(AddWindowsFileAssociations(kTestProgid,
63 OpenCommand(),
64 kTestFileTypeName,
65 base::FilePath(kTestIconPath),
66 FileExtensions()));
67
68 // Ensure that the registry keys have been correctly set.
69 base::win::RegKey key;
70 std::wstring value;
71 ASSERT_EQ(
72 ERROR_SUCCESS,
73 key.Open(HKEY_CURRENT_USER, L"Software\\Classes\\TestApp", KEY_READ));
gab 2014/09/18 01:16:15 Build these test strings from the constants (mostl
Matt Giuca 2014/09/30 10:21:12 Now it's my turn to quote TOTT to you :) Episode
gab 2014/10/01 14:31:11 Fair enough ;-)!
74 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
75 EXPECT_EQ(L"Test File Type", value);
gab 2014/09/18 01:16:15 Same here/below, use constants.
76 ASSERT_EQ(ERROR_SUCCESS,
77 key.Open(HKEY_CURRENT_USER,
78 L"Software\\Classes\\TestApp\\DefaultIcon",
79 KEY_READ));
80 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
81 EXPECT_EQ(L"D:\\test.ico,0", value);
82 ASSERT_EQ(ERROR_SUCCESS,
83 key.Open(HKEY_CURRENT_USER,
84 L"Software\\Classes\\TestApp\\shell\\open\\command",
85 KEY_READ));
86 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
87 EXPECT_EQ(L"\"C:\\test.exe\"", value);
88
89 // .test1 should be default-associated with our test app.
90 ASSERT_EQ(
91 ERROR_SUCCESS,
92 key.Open(HKEY_CURRENT_USER, L"Software\\Classes\\.test1", KEY_READ));
93 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
94 EXPECT_EQ(L"TestApp", value);
95 ASSERT_EQ(ERROR_SUCCESS,
96 key.Open(HKEY_CURRENT_USER,
97 L"Software\\Classes\\.test1\\OpenWithProgids",
98 KEY_READ));
99 EXPECT_TRUE(key.HasValue(L"TestApp"));
100 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"TestApp", &value));
101 EXPECT_EQ(L"", value);
102
103 // .test2 should still be associated with the other app (should not have been
104 // overridden). But it should have our app in its Open With list.
105 ASSERT_EQ(
106 ERROR_SUCCESS,
107 key.Open(HKEY_CURRENT_USER, L"Software\\Classes\\.test2", KEY_READ));
108 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
109 EXPECT_EQ(L"SomeOtherApp", value);
110 ASSERT_EQ(ERROR_SUCCESS,
111 key.Open(HKEY_CURRENT_USER,
112 L"Software\\Classes\\.test2\\OpenWithProgids",
113 KEY_READ));
114 EXPECT_TRUE(key.HasValue(L"TestApp"));
115 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"TestApp", &value));
116 EXPECT_EQ(L"", value);
117 }
118
119 TEST_F(FileAssociationsTest, DeleteWindowsFileAssociations) {
120 // Create file associations.
121 EXPECT_TRUE(AddWindowsFileAssociations(kTestProgid,
122 OpenCommand(),
123 kTestFileTypeName,
124 base::FilePath(kTestIconPath),
125 FileExtensions()));
126
127 // Delete them again.
gab 2014/09/18 01:16:15 What do you mean by "again"; this is the first tim
Matt Giuca 2014/09/30 10:21:12 I guess I meant "again" in the "All the king's hor
128 EXPECT_TRUE(DeleteWindowsFileAssociations(kTestProgid));
129
130 // The class key should have been completely deleted.
131 base::win::RegKey key;
132 std::wstring value;
133 ASSERT_NE(
134 ERROR_SUCCESS,
135 key.Open(HKEY_CURRENT_USER, L"Software\\Classes\\TestApp", KEY_READ));
136
137 // We don't need to delete the associations with the particular extensions
138 // (Windows will ignore those). However, we must ensure that .test2 is still
139 // associated with the other app.
140 ASSERT_EQ(
141 ERROR_SUCCESS,
142 key.Open(HKEY_CURRENT_USER, L"Software\\Classes\\.test2", KEY_READ));
143 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
144 EXPECT_EQ(L"SomeOtherApp", value);
145 }
146
147 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698