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

Unified Diff: chrome/browser/profiles/profile_shortcut_manager_unittest_win.cc

Issue 11852022: Make profile shortcuts more sane when using --user-data-dir=. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 11 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
« no previous file with comments | « no previous file | chrome/browser/profiles/profile_shortcut_manager_win.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/profiles/profile_shortcut_manager_unittest_win.cc
===================================================================
--- chrome/browser/profiles/profile_shortcut_manager_unittest_win.cc (revision 178353)
+++ chrome/browser/profiles/profile_shortcut_manager_unittest_win.cc (working copy)
@@ -5,18 +5,21 @@
#include <objbase.h> // For CoInitialize().
#include "base/base_paths.h"
+#include "base/command_line.h"
#include "base/file_util.h"
#include "base/location.h"
#include "base/message_loop.h"
#include "base/path_service.h"
+#include "base/string16.h"
#include "base/test/scoped_path_override.h"
-#include "base/string16.h"
#include "base/test/test_shortcut_win.h"
#include "base/win/shortcut.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_shortcut_manager.h"
#include "chrome/browser/profiles/profile_shortcut_manager_win.h"
+#include "chrome/common/chrome_paths.h"
+#include "chrome/common/chrome_switches.h"
#include "chrome/installer/util/browser_distribution.h"
#include "chrome/installer/util/product.h"
#include "chrome/installer/util/shell_util.h"
@@ -141,8 +144,10 @@
expected_properties.set_target(GetExePath());
expected_properties.set_description(GetDistribution()->GetAppDescription());
expected_properties.set_dual_mode(false);
- expected_properties.set_arguments(
- profiles::internal::CreateProfileShortcutFlags(profile_path));
+ const CommandLine command_line =
+ profiles::internal::CreateCommandLineForProfileShortcut(GetExePath(),
+ profile_path);
+ expected_properties.set_arguments(command_line.GetArgumentsString());
expected_properties.set_icon(icon_path, 0);
base::win::ValidateShortcut(shortcut_path, expected_properties);
}
@@ -225,6 +230,68 @@
return system_level_shortcut_path;
}
+ // Returns the CommandLine of the shortcut at |shortcut_path|.
+ CommandLine GetShortcutCommandLine(const tracked_objects::Location& location,
+ const FilePath& shortcut_path) {
+ FilePath target_path;
+ string16 shortcut_args;
+ const bool success = base::win::ResolveShortcut(shortcut_path, &target_path,
+ &shortcut_args);
+ EXPECT_TRUE(success) << location.ToString();
+ return CommandLine::FromString(target_path.value() + L" " + shortcut_args);
+ }
+
+ // Replaces the command line args of the shortcut at |shortcut_path| with
+ // the ones in |command_line|.
+ void SetShortcutCommandLine(const tracked_objects::Location& location,
+ const FilePath& shortcut_path,
+ const CommandLine& command_line) {
+ ShellUtil::ShortcutProperties properties(ShellUtil::CURRENT_USER);
+ properties.set_shortcut_name(
+ shortcut_path.BaseName().RemoveExtension().value());
+ properties.set_arguments(command_line.GetArgumentsString());
+ const bool success = ShellUtil::CreateOrUpdateShortcut(
+ ShellUtil::SHORTCUT_LOCATION_DESKTOP, GetDistribution(), properties,
+ ShellUtil::SHELL_SHORTCUT_UPDATE_EXISTING);
+ ASSERT_TRUE(success) << location.ToString();
+ }
+
+ // Makes a copy of |command_line| with the --user-data-dir argument set to
+ // |user_data_dir|. Removes the argument if |user_data_dir| is empty.
+ CommandLine ReplaceCommandLineUserDataDir(const CommandLine& command_line,
+ const FilePath& user_data_dir) {
+ CommandLine result(command_line.GetProgram());
+ const CommandLine::SwitchMap& switches = command_line.GetSwitches();
+ for (CommandLine::SwitchMap::const_iterator it = switches.begin();
+ it != switches.end(); ++it) {
+ if (it->first != switches::kUserDataDir)
+ result.AppendSwitchNative(it->first, it->second);
+ }
+ if (!user_data_dir.empty())
+ result.AppendSwitchPath(switches::kUserDataDir, user_data_dir);
+ return result;
+ }
+
+ // Creates a copy of the shortcut at |shortcut_path| and makes the copy have
+ // |user_data_dir| specified as a custom --user-data-dir argument. Removes
+ // any --user-data-dir argument if |user_data_dir| is empty.
+ FilePath CopyShortcutAndSetUserDataDir(
+ const tracked_objects::Location& location,
+ const FilePath& shortcut_path,
+ const string16& new_shortcut_name,
+ const FilePath& user_data_dir) {
+ const FilePath new_shortcut_path =
+ GetUserShortcutsDirectory().Append(new_shortcut_name);
+ EXPECT_TRUE(file_util::CopyFile(shortcut_path, new_shortcut_path))
+ << location.ToString();
+ const CommandLine command_line =
+ GetShortcutCommandLine(FROM_HERE, shortcut_path);
+ SetShortcutCommandLine(location, new_shortcut_path,
+ ReplaceCommandLineUserDataDir(command_line,
+ user_data_dir));
+ return new_shortcut_path;
+ }
+
void RenameProfile(const tracked_objects::Location& location,
const FilePath& profile_path,
const string16& new_profile_name) {
@@ -248,6 +315,12 @@
return exe_path;
}
+ FilePath GetUserDataDir() {
+ FilePath user_data_dir;
+ EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));
+ return user_data_dir;
+ }
+
FilePath GetUserShortcutsDirectory() {
FilePath user_shortcuts_directory;
EXPECT_TRUE(ShellUtil::GetShortcutPath(ShellUtil::SHORTCUT_LOCATION_DESKTOP,
@@ -324,8 +397,11 @@
const string16 kProfileName = L"MyProfileX";
const FilePath profile_path =
profile_info_cache_->GetUserDataDir().Append(kProfileName);
- EXPECT_EQ(L"--profile-directory=\"" + kProfileName + L"\"",
- profiles::internal::CreateProfileShortcutFlags(profile_path));
+ const CommandLine command_line =
+ profiles::internal::CreateCommandLineForProfileShortcut(GetExePath(),
+ profile_path);
+ EXPECT_EQ(kProfileName,
+ command_line.GetSwitchValueNative(switches::kProfileDirectory));
}
TEST_F(ProfileShortcutManagerTest, DesktopShortcutsCreate) {
@@ -739,3 +815,70 @@
EXPECT_FALSE(file_util::PathExists(profile_1_shortcut_path));
EXPECT_FALSE(file_util::PathExists(profile_2_shortcut_path));
}
+
+TEST_F(ProfileShortcutManagerTest, DefaultUserDataDir_UpdateUserDataShortcuts) {
+ // Checks that other shortcuts with --user-data-dir specified are not touched.
+ ASSERT_TRUE(profiles::internal::GetCustomUserDataDirectory().empty());
+ SetupAndCreateTwoShortcuts(FROM_HERE);
+
+ const FilePath profile_1_shortcut_path =
+ GetDefaultShortcutPathForProfile(profile_1_name_);
+ // Ensure that --user-data-dir is not specified by default.
+ ASSERT_FALSE(GetShortcutCommandLine(FROM_HERE, profile_1_shortcut_path)
+ .HasSwitch(switches::kUserDataDir));
+
+ const FilePath shortcut_with_current_user_data_path =
+ CopyShortcutAndSetUserDataDir(FROM_HERE, profile_1_shortcut_path,
+ L"Copied1.lnk", GetUserDataDir());
+
+ base::ScopedTempDir temp_dir;
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+ const FilePath shortcut_with_other_user_data_path =
+ CopyShortcutAndSetUserDataDir(FROM_HERE, profile_1_shortcut_path,
+ L"Copied2.lnk", temp_dir.path());
+
+ // Now, delete the profile and ensure that the original shortcut and the
+ // shortcuts with the current user data dir set explictly get deleted, but
+ // not the one specifying a different one.
+ profile_info_cache_->DeleteProfileFromCache(profile_1_path_);
+ RunPendingTasks();
+
+ EXPECT_FALSE(file_util::PathExists(profile_1_shortcut_path));
+ EXPECT_FALSE(file_util::PathExists(shortcut_with_current_user_data_path));
+ EXPECT_TRUE(file_util::PathExists(shortcut_with_other_user_data_path));
+}
+
+TEST_F(ProfileShortcutManagerTest, CustomUserDataDir_UpdateUserDataShortcuts) {
+ // Overrides the user data dir with a custom one (simulating a custom
+ // --user-data-dir specified on the command line) and ensure things behave
+ // as expected.
+ base::ScopedPathOverride fake_custom_user_data_dir(chrome::DIR_USER_DATA);
+ ASSERT_FALSE(profiles::internal::GetCustomUserDataDirectory().empty());
+ SetupAndCreateTwoShortcuts(FROM_HERE);
+
+ const FilePath profile_1_shortcut_path =
+ GetDefaultShortcutPathForProfile(profile_1_name_);
+ // Ensure that the custom --user-data-dir has been specified.
+ ASSERT_EQ(GetUserDataDir(),
+ GetShortcutCommandLine(FROM_HERE, profile_1_shortcut_path)
+ .GetSwitchValuePath(switches::kUserDataDir));
+
+ const FilePath shortcut_with_empty_user_data_path =
+ CopyShortcutAndSetUserDataDir(FROM_HERE, profile_1_shortcut_path,
+ L"Copied1.lnk", FilePath());
+
+ base::ScopedTempDir temp_dir;
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+ const FilePath shortcut_with_other_user_data_path =
+ CopyShortcutAndSetUserDataDir(FROM_HERE, profile_1_shortcut_path,
+ L"Copied2.lnk", temp_dir.path());
+
+ // Now, delete the profile and ensure that the original shortcut got deleted,
+ // but the shortcuts without user data dir and with a different one did not.
+ profile_info_cache_->DeleteProfileFromCache(profile_1_path_);
+ RunPendingTasks();
+
+ EXPECT_FALSE(file_util::PathExists(profile_1_shortcut_path));
+ EXPECT_TRUE(file_util::PathExists(shortcut_with_empty_user_data_path));
+ EXPECT_TRUE(file_util::PathExists(shortcut_with_other_user_data_path));
+}
« no previous file with comments | « no previous file | chrome/browser/profiles/profile_shortcut_manager_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698