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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/file_associations_win_unittest.cc
diff --git a/chrome/browser/file_associations_win_unittest.cc b/chrome/browser/file_associations_win_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..10b534d3448b3ff6edf2334246c48e1183b80e65
--- /dev/null
+++ b/chrome/browser/file_associations_win_unittest.cc
@@ -0,0 +1,147 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/file_associations_win.h"
+
+#include "base/command_line.h"
+#include "base/files/file_path.h"
+#include "base/macros.h"
+#include "base/strings/utf_string_conversions.h"
+#include "base/test/test_reg_util_win.h"
+#include "base/win/registry.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+const char kTestProgid[] = "TestApp";
+const wchar_t kTestOpenCommand[] = L"C:\\test.exe";
+const char kTestFileTypeName[] = "Test File Type";
+const wchar_t kTestIconPath[] = L"D:\\test.ico";
+const char* kTestFileExtensions[] = {
+ "test1", "test2",
+};
+
+class FileAssociationsTest : public testing::Test {
+ public:
+ FileAssociationsTest() {}
+
+ protected:
+ virtual void SetUp() OVERRIDE {
+ registry_overrides_.OverrideRegistry(HKEY_CURRENT_USER, base::string16());
+
+ // .test2 files already have a default application.
+ base::win::RegKey key;
+ ASSERT_EQ(
+ ERROR_SUCCESS,
+ key.Create(
+ HKEY_CURRENT_USER, L"Software\\Classes\\.test2", KEY_ALL_ACCESS));
+ EXPECT_EQ(ERROR_SUCCESS, key.WriteValue(L"", L"SomeOtherApp"));
+ }
+
+ static base::CommandLine OpenCommand() {
+ base::FilePath open_command_path(kTestOpenCommand);
+ return base::CommandLine(open_command_path);
+ }
+
+ static std::set<std::string> FileExtensions() {
+ std::set<std::string> file_extensions;
+ for (size_t i = 0; i < arraysize(kTestFileExtensions); ++i)
+ file_extensions.insert(kTestFileExtensions[i]);
+ return file_extensions;
+ }
+
+ private:
+ registry_util::RegistryOverrideManager registry_overrides_;
+
+ DISALLOW_COPY_AND_ASSIGN(FileAssociationsTest);
+};
+
+TEST_F(FileAssociationsTest, AddWindowsFileAssociations) {
+ // Create file associations.
+ EXPECT_TRUE(AddWindowsFileAssociations(kTestProgid,
+ OpenCommand(),
+ kTestFileTypeName,
+ base::FilePath(kTestIconPath),
+ FileExtensions()));
+
+ // Ensure that the registry keys have been correctly set.
+ base::win::RegKey key;
+ std::wstring value;
+ ASSERT_EQ(
+ ERROR_SUCCESS,
+ 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 ;-)!
+ EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
+ EXPECT_EQ(L"Test File Type", value);
gab 2014/09/18 01:16:15 Same here/below, use constants.
+ ASSERT_EQ(ERROR_SUCCESS,
+ key.Open(HKEY_CURRENT_USER,
+ L"Software\\Classes\\TestApp\\DefaultIcon",
+ KEY_READ));
+ EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
+ EXPECT_EQ(L"D:\\test.ico,0", value);
+ ASSERT_EQ(ERROR_SUCCESS,
+ key.Open(HKEY_CURRENT_USER,
+ L"Software\\Classes\\TestApp\\shell\\open\\command",
+ KEY_READ));
+ EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
+ EXPECT_EQ(L"\"C:\\test.exe\"", value);
+
+ // .test1 should be default-associated with our test app.
+ ASSERT_EQ(
+ ERROR_SUCCESS,
+ key.Open(HKEY_CURRENT_USER, L"Software\\Classes\\.test1", KEY_READ));
+ EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
+ EXPECT_EQ(L"TestApp", value);
+ ASSERT_EQ(ERROR_SUCCESS,
+ key.Open(HKEY_CURRENT_USER,
+ L"Software\\Classes\\.test1\\OpenWithProgids",
+ KEY_READ));
+ EXPECT_TRUE(key.HasValue(L"TestApp"));
+ EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"TestApp", &value));
+ EXPECT_EQ(L"", value);
+
+ // .test2 should still be associated with the other app (should not have been
+ // overridden). But it should have our app in its Open With list.
+ ASSERT_EQ(
+ ERROR_SUCCESS,
+ key.Open(HKEY_CURRENT_USER, L"Software\\Classes\\.test2", KEY_READ));
+ EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
+ EXPECT_EQ(L"SomeOtherApp", value);
+ ASSERT_EQ(ERROR_SUCCESS,
+ key.Open(HKEY_CURRENT_USER,
+ L"Software\\Classes\\.test2\\OpenWithProgids",
+ KEY_READ));
+ EXPECT_TRUE(key.HasValue(L"TestApp"));
+ EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"TestApp", &value));
+ EXPECT_EQ(L"", value);
+}
+
+TEST_F(FileAssociationsTest, DeleteWindowsFileAssociations) {
+ // Create file associations.
+ EXPECT_TRUE(AddWindowsFileAssociations(kTestProgid,
+ OpenCommand(),
+ kTestFileTypeName,
+ base::FilePath(kTestIconPath),
+ FileExtensions()));
+
+ // 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
+ EXPECT_TRUE(DeleteWindowsFileAssociations(kTestProgid));
+
+ // The class key should have been completely deleted.
+ base::win::RegKey key;
+ std::wstring value;
+ ASSERT_NE(
+ ERROR_SUCCESS,
+ key.Open(HKEY_CURRENT_USER, L"Software\\Classes\\TestApp", KEY_READ));
+
+ // We don't need to delete the associations with the particular extensions
+ // (Windows will ignore those). However, we must ensure that .test2 is still
+ // associated with the other app.
+ ASSERT_EQ(
+ ERROR_SUCCESS,
+ key.Open(HKEY_CURRENT_USER, L"Software\\Classes\\.test2", KEY_READ));
+ EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
+ EXPECT_EQ(L"SomeOtherApp", value);
+}
+
+} // namespace

Powered by Google App Engine
This is Rietveld 408576698