1 // Copyright (c) 2012 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/chromeos/settings/stub_cros_settings_provider.h" 6 7 #include <string> 8 9 #include "base/bind.h" 10 #include "base/memory/scoped_ptr.h" 11 #include "base/values.h" 12 #include "chromeos/settings/cros_settings_names.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 15 namespace chromeos { 16 17 namespace { 18 19 void Fail() { 20 // Should never be called. 21 FAIL(); 22 } 23 24 } // namespace 25 26 class StubCrosSettingsProviderTest : public testing::Test { 27 protected: 28 StubCrosSettingsProviderTest() 29 : provider_(new StubCrosSettingsProvider( 30 base::Bind(&StubCrosSettingsProviderTest::FireObservers, 31 base::Unretained(this)))) { 32 } 33 34 virtual ~StubCrosSettingsProviderTest() { 35 } 36 37 virtual void SetUp() OVERRIDE { 38 // Reset the observer notification count. 39 observer_count_.clear(); 40 } 41 42 void AssertPref(const std::string& prefName, const base::Value* value) { 43 const base::Value* pref = provider_->Get(prefName); 44 ASSERT_TRUE(pref); 45 ASSERT_TRUE(pref->Equals(value)); 46 } 47 48 void ExpectObservers(const std::string& prefName, int count) { 49 EXPECT_EQ(observer_count_[prefName], count); 50 } 51 52 void FireObservers(const std::string& path) { 53 observer_count_[path]++; 54 } 55 56 scoped_ptr<StubCrosSettingsProvider> provider_; 57 std::map<std::string, int> observer_count_; 58 }; 59 60 TEST_F(StubCrosSettingsProviderTest, HandlesSettings) { 61 // HandlesSettings should return false for unknown settings. 62 ASSERT_TRUE(provider_->HandlesSetting(kDeviceOwner)); 63 ASSERT_FALSE(provider_->HandlesSetting("no.such.setting")); 64 } 65 66 TEST_F(StubCrosSettingsProviderTest, Defaults) { 67 // Verify default values. 68 const base::FundamentalValue kTrueValue(true); 69 AssertPref(kAccountsPrefAllowGuest, &kTrueValue); 70 AssertPref(kAccountsPrefAllowNewUser, &kTrueValue); 71 AssertPref(kAccountsPrefShowUserNamesOnSignIn, &kTrueValue); 72 AssertPref(kAccountsPrefSupervisedUsersEnabled, &kTrueValue); 73 } 74 75 TEST_F(StubCrosSettingsProviderTest, Set) { 76 // Setting value and reading it afterwards returns the same value. 77 base::StringValue owner_value("me@owner"); 78 provider_->Set(kDeviceOwner, owner_value); 79 AssertPref(kDeviceOwner, &owner_value); 80 ExpectObservers(kDeviceOwner, 1); 81 } 82 83 TEST_F(StubCrosSettingsProviderTest, SetMissing) { 84 // Setting is missing initially but is added by |Set|. 85 base::StringValue pref_value("testing"); 86 ASSERT_FALSE(provider_->Get(kReleaseChannel)); 87 provider_->Set(kReleaseChannel, pref_value); 88 AssertPref(kReleaseChannel, &pref_value); 89 ExpectObservers(kReleaseChannel, 1); 90 } 91 92 TEST_F(StubCrosSettingsProviderTest, PrepareTrustedValues) { 93 // Should return immediately without invoking the callback. 94 CrosSettingsProvider::TrustedStatus trusted = 95 provider_->PrepareTrustedValues(base::Bind(&Fail)); 96 EXPECT_EQ(CrosSettingsProvider::TRUSTED, trusted); 97 } 98 99 } // namespace chromeos 100