OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/installer/util/shell_util.h" | 5 #include "chrome/installer/util/shell_util.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
10 #include "base/base_paths_win.h" | 10 #include "base/base_paths_win.h" |
11 #include "base/command_line.h" | |
11 #include "base/files/file_enumerator.h" | 12 #include "base/files/file_enumerator.h" |
13 #include "base/files/file_path.h" | |
12 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
13 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
14 #include "base/md5.h" | 16 #include "base/md5.h" |
15 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
16 #include "base/strings/string16.h" | 18 #include "base/strings/string16.h" |
17 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
18 #include "base/synchronization/cancellation_flag.h" | 20 #include "base/synchronization/cancellation_flag.h" |
19 #include "base/test/scoped_path_override.h" | 21 #include "base/test/scoped_path_override.h" |
22 #include "base/test/test_reg_util_win.h" | |
20 #include "base/test/test_shortcut_win.h" | 23 #include "base/test/test_shortcut_win.h" |
24 #include "base/win/registry.h" | |
21 #include "base/win/shortcut.h" | 25 #include "base/win/shortcut.h" |
22 #include "base/win/windows_version.h" | 26 #include "base/win/windows_version.h" |
23 #include "chrome/installer/util/browser_distribution.h" | 27 #include "chrome/installer/util/browser_distribution.h" |
24 #include "chrome/installer/util/product.h" | 28 #include "chrome/installer/util/product.h" |
25 #include "chrome/installer/util/util_constants.h" | 29 #include "chrome/installer/util/util_constants.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
27 | 31 |
28 namespace { | 32 namespace { |
29 | 33 |
30 const wchar_t kManganeseExe[] = L"manganese.exe"; | 34 const wchar_t kManganeseExe[] = L"manganese.exe"; |
31 const wchar_t kIronExe[] = L"iron.exe"; | 35 const wchar_t kIronExe[] = L"iron.exe"; |
32 const wchar_t kOtherIco[] = L"other.ico"; | 36 const wchar_t kOtherIco[] = L"other.ico"; |
33 | 37 |
38 // For registry tests. | |
39 const wchar_t kTestProgid[] = L"TestApp"; | |
40 const wchar_t kTestOpenCommand[] = L"C:\\test.exe"; | |
41 const wchar_t kTestFileTypeName[] = L"Test File Type"; | |
42 const wchar_t kTestIconPath[] = L"D:\\test.ico"; | |
43 const wchar_t* kTestFileExtensions[] = { | |
44 L"test1", | |
45 L"test2", | |
46 }; | |
47 | |
34 // TODO(huangs): Separate this into generic shortcut tests and Chrome-specific | 48 // TODO(huangs): Separate this into generic shortcut tests and Chrome-specific |
35 // tests. Specifically, we should not overly rely on getting shortcut properties | 49 // tests. Specifically, we should not overly rely on getting shortcut properties |
36 // from product_->AddDefaultShortcutProperties(). | 50 // from product_->AddDefaultShortcutProperties(). |
37 class ShellUtilShortcutTest : public testing::Test { | 51 class ShellUtilShortcutTest : public testing::Test { |
38 protected: | 52 protected: |
39 ShellUtilShortcutTest() : test_properties_(ShellUtil::CURRENT_USER) {} | 53 ShellUtilShortcutTest() : test_properties_(ShellUtil::CURRENT_USER) {} |
40 | 54 |
41 virtual void SetUp() OVERRIDE { | 55 virtual void SetUp() OVERRIDE { |
42 dist_ = BrowserDistribution::GetDistribution(); | 56 dist_ = BrowserDistribution::GetDistribution(); |
43 ASSERT_TRUE(dist_ != NULL); | 57 ASSERT_TRUE(dist_ != NULL); |
(...skipping 741 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
785 // The shortcut shouldn't be removed as it was installed pointing to | 799 // The shortcut shouldn't be removed as it was installed pointing to |
786 // |other_chrome_exe| and RemoveChromeShortcut() is being told that the | 800 // |other_chrome_exe| and RemoveChromeShortcut() is being told that the |
787 // removed shortcut should point to |chrome_exe_|. | 801 // removed shortcut should point to |chrome_exe_|. |
788 ASSERT_TRUE(ShellUtil::RemoveShortcuts( | 802 ASSERT_TRUE(ShellUtil::RemoveShortcuts( |
789 ShellUtil::SHORTCUT_LOCATION_DESKTOP, dist_, ShellUtil::CURRENT_USER, | 803 ShellUtil::SHORTCUT_LOCATION_DESKTOP, dist_, ShellUtil::CURRENT_USER, |
790 chrome_exe_)); | 804 chrome_exe_)); |
791 ASSERT_TRUE(base::PathExists(shortcut_path)); | 805 ASSERT_TRUE(base::PathExists(shortcut_path)); |
792 ASSERT_TRUE(base::PathExists(shortcut_path.DirName())); | 806 ASSERT_TRUE(base::PathExists(shortcut_path.DirName())); |
793 } | 807 } |
794 | 808 |
809 class ShellUtilRegistryTest : public testing::Test { | |
gab
2014/10/01 14:31:11
I didn't re-review the expectation below in detail
Matt Giuca
2014/10/02 09:36:34
Correct.
| |
810 public: | |
811 ShellUtilRegistryTest() {} | |
812 | |
813 protected: | |
814 virtual void SetUp() OVERRIDE { | |
815 registry_overrides_.OverrideRegistry(HKEY_CURRENT_USER); | |
816 | |
817 // .test2 files already have a default application. | |
818 base::win::RegKey key; | |
819 ASSERT_EQ( | |
820 ERROR_SUCCESS, | |
821 key.Create( | |
822 HKEY_CURRENT_USER, L"Software\\Classes\\.test2", KEY_ALL_ACCESS)); | |
823 EXPECT_EQ(ERROR_SUCCESS, key.WriteValue(L"", L"SomeOtherApp")); | |
824 } | |
825 | |
826 static base::CommandLine OpenCommand() { | |
827 base::FilePath open_command_path(kTestOpenCommand); | |
828 return base::CommandLine(open_command_path); | |
829 } | |
830 | |
831 static std::set<base::string16> FileExtensions() { | |
832 std::set<base::string16> file_extensions; | |
833 for (size_t i = 0; i < arraysize(kTestFileExtensions); ++i) | |
834 file_extensions.insert(kTestFileExtensions[i]); | |
835 return file_extensions; | |
836 } | |
837 | |
838 private: | |
839 registry_util::RegistryOverrideManager registry_overrides_; | |
840 | |
841 DISALLOW_COPY_AND_ASSIGN(ShellUtilRegistryTest); | |
842 }; | |
843 | |
844 TEST_F(ShellUtilRegistryTest, AddFileAssociations) { | |
845 // Create file associations. | |
846 EXPECT_TRUE(ShellUtil::AddFileAssociations(kTestProgid, | |
847 OpenCommand(), | |
848 kTestFileTypeName, | |
849 base::FilePath(kTestIconPath), | |
850 FileExtensions())); | |
851 | |
852 // Ensure that the registry keys have been correctly set. | |
853 base::win::RegKey key; | |
854 std::wstring value; | |
855 ASSERT_EQ( | |
856 ERROR_SUCCESS, | |
857 key.Open(HKEY_CURRENT_USER, L"Software\\Classes\\TestApp", KEY_READ)); | |
858 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value)); | |
859 EXPECT_EQ(L"Test File Type", value); | |
860 ASSERT_EQ(ERROR_SUCCESS, | |
861 key.Open(HKEY_CURRENT_USER, | |
862 L"Software\\Classes\\TestApp\\DefaultIcon", | |
863 KEY_READ)); | |
864 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value)); | |
865 EXPECT_EQ(L"D:\\test.ico,0", value); | |
866 ASSERT_EQ(ERROR_SUCCESS, | |
867 key.Open(HKEY_CURRENT_USER, | |
868 L"Software\\Classes\\TestApp\\shell\\open\\command", | |
869 KEY_READ)); | |
870 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value)); | |
871 EXPECT_EQ(L"\"C:\\test.exe\"", value); | |
872 | |
873 // .test1 should be default-associated with our test app. | |
874 ASSERT_EQ( | |
875 ERROR_SUCCESS, | |
876 key.Open(HKEY_CURRENT_USER, L"Software\\Classes\\.test1", KEY_READ)); | |
877 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value)); | |
878 EXPECT_EQ(L"TestApp", value); | |
879 ASSERT_EQ(ERROR_SUCCESS, | |
880 key.Open(HKEY_CURRENT_USER, | |
881 L"Software\\Classes\\.test1\\OpenWithProgids", | |
882 KEY_READ)); | |
883 EXPECT_TRUE(key.HasValue(L"TestApp")); | |
884 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"TestApp", &value)); | |
885 EXPECT_EQ(L"", value); | |
886 | |
887 // .test2 should still be associated with the other app (should not have been | |
888 // overridden). But it should have our app in its Open With list. | |
889 ASSERT_EQ( | |
890 ERROR_SUCCESS, | |
891 key.Open(HKEY_CURRENT_USER, L"Software\\Classes\\.test2", KEY_READ)); | |
892 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value)); | |
893 EXPECT_EQ(L"SomeOtherApp", value); | |
894 ASSERT_EQ(ERROR_SUCCESS, | |
895 key.Open(HKEY_CURRENT_USER, | |
896 L"Software\\Classes\\.test2\\OpenWithProgids", | |
897 KEY_READ)); | |
898 EXPECT_TRUE(key.HasValue(L"TestApp")); | |
899 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"TestApp", &value)); | |
900 EXPECT_EQ(L"", value); | |
901 } | |
902 | |
903 TEST_F(ShellUtilRegistryTest, DeleteFileAssociations) { | |
904 // Create file associations. | |
905 EXPECT_TRUE(ShellUtil::AddFileAssociations(kTestProgid, | |
906 OpenCommand(), | |
907 kTestFileTypeName, | |
908 base::FilePath(kTestIconPath), | |
909 FileExtensions())); | |
910 | |
911 // Delete them. | |
912 EXPECT_TRUE(ShellUtil::DeleteFileAssociations(kTestProgid)); | |
913 | |
914 // The class key should have been completely deleted. | |
915 base::win::RegKey key; | |
916 std::wstring value; | |
917 ASSERT_NE( | |
918 ERROR_SUCCESS, | |
919 key.Open(HKEY_CURRENT_USER, L"Software\\Classes\\TestApp", KEY_READ)); | |
920 | |
921 // We don't need to delete the associations with the particular extensions | |
gab
2014/10/01 14:31:11
It's not true (as discussed) that we should not de
Matt Giuca
2014/10/02 09:36:34
Done.
| |
922 // (Windows will ignore those). However, we must ensure that .test2 is still | |
923 // associated with the other app. | |
924 ASSERT_EQ( | |
925 ERROR_SUCCESS, | |
926 key.Open(HKEY_CURRENT_USER, L"Software\\Classes\\.test2", KEY_READ)); | |
927 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(L"", &value)); | |
928 EXPECT_EQ(L"SomeOtherApp", value); | |
929 } | |
930 | |
795 TEST(ShellUtilTest, BuildAppModelIdBasic) { | 931 TEST(ShellUtilTest, BuildAppModelIdBasic) { |
796 std::vector<base::string16> components; | 932 std::vector<base::string16> components; |
797 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 933 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
798 const base::string16 base_app_id(dist->GetBaseAppId()); | 934 const base::string16 base_app_id(dist->GetBaseAppId()); |
799 components.push_back(base_app_id); | 935 components.push_back(base_app_id); |
800 ASSERT_EQ(base_app_id, ShellUtil::BuildAppModelId(components)); | 936 ASSERT_EQ(base_app_id, ShellUtil::BuildAppModelId(components)); |
801 } | 937 } |
802 | 938 |
803 TEST(ShellUtilTest, BuildAppModelIdManySmall) { | 939 TEST(ShellUtilTest, BuildAppModelIdManySmall) { |
804 std::vector<base::string16> components; | 940 std::vector<base::string16> components; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
864 | 1000 |
865 const base::string16 expected[] = { L"", L"MY", L"MZXQ", L"MZXW6", L"MZXW6YQ", | 1001 const base::string16 expected[] = { L"", L"MY", L"MZXQ", L"MZXW6", L"MZXW6YQ", |
866 L"MZXW6YTB", L"MZXW6YTBOI"}; | 1002 L"MZXW6YTB", L"MZXW6YTBOI"}; |
867 | 1003 |
868 // Run the tests, with one more letter in the input every pass. | 1004 // Run the tests, with one more letter in the input every pass. |
869 for (int i = 0; i < arraysize(expected); ++i) { | 1005 for (int i = 0; i < arraysize(expected); ++i) { |
870 ASSERT_EQ(expected[i], | 1006 ASSERT_EQ(expected[i], |
871 ShellUtil::ByteArrayToBase32(test_array, i)); | 1007 ShellUtil::ByteArrayToBase32(test_array, i)); |
872 } | 1008 } |
873 } | 1009 } |
OLD | NEW |