Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / bookmarks / bookmark_browsertest.cc
blob1a998d4723b34f245a2108636551d227ffd83352
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.
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_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;
32 namespace {
33 const char kPersistBookmarkURL[] = "http://www.cnn.com/";
34 const char kPersistBookmarkTitle[] = "CNN";
35 } // namespace
37 class TestBookmarkTabHelperDelegate : public BookmarkTabHelperDelegate {
38 public:
39 TestBookmarkTabHelperDelegate()
40 : starred_(false) {
42 ~TestBookmarkTabHelperDelegate() override {}
44 void URLStarredChanged(content::WebContents*, bool starred) override {
45 starred_ = starred;
47 bool is_starred() const { return starred_; }
49 private:
50 bool starred_;
52 DISALLOW_COPY_AND_ASSIGN(TestBookmarkTabHelperDelegate);
55 class BookmarkBrowsertest : public InProcessBrowserTest {
56 public:
57 BookmarkBrowsertest() {}
59 bool IsVisible() {
60 return browser()->bookmark_bar_state() == BookmarkBar::SHOW;
63 static void CheckAnimation(Browser* browser, const base::Closure& quit_task) {
64 if (!browser->window()->IsBookmarkBarAnimating())
65 quit_task.Run();
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);
74 timer.Start(
75 FROM_HERE,
76 base::TimeDelta::FromMilliseconds(15),
77 base::Bind(&CheckAnimation, browser(), runner->QuitClosure()));
78 runner->Run();
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;
89 private:
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
119 #else
120 #define MAYBE_Persist Persist
121 #endif
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(),
146 std::string());
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());
160 #endif
162 // Flaky on Linux: http://crbug.com/504869.
163 #if defined(OS_LINUX)
164 #define MAYBE_HideStarOnNonbookmarkedInterstitial \
165 DISABLED_HideStarOnNonbookmarkedInterstitial
166 #else
167 #define MAYBE_HideStarOnNonbookmarkedInterstitial \
168 HideStarOnNonbookmarkedInterstitial
169 #endif
170 IN_PROC_BROWSER_TEST_F(BookmarkBrowsertest,
171 MAYBE_HideStarOnNonbookmarkedInterstitial) {
172 // Start an HTTPS server with a certificate error.
173 net::SpawnedTestServer::SSLOptions https_options;
174 https_options.server_certificate =
175 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME;
176 net::SpawnedTestServer https_server(
177 net::SpawnedTestServer::TYPE_HTTPS, https_options,
178 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
179 ASSERT_TRUE(https_server.Start());
181 BookmarkModel* bookmark_model = WaitForBookmarkModel(browser()->profile());
182 GURL bookmark_url = test_server()->GetURL("http://example.test");
183 bookmarks::AddIfNotBookmarked(bookmark_model,
184 bookmark_url,
185 base::ASCIIToUTF16("Bookmark"));
187 TestBookmarkTabHelperDelegate bookmark_delegate;
188 content::WebContents* web_contents =
189 browser()->tab_strip_model()->GetActiveWebContents();
190 BookmarkTabHelper* tab_helper =
191 BookmarkTabHelper::FromWebContents(web_contents);
192 tab_helper->set_delegate(&bookmark_delegate);
194 // Go to a bookmarked url. Bookmark star should show.
195 ui_test_utils::NavigateToURL(browser(), bookmark_url);
196 EXPECT_FALSE(web_contents->ShowingInterstitialPage());
197 EXPECT_TRUE(bookmark_delegate.is_starred());
199 // Now go to a non-bookmarked url which triggers an SSL warning. Bookmark
200 // star should disappear.
201 GURL error_url = https_server.GetURL(".");
202 ui_test_utils::NavigateToURL(browser(), error_url);
203 web_contents = browser()->tab_strip_model()->GetActiveWebContents();
204 content::WaitForInterstitialAttach(web_contents);
205 EXPECT_TRUE(web_contents->ShowingInterstitialPage());
206 EXPECT_FALSE(bookmark_delegate.is_starred());
208 // The delegate is required to outlive the tab helper.
209 tab_helper->set_delegate(nullptr);