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