This source file includes following definitions.
- SetUp
- TEST_F
- TEST_F
- TEST_F
#include "extensions/browser/error_map.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "extensions/browser/extension_error.h"
#include "extensions/browser/extension_error_test_util.h"
#include "extensions/common/constants.h"
#include "extensions/common/id_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace extensions {
using error_test_util::CreateNewRuntimeError;
class ErrorMapUnitTest : public testing::Test {
public:
ErrorMapUnitTest() { }
virtual ~ErrorMapUnitTest() { }
virtual void SetUp() OVERRIDE {
testing::Test::SetUp();
}
protected:
ErrorMap errors_;
};
TEST_F(ErrorMapUnitTest, AddAndRemoveErrors) {
ASSERT_EQ(0u, errors_.size());
const size_t kNumTotalErrors = 6;
const size_t kNumNonIncognitoErrors = 3;
const std::string kId = id_util::GenerateId("id");
for (size_t i = 0; i < kNumTotalErrors; ++i) {
ASSERT_TRUE(errors_.AddError(
CreateNewRuntimeError(kId, base::UintToString(i), i % 2 == 0)));
}
ASSERT_EQ(1u, errors_.size());
ASSERT_EQ(kNumTotalErrors, errors_.GetErrorsForExtension(kId).size());
errors_.RemoveIncognitoErrors();
const ErrorList& list = errors_.GetErrorsForExtension(kId);
ASSERT_EQ(kNumNonIncognitoErrors, list.size());
for (size_t i = 0; i < list.size(); ++i)
ASSERT_FALSE(list[i]->from_incognito());
const std::string kSecondId = id_util::GenerateId("id2");
ASSERT_TRUE(errors_.AddError(CreateNewRuntimeError(kSecondId, "foo")));
ASSERT_EQ(2u, errors_.size());
ASSERT_EQ(1u, errors_.GetErrorsForExtension(kSecondId).size());
errors_.Remove(kSecondId);
ASSERT_EQ(1u, errors_.size());
ASSERT_EQ(0u, errors_.GetErrorsForExtension(kSecondId).size());
ASSERT_EQ(kNumNonIncognitoErrors,
errors_.GetErrorsForExtension(kId).size());
errors_.RemoveAllErrors();
ASSERT_EQ(0u, errors_.size());
ASSERT_EQ(0u, errors_.GetErrorsForExtension(kId).size());
}
TEST_F(ErrorMapUnitTest, ExcessiveErrorsGetCropped) {
ASSERT_EQ(0u, errors_.size());
const size_t kMaxErrorsPerExtension = 100;
const size_t kNumExtraErrors = 5;
const std::string kId = id_util::GenerateId("id");
for (size_t i = 0; i < kMaxErrorsPerExtension + kNumExtraErrors; ++i) {
ASSERT_TRUE(errors_.AddError(
CreateNewRuntimeError(kId, base::UintToString(i))));
}
ASSERT_EQ(1u, errors_.size());
const ErrorList& list = errors_.GetErrorsForExtension(kId);
ASSERT_EQ(kMaxErrorsPerExtension, list.size());
ASSERT_EQ(base::UintToString16(kNumExtraErrors),
list.front()->message());
ASSERT_EQ(base::UintToString16(kMaxErrorsPerExtension + kNumExtraErrors - 1),
list.back()->message());
}
TEST_F(ErrorMapUnitTest, DuplicateErrorsAreReplaced) {
ASSERT_EQ(0u, errors_.size());
const std::string kId = id_util::GenerateId("id");
const size_t kNumErrors = 3u;
for (size_t i = 0; i < kNumErrors; ++i) {
ASSERT_TRUE(errors_.AddError(
CreateNewRuntimeError(kId, base::UintToString(i))));
}
scoped_ptr<ExtensionError> runtime_error2 =
CreateNewRuntimeError(kId, base::UintToString(1u));
const ExtensionError* weak_error = runtime_error2.get();
ASSERT_TRUE(errors_.AddError(runtime_error2.Pass()));
ASSERT_EQ(1u, errors_.size());
const ErrorList& list = errors_.GetErrorsForExtension(kId);
ASSERT_EQ(kNumErrors, list.size());
ASSERT_EQ(weak_error, list.back());
ASSERT_EQ(2u, list.back()->occurrences());
}
}