Roll src/third_party/WebKit 48e5493c:ad9da0e (svn 202519:202521)
[chromium-blink-merge.git] / extensions / browser / error_map_unittest.cc
blobf3880c68f3ff6c63ecf878020a1e7cfc4af46049
1 // Copyright 2014 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.
5 #include "extensions/browser/error_map.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "components/crx_file/id_util.h"
11 #include "extensions/browser/extension_error.h"
12 #include "extensions/browser/extension_error_test_util.h"
13 #include "extensions/common/constants.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace extensions {
18 using error_test_util::CreateNewRuntimeError;
19 using error_test_util::CreateNewManifestError;
21 class ErrorMapUnitTest : public testing::Test {
22 public:
23 ErrorMapUnitTest() { }
24 ~ErrorMapUnitTest() override {}
26 void SetUp() override { testing::Test::SetUp(); }
28 protected:
29 ErrorMap errors_;
32 // Test adding errors, and removing them by reference, by incognito status,
33 // and in bulk.
34 TEST_F(ErrorMapUnitTest, AddAndRemoveErrors) {
35 ASSERT_EQ(0u, errors_.size());
37 const size_t kNumTotalErrors = 6;
38 const size_t kNumNonIncognitoErrors = 3;
39 const std::string kId = crx_file::id_util::GenerateId("id");
40 // Populate with both incognito and non-incognito errors (evenly distributed).
41 for (size_t i = 0; i < kNumTotalErrors; ++i) {
42 ASSERT_TRUE(errors_.AddError(
43 CreateNewRuntimeError(kId, base::UintToString(i), i % 2 == 0)));
46 // There should only be one entry in the map, since errors are stored in lists
47 // keyed by extension id.
48 EXPECT_EQ(1u, errors_.size());
50 EXPECT_EQ(kNumTotalErrors, errors_.GetErrorsForExtension(kId).size());
52 // Remove the incognito errors; three errors should remain, and all should
53 // be from non-incognito contexts.
54 std::set<std::string> affected_ids;
55 errors_.RemoveErrors(ErrorMap::Filter::IncognitoErrors(), &affected_ids);
56 const ErrorList& list = errors_.GetErrorsForExtension(kId);
57 EXPECT_EQ(kNumNonIncognitoErrors, list.size());
58 for (size_t i = 0; i < list.size(); ++i)
59 EXPECT_FALSE(list[i]->from_incognito());
60 EXPECT_EQ(1u, affected_ids.size());
61 EXPECT_TRUE(affected_ids.count(kId));
63 // Add another error for a different extension id.
64 const std::string kSecondId = crx_file::id_util::GenerateId("id2");
65 EXPECT_TRUE(errors_.AddError(CreateNewRuntimeError(kSecondId, "foo")));
67 // There should be two entries now, one for each id, and there should be one
68 // error for the second extension.
69 EXPECT_EQ(2u, errors_.size());
70 EXPECT_EQ(1u, errors_.GetErrorsForExtension(kSecondId).size());
72 // Remove all errors for the second id.
73 affected_ids.clear();
74 errors_.RemoveErrors(ErrorMap::Filter::ErrorsForExtension(kSecondId),
75 &affected_ids);
76 EXPECT_EQ(0u, errors_.GetErrorsForExtension(kSecondId).size());
77 // First extension should be unaffected.
78 EXPECT_EQ(kNumNonIncognitoErrors, errors_.GetErrorsForExtension(kId).size());
79 EXPECT_EQ(1u, affected_ids.size());
80 EXPECT_TRUE(affected_ids.count(kSecondId));
82 errors_.AddError(CreateNewManifestError(kId, "manifest error"));
83 EXPECT_EQ(kNumNonIncognitoErrors + 1,
84 errors_.GetErrorsForExtension(kId).size());
85 errors_.RemoveErrors(ErrorMap::Filter::ErrorsForExtensionWithType(
86 kId, ExtensionError::MANIFEST_ERROR), nullptr);
87 EXPECT_EQ(kNumNonIncognitoErrors, errors_.GetErrorsForExtension(kId).size());
89 const ExtensionError* added_error =
90 errors_.AddError(CreateNewManifestError(kId, "manifest error2"));
91 EXPECT_EQ(kNumNonIncognitoErrors + 1,
92 errors_.GetErrorsForExtension(kId).size());
93 std::set<int> ids;
94 ids.insert(added_error->id());
95 errors_.RemoveErrors(ErrorMap::Filter::ErrorsForExtensionWithIds(kId, ids),
96 nullptr);
97 EXPECT_EQ(kNumNonIncognitoErrors, errors_.GetErrorsForExtension(kId).size());
99 // Remove remaining errors.
100 errors_.RemoveAllErrors();
101 EXPECT_EQ(0u, errors_.size());
102 EXPECT_EQ(0u, errors_.GetErrorsForExtension(kId).size());
105 // Test that if we add enough errors, only the most recent
106 // kMaxErrorsPerExtension are kept.
107 TEST_F(ErrorMapUnitTest, ExcessiveErrorsGetCropped) {
108 ASSERT_EQ(0u, errors_.size());
110 // This constant matches one of the same name in error_console.cc.
111 const size_t kMaxErrorsPerExtension = 100;
112 const size_t kNumExtraErrors = 5;
113 const std::string kId = crx_file::id_util::GenerateId("id");
115 // Add new errors, with each error's message set to its number.
116 for (size_t i = 0; i < kMaxErrorsPerExtension + kNumExtraErrors; ++i) {
117 ASSERT_TRUE(errors_.AddError(
118 CreateNewRuntimeError(kId, base::UintToString(i))));
121 ASSERT_EQ(1u, errors_.size());
123 const ErrorList& list = errors_.GetErrorsForExtension(kId);
124 ASSERT_EQ(kMaxErrorsPerExtension, list.size());
126 // We should have popped off errors in the order they arrived, so the
127 // first stored error should be the 6th reported (zero-based)...
128 ASSERT_EQ(base::UintToString16(kNumExtraErrors),
129 list.front()->message());
130 // ..and the last stored should be the 105th reported.
131 ASSERT_EQ(base::UintToString16(kMaxErrorsPerExtension + kNumExtraErrors - 1),
132 list.back()->message());
135 // Test to ensure that the error console will not add duplicate errors, but will
136 // keep the latest version of an error.
137 TEST_F(ErrorMapUnitTest, DuplicateErrorsAreReplaced) {
138 ASSERT_EQ(0u, errors_.size());
140 const std::string kId = crx_file::id_util::GenerateId("id");
141 const size_t kNumErrors = 3u;
143 // Report three errors.
144 for (size_t i = 0; i < kNumErrors; ++i) {
145 ASSERT_TRUE(errors_.AddError(
146 CreateNewRuntimeError(kId, base::UintToString(i))));
149 // Create an error identical to the second error reported, save its
150 // location, and add it to the error map.
151 scoped_ptr<ExtensionError> runtime_error2 =
152 CreateNewRuntimeError(kId, base::UintToString(1u));
153 const ExtensionError* weak_error = runtime_error2.get();
154 ASSERT_TRUE(errors_.AddError(runtime_error2.Pass()));
156 // We should only have three errors stored, since two of the four reported
157 // were identical, and the older should have been replaced.
158 ASSERT_EQ(1u, errors_.size());
159 const ErrorList& list = errors_.GetErrorsForExtension(kId);
160 ASSERT_EQ(kNumErrors, list.size());
162 // The duplicate error should be the last reported (pointer comparison)...
163 ASSERT_EQ(weak_error, list.back());
164 // ... and should have two reported occurrences.
165 ASSERT_EQ(2u, list.back()->occurrences());
168 } // namespace extensions