OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/shell_integration.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" |
| 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/string_number_conversions.h" |
| 13 #include "base/test/test_shortcut_win.h" |
| 14 #include "base/win/scoped_com_initializer.h" |
| 15 #include "base/win/windows_version.h" |
| 16 #include "chrome/common/chrome_constants.h" |
| 17 #include "chrome/common/chrome_paths_internal.h" |
| 18 #include "chrome/installer/util/browser_distribution.h" |
| 19 #include "chrome/installer/util/shell_util.h" |
| 20 #include "chrome/installer/util/util_constants.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 |
| 23 namespace { |
| 24 |
| 25 class ShellIntegrationWinMigrateShortcutTest : public testing::Test { |
| 26 protected: |
| 27 virtual void SetUp() OVERRIDE { |
| 28 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 29 |
| 30 // A path to a random target. |
| 31 FilePath other_target; |
| 32 file_util::CreateTemporaryFileInDir(temp_dir_.path(), &other_target); |
| 33 |
| 34 // This doesn't need to actually have a base name of "chrome.exe". |
| 35 file_util::CreateTemporaryFileInDir(temp_dir_.path(), &chrome_exe_); |
| 36 |
| 37 chrome_app_id_ = |
| 38 ShellUtil::GetBrowserModelId(BrowserDistribution::GetDistribution(), |
| 39 true); |
| 40 |
| 41 // A temporary object to pass properties to AddTestShortcut(). |
| 42 base::win::ShortcutProperties temp_properties; |
| 43 |
| 44 // Shortcut 0 doesn't point to chrome.exe and thus should never be migrated. |
| 45 temp_properties.set_target(other_target); |
| 46 temp_properties.set_app_id(L"Dumbo"); |
| 47 temp_properties.set_dual_mode(false); |
| 48 ASSERT_NO_FATAL_FAILURE(AddTestShortcut(temp_properties)); |
| 49 |
| 50 // Shortcut 1 points to chrome.exe and thus should be migrated. |
| 51 temp_properties.set_target(chrome_exe_); |
| 52 temp_properties.set_app_id(L"Dumbo"); |
| 53 temp_properties.set_dual_mode(false); |
| 54 ASSERT_NO_FATAL_FAILURE(AddTestShortcut(temp_properties)); |
| 55 |
| 56 // Shortcut 2 points to chrome.exe, but already has the right appid and thus |
| 57 // should only be migrated if dual_mode is desired. |
| 58 temp_properties.set_target(chrome_exe_); |
| 59 temp_properties.set_app_id(chrome_app_id_); |
| 60 temp_properties.set_dual_mode(false); |
| 61 ASSERT_NO_FATAL_FAILURE(AddTestShortcut(temp_properties)); |
| 62 |
| 63 // Shortcut 3 is like shortcut 2 except it has dual_mode and thus should |
| 64 // never be migrated. |
| 65 temp_properties.set_target(chrome_exe_); |
| 66 temp_properties.set_app_id(chrome_app_id_); |
| 67 temp_properties.set_dual_mode(true); |
| 68 ASSERT_NO_FATAL_FAILURE(AddTestShortcut(temp_properties)); |
| 69 |
| 70 // Shortcut 4 is like shortcut 1, but it's appid is a prefix of the expected |
| 71 // appid instead of being totally different. |
| 72 string16 chrome_app_id_is_prefix(chrome_app_id_); |
| 73 chrome_app_id_is_prefix.push_back(L'1'); |
| 74 temp_properties.set_target(chrome_exe_); |
| 75 temp_properties.set_app_id(chrome_app_id_is_prefix); |
| 76 temp_properties.set_dual_mode(false); |
| 77 ASSERT_NO_FATAL_FAILURE(AddTestShortcut(temp_properties)); |
| 78 |
| 79 // Shortcut 5 is like shortcut 1, but it's appid is of the same size as the |
| 80 // expected appid. |
| 81 string16 same_size_as_chrome_app_id(L'1', chrome_app_id_.size()); |
| 82 temp_properties.set_target(chrome_exe_); |
| 83 temp_properties.set_app_id(same_size_as_chrome_app_id); |
| 84 temp_properties.set_dual_mode(false); |
| 85 ASSERT_NO_FATAL_FAILURE(AddTestShortcut(temp_properties)); |
| 86 |
| 87 // Shortcut 6 doesn't have an app_id, nor is dual_mode even set; they should |
| 88 // be set as expected upon migration. |
| 89 base::win::ShortcutProperties no_properties; |
| 90 no_properties.set_target(chrome_exe_); |
| 91 ASSERT_NO_FATAL_FAILURE(AddTestShortcut(no_properties)); |
| 92 } |
| 93 |
| 94 void AddTestShortcut( |
| 95 const base::win::ShortcutProperties& shortcut_properties) { |
| 96 shortcuts_properties_.push_back(shortcut_properties); |
| 97 FilePath shortcut_path = |
| 98 temp_dir_.path().Append(L"Shortcut " + |
| 99 base::IntToString16(shortcuts_.size()) + |
| 100 installer::kLnkExt); |
| 101 shortcuts_.push_back(shortcut_path); |
| 102 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink( |
| 103 shortcut_path, shortcut_properties, |
| 104 base::win::SHORTCUT_CREATE_ALWAYS)); |
| 105 } |
| 106 |
| 107 base::win::ScopedCOMInitializer com_initializer_; |
| 108 base::ScopedTempDir temp_dir_; |
| 109 |
| 110 // The path to a fake chrome.exe. |
| 111 FilePath chrome_exe_; |
| 112 |
| 113 // Test shortcuts. |
| 114 std::vector<FilePath> shortcuts_; |
| 115 |
| 116 // Initial properties for the test shortcuts. |
| 117 std::vector<base::win::ShortcutProperties> shortcuts_properties_; |
| 118 |
| 119 // Chrome's AppUserModelId. |
| 120 string16 chrome_app_id_; |
| 121 }; |
| 122 |
| 123 } // namespace |
| 124 |
| 125 // Test migration when not checking for dual mode. |
| 126 TEST_F(ShellIntegrationWinMigrateShortcutTest, DontCheckDualMode) { |
| 127 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
| 128 return; |
| 129 |
| 130 EXPECT_EQ(4, |
| 131 ShellIntegration::MigrateShortcutsInPathInternal( |
| 132 chrome_exe_, temp_dir_.path(), false)); |
| 133 |
| 134 // Only shortcut 1, 4, 5, and 6 should have been migrated. |
| 135 shortcuts_properties_[1].set_app_id(chrome_app_id_); |
| 136 shortcuts_properties_[4].set_app_id(chrome_app_id_); |
| 137 shortcuts_properties_[5].set_app_id(chrome_app_id_); |
| 138 shortcuts_properties_[6].set_app_id(chrome_app_id_); |
| 139 |
| 140 for (size_t i = 0; i < shortcuts_.size(); ++i) |
| 141 base::win::ValidateShortcut(shortcuts_[i], shortcuts_properties_[i]); |
| 142 } |
| 143 |
| 144 // Test migration when also checking for dual mode. |
| 145 TEST_F(ShellIntegrationWinMigrateShortcutTest, CheckDualMode) { |
| 146 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
| 147 return; |
| 148 |
| 149 EXPECT_EQ(5, |
| 150 ShellIntegration::MigrateShortcutsInPathInternal( |
| 151 chrome_exe_, temp_dir_.path(), true)); |
| 152 |
| 153 // Shortcut 1, 4, 5, and 6 should have had both their app_id and dual_mode |
| 154 // properties fixed and shortcut 2 should also have had it's dual_mode |
| 155 // property fixed. |
| 156 shortcuts_properties_[1].set_app_id(chrome_app_id_); |
| 157 shortcuts_properties_[4].set_app_id(chrome_app_id_); |
| 158 shortcuts_properties_[5].set_app_id(chrome_app_id_); |
| 159 shortcuts_properties_[6].set_app_id(chrome_app_id_); |
| 160 |
| 161 shortcuts_properties_[1].set_dual_mode(true); |
| 162 shortcuts_properties_[2].set_dual_mode(true); |
| 163 shortcuts_properties_[4].set_dual_mode(true); |
| 164 shortcuts_properties_[5].set_dual_mode(true); |
| 165 shortcuts_properties_[6].set_dual_mode(true); |
| 166 |
| 167 for (size_t i = 0; i < shortcuts_.size(); ++i) |
| 168 base::win::ValidateShortcut(shortcuts_[i], shortcuts_properties_[i]); |
| 169 } |
| 170 |
| 171 TEST(ShellIntegrationWinTest, GetAppModelIdForProfileTest) { |
| 172 const string16 base_app_id( |
| 173 BrowserDistribution::GetDistribution()->GetBaseAppId()); |
| 174 |
| 175 // Empty profile path should get chrome::kBrowserAppID |
| 176 FilePath empty_path; |
| 177 EXPECT_EQ(base_app_id, |
| 178 ShellIntegration::GetAppModelIdForProfile(base_app_id, empty_path)); |
| 179 |
| 180 // Default profile path should get chrome::kBrowserAppID |
| 181 FilePath default_user_data_dir; |
| 182 chrome::GetDefaultUserDataDirectory(&default_user_data_dir); |
| 183 FilePath default_profile_path = |
| 184 default_user_data_dir.AppendASCII(chrome::kInitialProfile); |
| 185 EXPECT_EQ(base_app_id, |
| 186 ShellIntegration::GetAppModelIdForProfile(base_app_id, |
| 187 default_profile_path)); |
| 188 |
| 189 // Non-default profile path should get chrome::kBrowserAppID joined with |
| 190 // profile info. |
| 191 FilePath profile_path(FILE_PATH_LITERAL("root")); |
| 192 profile_path = profile_path.Append(FILE_PATH_LITERAL("udd")); |
| 193 profile_path = profile_path.Append(FILE_PATH_LITERAL("User Data - Test")); |
| 194 EXPECT_EQ(base_app_id + L".udd.UserDataTest", |
| 195 ShellIntegration::GetAppModelIdForProfile(base_app_id, |
| 196 profile_path)); |
| 197 } |
OLD | NEW |