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 |
50 // observers is the same. | 54 // observers is the same. |
51 Profile* g_created_profile; | 55 Profile* g_created_profile; |
52 | 56 |
57 // Profile shortcut manager for desktop shortcuts | |
58 ProfileShortcutManager* profile_shortcut_manager; | |
sail
2012/08/13 22:09:36
you don't need a global for this, this can just be
Halli
2012/08/14 18:25:57
Kept as a global in the new test class profile_sho
| |
59 | |
53 } // namespace | 60 } // namespace |
54 | 61 |
55 namespace testing { | 62 namespace testing { |
56 | 63 |
57 class ProfileManager : public ::ProfileManagerWithoutInit { | 64 class ProfileManager : public ::ProfileManagerWithoutInit { |
58 public: | 65 public: |
59 explicit ProfileManager(const FilePath& user_data_dir) | 66 explicit ProfileManager(const FilePath& user_data_dir) |
60 : ::ProfileManagerWithoutInit(user_data_dir) {} | 67 : ::ProfileManagerWithoutInit(user_data_dir) {} |
61 | 68 |
62 protected: | 69 protected: |
63 virtual Profile* CreateProfileHelper(const FilePath& file_path) OVERRIDE { | 70 virtual Profile* CreateProfileHelper(const FilePath& file_path) OVERRIDE { |
64 if (!file_util::PathExists(file_path)) { | 71 if (!file_util::PathExists(file_path)) { |
65 if (!file_util::CreateDirectory(file_path)) | 72 if (!file_util::CreateDirectory(file_path)) |
66 return NULL; | 73 return NULL; |
67 } | 74 } |
68 return new TestingProfile(file_path, NULL); | 75 return new TestingProfile(file_path, NULL); |
69 } | 76 } |
70 | 77 |
71 virtual Profile* CreateProfileAsyncHelper(const FilePath& path, | 78 virtual Profile* CreateProfileAsyncHelper(const FilePath& path, |
72 Delegate* delegate) OVERRIDE { | 79 Delegate* delegate) OVERRIDE { |
73 // This is safe while all file operations are done on the FILE thread. | 80 // This is safe while all file operations are done on the FILE thread. |
74 BrowserThread::PostTask( | 81 BrowserThread::PostTask( |
75 BrowserThread::FILE, FROM_HERE, | 82 BrowserThread::FILE, FROM_HERE, |
76 base::Bind(base::IgnoreResult(&file_util::CreateDirectory), path)); | 83 base::Bind(base::IgnoreResult(&file_util::CreateDirectory), path)); |
77 | 84 |
78 return new TestingProfile(path, this); | 85 return new TestingProfile(path, this); |
79 } | 86 } |
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 }; | 87 }; |
88 | 88 |
89 } // namespace testing | 89 } // namespace testing |
90 | 90 |
91 class ProfileManagerTest : public testing::Test { | 91 class ProfileManagerTest : public testing::Test { |
92 protected: | 92 protected: |
93 class MockObserver { | 93 class MockObserver { |
94 public: | 94 public: |
95 MOCK_METHOD2(OnProfileCreated, | 95 MOCK_METHOD2(OnProfileCreated, |
96 void(Profile* profile, Profile::CreateStatus status)); | 96 void(Profile* profile, Profile::CreateStatus status)); |
(...skipping 13 matching lines...) Expand all Loading... | |
110 system_monitor_dummy_.reset(new base::SystemMonitor); | 110 system_monitor_dummy_.reset(new base::SystemMonitor); |
111 static_cast<TestingBrowserProcess*>(g_browser_process)->SetIOThread( | 111 static_cast<TestingBrowserProcess*>(g_browser_process)->SetIOThread( |
112 &io_thread_); | 112 &io_thread_); |
113 } | 113 } |
114 | 114 |
115 virtual void SetUp() { | 115 virtual void SetUp() { |
116 // Create a new temporary directory, and store the path | 116 // Create a new temporary directory, and store the path |
117 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 117 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
118 static_cast<TestingBrowserProcess*>(g_browser_process)->SetProfileManager( | 118 static_cast<TestingBrowserProcess*>(g_browser_process)->SetProfileManager( |
119 new testing::ProfileManager(temp_dir_.path())); | 119 new testing::ProfileManager(temp_dir_.path())); |
120 | |
121 // Profile shortcut manager will be NULL for non-windows platforms | |
122 profile_shortcut_manager = ProfileShortcutManager::Create(); | |
123 | |
120 #if defined(OS_CHROMEOS) | 124 #if defined(OS_CHROMEOS) |
121 CommandLine *cl = CommandLine::ForCurrentProcess(); | 125 CommandLine* cl = CommandLine::ForCurrentProcess(); |
122 cl->AppendSwitch(switches::kTestType); | 126 cl->AppendSwitch(switches::kTestType); |
123 #endif | 127 #endif |
124 } | 128 } |
125 | 129 |
126 virtual void TearDown() { | 130 virtual void TearDown() { |
127 static_cast<TestingBrowserProcess*>(g_browser_process)->SetProfileManager( | 131 static_cast<TestingBrowserProcess*>(g_browser_process)->SetProfileManager( |
128 NULL); | 132 NULL); |
129 message_loop_.RunAllPending(); | 133 message_loop_.RunAllPending(); |
130 } | 134 } |
131 | 135 |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
280 EXPECT_CALL(mock_observer2, OnProfileCreated( | 284 EXPECT_CALL(mock_observer2, OnProfileCreated( |
281 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); | 285 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
282 MockObserver mock_observer3; | 286 MockObserver mock_observer3; |
283 EXPECT_CALL(mock_observer3, OnProfileCreated( | 287 EXPECT_CALL(mock_observer3, OnProfileCreated( |
284 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); | 288 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
285 | 289 |
286 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 290 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
287 | 291 |
288 profile_manager->CreateProfileAsync(dest_path, | 292 profile_manager->CreateProfileAsync(dest_path, |
289 base::Bind(&MockObserver::OnProfileCreated, | 293 base::Bind(&MockObserver::OnProfileCreated, |
290 base::Unretained(&mock_observer1)), string16(), string16()); | 294 base::Unretained(&mock_observer1)), |
295 string16(), string16()); | |
291 profile_manager->CreateProfileAsync(dest_path, | 296 profile_manager->CreateProfileAsync(dest_path, |
292 base::Bind(&MockObserver::OnProfileCreated, | 297 base::Bind(&MockObserver::OnProfileCreated, |
293 base::Unretained(&mock_observer2)), string16(), string16()); | 298 base::Unretained(&mock_observer2)), |
299 string16(), string16()); | |
294 profile_manager->CreateProfileAsync(dest_path, | 300 profile_manager->CreateProfileAsync(dest_path, |
295 base::Bind(&MockObserver::OnProfileCreated, | 301 base::Bind(&MockObserver::OnProfileCreated, |
296 base::Unretained(&mock_observer3)), string16(), string16()); | 302 base::Unretained(&mock_observer3)), |
303 string16(), string16()); | |
297 | 304 |
298 message_loop_.RunAllPending(); | 305 message_loop_.RunAllPending(); |
299 } | 306 } |
300 | 307 |
301 TEST_F(ProfileManagerTest, CreateProfilesAsync) { | 308 TEST_F(ProfileManagerTest, CreateProfilesAsync) { |
302 FilePath dest_path1 = | 309 FilePath dest_path1 = |
303 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 1")); | 310 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 1")); |
304 FilePath dest_path2 = | 311 FilePath dest_path2 = |
305 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 2")); | 312 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 2")); |
306 | 313 |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
562 browser2b.reset(); | 569 browser2b.reset(); |
563 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); | 570 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
564 ASSERT_EQ(1U, last_opened_profiles.size()); | 571 ASSERT_EQ(1U, last_opened_profiles.size()); |
565 EXPECT_EQ(profile1, last_opened_profiles[0]); | 572 EXPECT_EQ(profile1, last_opened_profiles[0]); |
566 | 573 |
567 browser1.reset(); | 574 browser1.reset(); |
568 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); | 575 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
569 ASSERT_EQ(0U, last_opened_profiles.size()); | 576 ASSERT_EQ(0U, last_opened_profiles.size()); |
570 } | 577 } |
571 #endif // !defined(OS_ANDROID) | 578 #endif // !defined(OS_ANDROID) |
579 | |
580 TEST_F(ProfileManagerTest, WinDesktopShortcutsIconExists) { | |
sail
2012/08/13 22:09:36
This isn't related to profile manager so it should
Halli
2012/08/14 18:25:57
Done.
| |
581 if (!profile_shortcut_manager) | |
582 return; | |
583 | |
584 FilePath dest_path = temp_dir_.path(); | |
585 string16 profile_name = ASCIIToUTF16("My Profile"); | |
586 | |
587 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
588 | |
589 gfx::Image& avatar = ResourceBundle::GetSharedInstance(). | |
590 GetNativeImageNamed(profile_manager->GetProfileInfoCache(). | |
591 GetDefaultAvatarIconResourceIDAtIndex(0)); | |
592 | |
593 profile_shortcut_manager->CreateChromeDesktopShortcut(dest_path, | |
594 profile_name, avatar); | |
595 | |
596 ASSERT_TRUE(file_util::ContainsPath(temp_dir_.path(), dest_path.Append( | |
sail
2012/08/13 22:14:35
maybe file_util::PathExists() instead of ContainsP
Halli
2012/08/14 18:25:57
Done.
| |
597 (FILE_PATH_LITERAL("Google Profile.ico"))))); | |
598 | |
599 profile_shortcut_manager->DeleteChromeDesktopShortcut(dest_path); | |
sail
2012/08/13 22:14:35
maybe test that the file no longer exists?
Halli
2012/08/14 18:25:57
TODO in separate CL
| |
600 } | |
601 | |
602 TEST_F(ProfileManagerTest, WinDesktopShortcutsLnk) { | |
603 if (!profile_shortcut_manager) | |
604 return; | |
605 | |
606 FilePath dest_path = temp_dir_.path(); | |
607 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile 1")); | |
608 | |
609 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
610 | |
611 // Successfully create the profile. | |
612 TestingProfile* profile = | |
613 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path)); | |
614 ASSERT_TRUE(profile); | |
615 | |
616 profile->SetName(ASCIIToWide("My Profile")); | |
617 | |
618 gfx::Image& avatar = ResourceBundle::GetSharedInstance(). | |
619 GetNativeImageNamed(profile_manager->GetProfileInfoCache(). | |
620 GetDefaultAvatarIconResourceIDAtIndex(0)); | |
621 | |
622 profile_shortcut_manager->CreateChromeDesktopShortcut(dest_path, | |
623 profile->GetName(), avatar); | |
624 | |
625 FilePath exe_path; | |
626 ASSERT_TRUE(PathService::Get(base::FILE_EXE, &exe_path)); | |
627 | |
628 FilePath shortcut; | |
629 string16 shortcut_name; | |
630 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
631 | |
632 // Get the desktop path of the current user | |
633 ShellUtil::GetDesktopPath(false, &shortcut); | |
634 // Get the name of the shortcut with profile attached | |
635 ShellUtil::GetChromeShortcutName(dist, false, profile->GetName(), | |
636 &shortcut_name); | |
637 shortcut = shortcut.Append(shortcut_name); | |
638 | |
639 EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS, | |
640 ShellUtil::VerifyChromeShortcut(exe_path.value(), | |
641 shortcut.value(), dist->GetAppDescription(), 0)); | |
642 | |
643 profile_shortcut_manager->DeleteChromeDesktopShortcut(dest_path); | |
644 } | |
645 | |
OLD | NEW |