1 // Copyright (c) 2010 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/policy/managed_prefs_banner_base.h" 6 #include "chrome/common/pref_names.h" 7 #include "chrome/test/testing_pref_service.h" 8 #include "testing/gmock/include/gmock/gmock.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 namespace policy { 12 13 // Tests whether managed preferences banner base functionality correctly 14 // determines banner visiblity. 15 class ManagedPrefsBannerBaseTest : public testing::Test { 16 public: 17 virtual void SetUp() { 18 user_prefs_.reset(new TestingPrefService); 19 user_prefs_->RegisterBooleanPref(prefs::kHomePageIsNewTabPage, false); 20 user_prefs_->RegisterBooleanPref(prefs::kSearchSuggestEnabled, false); 21 local_state_.reset(new TestingPrefService); 22 local_state_->RegisterBooleanPref(prefs::kHomePageIsNewTabPage, false); 23 local_state_->RegisterBooleanPref(prefs::kMetricsReportingEnabled, false); 24 } 25 26 scoped_ptr<TestingPrefService> local_state_; 27 scoped_ptr<TestingPrefService> user_prefs_; 28 }; 29 30 TEST_F(ManagedPrefsBannerBaseTest, VisibilityTest) { 31 ManagedPrefsBannerBase banner(local_state_.get(), 32 user_prefs_.get(), 33 OPTIONS_PAGE_ADVANCED); 34 banner.AddLocalStatePref(prefs::kMetricsReportingEnabled); 35 EXPECT_FALSE(banner.DetermineVisibility()); 36 user_prefs_->SetManagedPref(prefs::kHomePageIsNewTabPage, 37 Value::CreateBooleanValue(true)); 38 EXPECT_FALSE(banner.DetermineVisibility()); 39 user_prefs_->SetUserPref(prefs::kSearchSuggestEnabled, 40 Value::CreateBooleanValue(true)); 41 EXPECT_FALSE(banner.DetermineVisibility()); 42 user_prefs_->SetManagedPref(prefs::kSearchSuggestEnabled, 43 Value::CreateBooleanValue(false)); 44 EXPECT_TRUE(banner.DetermineVisibility()); 45 local_state_->SetManagedPref(prefs::kMetricsReportingEnabled, 46 Value::CreateBooleanValue(true)); 47 EXPECT_TRUE(banner.DetermineVisibility()); 48 user_prefs_->RemoveManagedPref(prefs::kSearchSuggestEnabled); 49 EXPECT_TRUE(banner.DetermineVisibility()); 50 local_state_->RemoveManagedPref(prefs::kMetricsReportingEnabled); 51 EXPECT_FALSE(banner.DetermineVisibility()); 52 local_state_->SetManagedPref(prefs::kHomePageIsNewTabPage, 53 Value::CreateBooleanValue(true)); 54 EXPECT_FALSE(banner.DetermineVisibility()); 55 } 56 57 // Mock class that allows to capture the notification callback. 58 class ManagedPrefsBannerBaseMock : public ManagedPrefsBannerBase { 59 public: 60 ManagedPrefsBannerBaseMock(PrefService* local_state, 61 PrefService* user_prefs, 62 OptionsPage page) 63 : ManagedPrefsBannerBase(local_state, user_prefs, page) { } 64 65 MOCK_METHOD0(OnUpdateVisibility, void()); 66 }; 67 68 TEST_F(ManagedPrefsBannerBaseTest, NotificationTest) { 69 ManagedPrefsBannerBaseMock banner(local_state_.get(), 70 user_prefs_.get(), 71 OPTIONS_PAGE_ADVANCED); 72 banner.AddLocalStatePref(prefs::kMetricsReportingEnabled); 73 EXPECT_CALL(banner, OnUpdateVisibility()).Times(0); 74 user_prefs_->SetBoolean(prefs::kHomePageIsNewTabPage, true); 75 EXPECT_CALL(banner, OnUpdateVisibility()).Times(1); 76 user_prefs_->SetManagedPref(prefs::kSearchSuggestEnabled, 77 Value::CreateBooleanValue(false)); 78 EXPECT_CALL(banner, OnUpdateVisibility()).Times(1); 79 local_state_->SetManagedPref(prefs::kMetricsReportingEnabled, 80 Value::CreateBooleanValue(true)); 81 } 82 83 } // namespace policy 84