OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/stub_cros_settings_provider.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/browser/chromeos/cros_settings_names.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace chromeos { |
| 15 |
| 16 namespace { |
| 17 |
| 18 const Value* kTrueValue = base::Value::CreateBooleanValue(true); |
| 19 const Value* kFalseValue = base::Value::CreateBooleanValue(false); |
| 20 |
| 21 void Fail() { |
| 22 // Should never be called. |
| 23 FAIL(); |
| 24 } |
| 25 |
| 26 } // namespace |
| 27 |
| 28 class StubCrosSettingsProviderTest : public testing::Test { |
| 29 protected: |
| 30 StubCrosSettingsProviderTest() |
| 31 : provider_(new StubCrosSettingsProvider( |
| 32 base::Bind(&StubCrosSettingsProviderTest::FireObservers, |
| 33 base::Unretained(this)))) { |
| 34 } |
| 35 |
| 36 virtual ~StubCrosSettingsProviderTest() { |
| 37 } |
| 38 |
| 39 virtual void SetUp() OVERRIDE { |
| 40 // Reset the observer notification count. |
| 41 observer_count_.clear(); |
| 42 } |
| 43 |
| 44 void AssertPref(const std::string& prefName, const Value* value) { |
| 45 const Value* pref = provider_->Get(prefName); |
| 46 ASSERT_TRUE(pref); |
| 47 ASSERT_TRUE(pref->Equals(value)); |
| 48 } |
| 49 |
| 50 void ExpectObservers(const std::string& prefName, int count) { |
| 51 EXPECT_EQ(observer_count_[prefName], count); |
| 52 } |
| 53 |
| 54 void FireObservers(const std::string& path) { |
| 55 observer_count_[path]++; |
| 56 } |
| 57 |
| 58 StubCrosSettingsProvider* provider_; |
| 59 std::map<std::string, int> observer_count_; |
| 60 }; |
| 61 |
| 62 TEST_F(StubCrosSettingsProviderTest, HandlesSettings) { |
| 63 // HandlesSettings should return false for unknown settings. |
| 64 ASSERT_TRUE(provider_->HandlesSetting(kDeviceOwner)); |
| 65 ASSERT_FALSE(provider_->HandlesSetting("no.such.setting")); |
| 66 } |
| 67 |
| 68 TEST_F(StubCrosSettingsProviderTest, Defaults) { |
| 69 // Verify default values. |
| 70 AssertPref(kAccountsPrefAllowGuest, kTrueValue); |
| 71 AssertPref(kAccountsPrefAllowNewUser, kTrueValue); |
| 72 AssertPref(kAccountsPrefShowUserNamesOnSignIn, 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, GetTrusted) { |
| 93 // Should return immediately without invoking the callback. |
| 94 bool trusted = provider_->GetTrusted(kDeviceOwner, base::Bind(&Fail)); |
| 95 EXPECT_TRUE(trusted); |
| 96 } |
| 97 |
| 98 } // namespace chromeos |
OLD | NEW |