12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "base/values.h"
9010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "chrome/browser/bookmarks/bookmark_model_factory.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/search_engines/template_url_service_factory.h"
121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/ui/app_list/app_list_util.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/pref_names.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/url_constants.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/test/base/browser_with_test_window_test.h"
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/test/base/scoped_testing_local_state.h"
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/test/base/testing_browser_process.h"
1846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chrome/test/base/testing_pref_service_syncable.h"
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/bookmarks/test/bookmark_test_helpers.h"
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/search_engines/search_terms_data.h"
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/search_engines/template_url_service.h"
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/search_engines/template_url_service_client.h"
23f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "ui/views/controls/button/label_button.h"
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class BookmarkBarViewInstantExtendedTest : public BrowserWithTestWindowTest {
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BookmarkBarViewInstantExtendedTest() {
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) protected:
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual TestingProfile* CreateProfile() OVERRIDE {
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TestingProfile* profile = BrowserWithTestWindowTest::CreateProfile();
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // TemplateURLService is normally NULL during testing. Instant extended
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // needs this service so set a custom factory function.
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        profile, &BookmarkBarViewInstantExtendedTest::CreateTemplateURLService);
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return profile;
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static KeyedService* CreateTemplateURLService(
42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::BrowserContext* profile) {
43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return new TemplateURLService(
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        static_cast<Profile*>(profile)->GetPrefs(),
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        make_scoped_ptr(new SearchTermsData), NULL,
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        scoped_ptr<TemplateURLServiceClient>(), NULL, NULL, base::Closure());
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(BookmarkBarViewInstantExtendedTest);
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Verify that in instant extended mode the visibility of the apps shortcut
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// button properly follows the pref value.
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(BookmarkBarViewInstantExtendedTest, AppsShortcutVisibility) {
55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ScopedTestingLocalState local_state(TestingBrowserProcess::GetGlobal());
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile()->CreateBookmarkModel(true);
57010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  test::WaitForBookmarkModelToLoad(
58010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      BookmarkModelFactory::GetForProfile(profile()));
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BookmarkBarView bookmark_bar_view(browser(), NULL);
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bookmark_bar_view.set_owned_by_client();
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  browser()->profile()->GetPrefs()->SetBoolean(
621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      bookmarks::prefs::kShowAppsShortcutInBookmarkBar, false);
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(bookmark_bar_view.apps_page_shortcut_->visible());
64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
65c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Try to make the Apps shortcut visible. Its visibility depends on whether
66c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // the app launcher is enabled.
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  browser()->profile()->GetPrefs()->SetBoolean(
681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      bookmarks::prefs::kShowAppsShortcutInBookmarkBar, true);
694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (IsAppLauncherEnabled()) {
70c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_FALSE(bookmark_bar_view.apps_page_shortcut_->visible());
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
72c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_TRUE(bookmark_bar_view.apps_page_shortcut_->visible());
73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
74c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Make sure we can also properly transition from true to false.
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  browser()->profile()->GetPrefs()->SetBoolean(
771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      bookmarks::prefs::kShowAppsShortcutInBookmarkBar, false);
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(bookmark_bar_view.apps_page_shortcut_->visible());
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
8146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#if !defined(OS_CHROMEOS)
8246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)typedef BrowserWithTestWindowTest BookmarkBarViewTest;
8346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
8446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Verifies that the apps shortcut is shown or hidden following the policy
8546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// value. This policy (and the apps shortcut) isn't present on ChromeOS.
8646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST_F(BookmarkBarViewTest, ManagedShowAppsShortcutInBookmarksBar) {
8746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ScopedTestingLocalState local_state(TestingBrowserProcess::GetGlobal());
8846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  profile()->CreateBookmarkModel(true);
8946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  test::WaitForBookmarkModelToLoad(
9046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      BookmarkModelFactory::GetForProfile(profile()));
9146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  BookmarkBarView bookmark_bar_view(browser(), NULL);
9246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bookmark_bar_view.set_owned_by_client();
9346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
9446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // By default, the pref is not managed and the apps shortcut is shown.
9546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestingPrefServiceSyncable* prefs = profile()->GetTestingPrefService();
961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_FALSE(prefs->IsManagedPreference(
971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      bookmarks::prefs::kShowAppsShortcutInBookmarkBar));
9846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(bookmark_bar_view.apps_page_shortcut_->visible());
9946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
10046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Hide the apps shortcut by policy, via the managed pref.
1011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar,
10246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                        new base::FundamentalValue(false));
10346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_FALSE(bookmark_bar_view.apps_page_shortcut_->visible());
10446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
10546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // And try showing it via policy too.
1061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar,
10746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                        new base::FundamentalValue(true));
10846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(bookmark_bar_view.apps_page_shortcut_->visible());
10946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
11046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#endif
111