Chromium Code Reviews| 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 "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
| 7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "base/scoped_temp_dir.h" | 8 #include "base/scoped_temp_dir.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/profiles/profile_shortcut_manager.h" | 11 #include "chrome/browser/profiles/profile_shortcut_manager.h" |
| 12 #include "chrome/installer/util/browser_distribution.h" | 12 #include "chrome/installer/util/browser_distribution.h" |
| 13 #include "chrome/installer/util/shell_util.h" | 13 #include "chrome/installer/util/shell_util.h" |
| 14 #include "chrome/test/base/testing_browser_process.h" | |
| 14 #include "chrome/test/base/testing_pref_service.h" | 15 #include "chrome/test/base/testing_pref_service.h" |
| 16 #include "chrome/test/base/testing_profile.h" | |
| 17 #include "chrome/test/base/testing_profile_manager.h" | |
| 15 #include "content/public/test/test_browser_thread.h" | 18 #include "content/public/test/test_browser_thread.h" |
| 16 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
| 19 | 22 |
| 20 using content::BrowserThread; | 23 using content::BrowserThread; |
| 21 | 24 |
| 25 namespace { | |
| 26 | |
| 27 bool ProfileShortcutExists(const string16& profile_name) { | |
|
gab
2012/08/24 19:17:32
Rename this to VerifyProfileShortcut and have it
Halli
2012/08/24 19:54:24
Done.
| |
| 28 FilePath exe_path; | |
| 29 CHECK(PathService::Get(base::FILE_EXE, &exe_path)); | |
| 30 | |
| 31 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
| 32 | |
| 33 // Get the desktop path of the current user. | |
| 34 FilePath shortcut_path; | |
| 35 ShellUtil::GetDesktopPath(false, &shortcut_path); | |
| 36 // Get the name of the shortcut with profile attached | |
| 37 string16 shortcut_name; | |
| 38 ShellUtil::GetChromeShortcutName(dist, false, profile_name, | |
| 39 &shortcut_name); | |
|
gab
2012/08/24 19:17:33
nit: indent this to align with |dist|.
Halli
2012/08/24 19:54:24
Done.
| |
| 40 shortcut_path = shortcut_path.Append(shortcut_name); | |
| 41 | |
| 42 return ShellUtil::VERIFY_SHORTCUT_SUCCESS == | |
| 43 ShellUtil::VerifyChromeShortcut( | |
| 44 exe_path.value(), shortcut_path.value(), dist->GetAppDescription(), | |
| 45 0); | |
| 46 } | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 22 class ProfileShortcutManagerTest : public testing::Test { | 50 class ProfileShortcutManagerTest : public testing::Test { |
| 23 protected: | 51 protected: |
| 24 ProfileShortcutManagerTest() | 52 ProfileShortcutManagerTest() |
| 25 : file_thread_(BrowserThread::FILE, &message_loop_) { | 53 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 54 file_thread_(BrowserThread::FILE, &message_loop_) { | |
| 26 } | 55 } |
| 27 | 56 |
| 28 virtual void SetUp() { | 57 virtual void SetUp() { |
| 29 // Create a new temporary directory, and store the path | 58 TestingBrowserProcess* browser_process = |
| 30 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 59 static_cast<TestingBrowserProcess*>(g_browser_process); |
| 60 profile_manager_.reset(new TestingProfileManager(browser_process)); | |
| 61 ASSERT_TRUE(profile_manager_->SetUp()); | |
| 62 ProfileInfoCache* cache = profile_manager_->profile_info_cache(); | |
| 63 // Profile shortcut manager will be NULL for non-windows platforms | |
| 64 profile_shortcut_manager_.reset(ProfileShortcutManager::Create(*cache)); | |
| 31 } | 65 } |
| 32 | 66 |
| 33 virtual void TearDown() { | 67 virtual void TearDown() { |
| 34 message_loop_.RunAllPending(); | 68 message_loop_.RunAllPending(); |
| 35 } | 69 } |
| 36 | 70 |
| 37 // The path to temporary directory used to contain the test operations. | |
| 38 ScopedTempDir temp_dir_; | |
| 39 | |
| 40 MessageLoopForUI message_loop_; | 71 MessageLoopForUI message_loop_; |
| 72 content::TestBrowserThread ui_thread_; | |
| 41 content::TestBrowserThread file_thread_; | 73 content::TestBrowserThread file_thread_; |
| 74 scoped_ptr<TestingProfileManager> profile_manager_; | |
| 75 scoped_ptr<ProfileShortcutManager> profile_shortcut_manager_; | |
| 42 }; | 76 }; |
| 43 | 77 |
| 44 TEST_F(ProfileShortcutManagerTest, DesktopShortcutsIconExists) { | 78 TEST_F(ProfileShortcutManagerTest, DesktopShortcutsCreate) { |
| 45 // Profile shortcut manager will be NULL for non-windows platforms | 79 if (!profile_shortcut_manager_.get()) |
| 46 ProfileShortcutManager* profile_shortcut_manager = | 80 return; |
| 47 ProfileShortcutManager::Create(); | 81 FilePath dest_path = profile_manager_->profile_info_cache()->GetUserDataDir(); |
| 82 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile 1")); | |
| 83 file_util::CreateDirectoryW(dest_path); | |
| 84 string16 profile_name = ASCIIToUTF16("My profile"); | |
| 48 | 85 |
| 49 if (!profile_shortcut_manager) | 86 EXPECT_FALSE(ProfileShortcutExists(profile_name)); |
| 50 return; | 87 ASSERT_FALSE(file_util::PathExists(dest_path.Append( |
| 51 | 88 FILE_PATH_LITERAL("Google Profile.ico")))); |
| 52 FilePath dest_path = temp_dir_.path(); | |
| 53 string16 profile_name = ASCIIToUTF16("My Profile"); | |
| 54 | 89 |
| 55 gfx::Image& avatar = ResourceBundle::GetSharedInstance(). | 90 gfx::Image& avatar = ResourceBundle::GetSharedInstance(). |
| 56 GetNativeImageNamed(IDR_PROFILE_AVATAR_0); | 91 GetNativeImageNamed(IDR_PROFILE_AVATAR_0); |
| 57 | 92 |
| 58 profile_shortcut_manager->CreateChromeDesktopShortcut(dest_path, | 93 profile_shortcut_manager_->StartProfileDesktopShortcutCreation( |
| 59 profile_name, avatar); | 94 dest_path, profile_name, avatar); |
| 95 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | |
| 96 MessageLoop::current()->Run(); | |
| 60 | 97 |
| 98 EXPECT_TRUE(ProfileShortcutExists(profile_name)); | |
| 61 ASSERT_TRUE(file_util::PathExists(dest_path.Append( | 99 ASSERT_TRUE(file_util::PathExists(dest_path.Append( |
| 62 (FILE_PATH_LITERAL("Google Profile.ico"))))); | 100 (FILE_PATH_LITERAL("Google Profile.ico"))))); |
| 63 | 101 |
| 64 profile_shortcut_manager->DeleteChromeDesktopShortcut(dest_path); | 102 profile_shortcut_manager_->DeleteProfileDesktopShortcut( |
| 65 | 103 dest_path, profile_name); |
| 66 // TODO(hallielaine): Verify shortcut deletion | 104 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 105 MessageLoop::current()->Run(); | |
| 106 EXPECT_FALSE(ProfileShortcutExists(profile_name)); | |
| 107 ASSERT_FALSE(file_util::PathExists(dest_path.Append( | |
| 108 (FILE_PATH_LITERAL("Google Profile.ico"))))); | |
|
gab
2012/08/24 19:17:33
nit: Remove extra brackets around FILE_PATH_LITERA
Halli
2012/08/24 19:54:24
Done.
| |
| 67 } | 109 } |
| 68 | 110 |
| 69 TEST_F(ProfileShortcutManagerTest, DesktopShortcutsLnk) { | 111 TEST_F(ProfileShortcutManagerTest, DesktopShortcutsUpdate) { |
| 70 // Profile shortcut manager will be NULL for non-windows platforms | 112 if (!profile_shortcut_manager_.get()) |
| 71 ProfileShortcutManager* profile_shortcut_manager = | 113 return; |
| 72 ProfileShortcutManager::Create(); | 114 string16 profile_name = ASCIIToUTF16("My profile"); |
| 115 EXPECT_FALSE(ProfileShortcutExists(profile_name)); | |
| 73 | 116 |
| 74 if (!profile_shortcut_manager) | 117 FilePath dest_path = profile_manager_->profile_info_cache()->GetUserDataDir(); |
| 75 return; | 118 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile 1")); |
| 119 file_util::CreateDirectoryW(dest_path); | |
| 76 | 120 |
| 77 FilePath dest_path = temp_dir_.path(); | 121 profile_manager_->profile_info_cache()->AddProfileToCache( |
| 78 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile 1")); | 122 dest_path, profile_name, string16(), 0); |
| 79 | |
| 80 gfx::Image& avatar = ResourceBundle::GetSharedInstance(). | 123 gfx::Image& avatar = ResourceBundle::GetSharedInstance(). |
| 81 GetNativeImageNamed(IDR_PROFILE_AVATAR_0); | 124 GetNativeImageNamed(IDR_PROFILE_AVATAR_0); |
| 82 | 125 |
| 83 profile_shortcut_manager->CreateChromeDesktopShortcut(dest_path, | 126 profile_shortcut_manager_->StartProfileDesktopShortcutCreation( |
| 84 ASCIIToUTF16("My Profile"), avatar); | 127 dest_path, profile_name, avatar); |
| 128 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | |
| 129 MessageLoop::current()->Run(); | |
| 130 EXPECT_TRUE(ProfileShortcutExists(profile_name)); | |
| 131 string16 new_profile_name = ASCIIToUTF16("My New Profile Name"); | |
| 132 EXPECT_FALSE(ProfileShortcutExists(new_profile_name)); | |
| 85 | 133 |
| 86 FilePath exe_path; | 134 // Cause an update in ProfileShortcutManager by modifying the profile info |
| 87 ASSERT_TRUE(PathService::Get(base::FILE_EXE, &exe_path)); | 135 // cache |
| 136 profile_manager_->profile_info_cache()->SetNameOfProfileAtIndex( | |
| 137 profile_manager_->profile_info_cache()->GetIndexOfProfileWithPath( | |
| 138 dest_path), new_profile_name); | |
| 139 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | |
| 140 MessageLoop::current()->Run(); | |
| 141 EXPECT_FALSE(ProfileShortcutExists(profile_name)); | |
| 142 EXPECT_TRUE(ProfileShortcutExists(new_profile_name)); | |
| 88 | 143 |
| 89 FilePath shortcut; | 144 profile_shortcut_manager_->DeleteProfileDesktopShortcut( |
| 90 string16 shortcut_name; | 145 dest_path, new_profile_name); |
| 91 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 146 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 147 MessageLoop::current()->Run(); | |
| 148 EXPECT_FALSE(ProfileShortcutExists(profile_name)); | |
| 149 EXPECT_FALSE(ProfileShortcutExists(new_profile_name)); | |
| 150 } | |
| 92 | 151 |
| 93 // Get the desktop path of the current user | |
| 94 ShellUtil::GetDesktopPath(false, &shortcut); | |
| 95 // Get the name of the shortcut with profile attached | |
| 96 ShellUtil::GetChromeShortcutName(dist, false, ASCIIToUTF16("My Profile"), | |
| 97 &shortcut_name); | |
| 98 shortcut = shortcut.Append(shortcut_name); | |
| 99 | |
| 100 EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS, | |
| 101 ShellUtil::VerifyChromeShortcut(exe_path.value(), | |
| 102 shortcut.value(), dist->GetAppDescription(), 0)); | |
| 103 | |
| 104 profile_shortcut_manager->DeleteChromeDesktopShortcut(dest_path); | |
| 105 } | |
| OLD | NEW |