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 3 // found in the LICENSE file. 4 5 #include "base/bind.h" 6 #include "base/strings/utf_string_conversions.h" 7 #include "base/timer/timer.h" 8 #include "chrome/app/chrome_command_ids.h" 9 #include "chrome/browser/bookmarks/bookmark_model.h" 10 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 11 #include "chrome/browser/bookmarks/bookmark_test_helpers.h" 12 #include "chrome/browser/bookmarks/bookmark_utils.h" 13 #include "chrome/browser/browser_process.h" 14 #include "chrome/browser/chrome_notification_types.h" 15 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile_manager.h" 17 #include "chrome/browser/ui/browser.h" 18 #include "chrome/browser/ui/browser_commands.h" 19 #include "chrome/browser/ui/browser_window.h" 20 #include "chrome/browser/ui/host_desktop.h" 21 #include "chrome/test/base/in_process_browser_test.h" 22 #include "chrome/test/base/ui_test_utils.h" 23 #include "content/public/browser/notification_service.h" 24 #include "content/public/test/browser_test_utils.h" 25 26 namespace { 27 const char kPersistBookmarkURL[] = "http://www.cnn.com/"; 28 const char kPersistBookmarkTitle[] = "CNN"; 29 } 30 31 class BookmarkBrowsertest : public InProcessBrowserTest { 32 public: 33 BookmarkBrowsertest() {} 34 35 bool IsVisible() { 36 return browser()->bookmark_bar_state() == BookmarkBar::SHOW; 37 } 38 39 static void CheckAnimation(Browser* browser, const base::Closure& quit_task) { 40 if (!browser->window()->IsBookmarkBarAnimating()) 41 quit_task.Run(); 42 } 43 44 base::TimeDelta WaitForBookmarkBarAnimationToFinish() { 45 base::Time start(base::Time::Now()); 46 scoped_refptr<content::MessageLoopRunner> runner = 47 new content::MessageLoopRunner; 48 49 base::Timer timer(false, true); 50 timer.Start( 51 FROM_HERE, 52 base::TimeDelta::FromMilliseconds(15), 53 base::Bind(&CheckAnimation, browser(), runner->QuitClosure())); 54 runner->Run(); 55 return base::Time::Now() - start; 56 } 57 58 BookmarkModel* WaitForBookmarkModel(Profile* profile) { 59 BookmarkModel* bookmark_model = 60 BookmarkModelFactory::GetForProfile(profile); 61 test::WaitForBookmarkModelToLoad(bookmark_model); 62 return bookmark_model; 63 } 64 }; 65 66 // Test of bookmark bar toggling, visibility, and animation. 67 IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest, BookmarkBarVisibleWait) { 68 ASSERT_FALSE(IsVisible()); 69 chrome::ExecuteCommand(browser(), IDC_SHOW_BOOKMARK_BAR); 70 base::TimeDelta delay = WaitForBookmarkBarAnimationToFinish(); 71 LOG(INFO) << "Took " << delay.InMilliseconds() << " ms to show bookmark bar"; 72 ASSERT_TRUE(IsVisible()); 73 chrome::ExecuteCommand(browser(), IDC_SHOW_BOOKMARK_BAR); 74 delay = WaitForBookmarkBarAnimationToFinish(); 75 LOG(INFO) << "Took " << delay.InMilliseconds() << " ms to hide bookmark bar"; 76 ASSERT_FALSE(IsVisible()); 77 } 78 79 // Verify that bookmarks persist browser restart. 80 IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest, PRE_Persist) { 81 BookmarkModel* bookmark_model = WaitForBookmarkModel(browser()->profile()); 82 83 bookmark_utils::AddIfNotBookmarked( 84 bookmark_model, GURL(kPersistBookmarkURL), 85 ASCIIToUTF16(kPersistBookmarkTitle)); 86 } 87 88 IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest, Persist) { 89 BookmarkModel* bookmark_model = WaitForBookmarkModel(browser()->profile()); 90 91 std::vector<BookmarkService::URLAndTitle> urls; 92 bookmark_model->GetBookmarks(&urls); 93 94 ASSERT_EQ(1u, urls.size()); 95 ASSERT_EQ(GURL(kPersistBookmarkURL), urls[0].url); 96 ASSERT_EQ(ASCIIToUTF16(kPersistBookmarkTitle), urls[0].title); 97 } 98 99 #if !defined(OS_CHROMEOS) // No multi-profile on ChromeOS. 100 101 // Sanity check that bookmarks from different profiles are separate. 102 // DISABLED_ because it regularly times out: http://crbug.com/159002. 103 IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest, DISABLED_MultiProfile) { 104 base::ScopedTempDir temp_dir; 105 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 106 107 BookmarkModel* bookmark_model1 = WaitForBookmarkModel(browser()->profile()); 108 109 ui_test_utils::BrowserAddedObserver observer; 110 g_browser_process->profile_manager()->CreateMultiProfileAsync( 111 base::string16(), base::string16(), ProfileManager::CreateCallback(), 112 std::string()); 113 Browser* browser2 = observer.WaitForSingleNewBrowser(); 114 BookmarkModel* bookmark_model2 = WaitForBookmarkModel(browser2->profile()); 115 116 bookmark_utils::AddIfNotBookmarked( 117 bookmark_model1, GURL(kPersistBookmarkURL), 118 ASCIIToUTF16(kPersistBookmarkTitle)); 119 std::vector<BookmarkService::URLAndTitle> urls1, urls2; 120 bookmark_model1->GetBookmarks(&urls1); 121 bookmark_model2->GetBookmarks(&urls2); 122 ASSERT_EQ(1u, urls1.size()); 123 ASSERT_TRUE(urls2.empty()); 124 } 125 126 #endif 127