bookmark_bar_view_unittest.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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_bar_view.h"
6
7#include "base/prefs/pref_service.h"
8#include "base/values.h"
9#include "chrome/browser/bookmarks/bookmark_model_factory.h"
10#include "chrome/browser/profiles/profile.h"
11#include "chrome/browser/search_engines/template_url_service_factory.h"
12#include "chrome/browser/ui/app_list/app_list_util.h"
13#include "chrome/common/pref_names.h"
14#include "chrome/common/url_constants.h"
15#include "chrome/test/base/browser_with_test_window_test.h"
16#include "chrome/test/base/scoped_testing_local_state.h"
17#include "chrome/test/base/testing_browser_process.h"
18#include "chrome/test/base/testing_pref_service_syncable.h"
19#include "components/bookmarks/test/bookmark_test_helpers.h"
20#include "components/search_engines/search_terms_data.h"
21#include "components/search_engines/template_url_service.h"
22#include "components/search_engines/template_url_service_client.h"
23#include "ui/views/controls/button/label_button.h"
24
25class BookmarkBarViewInstantExtendedTest : public BrowserWithTestWindowTest {
26 public:
27  BookmarkBarViewInstantExtendedTest() {
28  }
29
30 protected:
31  virtual TestingProfile* CreateProfile() OVERRIDE {
32    TestingProfile* profile = BrowserWithTestWindowTest::CreateProfile();
33    // TemplateURLService is normally NULL during testing. Instant extended
34    // needs this service so set a custom factory function.
35    TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
36        profile, &BookmarkBarViewInstantExtendedTest::CreateTemplateURLService);
37    return profile;
38  }
39
40 private:
41  static KeyedService* CreateTemplateURLService(
42      content::BrowserContext* profile) {
43    return new TemplateURLService(
44        static_cast<Profile*>(profile)->GetPrefs(),
45        make_scoped_ptr(new SearchTermsData), NULL,
46        scoped_ptr<TemplateURLServiceClient>(), NULL, NULL, base::Closure());
47  }
48
49  DISALLOW_COPY_AND_ASSIGN(BookmarkBarViewInstantExtendedTest);
50};
51
52// Verify that in instant extended mode the visibility of the apps shortcut
53// button properly follows the pref value.
54TEST_F(BookmarkBarViewInstantExtendedTest, AppsShortcutVisibility) {
55  ScopedTestingLocalState local_state(TestingBrowserProcess::GetGlobal());
56  profile()->CreateBookmarkModel(true);
57  test::WaitForBookmarkModelToLoad(
58      BookmarkModelFactory::GetForProfile(profile()));
59  BookmarkBarView bookmark_bar_view(browser(), NULL);
60  bookmark_bar_view.set_owned_by_client();
61  browser()->profile()->GetPrefs()->SetBoolean(
62      bookmarks::prefs::kShowAppsShortcutInBookmarkBar, false);
63  EXPECT_FALSE(bookmark_bar_view.apps_page_shortcut_->visible());
64
65  // Try to make the Apps shortcut visible. Its visibility depends on whether
66  // the app launcher is enabled.
67  browser()->profile()->GetPrefs()->SetBoolean(
68      bookmarks::prefs::kShowAppsShortcutInBookmarkBar, true);
69  if (IsAppLauncherEnabled()) {
70    EXPECT_FALSE(bookmark_bar_view.apps_page_shortcut_->visible());
71  } else {
72    EXPECT_TRUE(bookmark_bar_view.apps_page_shortcut_->visible());
73  }
74
75  // Make sure we can also properly transition from true to false.
76  browser()->profile()->GetPrefs()->SetBoolean(
77      bookmarks::prefs::kShowAppsShortcutInBookmarkBar, false);
78  EXPECT_FALSE(bookmark_bar_view.apps_page_shortcut_->visible());
79}
80
81#if !defined(OS_CHROMEOS)
82typedef BrowserWithTestWindowTest BookmarkBarViewTest;
83
84// Verifies that the apps shortcut is shown or hidden following the policy
85// value. This policy (and the apps shortcut) isn't present on ChromeOS.
86TEST_F(BookmarkBarViewTest, ManagedShowAppsShortcutInBookmarksBar) {
87  ScopedTestingLocalState local_state(TestingBrowserProcess::GetGlobal());
88  profile()->CreateBookmarkModel(true);
89  test::WaitForBookmarkModelToLoad(
90      BookmarkModelFactory::GetForProfile(profile()));
91  BookmarkBarView bookmark_bar_view(browser(), NULL);
92  bookmark_bar_view.set_owned_by_client();
93
94  // By default, the pref is not managed and the apps shortcut is shown.
95  TestingPrefServiceSyncable* prefs = profile()->GetTestingPrefService();
96  EXPECT_FALSE(prefs->IsManagedPreference(
97      bookmarks::prefs::kShowAppsShortcutInBookmarkBar));
98  EXPECT_TRUE(bookmark_bar_view.apps_page_shortcut_->visible());
99
100  // Hide the apps shortcut by policy, via the managed pref.
101  prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar,
102                        new base::FundamentalValue(false));
103  EXPECT_FALSE(bookmark_bar_view.apps_page_shortcut_->visible());
104
105  // And try showing it via policy too.
106  prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar,
107                        new base::FundamentalValue(true));
108  EXPECT_TRUE(bookmark_bar_view.apps_page_shortcut_->visible());
109}
110#endif
111