summaryrefslogtreecommitdiffstats
path: root/chrome/browser/profiles/profile_shortcut_manager_unittest_win.cc
diff options
context:
space:
mode:
authorasvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-22 19:11:45 +0000
committerasvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-22 19:11:45 +0000
commited116523c7fac300437919cb702230cc7b57ae56 (patch)
tree4630f5ca7fb58a69608c15b3abd2ae83f32f3b70 /chrome/browser/profiles/profile_shortcut_manager_unittest_win.cc
parent82c08595c6d66ca835659ed1e06234e80bf818a6 (diff)
downloadchromium_src-ed116523c7fac300437919cb702230cc7b57ae56.zip
chromium_src-ed116523c7fac300437919cb702230cc7b57ae56.tar.gz
chromium_src-ed116523c7fac300437919cb702230cc7b57ae56.tar.bz2
Make profile shortcut manager be able to find renamed shortcuts.
Also cleans up some of the existing unit test code. BUG=108203 TEST=New unit tests and manually testing that copied and renamed profile shortcuts get updated and deleted. Review URL: https://chromiumcodereview.appspot.com/11417097 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@169292 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/profiles/profile_shortcut_manager_unittest_win.cc')
-rw-r--r--chrome/browser/profiles/profile_shortcut_manager_unittest_win.cc367
1 files changed, 215 insertions, 152 deletions
diff --git a/chrome/browser/profiles/profile_shortcut_manager_unittest_win.cc b/chrome/browser/profiles/profile_shortcut_manager_unittest_win.cc
index 000bc91..4a4234e 100644
--- a/chrome/browser/profiles/profile_shortcut_manager_unittest_win.cc
+++ b/chrome/browser/profiles/profile_shortcut_manager_unittest_win.cc
@@ -28,76 +28,42 @@
using content::BrowserThread;
-namespace {
-
-// Sets |path| to the path to the shortcut for this profile.
-// Automatically fails the test on failure to do so.
-void GetShortcutPathForProfile(BrowserDistribution* dist,
- const string16& profile_name,
- FilePath* path) {
- EXPECT_TRUE(ShellUtil::GetShortcutPath(ShellUtil::SHORTCUT_LOCATION_DESKTOP,
- dist, ShellUtil::CURRENT_USER, path));
- string16 shortcut_name(
- ProfileShortcutManager::GetShortcutNameForProfile(profile_name));
- *path = path->Append(shortcut_name);
-}
-
-// Returns true if the shortcut for this profile exists.
-bool ProfileShortcutExists(BrowserDistribution* dist,
- const string16& profile_name) {
- FilePath shortcut_path;
- GetShortcutPathForProfile(dist, profile_name, &shortcut_path);
- return file_util::PathExists(shortcut_path);
-}
-
-// Calls base::win::ValidateShortcut() with expected properties for
-// |profile_name|'s shortcut.
-void ValidateProfileShortcut(BrowserDistribution* dist,
- const string16& profile_name) {
- EXPECT_TRUE(ProfileShortcutExists(dist, profile_name));
-
- FilePath exe_path;
- EXPECT_TRUE(PathService::Get(base::FILE_EXE, &exe_path));
-
- FilePath shortcut_path;
- GetShortcutPathForProfile(dist, profile_name, &shortcut_path);
-
- // TODO(hallielaine): With this new struct method for VerifyShortcut you can
- // now test more properties like: arguments, icon, icon_index, and app_id.
- base::win::ShortcutProperties expected_properties;
- expected_properties.set_target(exe_path);
- expected_properties.set_description(dist->GetAppDescription());
- expected_properties.set_dual_mode(false);
- base::win::ValidateShortcut(shortcut_path, expected_properties);
-}
-
-} // namespace
-
class ProfileShortcutManagerTest : public testing::Test {
protected:
ProfileShortcutManagerTest()
- : ui_thread_(BrowserThread::UI, &message_loop_),
+ : distribution_(NULL),
+ ui_thread_(BrowserThread::UI, &message_loop_),
file_thread_(BrowserThread::FILE, &message_loop_),
+ profile_shortcut_manager_(NULL),
+ profile_info_cache_(NULL),
fake_user_desktop_(base::DIR_USER_DESKTOP) {
}
virtual void SetUp() OVERRIDE {
CoInitialize(NULL);
- dist_ = BrowserDistribution::GetDistribution();
- ASSERT_TRUE(dist_ != NULL);
+ distribution_ = BrowserDistribution::GetDistribution();
+ ASSERT_TRUE(distribution_ != NULL);
TestingBrowserProcess* browser_process =
static_cast<TestingBrowserProcess*>(g_browser_process);
profile_manager_.reset(new TestingProfileManager(browser_process));
ASSERT_TRUE(profile_manager_->SetUp());
+ profile_info_cache_ = profile_manager_->profile_info_cache();
+ profile_shortcut_manager_.reset(
+ ProfileShortcutManager::Create(profile_manager_->profile_manager()));
- dest_path_ = profile_manager_->profile_info_cache()->GetUserDataDir();
+ ASSERT_TRUE(PathService::Get(base::FILE_EXE, &exe_path_));
+ dest_path_ = profile_info_cache_->GetUserDataDir();
dest_path_ = dest_path_.Append(FILE_PATH_LITERAL("My profile"));
file_util::CreateDirectoryW(dest_path_);
profile_name_ = ASCIIToUTF16("My profile");
- second_dest_path_ =
- profile_manager_->profile_info_cache()->GetUserDataDir();
+ ASSERT_TRUE(ShellUtil::GetShortcutPath(ShellUtil::SHORTCUT_LOCATION_DESKTOP,
+ distribution_,
+ ShellUtil::CURRENT_USER,
+ &shortcuts_directory_));
+
+ second_dest_path_ = profile_info_cache_->GetUserDataDir();
second_dest_path_ =
second_dest_path_.Append(FILE_PATH_LITERAL("My profile 2"));
file_util::CreateDirectoryW(second_dest_path_);
@@ -107,171 +73,268 @@ class ProfileShortcutManagerTest : public testing::Test {
virtual void TearDown() OVERRIDE {
message_loop_.RunUntilIdle();
- int num_profiles =
- profile_manager_->profile_info_cache()->GetNumberOfProfiles();
-
// Remove all shortcuts except the last (since it will no longer have
// an appended name).
+ const int num_profiles = profile_info_cache_->GetNumberOfProfiles();
for (int i = 0; i < num_profiles; ++i) {
const FilePath profile_path =
- profile_manager_->profile_info_cache()->GetPathOfProfileAtIndex(0);
+ profile_info_cache_->GetPathOfProfileAtIndex(0);
string16 profile_name;
- if (i != num_profiles - 1) {
- profile_name =
- profile_manager_->profile_info_cache()->GetNameOfProfileAtIndex(0);
- }
- profile_manager_->profile_info_cache()->DeleteProfileFromCache(
- profile_path);
- MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
- MessageLoop::current()->Run();
- ASSERT_FALSE(ProfileShortcutExists(dist_, profile_name));
+ if (i != num_profiles - 1)
+ profile_name = profile_info_cache_->GetNameOfProfileAtIndex(0);
+ profile_info_cache_->DeleteProfileFromCache(profile_path);
+ RunPendingTasks();
+ ASSERT_FALSE(ProfileShortcutExistsAtDefaultPath(profile_name));
ASSERT_FALSE(file_util::PathExists(profile_path.Append(
FILE_PATH_LITERAL("Google Profile.ico"))));
}
}
- void SetupDefaultProfileShortcut() {
- ASSERT_FALSE(ProfileShortcutExists(dist_, profile_name_));
- // A non-badged shortcut for chrome is automatically created with the
- // first profile (for the case when the user deletes their only
- // profile).
- profile_manager_->profile_info_cache()->AddProfileToCache(
- dest_path_, profile_name_, string16(), 0);
+ void RunPendingTasks() {
MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
MessageLoop::current()->Run();
+ }
+
+ void SetupDefaultProfileShortcut() {
+ ASSERT_FALSE(ProfileShortcutExistsAtDefaultPath(profile_name_));
+ // A non-badged shortcut for chrome is automatically created with the
+ // first profile (for the case when the user deletes their only profile).
+ profile_info_cache_->AddProfileToCache(dest_path_, profile_name_,
+ string16(), 0);
+ RunPendingTasks();
// We now have 1 profile, so we expect a new shortcut with no profile
// information.
- ValidateProfileShortcut(dist_, string16());
+ ValidateProfileShortcut(string16());
}
void SetupAndCreateTwoShortcuts() {
- ASSERT_EQ(0, profile_manager_->profile_info_cache()->GetNumberOfProfiles());
- ASSERT_FALSE(ProfileShortcutExists(dist_, profile_name_));
- ASSERT_FALSE(ProfileShortcutExists(dist_, second_profile_name_));
-
- profile_manager_->profile_info_cache()->AddProfileToCache(
- dest_path_, profile_name_, string16(), 0);
- profile_manager_->profile_info_cache()->AddProfileToCache(
- second_dest_path_, second_profile_name_, string16(), 0);
- profile_manager_->profile_manager()->profile_shortcut_manager()->
- CreateProfileShortcut(second_dest_path_);
- MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
- MessageLoop::current()->Run();
- ValidateProfileShortcut(dist_, profile_name_);
- ValidateProfileShortcut(dist_, second_profile_name_);
+ ASSERT_EQ(0, profile_info_cache_->GetNumberOfProfiles());
+ ASSERT_FALSE(ProfileShortcutExistsAtDefaultPath(profile_name_));
+ ASSERT_FALSE(ProfileShortcutExistsAtDefaultPath(second_profile_name_));
+
+ profile_info_cache_->AddProfileToCache(dest_path_, profile_name_,
+ string16(), 0);
+ profile_info_cache_->AddProfileToCache(second_dest_path_,
+ second_profile_name_, string16(), 0);
+ profile_shortcut_manager_->CreateProfileShortcut(second_dest_path_);
+ RunPendingTasks();
+ ValidateProfileShortcut(profile_name_);
+ ValidateProfileShortcut(second_profile_name_);
+ }
+
+ // Returns the default shortcut path for this profile.
+ FilePath GetDefaultShortcutPathForProfile(const string16& profile_name) {
+ return shortcuts_directory_.Append(
+ ProfileShortcutManager::GetShortcutNameForProfile(profile_name));
+ }
+
+ // Returns true if the shortcut for this profile exists.
+ bool ProfileShortcutExistsAtDefaultPath(const string16& profile_name) {
+ return file_util::PathExists(
+ GetDefaultShortcutPathForProfile(profile_name));
+ }
+
+ // Calls base::win::ValidateShortcut() with expected properties for
+ // |profile_name|'s shortcut.
+ void ValidateProfileShortcut(const string16& profile_name) {
+ EXPECT_TRUE(ProfileShortcutExistsAtDefaultPath(profile_name));
+
+ FilePath shortcut_path = GetDefaultShortcutPathForProfile(profile_name);
+
+ // TODO(asvitkine): With this new struct method for VerifyShortcut you can
+ // now test more properties like: arguments, icon, icon_index, and app_id.
+ base::win::ShortcutProperties expected_properties;
+ expected_properties.set_target(exe_path_);
+ expected_properties.set_description(distribution_->GetAppDescription());
+ expected_properties.set_dual_mode(false);
+ base::win::ValidateShortcut(shortcut_path, expected_properties);
}
- BrowserDistribution* dist_;
+ BrowserDistribution* distribution_;
MessageLoopForUI message_loop_;
content::TestBrowserThread ui_thread_;
content::TestBrowserThread file_thread_;
scoped_ptr<TestingProfileManager> profile_manager_;
+ scoped_ptr<ProfileShortcutManager> profile_shortcut_manager_;
+ ProfileInfoCache* profile_info_cache_;
base::ScopedPathOverride fake_user_desktop_;
+ FilePath exe_path_;
FilePath dest_path_;
+ FilePath shortcuts_directory_;
string16 profile_name_;
FilePath second_dest_path_;
string16 second_profile_name_;
};
-TEST_F(ProfileShortcutManagerTest, UnbadgedShortcutName) {
- EXPECT_EQ(dist_->GetAppShortCutName() + L".lnk",
+TEST_F(ProfileShortcutManagerTest, ShortcutFilename) {
+ const string16 kProfileName = L"Harry";
+ const string16 expected_name = kProfileName + L" - " +
+ distribution_->GetAppShortCutName() + installer::kLnkExt;
+ EXPECT_EQ(expected_name,
+ ProfileShortcutManager::GetShortcutNameForProfile(kProfileName));
+}
+
+TEST_F(ProfileShortcutManagerTest, UnbadgedShortcutFilename) {
+ EXPECT_EQ(distribution_->GetAppShortCutName() + installer::kLnkExt,
ProfileShortcutManager::GetShortcutNameForProfile(string16()));
}
TEST_F(ProfileShortcutManagerTest, DesktopShortcutsCreate) {
- if (!profile_manager_->profile_manager()->profile_shortcut_manager())
- return;
ProfileShortcutManagerTest::SetupDefaultProfileShortcut();
- profile_manager_->profile_info_cache()->AddProfileToCache(
- second_dest_path_, second_profile_name_, string16(), 0);
- profile_manager_->profile_manager()->profile_shortcut_manager()->
- CreateProfileShortcut(second_dest_path_);
- MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
- MessageLoop::current()->Run();
+ profile_info_cache_->AddProfileToCache(second_dest_path_,
+ second_profile_name_, string16(), 0);
+ profile_shortcut_manager_->CreateProfileShortcut(second_dest_path_);
+ RunPendingTasks();
// We now have 2 profiles, so we expect a new shortcut with profile
// information for this 2nd profile.
- ValidateProfileShortcut(dist_, second_profile_name_);
+ ValidateProfileShortcut(second_profile_name_);
ASSERT_TRUE(file_util::PathExists(second_dest_path_.Append(
FILE_PATH_LITERAL("Google Profile.ico"))));
}
TEST_F(ProfileShortcutManagerTest, DesktopShortcutsUpdate) {
- if (!profile_manager_->profile_manager()->profile_shortcut_manager())
- return;
ProfileShortcutManagerTest::SetupDefaultProfileShortcut();
- EXPECT_FALSE(ProfileShortcutExists(dist_, second_profile_name_));
+ EXPECT_FALSE(ProfileShortcutExistsAtDefaultPath(second_profile_name_));
- profile_manager_->profile_info_cache()->AddProfileToCache(
- second_dest_path_, second_profile_name_, string16(), 0);
- profile_manager_->profile_manager()->profile_shortcut_manager()->
- CreateProfileShortcut(second_dest_path_);
- MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
- MessageLoop::current()->Run();
- ValidateProfileShortcut(dist_, second_profile_name_);
+ profile_info_cache_->AddProfileToCache(second_dest_path_,
+ second_profile_name_, string16(), 0);
+ profile_shortcut_manager_->CreateProfileShortcut(second_dest_path_);
+ RunPendingTasks();
+ ValidateProfileShortcut(second_profile_name_);
// Cause an update in ProfileShortcutManager by modifying the profile info
// cache.
string16 new_profile_name = ASCIIToUTF16("New Profile Name");
- profile_manager_->profile_info_cache()->SetNameOfProfileAtIndex(
- profile_manager_->profile_info_cache()->GetIndexOfProfileWithPath(
- second_dest_path_),
+ profile_info_cache_->SetNameOfProfileAtIndex(
+ profile_info_cache_->GetIndexOfProfileWithPath(second_dest_path_),
new_profile_name);
- MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
- MessageLoop::current()->Run();
- EXPECT_FALSE(ProfileShortcutExists(dist_, second_profile_name_));
- ValidateProfileShortcut(dist_, new_profile_name);
+ RunPendingTasks();
+ EXPECT_FALSE(ProfileShortcutExistsAtDefaultPath(second_profile_name_));
+ ValidateProfileShortcut(new_profile_name);
}
TEST_F(ProfileShortcutManagerTest, DesktopShortcutsDeleteSecondToLast) {
- if (!profile_manager_->profile_manager()->profile_shortcut_manager())
- return;
ProfileShortcutManagerTest::SetupAndCreateTwoShortcuts();
- // Delete one shortcut
- profile_manager_->profile_info_cache()->DeleteProfileFromCache(
- second_dest_path_);
- MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
- MessageLoop::current()->Run();
- EXPECT_FALSE(ProfileShortcutExists(dist_, second_profile_name_));
+ // Delete one shortcut.
+ profile_info_cache_->DeleteProfileFromCache(second_dest_path_);
+ RunPendingTasks();
+ EXPECT_FALSE(ProfileShortcutExistsAtDefaultPath(second_profile_name_));
- // Verify that the profile name has been removed from the remaining shortcut
- ValidateProfileShortcut(dist_, string16());
+ // Verify that the profile name has been removed from the remaining shortcut.
+ ValidateProfileShortcut(string16());
// Verify that an additional shortcut, with the default profile's name does
- // not exist
- EXPECT_FALSE(ProfileShortcutExists(dist_, profile_name_));
+ // not exist.
+ EXPECT_FALSE(ProfileShortcutExistsAtDefaultPath(profile_name_));
}
TEST_F(ProfileShortcutManagerTest, DesktopShortcutsCreateSecond) {
- if (!profile_manager_->profile_manager()->profile_shortcut_manager())
- return;
ProfileShortcutManagerTest::SetupAndCreateTwoShortcuts();
- // Delete one shortcut
- profile_manager_->profile_info_cache()->DeleteProfileFromCache(
- second_dest_path_);
- MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
- MessageLoop::current()->Run();
+ // Delete one shortcut.
+ profile_info_cache_->DeleteProfileFromCache(second_dest_path_);
+ RunPendingTasks();
- // Verify that a default shortcut exists (no profile name/avatar)
- ValidateProfileShortcut(dist_, string16());
+ // Verify that a default shortcut exists (no profile name/avatar).
+ ValidateProfileShortcut(string16());
// Verify that an additional shortcut, with the default profile's name does
- // not exist
- EXPECT_FALSE(ProfileShortcutExists(dist_, profile_name_));
-
- // Create a second profile and shortcut
- profile_manager_->profile_info_cache()->AddProfileToCache(
- second_dest_path_, second_profile_name_, string16(), 0);
- profile_manager_->profile_manager()->profile_shortcut_manager()->
- CreateProfileShortcut(second_dest_path_);
- MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
- MessageLoop::current()->Run();
- ValidateProfileShortcut(dist_, second_profile_name_);
-
- // Verify that the original shortcut received the profile's name
- ValidateProfileShortcut(dist_, profile_name_);
- // Verify that a default shortcut no longer exists
- EXPECT_FALSE(ProfileShortcutExists(dist_, string16()));
+ // not exist.
+ EXPECT_FALSE(ProfileShortcutExistsAtDefaultPath(profile_name_));
+
+ // Create a second profile and shortcut.
+ profile_info_cache_->AddProfileToCache(second_dest_path_,
+ second_profile_name_, string16(), 0);
+ profile_shortcut_manager_->CreateProfileShortcut(second_dest_path_);
+ RunPendingTasks();
+ ValidateProfileShortcut(second_profile_name_);
+
+ // Verify that the original shortcut received the profile's name.
+ ValidateProfileShortcut(profile_name_);
+ // Verify that a default shortcut no longer exists.
+ EXPECT_FALSE(ProfileShortcutExistsAtDefaultPath(string16()));
+}
+
+TEST_F(ProfileShortcutManagerTest, RenamedDesktopShortcuts) {
+ ProfileShortcutManagerTest::SetupAndCreateTwoShortcuts();
+
+ const FilePath old_shortcut_path =
+ GetDefaultShortcutPathForProfile(second_profile_name_);
+ const FilePath new_shortcut_path =
+ shortcuts_directory_.Append(L"MyChrome.lnk");
+ ASSERT_TRUE(file_util::Move(old_shortcut_path, new_shortcut_path));
+
+ // Ensure that a new shortcut does not get made if the old one was renamed.
+ profile_shortcut_manager_->CreateProfileShortcut(second_dest_path_);
+ RunPendingTasks();
+ EXPECT_FALSE(ProfileShortcutExistsAtDefaultPath(second_profile_name_));
+ EXPECT_TRUE(file_util::PathExists(new_shortcut_path));
+
+ // Delete the renamed shortcut and try to create it again, which should work.
+ ASSERT_TRUE(file_util::Delete(new_shortcut_path, false));
+ EXPECT_FALSE(file_util::PathExists(new_shortcut_path));
+ profile_shortcut_manager_->CreateProfileShortcut(second_dest_path_);
+ RunPendingTasks();
+ EXPECT_TRUE(ProfileShortcutExistsAtDefaultPath(second_profile_name_));
}
+
+TEST_F(ProfileShortcutManagerTest, RenamedDesktopShortcutsGetDeleted) {
+ ProfileShortcutManagerTest::SetupAndCreateTwoShortcuts();
+
+ const FilePath old_shortcut_path =
+ GetDefaultShortcutPathForProfile(second_profile_name_);
+ const FilePath new_shortcut_path =
+ shortcuts_directory_.Append(L"MyChrome.lnk");
+ // Make a copy of the shortcut.
+ ASSERT_TRUE(file_util::CopyFile(old_shortcut_path, new_shortcut_path));
+ EXPECT_TRUE(file_util::PathExists(old_shortcut_path));
+ EXPECT_TRUE(file_util::PathExists(new_shortcut_path));
+
+ // Also, copy the shortcut for the first user and ensure it gets preserved.
+ const FilePath preserved_shortcut_path =
+ shortcuts_directory_.Append(L"Preserved.lnk");
+ ASSERT_TRUE(file_util::CopyFile(
+ GetDefaultShortcutPathForProfile(profile_name_),
+ preserved_shortcut_path));
+ EXPECT_TRUE(file_util::PathExists(preserved_shortcut_path));
+
+ // Delete the profile and ensure both shortcuts were also deleted.
+ profile_info_cache_->DeleteProfileFromCache(second_dest_path_);
+ RunPendingTasks();
+ EXPECT_FALSE(file_util::PathExists(old_shortcut_path));
+ EXPECT_FALSE(file_util::PathExists(new_shortcut_path));
+ EXPECT_TRUE(file_util::PathExists(preserved_shortcut_path));
+}
+
+TEST_F(ProfileShortcutManagerTest, RenamedDesktopShortcutsAfterProfileRename) {
+ ProfileShortcutManagerTest::SetupAndCreateTwoShortcuts();
+
+ const FilePath old_shortcut_path =
+ GetDefaultShortcutPathForProfile(second_profile_name_);
+ const FilePath new_shortcut_path =
+ shortcuts_directory_.Append(L"MyChrome.lnk");
+
+ // Make a copy of the shortcut.
+ ASSERT_TRUE(file_util::CopyFile(old_shortcut_path, new_shortcut_path));
+ EXPECT_TRUE(file_util::PathExists(old_shortcut_path));
+ EXPECT_TRUE(file_util::PathExists(new_shortcut_path));
+
+ // Now, rename the profile.
+ const string16 new_profile_name = ASCIIToUTF16("New profile");
+ ASSERT_NE(second_profile_name_, new_profile_name);
+ profile_info_cache_->SetNameOfProfileAtIndex(
+ profile_info_cache_->GetIndexOfProfileWithPath(second_dest_path_),
+ new_profile_name);
+ RunPendingTasks();
+
+ // The original shortcut should be renamed but the copied shortcut should
+ // keep its name.
+ EXPECT_FALSE(file_util::PathExists(old_shortcut_path));
+ EXPECT_TRUE(file_util::PathExists(new_shortcut_path));
+ const FilePath updated_shortcut_path =
+ GetDefaultShortcutPathForProfile(new_profile_name);
+ EXPECT_TRUE(file_util::PathExists(updated_shortcut_path));
+}
+