This source file includes following definitions.
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST_F
- TEST_F
- TEST_F
#include <string>
#include "base/prefs/json_pref_store.h"
#include "base/prefs/mock_pref_change_callback.h"
#include "base/prefs/pref_change_registrar.h"
#include "base/prefs/pref_registry_simple.h"
#include "base/prefs/pref_value_store.h"
#include "base/prefs/testing_pref_service.h"
#include "base/prefs/testing_pref_store.h"
#include "base/values.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::Mock;
const char kPrefName[] = "pref.name";
TEST(PrefServiceTest, NoObserverFire) {
TestingPrefServiceSimple prefs;
const char pref_name[] = "homepage";
prefs.registry()->RegisterStringPref(pref_name, std::string());
const char new_pref_value[] = "http://www.google.com/";
MockPrefChangeCallback obs(&prefs);
PrefChangeRegistrar registrar;
registrar.Init(&prefs);
registrar.Add(pref_name, obs.GetCallback());
const base::StringValue expected_value(new_pref_value);
obs.Expect(pref_name, &expected_value);
prefs.SetString(pref_name, new_pref_value);
Mock::VerifyAndClearExpectations(&obs);
EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
prefs.SetString(pref_name, new_pref_value);
Mock::VerifyAndClearExpectations(&obs);
const base::StringValue expected_default_value((std::string()));
obs.Expect(pref_name, &expected_default_value);
prefs.ClearPref(pref_name);
Mock::VerifyAndClearExpectations(&obs);
EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
prefs.ClearPref(pref_name);
Mock::VerifyAndClearExpectations(&obs);
}
TEST(PrefServiceTest, HasPrefPath) {
TestingPrefServiceSimple prefs;
const char path[] = "fake.path";
EXPECT_FALSE(prefs.HasPrefPath(path));
prefs.registry()->RegisterStringPref(path, std::string());
EXPECT_FALSE(prefs.HasPrefPath(path));
prefs.SetString(path, "blah");
EXPECT_TRUE(prefs.HasPrefPath(path));
}
TEST(PrefServiceTest, Observers) {
const char pref_name[] = "homepage";
TestingPrefServiceSimple prefs;
prefs.SetUserPref(pref_name,
new base::StringValue("http://www.cnn.com"));
prefs.registry()->RegisterStringPref(pref_name, std::string());
const char new_pref_value[] = "http://www.google.com/";
const base::StringValue expected_new_pref_value(new_pref_value);
MockPrefChangeCallback obs(&prefs);
PrefChangeRegistrar registrar;
registrar.Init(&prefs);
registrar.Add(pref_name, obs.GetCallback());
PrefChangeRegistrar registrar_two;
registrar_two.Init(&prefs);
obs.Expect(pref_name, &expected_new_pref_value);
prefs.SetString(pref_name, new_pref_value);
Mock::VerifyAndClearExpectations(&obs);
const char new_pref_value2[] = "http://www.youtube.com/";
const base::StringValue expected_new_pref_value2(new_pref_value2);
MockPrefChangeCallback obs2(&prefs);
obs.Expect(pref_name, &expected_new_pref_value2);
obs2.Expect(pref_name, &expected_new_pref_value2);
registrar_two.Add(pref_name, obs2.GetCallback());
prefs.SetString(pref_name, new_pref_value2);
Mock::VerifyAndClearExpectations(&obs);
Mock::VerifyAndClearExpectations(&obs2);
const base::StringValue recommended_pref_value("http://www.gmail.com/");
obs.Expect(pref_name, &expected_new_pref_value2);
obs2.Expect(pref_name, &expected_new_pref_value2);
prefs.SetRecommendedPref(pref_name, recommended_pref_value.DeepCopy());
Mock::VerifyAndClearExpectations(&obs);
Mock::VerifyAndClearExpectations(&obs2);
registrar.Remove(pref_name);
EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
obs2.Expect(pref_name, &expected_new_pref_value);
prefs.SetString(pref_name, new_pref_value);
Mock::VerifyAndClearExpectations(&obs);
Mock::VerifyAndClearExpectations(&obs2);
}
TEST(PrefServiceTest, GetValueChangedType) {
const int kTestValue = 10;
TestingPrefServiceSimple prefs;
prefs.registry()->RegisterIntegerPref(kPrefName, kTestValue);
prefs.SetUserPref(kPrefName,
new base::StringValue("not an integer"));
const PrefService::Preference* pref = prefs.FindPreference(kPrefName);
ASSERT_TRUE(pref);
const base::Value* value = pref->GetValue();
ASSERT_TRUE(value);
EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
int actual_int_value = -1;
EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
EXPECT_EQ(kTestValue, actual_int_value);
}
TEST(PrefServiceTest, GetValueAndGetRecommendedValue) {
const int kDefaultValue = 5;
const int kUserValue = 10;
const int kRecommendedValue = 15;
TestingPrefServiceSimple prefs;
prefs.registry()->RegisterIntegerPref(kPrefName, kDefaultValue);
const PrefService::Preference* pref = prefs.FindPreference(kPrefName);
ASSERT_TRUE(pref);
const base::Value* value = pref->GetValue();
ASSERT_TRUE(value);
EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
int actual_int_value = -1;
EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
EXPECT_EQ(kDefaultValue, actual_int_value);
value = pref->GetRecommendedValue();
ASSERT_FALSE(value);
prefs.SetUserPref(kPrefName, new base::FundamentalValue(kUserValue));
value = pref->GetValue();
ASSERT_TRUE(value);
EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
actual_int_value = -1;
EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
EXPECT_EQ(kUserValue, actual_int_value);
value = pref->GetRecommendedValue();
ASSERT_FALSE(value);
prefs.SetRecommendedPref(kPrefName,
new base::FundamentalValue(kRecommendedValue));
value = pref->GetValue();
ASSERT_TRUE(value);
EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
actual_int_value = -1;
EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
EXPECT_EQ(kUserValue, actual_int_value);
value = pref->GetRecommendedValue();
ASSERT_TRUE(value);
EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
actual_int_value = -1;
EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
EXPECT_EQ(kRecommendedValue, actual_int_value);
prefs.RemoveUserPref(kPrefName);
value = pref->GetValue();
ASSERT_TRUE(value);
EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
actual_int_value = -1;
EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
EXPECT_EQ(kRecommendedValue, actual_int_value);
value = pref->GetRecommendedValue();
ASSERT_TRUE(value);
EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
actual_int_value = -1;
EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
EXPECT_EQ(kRecommendedValue, actual_int_value);
}
class PrefServiceSetValueTest : public testing::Test {
protected:
static const char kName[];
static const char kValue[];
PrefServiceSetValueTest() : observer_(&prefs_) {}
TestingPrefServiceSimple prefs_;
MockPrefChangeCallback observer_;
};
const char PrefServiceSetValueTest::kName[] = "name";
const char PrefServiceSetValueTest::kValue[] = "value";
TEST_F(PrefServiceSetValueTest, SetStringValue) {
const char default_string[] = "default";
const base::StringValue default_value(default_string);
prefs_.registry()->RegisterStringPref(kName, default_string);
PrefChangeRegistrar registrar;
registrar.Init(&prefs_);
registrar.Add(kName, observer_.GetCallback());
observer_.Expect(kName, &default_value);
prefs_.Set(kName, default_value);
Mock::VerifyAndClearExpectations(&observer_);
EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
prefs_.Set(kName, default_value);
Mock::VerifyAndClearExpectations(&observer_);
base::StringValue new_value(kValue);
observer_.Expect(kName, &new_value);
prefs_.Set(kName, new_value);
Mock::VerifyAndClearExpectations(&observer_);
}
TEST_F(PrefServiceSetValueTest, SetDictionaryValue) {
prefs_.registry()->RegisterDictionaryPref(kName);
PrefChangeRegistrar registrar;
registrar.Init(&prefs_);
registrar.Add(kName, observer_.GetCallback());
EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
prefs_.RemoveUserPref(kName);
Mock::VerifyAndClearExpectations(&observer_);
base::DictionaryValue new_value;
new_value.SetString(kName, kValue);
observer_.Expect(kName, &new_value);
prefs_.Set(kName, new_value);
Mock::VerifyAndClearExpectations(&observer_);
EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
prefs_.Set(kName, new_value);
Mock::VerifyAndClearExpectations(&observer_);
base::DictionaryValue empty;
observer_.Expect(kName, &empty);
prefs_.Set(kName, empty);
Mock::VerifyAndClearExpectations(&observer_);
}
TEST_F(PrefServiceSetValueTest, SetListValue) {
prefs_.registry()->RegisterListPref(kName);
PrefChangeRegistrar registrar;
registrar.Init(&prefs_);
registrar.Add(kName, observer_.GetCallback());
EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
prefs_.RemoveUserPref(kName);
Mock::VerifyAndClearExpectations(&observer_);
base::ListValue new_value;
new_value.Append(new base::StringValue(kValue));
observer_.Expect(kName, &new_value);
prefs_.Set(kName, new_value);
Mock::VerifyAndClearExpectations(&observer_);
EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
prefs_.Set(kName, new_value);
Mock::VerifyAndClearExpectations(&observer_);
base::ListValue empty;
observer_.Expect(kName, &empty);
prefs_.Set(kName, empty);
Mock::VerifyAndClearExpectations(&observer_);
}