Home | History | Annotate | Download | only in bookmarks
      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.h"
     12 #include "chrome/browser/search_engines/template_url_service_factory.h"
     13 #include "chrome/browser/ui/app_list/app_list_util.h"
     14 #include "chrome/common/pref_names.h"
     15 #include "chrome/common/url_constants.h"
     16 #include "chrome/test/base/browser_with_test_window_test.h"
     17 #include "chrome/test/base/scoped_testing_local_state.h"
     18 #include "chrome/test/base/testing_browser_process.h"
     19 #include "chrome/test/base/testing_pref_service_syncable.h"
     20 #include "components/bookmarks/test/bookmark_test_helpers.h"
     21 #include "ui/views/controls/button/label_button.h"
     22 
     23 class BookmarkBarViewInstantExtendedTest : public BrowserWithTestWindowTest {
     24  public:
     25   BookmarkBarViewInstantExtendedTest() {
     26   }
     27 
     28  protected:
     29   virtual TestingProfile* CreateProfile() OVERRIDE {
     30     TestingProfile* profile = BrowserWithTestWindowTest::CreateProfile();
     31     // TemplateURLService is normally NULL during testing. Instant extended
     32     // needs this service so set a custom factory function.
     33     TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
     34         profile, &BookmarkBarViewInstantExtendedTest::CreateTemplateURLService);
     35     return profile;
     36   }
     37 
     38  private:
     39   static KeyedService* CreateTemplateURLService(
     40       content::BrowserContext* profile) {
     41     return new TemplateURLService(static_cast<Profile*>(profile));
     42   }
     43 
     44   DISALLOW_COPY_AND_ASSIGN(BookmarkBarViewInstantExtendedTest);
     45 };
     46 
     47 // Verify that in instant extended mode the visibility of the apps shortcut
     48 // button properly follows the pref value.
     49 TEST_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)
     77 typedef 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.
     81 TEST_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