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

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: Rebase. 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..57485f4c833ce2350f1a69103656811248054cab
--- /dev/null
+++ b/chrome/browser/file_associations_win_unittest.cc
@@ -0,0 +1,157 @@
+// 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/win/registry.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+// These will be written directly into the registry, so clearly mark them as
+// 'Chrome' and avoid clobbering common extensions. Note: By running this test,
+// these classes and extensions will be deleted from the real registry!
gab 2014/09/12 13:32:36 Oh no no no! This will make for flaky tests and bo
Matt Giuca 2014/09/15 03:49:45 Interesting. I looked for something like this but
grt (UTC plus 2) 2014/09/15 13:38:37 These tests will be flaky if you don't do it now s
Matt Giuca 2014/09/16 07:29:12 By "not right now", I meant not in this patch set
+const char kTestProgid[] = "Chrome.TestApp";
+const wchar_t kTestOpenCommand[] = L"C:\\test.exe";
+const char kTestFileTypeName[] = "Chrome Test File Type";
+const wchar_t kTestIconPath[] = L"D:\\test.ico";
+const char* kTestFileExtensions[] = {
+ "chrometest1", "chrometest2",
+};
+
+class FileAssociationsTest : public testing::Test {
+ public:
+ FileAssociationsTest() {}
+
+ virtual void SetUp() OVERRIDE {
+ // .chrometest2 files already have a default application.
+ base::win::RegKey key;
+ ASSERT_EQ(ERROR_SUCCESS,
+ key.Create(HKEY_CURRENT_USER,
+ L"Software\\Classes\\.chrometest2",
+ KEY_ALL_ACCESS));
+ EXPECT_EQ(ERROR_SUCCESS, key.WriteValue(L"", L"SomeOtherApp"));
+ }
+
+ virtual void TearDown() OVERRIDE {
+ // Clean up all keys that might have been created by this test.
+ base::win::RegKey classes_key;
+ ASSERT_EQ(ERROR_SUCCESS,
+ classes_key.Open(
+ HKEY_CURRENT_USER, L"Software\\Classes", KEY_ALL_ACCESS));
+
+ // Delete the class key in the registry.
+ classes_key.DeleteKey(base::ASCIIToUTF16(kTestProgid).c_str());
+
+ // Delete the extension keys for our test extensions.
+ for (size_t i = 0; i < arraysize(kTestFileExtensions); ++i) {
+ base::string16 ext(L"." + base::ASCIIToUTF16(kTestFileExtensions[i]));
+ classes_key.DeleteKey(ext.c_str());
+ }
+ }
+
+ protected:
+ 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:
+ 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. We'll read from
+ // HKEY_CLASSES_ROOT (not HKEY_CURRENT_USER) to ensure that when we write to
+ // HKEY_CURRENT_USER\Software\Classes, it is correctly being mirrored to
+ // HKEY_CLASSES_ROOT.
+ base::win::RegKey key;
+ std::wstring value;
+ ASSERT_EQ(ERROR_SUCCESS,
+ key.Open(HKEY_CLASSES_ROOT, L"Chrome.TestApp", KEY_READ));
+ EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
+ EXPECT_EQ(L"Chrome Test File Type", value);
+ ASSERT_EQ(
+ ERROR_SUCCESS,
+ key.Open(HKEY_CLASSES_ROOT, L"Chrome.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_CLASSES_ROOT,
+ L"Chrome.TestApp\\shell\\open\\command",
+ KEY_READ));
+ EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
+ EXPECT_EQ(L"\"C:\\test.exe\"", value);
+
+ // .chrometest1 should be default-associated with our test app.
+ ASSERT_EQ(ERROR_SUCCESS,
+ key.Open(HKEY_CLASSES_ROOT, L".chrometest1", KEY_READ));
+ EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
+ EXPECT_EQ(L"Chrome.TestApp", value);
+ ASSERT_EQ(
+ ERROR_SUCCESS,
+ key.Open(HKEY_CLASSES_ROOT, L".chrometest1\\OpenWithProgids", KEY_READ));
+ EXPECT_TRUE(key.HasValue(L"Chrome.TestApp"));
+ EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"Chrome.TestApp", &value));
+ EXPECT_EQ(L"", value);
+
+ // .chrometest2 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_CLASSES_ROOT, L".chrometest2", KEY_READ));
+ EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value));
+ EXPECT_EQ(L"SomeOtherApp", value);
+ ASSERT_EQ(
+ ERROR_SUCCESS,
+ key.Open(HKEY_CLASSES_ROOT, L".chrometest2\\OpenWithProgids", KEY_READ));
+ EXPECT_TRUE(key.HasValue(L"Chrome.TestApp"));
+ EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"Chrome.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.
+ 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_CLASSES_ROOT, L"Chrome.TestApp", KEY_READ));
+
+ // We don't need to delete the associations with the particular extensions
+ // (Windows will ignore those). However, we must ensure that .chrometest2 is
+ // still associated with the other app.
+ ASSERT_EQ(ERROR_SUCCESS,
+ key.Open(HKEY_CLASSES_ROOT, L".chrometest2", 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