bookmark_bar_view_unittest.cc revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
1997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu// Copyright 2013 The Chromium Authors. All rights reserved.
2997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu// Use of this source code is governed by a BSD-style license that can be
3997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu// found in the LICENSE file.
4997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu
5997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu#include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
6997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu
7997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu#include "base/prefs/pref_service.h"
8997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu#include "base/values.h"
9997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu#include "chrome/browser/bookmarks/bookmark_model_factory.h"
10997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu#include "chrome/browser/profiles/profile.h"
11997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu#include "chrome/browser/search_engines/template_url_service.h"
12997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu#include "chrome/browser/search_engines/template_url_service_factory.h"
13997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu#include "chrome/browser/ui/app_list/app_list_util.h"
14997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu#include "chrome/common/pref_names.h"
15997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu#include "chrome/common/url_constants.h"
16997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu#include "chrome/test/base/browser_with_test_window_test.h"
17997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu#include "chrome/test/base/scoped_testing_local_state.h"
18997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu#include "chrome/test/base/testing_browser_process.h"
19997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu#include "chrome/test/base/testing_pref_service_syncable.h"
20997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu#include "components/bookmarks/test/bookmark_test_helpers.h"
21997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu#include "ui/views/controls/button/text_button.h"
22997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu
23997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yuclass BookmarkBarViewInstantExtendedTest : public BrowserWithTestWindowTest {
24997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu public:
25997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu  BookmarkBarViewInstantExtendedTest() {
26997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu  }
27997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu
28997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu protected:
29997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu  virtual TestingProfile* CreateProfile() OVERRIDE {
30997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu    TestingProfile* profile = BrowserWithTestWindowTest::CreateProfile();
31997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu    // TemplateURLService is normally NULL during testing. Instant extended
32997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu    // needs this service so set a custom factory function.
33997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu    TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
34997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu        profile, &BookmarkBarViewInstantExtendedTest::CreateTemplateURLService);
35997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu    return profile;
36997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu  }
37997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu
38997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu private:
39997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu  static KeyedService* CreateTemplateURLService(
40997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu      content::BrowserContext* profile) {
41997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu    return new TemplateURLService(static_cast<Profile*>(profile));
42997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu  }
43997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu
44997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu  DISALLOW_COPY_AND_ASSIGN(BookmarkBarViewInstantExtendedTest);
45997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu};
46997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu
47997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu// Verify that in instant extended mode the visibility of the apps shortcut
48997171ed3fdf8baf96d8fdf9dd3201a459327d89Ryan Yu// button properly follows the pref value.
49TEST_F(BookmarkBarViewInstantExtendedTest, AppsShortcutVisibility) {
50  ScopedTestingLocalState local_state(TestingBrowserProcess::GetGlobal());
51  profile()->CreateBookmarkModel(true);
52  test::WaitForBookmarkModelToLoad(
53      BookmarkModelFactory::GetForProfile(profile()));
54  BookmarkBarView bookmark_bar_view(browser(), NULL);
55  bookmark_bar_view.set_owned_by_client();
56  browser()->profile()->GetPrefs()->SetBoolean(
57      prefs::kShowAppsShortcutInBookmarkBar, false);
58  EXPECT_FALSE(bookmark_bar_view.apps_page_shortcut_->visible());
59
60  // Try to make the Apps shortcut visible. Its visibility depends on whether
61  // the app launcher is enabled.
62  browser()->profile()->GetPrefs()->SetBoolean(
63      prefs::kShowAppsShortcutInBookmarkBar, true);
64  if (IsAppLauncherEnabled()) {
65    EXPECT_FALSE(bookmark_bar_view.apps_page_shortcut_->visible());
66  } else {
67    EXPECT_TRUE(bookmark_bar_view.apps_page_shortcut_->visible());
68  }
69
70  // Make sure we can also properly transition from true to false.
71  browser()->profile()->GetPrefs()->SetBoolean(
72      prefs::kShowAppsShortcutInBookmarkBar, false);
73  EXPECT_FALSE(bookmark_bar_view.apps_page_shortcut_->visible());
74}
75
76#if !defined(OS_CHROMEOS)
77typedef BrowserWithTestWindowTest BookmarkBarViewTest;
78
79// Verifies that the apps shortcut is shown or hidden following the policy
80// value. This policy (and the apps shortcut) isn't present on ChromeOS.
81TEST_F(BookmarkBarViewTest, ManagedShowAppsShortcutInBookmarksBar) {
82  ScopedTestingLocalState local_state(TestingBrowserProcess::GetGlobal());
83  profile()->CreateBookmarkModel(true);
84  test::WaitForBookmarkModelToLoad(
85      BookmarkModelFactory::GetForProfile(profile()));
86  BookmarkBarView bookmark_bar_view(browser(), NULL);
87  bookmark_bar_view.set_owned_by_client();
88
89  // By default, the pref is not managed and the apps shortcut is shown.
90  TestingPrefServiceSyncable* prefs = profile()->GetTestingPrefService();
91  EXPECT_FALSE(
92      prefs->IsManagedPreference(prefs::kShowAppsShortcutInBookmarkBar));
93  EXPECT_TRUE(bookmark_bar_view.apps_page_shortcut_->visible());
94
95  // Hide the apps shortcut by policy, via the managed pref.
96  prefs->SetManagedPref(prefs::kShowAppsShortcutInBookmarkBar,
97                        new base::FundamentalValue(false));
98  EXPECT_FALSE(bookmark_bar_view.apps_page_shortcut_->visible());
99
100  // And try showing it via policy too.
101  prefs->SetManagedPref(prefs::kShowAppsShortcutInBookmarkBar,
102                        new base::FundamentalValue(true));
103  EXPECT_TRUE(bookmark_bar_view.apps_page_shortcut_->visible());
104}
105#endif
106