bookmark_bubble_view_unittest.cc revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
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 <string>
8
9#include "base/memory/scoped_ptr.h"
10#include "chrome/browser/bookmarks/bookmark_model_factory.h"
11#include "chrome/browser/bookmarks/bookmark_test_helpers.h"
12#include "chrome/browser/signin/fake_signin_manager.h"
13#include "chrome/browser/signin/signin_manager_factory.h"
14#include "chrome/browser/ui/bookmarks/bookmark_bubble_delegate.h"
15#include "chrome/test/base/browser_with_test_window_test.h"
16#include "components/bookmarks/core/browser/bookmark_utils.h"
17#include "components/signin/core/browser/signin_manager.h"
18
19namespace {
20const char kTestBookmarkURL[] = "http://www.google.com";
21} // namespace
22
23class BookmarkBubbleViewTest : public BrowserWithTestWindowTest {
24 public:
25  BookmarkBubbleViewTest() {}
26
27  // testing::Test:
28  virtual void SetUp() OVERRIDE {
29    BrowserWithTestWindowTest::SetUp();
30
31    profile()->CreateBookmarkModel(true);
32    test::WaitForBookmarkModelToLoad(profile());
33
34    bookmark_utils::AddIfNotBookmarked(
35        BookmarkModelFactory::GetForProfile(profile()),
36        GURL(kTestBookmarkURL),
37        base::string16());
38  }
39
40  virtual void TearDown() OVERRIDE {
41    // Make sure the bubble is destroyed before the profile to avoid a crash.
42    bubble_.reset();
43
44    BrowserWithTestWindowTest::TearDown();
45  }
46
47  // BrowserWithTestWindowTest:
48  virtual TestingProfile* CreateProfile() OVERRIDE {
49    TestingProfile::Builder builder;
50    builder.AddTestingFactory(SigninManagerFactory::GetInstance(),
51                              FakeSigninManagerBase::Build);
52    return builder.Build().release();
53  }
54
55 protected:
56  // Creates a bookmark bubble view.
57  void CreateBubbleView() {
58    scoped_ptr<BookmarkBubbleDelegate> delegate;
59    bubble_.reset(new BookmarkBubbleView(NULL,
60                                         NULL,
61                                         delegate.Pass(),
62                                         profile(),
63                                         GURL(kTestBookmarkURL),
64                                         true));
65  }
66
67  void SetUpSigninManager(const std::string& username) {
68    if (username.empty())
69      return;
70    SigninManagerBase* signin_manager = static_cast<SigninManagerBase*>(
71        SigninManagerFactory::GetForProfile(profile()));
72    ASSERT_TRUE(signin_manager);
73    signin_manager->SetAuthenticatedUsername(username);
74  }
75
76  scoped_ptr<BookmarkBubbleView> bubble_;
77
78 private:
79  DISALLOW_COPY_AND_ASSIGN(BookmarkBubbleViewTest);
80};
81
82// Verifies that the sync promo is not displayed for a signed in user.
83TEST_F(BookmarkBubbleViewTest, SyncPromoSignedIn) {
84  SetUpSigninManager("fake_username");
85  CreateBubbleView();
86  bubble_->Init();
87  EXPECT_FALSE(bubble_->sync_promo_view_);
88}
89
90// Verifies that the sync promo is displayed for a user that is not signed in.
91TEST_F(BookmarkBubbleViewTest, SyncPromoNotSignedIn) {
92  CreateBubbleView();
93  bubble_->Init();
94#if defined(OS_CHROMEOS)
95  EXPECT_FALSE(bubble_->sync_promo_view_);
96#else  // !defined(OS_CHROMEOS)
97  EXPECT_TRUE(bubble_->sync_promo_view_);
98#endif
99}
100