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.
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_utils.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_commands.h"
18 #include "chrome/browser/ui/browser_window.h"
19 #include "chrome/browser/ui/host_desktop.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"
26 const char kPersistBookmarkURL
[] = "http://www.cnn.com/";
27 const char kPersistBookmarkTitle
[] = "CNN";
30 class BookmarkBrowsertest
: public InProcessBrowserTest
{
32 BookmarkBrowsertest() {}
35 return browser()->bookmark_bar_state() == BookmarkBar::SHOW
;
38 static void CheckAnimation(Browser
* browser
, const base::Closure
& quit_task
) {
39 if (!browser
->window()->IsBookmarkBarAnimating())
43 base::TimeDelta
WaitForBookmarkBarAnimationToFinish() {
44 base::Time
start(base::Time::Now());
45 scoped_refptr
<content::MessageLoopRunner
> runner
=
46 new content::MessageLoopRunner
;
48 base::Timer
timer(false, true);
51 base::TimeDelta::FromMilliseconds(15),
52 base::Bind(&CheckAnimation
, browser(), runner
->QuitClosure()));
54 return base::Time::Now() - start
;
57 BookmarkModel
* WaitForBookmarkModel(Profile
* profile
) {
58 BookmarkModel
* bookmark_model
=
59 BookmarkModelFactory::GetForProfile(profile
);
60 ui_test_utils::WaitForBookmarkModelToLoad(bookmark_model
);
61 return bookmark_model
;
65 // Test of bookmark bar toggling, visibility, and animation.
66 IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest
, BookmarkBarVisibleWait
) {
67 ASSERT_FALSE(IsVisible());
68 chrome::ExecuteCommand(browser(), IDC_SHOW_BOOKMARK_BAR
);
69 base::TimeDelta delay
= WaitForBookmarkBarAnimationToFinish();
70 LOG(INFO
) << "Took " << delay
.InMilliseconds() << " ms to show bookmark bar";
71 ASSERT_TRUE(IsVisible());
72 chrome::ExecuteCommand(browser(), IDC_SHOW_BOOKMARK_BAR
);
73 delay
= WaitForBookmarkBarAnimationToFinish();
74 LOG(INFO
) << "Took " << delay
.InMilliseconds() << " ms to hide bookmark bar";
75 ASSERT_FALSE(IsVisible());
78 // Verify that bookmarks persist browser restart.
79 IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest
, PRE_Persist
) {
80 BookmarkModel
* bookmark_model
= WaitForBookmarkModel(browser()->profile());
82 bookmark_utils::AddIfNotBookmarked(
83 bookmark_model
, GURL(kPersistBookmarkURL
),
84 ASCIIToUTF16(kPersistBookmarkTitle
));
87 IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest
, Persist
) {
88 BookmarkModel
* bookmark_model
= WaitForBookmarkModel(browser()->profile());
90 std::vector
<BookmarkService::URLAndTitle
> urls
;
91 bookmark_model
->GetBookmarks(&urls
);
93 ASSERT_EQ(1u, urls
.size());
94 ASSERT_EQ(GURL(kPersistBookmarkURL
), urls
[0].url
);
95 ASSERT_EQ(ASCIIToUTF16(kPersistBookmarkTitle
), urls
[0].title
);
98 #if !defined(OS_CHROMEOS) // No multi-profile on ChromeOS.
100 // Sanity check that bookmarks from different profiles are separate.
101 // DISABLED_ because it regularly times out: http://crbug.com/159002.
102 IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest
, DISABLED_MultiProfile
) {
103 base::ScopedTempDir temp_dir
;
104 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
106 BookmarkModel
* bookmark_model1
= WaitForBookmarkModel(browser()->profile());
108 ui_test_utils::BrowserAddedObserver observer
;
109 g_browser_process
->profile_manager()->CreateMultiProfileAsync(
110 string16(), string16(), ProfileManager::CreateCallback(), std::string());
111 Browser
* browser2
= observer
.WaitForSingleNewBrowser();
112 BookmarkModel
* bookmark_model2
= WaitForBookmarkModel(browser2
->profile());
114 bookmark_utils::AddIfNotBookmarked(
115 bookmark_model1
, GURL(kPersistBookmarkURL
),
116 ASCIIToUTF16(kPersistBookmarkTitle
));
117 std::vector
<BookmarkService::URLAndTitle
> urls1
, urls2
;
118 bookmark_model1
->GetBookmarks(&urls1
);
119 bookmark_model2
->GetBookmarks(&urls2
);
120 ASSERT_EQ(1u, urls1
.size());
121 ASSERT_TRUE(urls2
.empty());