Chromium Code Reviews| Index: chrome/browser/profiles/profile_shortcut_manager_unittest.cc |
| =================================================================== |
| --- chrome/browser/profiles/profile_shortcut_manager_unittest.cc (revision 0) |
| +++ chrome/browser/profiles/profile_shortcut_manager_unittest.cc (revision 0) |
| @@ -0,0 +1,183 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/file_util.h" |
| +#include "base/path_service.h" |
| +#include "base/scoped_temp_dir.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/extensions/event_router_forwarder.h" |
| +#include "chrome/browser/io_thread.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/profiles/profile_info_cache.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/browser/profiles/profile_shortcut_manager.h" |
| +#include "chrome/installer/util/browser_distribution.h" |
| +#include "chrome/installer/util/shell_util.h" |
| +#include "chrome/test/base/testing_browser_process.h" |
| +#include "chrome/test/base/testing_profile.h" |
| +#include "content/public/test/test_browser_thread.h" |
| +#include "chrome/test/base/testing_pref_service.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace { |
| + |
| + // Profile shortcut manager for desktop shortcuts |
|
sail
2012/08/14 18:40:41
this should just be declared inside the test direc
Halli
2012/08/14 20:38:46
Done.
|
| + ProfileShortcutManager* profile_shortcut_manager; |
| + |
| +} // namespace |
| + |
| +namespace testing { |
| + |
| +class ProfileManager : public ::ProfileManagerWithoutInit { |
|
sail
2012/08/14 18:40:41
you don't actually need a profile manager or a pro
Halli
2012/08/14 20:38:46
Done.
|
| + public: |
| + explicit ProfileManager(const FilePath& user_data_dir) |
| + : ::ProfileManagerWithoutInit(user_data_dir) {} |
| + |
| + protected: |
| + virtual Profile* CreateProfileHelper(const FilePath& file_path) OVERRIDE { |
| + if (!file_util::PathExists(file_path)) { |
| + if (!file_util::CreateDirectory(file_path)) |
| + return NULL; |
| + } |
| + return new TestingProfile(file_path, NULL); |
| + } |
| + |
| + virtual Profile* CreateProfileAsyncHelper(const FilePath& path, |
| + Delegate* delegate) OVERRIDE { |
| + // This is safe while all file operations are done on the FILE thread. |
| + BrowserThread::PostTask( |
| + BrowserThread::FILE, FROM_HERE, |
| + base::Bind(base::IgnoreResult(&file_util::CreateDirectory), path)); |
| + |
| + return new TestingProfile(path, this); |
| + } |
| +}; |
| + |
| +} // namespace testing |
| + |
| +class ProfileShortcutManagerTest : public testing::Test { |
| + protected: |
| + ProfileShortcutManagerTest() |
| + : local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)), |
| + extension_event_router_forwarder_( |
|
sail
2012/08/14 18:40:41
I don't think you need most of this. You probably
Halli
2012/08/14 20:38:46
Done.
|
| + new extensions::EventRouterForwarder), |
| + ui_thread_(BrowserThread::UI, &message_loop_), |
| + db_thread_(BrowserThread::DB, &message_loop_), |
| + file_thread_(BrowserThread::FILE, &message_loop_), |
| + io_thread_(local_state_.Get(), NULL, |
| + extension_event_router_forwarder_) { |
| + system_monitor_dummy_.reset(new base::SystemMonitor); |
| + static_cast<TestingBrowserProcess*>(g_browser_process)->SetIOThread( |
| + &io_thread_); |
| + } |
| + |
| + virtual void SetUp() { |
| + // Create a new temporary directory, and store the path |
| + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| + static_cast<TestingBrowserProcess*>(g_browser_process)->SetProfileManager( |
| + new testing::ProfileManager(temp_dir_.path())); |
| + |
| + // Profile shortcut manager will be NULL for non-windows platforms |
| + profile_shortcut_manager = ProfileShortcutManager::Create(); |
| + } |
| + |
| + virtual void TearDown() { |
| + static_cast<TestingBrowserProcess*>(g_browser_process)->SetProfileManager( |
| + NULL); |
| + message_loop_.RunAllPending(); |
| + } |
| + |
| + // The path to temporary directory used to contain the test operations. |
| + ScopedTempDir temp_dir_; |
| + ScopedTestingLocalState local_state_; |
| + scoped_refptr<extensions::EventRouterForwarder> |
| + extension_event_router_forwarder_; |
| + |
| + MessageLoopForUI message_loop_; |
| + content::TestBrowserThread ui_thread_; |
| + content::TestBrowserThread db_thread_; |
| + content::TestBrowserThread file_thread_; |
| + // IOThread is necessary for the creation of some services below. |
| + IOThread io_thread_; |
| + |
| + scoped_ptr<base::SystemMonitor> system_monitor_dummy_; |
| +}; |
| + |
| +TEST_F(ProfileShortcutManagerTest, DesktopShortcutsIconExists) { |
| + // Profile shortcut manager will be NULL for non-windows platforms |
| + ProfileShortcutManager* profile_shortcut_manager = |
| + ProfileShortcutManager::Create(); |
| + |
| + if (!profile_shortcut_manager) |
| + return; |
| + |
| + FilePath dest_path = temp_dir_.path(); |
| + string16 profile_name = ASCIIToUTF16("My Profile"); |
| + |
| + ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| + |
| + gfx::Image& avatar = ResourceBundle::GetSharedInstance(). |
| + GetNativeImageNamed(profile_manager->GetProfileInfoCache(). |
| + GetDefaultAvatarIconResourceIDAtIndex(0)); |
| + |
| + profile_shortcut_manager->CreateChromeDesktopShortcut(dest_path, |
| + profile_name, avatar); |
| + |
| + ASSERT_TRUE(file_util::PathExists(dest_path.Append( |
| + (FILE_PATH_LITERAL("Google Profile.ico"))))); |
| + |
| + profile_shortcut_manager->DeleteChromeDesktopShortcut(dest_path); |
| +} |
| + |
| +TEST_F(ProfileShortcutManagerTest, DesktopShortcutsLnk) { |
| + // Profile shortcut manager will be NULL for non-windows platforms |
| + ProfileShortcutManager* profile_shortcut_manager = |
| + ProfileShortcutManager::Create(); |
| + |
| + if (!profile_shortcut_manager) |
| + return; |
| + |
| + FilePath dest_path = temp_dir_.path(); |
| + dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile 1")); |
| + |
| + ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| + |
| + // Successfully create the profile. |
| + TestingProfile* profile = |
| + static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path)); |
| + ASSERT_TRUE(profile); |
| + |
| + profile->SetName(ASCIIToWide("My Profile")); |
| + |
| + gfx::Image& avatar = ResourceBundle::GetSharedInstance(). |
| + GetNativeImageNamed(profile_manager->GetProfileInfoCache(). |
| + GetDefaultAvatarIconResourceIDAtIndex(0)); |
| + |
| + profile_shortcut_manager->CreateChromeDesktopShortcut(dest_path, |
| + profile->GetName(), avatar); |
| + |
| + FilePath exe_path; |
| + ASSERT_TRUE(PathService::Get(base::FILE_EXE, &exe_path)); |
| + |
| + FilePath shortcut; |
| + string16 shortcut_name; |
| + BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| + |
| + // Get the desktop path of the current user |
| + ShellUtil::GetDesktopPath(false, &shortcut); |
| + // Get the name of the shortcut with profile attached |
| + ShellUtil::GetChromeShortcutName(dist, false, profile->GetName(), |
| + &shortcut_name); |
| + shortcut = shortcut.Append(shortcut_name); |
| + |
| + EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS, |
| + ShellUtil::VerifyChromeShortcut(exe_path.value(), |
| + shortcut.value(), dist->GetAppDescription(), 0)); |
| + |
| + profile_shortcut_manager->DeleteChromeDesktopShortcut(dest_path); |
| +} |