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_factory.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.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/browser/ui/tabs/tab_strip_model.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "chrome/test/base/ui_test_utils.h"
22 #include "components/bookmarks/browser/bookmark_model.h"
23 #include "components/bookmarks/browser/bookmark_utils.h"
24 #include "components/bookmarks/test/bookmark_test_helpers.h"
25 #include "content/public/browser/interstitial_page.h"
26 #include "content/public/browser/notification_service.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/test/browser_test_utils.h"
30 using bookmarks::BookmarkModel
;
33 const char kPersistBookmarkURL
[] = "http://www.cnn.com/";
34 const char kPersistBookmarkTitle
[] = "CNN";
37 class TestBookmarkTabHelperDelegate
: public BookmarkTabHelperDelegate
{
39 TestBookmarkTabHelperDelegate()
42 ~TestBookmarkTabHelperDelegate() override
{}
44 void URLStarredChanged(content::WebContents
*, bool starred
) override
{
47 bool is_starred() const { return starred_
; }
52 DISALLOW_COPY_AND_ASSIGN(TestBookmarkTabHelperDelegate
);
55 class BookmarkBrowsertest
: public InProcessBrowserTest
{
57 BookmarkBrowsertest() {}
60 return browser()->bookmark_bar_state() == BookmarkBar::SHOW
;
63 static void CheckAnimation(Browser
* browser
, const base::Closure
& quit_task
) {
64 if (!browser
->window()->IsBookmarkBarAnimating())
68 base::TimeDelta
WaitForBookmarkBarAnimationToFinish() {
69 base::Time
start(base::Time::Now());
70 scoped_refptr
<content::MessageLoopRunner
> runner
=
71 new content::MessageLoopRunner
;
73 base::Timer
timer(false, true);
76 base::TimeDelta::FromMilliseconds(15),
77 base::Bind(&CheckAnimation
, browser(), runner
->QuitClosure()));
79 return base::Time::Now() - start
;
82 BookmarkModel
* WaitForBookmarkModel(Profile
* profile
) {
83 BookmarkModel
* bookmark_model
=
84 BookmarkModelFactory::GetForProfile(profile
);
85 bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model
);
86 return bookmark_model
;
90 DISALLOW_COPY_AND_ASSIGN(BookmarkBrowsertest
);
93 // Test of bookmark bar toggling, visibility, and animation.
94 IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest
, BookmarkBarVisibleWait
) {
95 ASSERT_FALSE(IsVisible());
96 chrome::ExecuteCommand(browser(), IDC_SHOW_BOOKMARK_BAR
);
97 base::TimeDelta delay
= WaitForBookmarkBarAnimationToFinish();
98 LOG(INFO
) << "Took " << delay
.InMilliseconds() << " ms to show bookmark bar";
99 ASSERT_TRUE(IsVisible());
100 chrome::ExecuteCommand(browser(), IDC_SHOW_BOOKMARK_BAR
);
101 delay
= WaitForBookmarkBarAnimationToFinish();
102 LOG(INFO
) << "Took " << delay
.InMilliseconds() << " ms to hide bookmark bar";
103 ASSERT_FALSE(IsVisible());
106 // Verify that bookmarks persist browser restart.
107 IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest
, PRE_Persist
) {
108 BookmarkModel
* bookmark_model
= WaitForBookmarkModel(browser()->profile());
110 bookmarks::AddIfNotBookmarked(bookmark_model
,
111 GURL(kPersistBookmarkURL
),
112 base::ASCIIToUTF16(kPersistBookmarkTitle
));
115 #if defined(THREAD_SANITIZER)
116 // BookmarkBrowsertest.Persist fails under ThreadSanitizer on Linux, see
117 // http://crbug.com/340223.
118 #define MAYBE_Persist DISABLED_Persist
120 #define MAYBE_Persist Persist
122 IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest
, MAYBE_Persist
) {
123 BookmarkModel
* bookmark_model
= WaitForBookmarkModel(browser()->profile());
125 std::vector
<BookmarkModel::URLAndTitle
> urls
;
126 bookmark_model
->GetBookmarks(&urls
);
128 ASSERT_EQ(1u, urls
.size());
129 ASSERT_EQ(GURL(kPersistBookmarkURL
), urls
[0].url
);
130 ASSERT_EQ(base::ASCIIToUTF16(kPersistBookmarkTitle
), urls
[0].title
);
133 #if !defined(OS_CHROMEOS) // No multi-profile on ChromeOS.
135 // Sanity check that bookmarks from different profiles are separate.
136 // DISABLED_ because it regularly times out: http://crbug.com/159002.
137 IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest
, DISABLED_MultiProfile
) {
138 base::ScopedTempDir temp_dir
;
139 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
141 BookmarkModel
* bookmark_model1
= WaitForBookmarkModel(browser()->profile());
143 ui_test_utils::BrowserAddedObserver observer
;
144 g_browser_process
->profile_manager()->CreateMultiProfileAsync(
145 base::string16(), base::string16(), ProfileManager::CreateCallback(),
147 Browser
* browser2
= observer
.WaitForSingleNewBrowser();
148 BookmarkModel
* bookmark_model2
= WaitForBookmarkModel(browser2
->profile());
150 bookmarks::AddIfNotBookmarked(bookmark_model1
,
151 GURL(kPersistBookmarkURL
),
152 base::ASCIIToUTF16(kPersistBookmarkTitle
));
153 std::vector
<BookmarkModel::URLAndTitle
> urls1
, urls2
;
154 bookmark_model1
->GetBookmarks(&urls1
);
155 bookmark_model2
->GetBookmarks(&urls2
);
156 ASSERT_EQ(1u, urls1
.size());
157 ASSERT_TRUE(urls2
.empty());
162 IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest
,
163 HideStarOnNonbookmarkedInterstitial
) {
164 // Start an HTTPS server with a certificate error.
165 net::SpawnedTestServer::SSLOptions https_options
;
166 https_options
.server_certificate
=
167 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME
;
168 net::SpawnedTestServer
https_server(
169 net::SpawnedTestServer::TYPE_HTTPS
, https_options
,
170 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
171 ASSERT_TRUE(https_server
.Start());
173 BookmarkModel
* bookmark_model
= WaitForBookmarkModel(browser()->profile());
174 GURL bookmark_url
= test_server()->GetURL("http://example.test");
175 bookmarks::AddIfNotBookmarked(bookmark_model
,
177 base::ASCIIToUTF16("Bookmark"));
179 TestBookmarkTabHelperDelegate bookmark_delegate
;
180 content::WebContents
* web_contents
=
181 browser()->tab_strip_model()->GetActiveWebContents();
182 BookmarkTabHelper
* tab_helper
=
183 BookmarkTabHelper::FromWebContents(web_contents
);
184 tab_helper
->set_delegate(&bookmark_delegate
);
186 // Go to a bookmarked url. Bookmark star should show.
187 ui_test_utils::NavigateToURL(browser(), bookmark_url
);
188 EXPECT_FALSE(web_contents
->ShowingInterstitialPage());
189 EXPECT_TRUE(bookmark_delegate
.is_starred());
191 // Now go to a non-bookmarked url which triggers an SSL warning. Bookmark
192 // star should disappear.
193 GURL error_url
= https_server
.GetURL(".");
194 ui_test_utils::NavigateToURL(browser(), error_url
);
195 web_contents
= browser()->tab_strip_model()->GetActiveWebContents();
196 content::WaitForInterstitialAttach(web_contents
);
197 EXPECT_TRUE(web_contents
->ShowingInterstitialPage());
198 EXPECT_FALSE(bookmark_delegate
.is_starred());