This source file includes following definitions.
- CreateVal
- CreateTime
- GetValue
- RegisterExtension
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_P
#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_store_observer_mock.h"
#include "base/values.h"
#include "extensions/browser/extension_pref_value_map.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const char kExt1[] = "ext1";
const char kExt2[] = "ext2";
const char kExt3[] = "ext3";
const char kPref1[] = "path1.subpath";
const char kPref2[] = "path2";
const char kPref3[] = "path3";
const char kPref4[] = "path4";
}
static base::Value* CreateVal(const char* str) {
return new base::StringValue(str);
}
static base::Time CreateTime(int64 t) {
return base::Time::FromInternalValue(t);
}
template <typename BASECLASS>
class ExtensionPrefValueMapTestBase : public BASECLASS {
public:
static const extensions::ExtensionPrefsScope kRegular =
extensions::kExtensionPrefsScopeRegular;
static const extensions::ExtensionPrefsScope kRegularOnly =
extensions::kExtensionPrefsScopeRegularOnly;
static const extensions::ExtensionPrefsScope kIncognitoPersistent =
extensions::kExtensionPrefsScopeIncognitoPersistent;
static const extensions::ExtensionPrefsScope kIncognitoSessionOnly =
extensions::kExtensionPrefsScopeIncognitoSessionOnly;
std::string GetValue(const char * key, bool incognito) const {
const base::Value *value =
epvm_.GetEffectivePrefValue(key, incognito, NULL);
std::string string_value;
if (value)
value->GetAsString(&string_value);
return string_value;
}
void RegisterExtension(const std::string& ext_id,
const base::Time& install_time) {
epvm_.RegisterExtension(
ext_id, install_time, true , false );
}
protected:
ExtensionPrefValueMap epvm_;
};
class ExtensionPrefValueMapTest
: public ExtensionPrefValueMapTestBase<testing::Test> {
};
class ExtensionPrefValueMapObserverMock
: public ExtensionPrefValueMap::Observer {
public:
ExtensionPrefValueMapObserverMock() {}
virtual ~ExtensionPrefValueMapObserverMock() {}
MOCK_METHOD1(OnPrefValueChanged, void(const std::string&));
MOCK_METHOD0(OnInitializationCompleted, void());
MOCK_METHOD0(OnExtensionPrefValueMapDestruction, void());
private:
DISALLOW_COPY_AND_ASSIGN(ExtensionPrefValueMapObserverMock);
};
TEST_F(ExtensionPrefValueMapTest, SetAndGetPrefValue) {
RegisterExtension(kExt1, CreateTime(10));
epvm_.SetExtensionPref(kExt1, kPref1, kRegular, CreateVal("val1"));
EXPECT_EQ("val1", GetValue(kPref1, false));
};
TEST_F(ExtensionPrefValueMapTest, GetNotSetPrefValue) {
RegisterExtension(kExt1, CreateTime(10));
EXPECT_EQ(std::string(), GetValue(kPref1, false));
};
TEST_F(ExtensionPrefValueMapTest, Override) {
RegisterExtension(kExt1, CreateTime(10));
RegisterExtension(kExt2, CreateTime(20));
RegisterExtension(kExt3, CreateTime(30));
epvm_.SetExtensionPref(kExt1, kPref1, kRegular, CreateVal("val1"));
epvm_.SetExtensionPref(kExt2, kPref1, kRegular, CreateVal("val2"));
epvm_.SetExtensionPref(kExt3, kPref1, kRegular, CreateVal("val3"));
epvm_.SetExtensionPref(kExt1, kPref2, kRegular, CreateVal("val4"));
epvm_.SetExtensionPref(kExt2, kPref2, kRegular, CreateVal("val5"));
epvm_.SetExtensionPref(kExt1, kPref1, kRegular, CreateVal("val6"));
epvm_.SetExtensionPref(kExt1, kPref2, kRegular, CreateVal("val7"));
epvm_.SetExtensionPref(kExt1, kPref3, kRegular, CreateVal("val8"));
EXPECT_EQ("val3", GetValue(kPref1, false));
EXPECT_EQ("val5", GetValue(kPref2, false));
EXPECT_EQ("val8", GetValue(kPref3, false));
}
TEST_F(ExtensionPrefValueMapTest, OverrideChecks) {
RegisterExtension(kExt1, CreateTime(10));
RegisterExtension(kExt2, CreateTime(20));
RegisterExtension(kExt3, CreateTime(30));
EXPECT_FALSE(epvm_.DoesExtensionControlPref(kExt1, kPref1, NULL));
EXPECT_FALSE(epvm_.DoesExtensionControlPref(kExt2, kPref1, NULL));
EXPECT_FALSE(epvm_.DoesExtensionControlPref(kExt3, kPref1, NULL));
EXPECT_TRUE(epvm_.CanExtensionControlPref(kExt1, kPref1, false));
EXPECT_TRUE(epvm_.CanExtensionControlPref(kExt2, kPref1, false));
EXPECT_TRUE(epvm_.CanExtensionControlPref(kExt3, kPref1, false));
epvm_.SetExtensionPref(kExt2, kPref1, kRegular, CreateVal("val1"));
EXPECT_FALSE(epvm_.DoesExtensionControlPref(kExt1, kPref1, NULL));
EXPECT_TRUE(epvm_.DoesExtensionControlPref(kExt2, kPref1, NULL));
EXPECT_FALSE(epvm_.DoesExtensionControlPref(kExt3, kPref1, NULL));
EXPECT_FALSE(epvm_.CanExtensionControlPref(kExt1, kPref1, false));
EXPECT_TRUE(epvm_.CanExtensionControlPref(kExt2, kPref1, false));
EXPECT_TRUE(epvm_.CanExtensionControlPref(kExt3, kPref1, false));
}
TEST_F(ExtensionPrefValueMapTest, SetAndGetPrefValueIncognito) {
RegisterExtension(kExt1, CreateTime(10));
epvm_.SetExtensionPref(kExt1, kPref1, kRegular, CreateVal("val1"));
EXPECT_EQ(std::string(), GetValue(kPref1, true));
epvm_.SetExtensionIncognitoState(kExt1, true);
EXPECT_EQ("val1", GetValue(kPref1, true));
epvm_.SetExtensionIncognitoState(kExt1, false);
EXPECT_EQ(std::string(), GetValue(kPref1, true));
}
TEST_F(ExtensionPrefValueMapTest, UninstallOnlyExtension) {
RegisterExtension(kExt1, CreateTime(10));
epvm_.SetExtensionPref(kExt1, kPref1, kRegular, CreateVal("val1"));
epvm_.UnregisterExtension(kExt1);
EXPECT_EQ(std::string(), GetValue(kPref1, false));
}
TEST_F(ExtensionPrefValueMapTest, UninstallIrrelevantExtension) {
RegisterExtension(kExt1, CreateTime(10));
RegisterExtension(kExt2, CreateTime(10));
epvm_.SetExtensionPref(kExt1, kPref1, kRegular, CreateVal("val1"));
epvm_.SetExtensionPref(kExt2, kPref1, kRegular, CreateVal("val2"));
epvm_.SetExtensionPref(kExt1, kPref2, kRegular, CreateVal("val3"));
epvm_.SetExtensionPref(kExt2, kPref2, kRegular, CreateVal("val4"));
epvm_.UnregisterExtension(kExt1);
EXPECT_EQ("val2", GetValue(kPref1, false));
EXPECT_EQ("val4", GetValue(kPref2, false));
}
TEST_F(ExtensionPrefValueMapTest, UninstallExtensionFromTop) {
RegisterExtension(kExt1, CreateTime(10));
RegisterExtension(kExt2, CreateTime(20));
RegisterExtension(kExt3, CreateTime(30));
epvm_.SetExtensionPref(kExt1, kPref1, kRegular, CreateVal("val1"));
epvm_.SetExtensionPref(kExt2, kPref1, kRegular, CreateVal("val2"));
epvm_.SetExtensionPref(kExt3, kPref1, kRegular, CreateVal("val3"));
epvm_.SetExtensionPref(kExt1, kPref2, kRegular, CreateVal("val4"));
epvm_.SetExtensionPref(kExt3, kPref2, kRegular, CreateVal("val5"));
epvm_.UnregisterExtension(kExt3);
EXPECT_EQ("val2", GetValue(kPref1, false));
EXPECT_EQ("val4", GetValue(kPref2, false));
}
TEST_F(ExtensionPrefValueMapTest, UninstallExtensionFromMiddle) {
RegisterExtension(kExt1, CreateTime(10));
RegisterExtension(kExt2, CreateTime(20));
RegisterExtension(kExt3, CreateTime(30));
epvm_.SetExtensionPref(kExt1, kPref1, kRegular, CreateVal("val1"));
epvm_.SetExtensionPref(kExt2, kPref1, kRegular, CreateVal("val2"));
epvm_.SetExtensionPref(kExt3, kPref1, kRegular, CreateVal("val3"));
epvm_.SetExtensionPref(kExt1, kPref2, kRegular, CreateVal("val4"));
epvm_.SetExtensionPref(kExt2, kPref2, kRegular, CreateVal("val5"));
epvm_.SetExtensionPref(kExt1, kPref3, kRegular, CreateVal("val6"));
epvm_.SetExtensionPref(kExt2, kPref4, kRegular, CreateVal("val7"));
epvm_.UnregisterExtension(kExt2);
EXPECT_EQ("val3", GetValue(kPref1, false));
EXPECT_EQ("val4", GetValue(kPref2, false));
EXPECT_EQ("val6", GetValue(kPref3, false));
EXPECT_EQ(std::string(), GetValue(kPref4, false));
}
TEST_F(ExtensionPrefValueMapTest, NotifyWhenNeeded) {
using testing::Mock;
using testing::StrEq;
RegisterExtension(kExt1, CreateTime(10));
ExtensionPrefValueMapObserverMock observer;
epvm_.AddObserver(&observer);
EXPECT_CALL(observer, OnPrefValueChanged(std::string(kPref1)));
epvm_.SetExtensionPref(kExt1, kPref1, kRegular, CreateVal("val1"));
Mock::VerifyAndClearExpectations(&observer);
EXPECT_CALL(observer, OnPrefValueChanged(std::string(kPref1))).Times(0);
epvm_.SetExtensionPref(kExt1, kPref1, kRegular, CreateVal("val1"));
Mock::VerifyAndClearExpectations(&observer);
EXPECT_CALL(observer, OnPrefValueChanged(std::string(kPref1)));
epvm_.SetExtensionPref(kExt1, kPref1, kRegular, CreateVal("val2"));
Mock::VerifyAndClearExpectations(&observer);
EXPECT_CALL(observer, OnPrefValueChanged(std::string(kPref1)));
epvm_.SetExtensionPref(kExt1, kPref1, kRegular, CreateVal("val3"));
Mock::VerifyAndClearExpectations(&observer);
EXPECT_CALL(observer, OnPrefValueChanged(std::string(kPref1)));
epvm_.SetExtensionState(kExt1, false);
Mock::VerifyAndClearExpectations(&observer);
EXPECT_CALL(observer, OnPrefValueChanged(std::string(kPref1)));
epvm_.SetExtensionState(kExt1, true);
Mock::VerifyAndClearExpectations(&observer);
EXPECT_CALL(observer, OnPrefValueChanged(std::string(kPref1)));
epvm_.UnregisterExtension(kExt1);
Mock::VerifyAndClearExpectations(&observer);
epvm_.RemoveObserver(&observer);
EXPECT_CALL(observer, OnPrefValueChanged(std::string(kPref1))).Times(0);
RegisterExtension(kExt1, CreateTime(10));
epvm_.SetExtensionPref(kExt1, kPref1, kRegular, CreateVal("val4"));
Mock::VerifyAndClearExpectations(&observer);
}
TEST_F(ExtensionPrefValueMapTest, DisableExt) {
RegisterExtension(kExt1, CreateTime(10));
epvm_.SetExtensionPref(kExt1, kPref1, kRegular, CreateVal("val1"));
epvm_.SetExtensionState(kExt1, false);
EXPECT_EQ(std::string(), GetValue(kPref1, false));
}
TEST_F(ExtensionPrefValueMapTest, ReenableExt) {
RegisterExtension(kExt1, CreateTime(10));
epvm_.SetExtensionPref(kExt1, kPref1, kRegular, CreateVal("val1"));
epvm_.SetExtensionState(kExt1, false);
epvm_.SetExtensionState(kExt1, true);
EXPECT_EQ("val1", GetValue(kPref1, false));
}
struct OverrideIncognitoTestCase {
OverrideIncognitoTestCase(bool enable_ext1_in_incognito,
bool enable_ext2_in_incognito,
int val_ext1_regular,
int val_ext1_regular_only,
int val_ext1_incognito_pers,
int val_ext1_incognito_sess,
int val_ext2_regular,
int val_ext2_regular_only,
int val_ext2_incognito_pers,
int val_ext2_incognito_sess,
int effective_value_regular,
int effective_value_incognito)
: enable_ext1_in_incognito_(enable_ext1_in_incognito),
enable_ext2_in_incognito_(enable_ext2_in_incognito),
val_ext1_regular_(val_ext1_regular),
val_ext1_regular_only_(val_ext1_regular_only),
val_ext1_incognito_pers_(val_ext1_incognito_pers),
val_ext1_incognito_sess_(val_ext1_incognito_sess),
val_ext2_regular_(val_ext2_regular),
val_ext2_regular_only_(val_ext2_regular_only),
val_ext2_incognito_pers_(val_ext2_incognito_pers),
val_ext2_incognito_sess_(val_ext2_incognito_sess),
effective_value_regular_(effective_value_regular),
effective_value_incognito_(effective_value_incognito) {}
bool enable_ext1_in_incognito_;
bool enable_ext2_in_incognito_;
int val_ext1_regular_;
int val_ext1_regular_only_;
int val_ext1_incognito_pers_;
int val_ext1_incognito_sess_;
int val_ext2_regular_;
int val_ext2_regular_only_;
int val_ext2_incognito_pers_;
int val_ext2_incognito_sess_;
int effective_value_regular_;
int effective_value_incognito_;
};
class ExtensionPrefValueMapTestIncognitoTests
: public ExtensionPrefValueMapTestBase<
testing::TestWithParam<OverrideIncognitoTestCase> > {
};
TEST_P(ExtensionPrefValueMapTestIncognitoTests, OverrideIncognito) {
OverrideIncognitoTestCase test = GetParam();
const char* strings[] = {
"",
"val1",
"val2",
"val3",
"val4",
"val5",
"val6",
"val7",
"val8",
};
const bool kEnabled = true;
epvm_.RegisterExtension(
kExt1, CreateTime(10), kEnabled, test.enable_ext1_in_incognito_);
epvm_.RegisterExtension(
kExt2, CreateTime(20), kEnabled, test.enable_ext2_in_incognito_);
if (test.val_ext1_regular_) {
epvm_.SetExtensionPref(kExt1, kPref1, kRegular,
CreateVal(strings[test.val_ext1_regular_]));
}
if (test.val_ext1_regular_only_) {
epvm_.SetExtensionPref(kExt1, kPref1, kRegularOnly,
CreateVal(strings[test.val_ext1_regular_only_]));
}
if (test.val_ext1_incognito_pers_) {
epvm_.SetExtensionPref(kExt1, kPref1, kIncognitoPersistent,
CreateVal(strings[test.val_ext1_incognito_pers_]));
}
if (test.val_ext1_incognito_sess_) {
epvm_.SetExtensionPref(kExt1, kPref1, kIncognitoSessionOnly,
CreateVal(strings[test.val_ext1_incognito_sess_]));
}
if (test.val_ext2_regular_) {
epvm_.SetExtensionPref(kExt2, kPref1, kRegular,
CreateVal(strings[test.val_ext2_regular_]));
}
if (test.val_ext2_regular_only_) {
epvm_.SetExtensionPref(kExt2, kPref1, kRegularOnly,
CreateVal(strings[test.val_ext2_regular_only_]));
}
if (test.val_ext2_incognito_pers_) {
epvm_.SetExtensionPref(kExt2, kPref1, kIncognitoPersistent,
CreateVal(strings[test.val_ext2_incognito_pers_]));
}
if (test.val_ext2_incognito_sess_) {
epvm_.SetExtensionPref(kExt2, kPref1, kIncognitoSessionOnly,
CreateVal(strings[test.val_ext2_incognito_sess_]));
}
std::string actual;
EXPECT_EQ(strings[test.effective_value_regular_], GetValue(kPref1, false));
EXPECT_EQ(strings[test.effective_value_incognito_], GetValue(kPref1, true));
epvm_.UnregisterExtension(kExt1);
epvm_.UnregisterExtension(kExt2);
}
INSTANTIATE_TEST_CASE_P(
ExtensionPrefValueMapTestIncognitoTestsInstance,
ExtensionPrefValueMapTestIncognitoTests,
testing::Values(
OverrideIncognitoTestCase(true, true, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1),
OverrideIncognitoTestCase(true, true, 1, 2, 0, 0, 0, 0, 0, 0, 2, 1),
OverrideIncognitoTestCase(true, true, 1, 0, 3, 0, 0, 0, 0, 0, 1, 3),
OverrideIncognitoTestCase(true, true, 1, 0, 0, 4, 0, 0, 0, 0, 1, 4),
OverrideIncognitoTestCase(true, true, 1, 0, 3, 4, 0, 0, 0, 0, 1, 4),
OverrideIncognitoTestCase(true, true, 1, 2, 3, 0, 0, 0, 0, 0, 2, 3),
OverrideIncognitoTestCase(true, true, 1, 0, 0, 0, 5, 0, 0, 0, 5, 5),
OverrideIncognitoTestCase(true, true, 1, 2, 3, 0, 5, 0, 0, 0, 5, 5),
OverrideIncognitoTestCase(true, true, 1, 0, 0, 0, 0, 6, 0, 0, 6, 1),
OverrideIncognitoTestCase(true, true, 1, 0, 3, 0, 5, 6, 0, 0, 6, 5),
OverrideIncognitoTestCase(true, true, 1, 0, 0, 4, 5, 6, 0, 0, 6, 5),
OverrideIncognitoTestCase(true, true, 1, 0, 0, 0, 0, 0, 7, 0, 1, 7),
OverrideIncognitoTestCase(true, true, 1, 2, 0, 0, 5, 0, 7, 0, 5, 7),
OverrideIncognitoTestCase(true, true, 1, 2, 0, 0, 5, 0, 0, 8, 5, 8),
OverrideIncognitoTestCase(true, true, 1, 2, 0, 0, 5, 0, 7, 8, 5, 8),
OverrideIncognitoTestCase(true, true, 1, 2, 3, 0, 0, 6, 7, 0, 6, 7),
OverrideIncognitoTestCase(false, false, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0),
OverrideIncognitoTestCase(false, false, 1, 2, 0, 0, 0, 0, 0, 0, 2, 0),
OverrideIncognitoTestCase(false, false, 1, 0, 3, 0, 0, 0, 0, 0, 1, 0),
OverrideIncognitoTestCase(false, false, 1, 0, 0, 4, 0, 0, 0, 0, 1, 0),
OverrideIncognitoTestCase(false, false, 1, 0, 3, 4, 0, 0, 0, 0, 1, 0),
OverrideIncognitoTestCase(false, false, 1, 2, 3, 0, 0, 0, 0, 0, 2, 0),
OverrideIncognitoTestCase(false, false, 1, 0, 0, 0, 5, 0, 0, 0, 5, 0),
OverrideIncognitoTestCase(false, false, 1, 2, 3, 0, 5, 0, 0, 0, 5, 0),
OverrideIncognitoTestCase(false, false, 1, 0, 0, 0, 0, 6, 0, 0, 6, 0),
OverrideIncognitoTestCase(false, false, 1, 0, 3, 0, 5, 6, 0, 0, 6, 0),
OverrideIncognitoTestCase(false, false, 1, 0, 0, 4, 5, 6, 0, 0, 6, 0),
OverrideIncognitoTestCase(false, false, 1, 0, 0, 0, 0, 0, 7, 0, 1, 0),
OverrideIncognitoTestCase(false, false, 1, 2, 0, 0, 5, 0, 7, 0, 5, 0),
OverrideIncognitoTestCase(false, false, 1, 2, 0, 0, 5, 0, 0, 8, 5, 0),
OverrideIncognitoTestCase(false, false, 1, 2, 0, 0, 5, 0, 7, 8, 5, 0),
OverrideIncognitoTestCase(false, false, 1, 2, 3, 0, 0, 6, 7, 0, 6, 0)
));