1 // Copyright 2013 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 "chrome/browser/ui/views/bookmarks/bookmark_bubble_view.h"
6
7 #include <memory>
8 #include <string>
9 #include <utility>
10
11 #include "base/bind.h"
12 #include "base/macros.h"
13 #include "build/build_config.h"
14 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
15 #include "chrome/browser/signin/identity_manager_factory.h"
16 #include "chrome/browser/ui/sync/bubble_sync_promo_delegate.h"
17 #include "chrome/test/base/browser_with_test_window_test.h"
18 #include "chrome/test/views/chrome_test_widget.h"
19 #include "components/bookmarks/browser/bookmark_utils.h"
20 #include "components/bookmarks/test/bookmark_test_helpers.h"
21 #include "components/signin/public/identity_manager/identity_test_utils.h"
22 #include "ui/views/bubble/bubble_dialog_delegate_view.h"
23 #include "ui/views/test/widget_test.h"
24 #include "ui/views/widget/unique_widget_ptr.h"
25
26 using bookmarks::BookmarkModel;
27
28 namespace {
29 const char kTestBookmarkURL[] = "http://www.google.com";
30 } // namespace
31
32 class BookmarkBubbleViewTest : public BrowserWithTestWindowTest {
33 public:
34 // The test executes the UI code for displaying a window that should be
35 // executed on the UI thread. The test also hits the networking code that
36 // fails without the IO thread. We pass the REAL_IO_THREAD option to run UI
37 // and IO tasks on separate threads.
BookmarkBubbleViewTest()38 BookmarkBubbleViewTest()
39 : BrowserWithTestWindowTest(
40 content::BrowserTaskEnvironment::REAL_IO_THREAD) {}
41
42 // testing::Test:
SetUp()43 void SetUp() override {
44 BrowserWithTestWindowTest::SetUp();
45
46 anchor_widget_ =
47 views::UniqueWidgetPtr(std::make_unique<ChromeTestWidget>());
48 views::Widget::InitParams widget_params;
49 widget_params.context = GetContext();
50 anchor_widget_->Init(std::move(widget_params));
51
52 BookmarkModel* bookmark_model =
53 BookmarkModelFactory::GetForBrowserContext(profile());
54 bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model);
55
56 bookmarks::AddIfNotBookmarked(
57 bookmark_model, GURL(kTestBookmarkURL), base::string16());
58 }
59
TearDown()60 void TearDown() override {
61 // Make sure the bubble is destroyed before the profile to avoid a crash.
62 views::test::WidgetDestroyedWaiter destroyed_waiter(
63 BookmarkBubbleView::bookmark_bubble()->GetWidget());
64 BookmarkBubbleView::bookmark_bubble()->GetWidget()->Close();
65 destroyed_waiter.Wait();
66
67 anchor_widget_.reset();
68
69 BrowserWithTestWindowTest::TearDown();
70 }
71
GetTestingFactories()72 TestingProfile::TestingFactories GetTestingFactories() override {
73 return {{BookmarkModelFactory::GetInstance(),
74 BookmarkModelFactory::GetDefaultFactory()}};
75 }
76
77 protected:
78 // Creates a bookmark bubble view.
CreateBubbleView()79 void CreateBubbleView() {
80 // Create a fake anchor view for the bubble.
81 BookmarkBubbleView::ShowBubble(anchor_widget_->GetContentsView(), nullptr,
82 nullptr, nullptr, profile(),
83 GURL(kTestBookmarkURL), true);
84 }
85
86 private:
87 views::UniqueWidgetPtr anchor_widget_;
88
89 DISALLOW_COPY_AND_ASSIGN(BookmarkBubbleViewTest);
90 };
91
92 // Verifies that the sync promo is not displayed for a signed in user.
TEST_F(BookmarkBubbleViewTest,SyncPromoSignedIn)93 TEST_F(BookmarkBubbleViewTest, SyncPromoSignedIn) {
94 signin::MakePrimaryAccountAvailable(
95 IdentityManagerFactory::GetForProfile(profile()),
96 "fake_username@gmail.com");
97 CreateBubbleView();
98 EXPECT_FALSE(
99 BookmarkBubbleView::bookmark_bubble()->GetFootnoteViewForTesting());
100 }
101
102 // Verifies that the sync promo is displayed for a user that is not signed in.
TEST_F(BookmarkBubbleViewTest,SyncPromoNotSignedIn)103 TEST_F(BookmarkBubbleViewTest, SyncPromoNotSignedIn) {
104 CreateBubbleView();
105 views::View* footnote =
106 BookmarkBubbleView::bookmark_bubble()->GetFootnoteViewForTesting();
107 #if defined(OS_CHROMEOS)
108 EXPECT_FALSE(footnote);
109 #else // !defined(OS_CHROMEOS)
110 EXPECT_TRUE(footnote);
111 #endif
112 }
113