summaryrefslogtreecommitdiffstats
path: root/chrome/browser/managed_prefs_banner_base_unittest.cc
blob: 16c9f98726b98a803b1de0d382374e514fbd513d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/dummy_pref_store.h"
#include "chrome/browser/managed_prefs_banner_base.h"
#include "chrome/browser/pref_service.h"
#include "chrome/common/pref_names.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

const wchar_t* kDummyPref = L"dummy";

}  // namespace

// Tests whether managed preferences banner base functionality correctly
// determines banner visiblity.
class ManagedPrefsBannerBaseTest : public testing::Test {
 public:
  virtual void SetUp() {
    managed_prefs_ = new DummyPrefStore;
    extension_prefs_ = new DummyPrefStore;
    user_prefs_ = new DummyPrefStore;
    default_prefs_ = new DummyPrefStore;
    pref_service_.reset(new PrefService(new PrefValueStore(managed_prefs_,
                                                           extension_prefs_,
                                                           user_prefs_,
                                                           default_prefs_)));
    pref_service_->RegisterStringPref(prefs::kHomePage, "http://google.com");
    pref_service_->RegisterBooleanPref(prefs::kHomePageIsNewTabPage, false);
    pref_service_->RegisterBooleanPref(kDummyPref, false);
  }

  scoped_ptr<PrefService> pref_service_;
  DummyPrefStore* managed_prefs_;
  DummyPrefStore* extension_prefs_;
  DummyPrefStore* user_prefs_;
  DummyPrefStore* default_prefs_;
};

TEST_F(ManagedPrefsBannerBaseTest, VisibilityTest) {
  ManagedPrefsBannerBase banner(pref_service_.get(), OPTIONS_PAGE_GENERAL);
  EXPECT_FALSE(banner.DetermineVisibility());
  managed_prefs_->prefs()->SetBoolean(kDummyPref, true);
  EXPECT_FALSE(banner.DetermineVisibility());
  user_prefs_->prefs()->SetString(prefs::kHomePage, "http://foo.com");
  EXPECT_FALSE(banner.DetermineVisibility());
  managed_prefs_->prefs()->SetString(prefs::kHomePage, "http://bar.com");
  EXPECT_TRUE(banner.DetermineVisibility());
}

// Mock class that allows to capture the notification callback.
class ManagedPrefsBannerBaseMock : public ManagedPrefsBannerBase {
 public:
  ManagedPrefsBannerBaseMock(PrefService* pref_service, OptionsPage page)
      : ManagedPrefsBannerBase(pref_service, page) { }

  MOCK_METHOD0(OnUpdateVisibility, void());
};

TEST_F(ManagedPrefsBannerBaseTest, NotificationTest) {
  ManagedPrefsBannerBaseMock banner(pref_service_.get(), OPTIONS_PAGE_GENERAL);
  EXPECT_CALL(banner, OnUpdateVisibility()).Times(0);
  pref_service_->SetBoolean(kDummyPref, true);
  EXPECT_CALL(banner, OnUpdateVisibility()).Times(1);
  pref_service_->SetString(prefs::kHomePage, "http://foo.com");
}