| 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/scoped_temp_dir.h" | 11 #include "base/scoped_temp_dir.h" |
| 12 #include "base/system_monitor/system_monitor.h" | 12 #include "base/system_monitor/system_monitor.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 16 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 16 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 17 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
| 18 #include "chrome/browser/extensions/event_router_forwarder.h" | 18 #include "chrome/browser/extensions/event_router_forwarder.h" |
| 19 #include "chrome/browser/history/history.h" | 19 #include "chrome/browser/history/history.h" |
| 20 #include "chrome/browser/history/history_service_factory.h" | 20 #include "chrome/browser/history/history_service_factory.h" |
| 21 #include "chrome/browser/io_thread.h" | 21 #include "chrome/browser/io_thread.h" |
| 22 #include "chrome/browser/prefs/browser_prefs.h" | 22 #include "chrome/browser/prefs/browser_prefs.h" |
| 23 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 24 #include "chrome/browser/profiles/profile_info_cache.h" | 24 #include "chrome/browser/profiles/profile_info_cache.h" |
| 25 #include "chrome/browser/profiles/profile_manager.h" | 25 #include "chrome/browser/profiles/profile_manager.h" |
| 26 #include "chrome/browser/profiles/profile_shortcut_manager.h" |
| 26 #include "chrome/browser/ui/browser.h" | 27 #include "chrome/browser/ui/browser.h" |
| 27 #include "chrome/common/chrome_constants.h" | 28 #include "chrome/common/chrome_constants.h" |
| 28 #include "chrome/common/chrome_notification_types.h" | 29 #include "chrome/common/chrome_notification_types.h" |
| 29 #include "chrome/common/chrome_paths.h" | 30 #include "chrome/common/chrome_paths.h" |
| 30 #include "chrome/common/chrome_switches.h" | 31 #include "chrome/common/chrome_switches.h" |
| 31 #include "chrome/common/pref_names.h" | 32 #include "chrome/common/pref_names.h" |
| 33 #include "chrome/installer/util/browser_distribution.h" |
| 34 #include "chrome/installer/util/shell_util.h" |
| 32 #include "chrome/test/base/test_browser_window.h" | 35 #include "chrome/test/base/test_browser_window.h" |
| 33 #include "chrome/test/base/testing_browser_process.h" | 36 #include "chrome/test/base/testing_browser_process.h" |
| 34 #include "chrome/test/base/testing_pref_service.h" | 37 #include "chrome/test/base/testing_pref_service.h" |
| 35 #include "chrome/test/base/testing_profile.h" | 38 #include "chrome/test/base/testing_profile.h" |
| 36 #include "content/public/browser/notification_service.h" | 39 #include "content/public/browser/notification_service.h" |
| 37 #include "content/public/test/test_browser_thread.h" | 40 #include "content/public/test/test_browser_thread.h" |
| 38 #include "testing/gmock/include/gmock/gmock.h" | 41 #include "testing/gmock/include/gmock/gmock.h" |
| 39 #include "testing/gtest/include/gtest/gtest.h" | 42 #include "testing/gtest/include/gtest/gtest.h" |
| 43 #include "ui/base/resource/resource_bundle.h" |
| 40 | 44 |
| 41 #if defined(OS_CHROMEOS) | 45 #if defined(OS_CHROMEOS) |
| 42 #include "chrome/browser/chromeos/cros/cros_library.h" | 46 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 43 #endif | 47 #endif |
| 44 | 48 |
| 45 using content::BrowserThread; | 49 using content::BrowserThread; |
| 46 | 50 |
| 47 namespace { | 51 namespace { |
| 48 | 52 |
| 49 // This global variable is used to check that value returned to different | 53 // This global variable is used to check that value returned to different |
| (...skipping 20 matching lines...) Expand all Loading... |
| 70 | 74 |
| 71 virtual Profile* CreateProfileAsyncHelper(const FilePath& path, | 75 virtual Profile* CreateProfileAsyncHelper(const FilePath& path, |
| 72 Delegate* delegate) OVERRIDE { | 76 Delegate* delegate) OVERRIDE { |
| 73 // This is safe while all file operations are done on the FILE thread. | 77 // This is safe while all file operations are done on the FILE thread. |
| 74 BrowserThread::PostTask( | 78 BrowserThread::PostTask( |
| 75 BrowserThread::FILE, FROM_HERE, | 79 BrowserThread::FILE, FROM_HERE, |
| 76 base::Bind(base::IgnoreResult(&file_util::CreateDirectory), path)); | 80 base::Bind(base::IgnoreResult(&file_util::CreateDirectory), path)); |
| 77 | 81 |
| 78 return new TestingProfile(path, this); | 82 return new TestingProfile(path, this); |
| 79 } | 83 } |
| 80 | |
| 81 #if defined(OS_WIN) | |
| 82 virtual ProfileShortcutManagerWin* CreateShortcutManager() OVERRIDE { | |
| 83 // We should avoid creating shortcuts in these tests. | |
| 84 return NULL; | |
| 85 } | |
| 86 #endif | |
| 87 }; | 84 }; |
| 88 | 85 |
| 89 } // namespace testing | 86 } // namespace testing |
| 90 | 87 |
| 91 class ProfileManagerTest : public testing::Test { | 88 class ProfileManagerTest : public testing::Test { |
| 92 protected: | 89 protected: |
| 93 class MockObserver { | 90 class MockObserver { |
| 94 public: | 91 public: |
| 95 MOCK_METHOD2(OnProfileCreated, | 92 MOCK_METHOD2(OnProfileCreated, |
| 96 void(Profile* profile, Profile::CreateStatus status)); | 93 void(Profile* profile, Profile::CreateStatus status)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 114 | 111 |
| 115 virtual void SetUp() { | 112 virtual void SetUp() { |
| 116 // Create a new temporary directory, and store the path | 113 // Create a new temporary directory, and store the path |
| 117 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 114 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 118 static_cast<TestingBrowserProcess*>(g_browser_process)->SetProfileManager( | 115 static_cast<TestingBrowserProcess*>(g_browser_process)->SetProfileManager( |
| 119 new testing::ProfileManager(temp_dir_.path())); | 116 new testing::ProfileManager(temp_dir_.path())); |
| 120 #if defined(OS_CHROMEOS) | 117 #if defined(OS_CHROMEOS) |
| 121 CommandLine *cl = CommandLine::ForCurrentProcess(); | 118 CommandLine *cl = CommandLine::ForCurrentProcess(); |
| 122 cl->AppendSwitch(switches::kTestType); | 119 cl->AppendSwitch(switches::kTestType); |
| 123 #endif | 120 #endif |
| 121 #if defined(OS_WIN) |
| 122 CommandLine *c1 = CommandLine::ForCurrentProcess(); |
| 123 c1->AppendSwitch(switches::kProfileDesktopShortcuts); |
| 124 #endif |
| 124 } | 125 } |
| 125 | 126 |
| 126 virtual void TearDown() { | 127 virtual void TearDown() { |
| 127 static_cast<TestingBrowserProcess*>(g_browser_process)->SetProfileManager( | 128 static_cast<TestingBrowserProcess*>(g_browser_process)->SetProfileManager( |
| 128 NULL); | 129 NULL); |
| 129 message_loop_.RunAllPending(); | 130 message_loop_.RunAllPending(); |
| 130 } | 131 } |
| 131 | 132 |
| 132 #if defined(OS_CHROMEOS) | 133 #if defined(OS_CHROMEOS) |
| 133 // Do not change order of stub_cros_enabler_, which needs to be constructed | 134 // Do not change order of stub_cros_enabler_, which needs to be constructed |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 EXPECT_CALL(mock_observer2, OnProfileCreated( | 281 EXPECT_CALL(mock_observer2, OnProfileCreated( |
| 281 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); | 282 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
| 282 MockObserver mock_observer3; | 283 MockObserver mock_observer3; |
| 283 EXPECT_CALL(mock_observer3, OnProfileCreated( | 284 EXPECT_CALL(mock_observer3, OnProfileCreated( |
| 284 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); | 285 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
| 285 | 286 |
| 286 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 287 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 287 | 288 |
| 288 profile_manager->CreateProfileAsync(dest_path, | 289 profile_manager->CreateProfileAsync(dest_path, |
| 289 base::Bind(&MockObserver::OnProfileCreated, | 290 base::Bind(&MockObserver::OnProfileCreated, |
| 290 base::Unretained(&mock_observer1)), string16(), string16()); | 291 base::Unretained(&mock_observer1)), |
| 292 string16(), string16()); |
| 291 profile_manager->CreateProfileAsync(dest_path, | 293 profile_manager->CreateProfileAsync(dest_path, |
| 292 base::Bind(&MockObserver::OnProfileCreated, | 294 base::Bind(&MockObserver::OnProfileCreated, |
| 293 base::Unretained(&mock_observer2)), string16(), string16()); | 295 base::Unretained(&mock_observer2)), |
| 296 string16(), string16()); |
| 294 profile_manager->CreateProfileAsync(dest_path, | 297 profile_manager->CreateProfileAsync(dest_path, |
| 295 base::Bind(&MockObserver::OnProfileCreated, | 298 base::Bind(&MockObserver::OnProfileCreated, |
| 296 base::Unretained(&mock_observer3)), string16(), string16()); | 299 base::Unretained(&mock_observer3)), |
| 300 string16(), string16()); |
| 297 | 301 |
| 298 message_loop_.RunAllPending(); | 302 message_loop_.RunAllPending(); |
| 299 } | 303 } |
| 300 | 304 |
| 301 TEST_F(ProfileManagerTest, CreateProfilesAsync) { | 305 TEST_F(ProfileManagerTest, CreateProfilesAsync) { |
| 302 FilePath dest_path1 = | 306 FilePath dest_path1 = |
| 303 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 1")); | 307 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 1")); |
| 304 FilePath dest_path2 = | 308 FilePath dest_path2 = |
| 305 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 2")); | 309 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 2")); |
| 306 | 310 |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 562 browser2b.reset(); | 566 browser2b.reset(); |
| 563 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); | 567 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 564 ASSERT_EQ(1U, last_opened_profiles.size()); | 568 ASSERT_EQ(1U, last_opened_profiles.size()); |
| 565 EXPECT_EQ(profile1, last_opened_profiles[0]); | 569 EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 566 | 570 |
| 567 browser1.reset(); | 571 browser1.reset(); |
| 568 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); | 572 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 569 ASSERT_EQ(0U, last_opened_profiles.size()); | 573 ASSERT_EQ(0U, last_opened_profiles.size()); |
| 570 } | 574 } |
| 571 #endif // !defined(OS_ANDROID) | 575 #endif // !defined(OS_ANDROID) |
| 576 |
| 577 #if defined(OS_WIN) |
| 578 TEST_F(ProfileManagerTest, WinDesktopShortcutsIconExists) { |
| 579 FilePath dest_path = temp_dir_.path(); |
| 580 string16 profile_name = ASCIIToUTF16("My Profile"); |
| 581 |
| 582 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 583 |
| 584 gfx::Image& avatar = ResourceBundle::GetSharedInstance(). |
| 585 GetNativeImageNamed(profile_manager->GetProfileInfoCache(). |
| 586 GetDefaultAvatarIconResourceIDAtIndex(0)); |
| 587 |
| 588 ProfileShortcutManager::CreateChromeDesktopShortcut(dest_path, |
| 589 profile_name, avatar); |
| 590 |
| 591 ASSERT_TRUE(file_util::ContainsPath(temp_dir_.path(), dest_path.Append( |
| 592 (FILE_PATH_LITERAL("Google Profile.ico"))))); |
| 593 |
| 594 ProfileShortcutManager::DeleteChromeDesktopShortcut(dest_path); |
| 595 } |
| 596 |
| 597 TEST_F(ProfileManagerTest, WinDesktopShortcutsLnk) { |
| 598 FilePath dest_path = temp_dir_.path(); |
| 599 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile 1")); |
| 600 |
| 601 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 602 |
| 603 // Successfully create the profile. |
| 604 TestingProfile* profile = |
| 605 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path)); |
| 606 ASSERT_TRUE(profile); |
| 607 |
| 608 profile->SetName(ASCIIToWide("My Profile")); |
| 609 |
| 610 gfx::Image& avatar = ResourceBundle::GetSharedInstance(). |
| 611 GetNativeImageNamed(profile_manager->GetProfileInfoCache(). |
| 612 GetDefaultAvatarIconResourceIDAtIndex(0)); |
| 613 |
| 614 ProfileShortcutManager::CreateChromeDesktopShortcut(dest_path, |
| 615 profile->GetName(), avatar); |
| 616 |
| 617 FilePath exe_path; |
| 618 ASSERT_TRUE(PathService::Get(base::FILE_EXE, &exe_path)); |
| 619 |
| 620 FilePath shortcut; |
| 621 string16 shortcut_name; |
| 622 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 623 |
| 624 // Get the desktop path of the current user |
| 625 ShellUtil::GetDesktopPath(false, &shortcut); |
| 626 // Get the name of the shortcut with profile attached |
| 627 ShellUtil::GetChromeShortcutName(dist, false, profile->GetName(), |
| 628 &shortcut_name); |
| 629 shortcut = shortcut.Append(shortcut_name); |
| 630 |
| 631 EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS, |
| 632 ShellUtil::VerifyChromeShortcut(exe_path.value(), |
| 633 shortcut.value(), dist->GetAppDescription(), 0)); |
| 634 |
| 635 ProfileShortcutManager::DeleteChromeDesktopShortcut(dest_path); |
| 636 } |
| 637 #endif // defined(OS_WIN) |
| OLD | NEW |