Fix search results being clipped in app list.
[chromium-blink-merge.git] / extensions / browser / error_map_unittest.cc
blobb8ae20d062cee017f5565632d95442dfafe982d6
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 errors_.RemoveErrors(ErrorMap::Filter::IncognitoErrors());
55 const ErrorList& list = errors_.GetErrorsForExtension(kId);
56 EXPECT_EQ(kNumNonIncognitoErrors, list.size());
57 for (size_t i = 0; i < list.size(); ++i)
58 EXPECT_FALSE(list[i]->from_incognito());
60 // Add another error for a different extension id.
61 const std::string kSecondId = crx_file::id_util::GenerateId("id2");
62 EXPECT_TRUE(errors_.AddError(CreateNewRuntimeError(kSecondId, "foo")));
64 // There should be two entries now, one for each id, and there should be one
65 // error for the second extension.
66 EXPECT_EQ(2u, errors_.size());
67 EXPECT_EQ(1u, errors_.GetErrorsForExtension(kSecondId).size());
69 // Remove all errors for the second id.
70 errors_.RemoveErrors(ErrorMap::Filter::ErrorsForExtension(kSecondId));
71 EXPECT_EQ(0u, errors_.GetErrorsForExtension(kSecondId).size());
72 // First extension should be unaffected.
73 EXPECT_EQ(kNumNonIncognitoErrors, errors_.GetErrorsForExtension(kId).size());
75 errors_.AddError(CreateNewManifestError(kId, "manifest error"));
76 EXPECT_EQ(kNumNonIncognitoErrors + 1,
77 errors_.GetErrorsForExtension(kId).size());
78 errors_.RemoveErrors(ErrorMap::Filter::ErrorsForExtensionWithType(
79 kId, ExtensionError::MANIFEST_ERROR));
80 EXPECT_EQ(kNumNonIncognitoErrors, errors_.GetErrorsForExtension(kId).size());
82 const ExtensionError* added_error =
83 errors_.AddError(CreateNewManifestError(kId, "manifest error2"));
84 EXPECT_EQ(kNumNonIncognitoErrors + 1,
85 errors_.GetErrorsForExtension(kId).size());
86 std::set<int> ids;
87 ids.insert(added_error->id());
88 errors_.RemoveErrors(ErrorMap::Filter::ErrorsForExtensionWithIds(kId, ids));
89 EXPECT_EQ(kNumNonIncognitoErrors, errors_.GetErrorsForExtension(kId).size());
91 // Remove remaining errors.
92 errors_.RemoveAllErrors();
93 EXPECT_EQ(0u, errors_.size());
94 EXPECT_EQ(0u, errors_.GetErrorsForExtension(kId).size());
97 // Test that if we add enough errors, only the most recent
98 // kMaxErrorsPerExtension are kept.
99 TEST_F(ErrorMapUnitTest, ExcessiveErrorsGetCropped) {
100 ASSERT_EQ(0u, errors_.size());
102 // This constant matches one of the same name in error_console.cc.
103 const size_t kMaxErrorsPerExtension = 100;
104 const size_t kNumExtraErrors = 5;
105 const std::string kId = crx_file::id_util::GenerateId("id");
107 // Add new errors, with each error's message set to its number.
108 for (size_t i = 0; i < kMaxErrorsPerExtension + kNumExtraErrors; ++i) {
109 ASSERT_TRUE(errors_.AddError(
110 CreateNewRuntimeError(kId, base::UintToString(i))));
113 ASSERT_EQ(1u, errors_.size());
115 const ErrorList& list = errors_.GetErrorsForExtension(kId);
116 ASSERT_EQ(kMaxErrorsPerExtension, list.size());
118 // We should have popped off errors in the order they arrived, so the
119 // first stored error should be the 6th reported (zero-based)...
120 ASSERT_EQ(base::UintToString16(kNumExtraErrors),
121 list.front()->message());
122 // ..and the last stored should be the 105th reported.
123 ASSERT_EQ(base::UintToString16(kMaxErrorsPerExtension + kNumExtraErrors - 1),
124 list.back()->message());
127 // Test to ensure that the error console will not add duplicate errors, but will
128 // keep the latest version of an error.
129 TEST_F(ErrorMapUnitTest, DuplicateErrorsAreReplaced) {
130 ASSERT_EQ(0u, errors_.size());
132 const std::string kId = crx_file::id_util::GenerateId("id");
133 const size_t kNumErrors = 3u;
135 // Report three errors.
136 for (size_t i = 0; i < kNumErrors; ++i) {
137 ASSERT_TRUE(errors_.AddError(
138 CreateNewRuntimeError(kId, base::UintToString(i))));
141 // Create an error identical to the second error reported, save its
142 // location, and add it to the error map.
143 scoped_ptr<ExtensionError> runtime_error2 =
144 CreateNewRuntimeError(kId, base::UintToString(1u));
145 const ExtensionError* weak_error = runtime_error2.get();
146 ASSERT_TRUE(errors_.AddError(runtime_error2.Pass()));
148 // We should only have three errors stored, since two of the four reported
149 // were identical, and the older should have been replaced.
150 ASSERT_EQ(1u, errors_.size());
151 const ErrorList& list = errors_.GetErrorsForExtension(kId);
152 ASSERT_EQ(kNumErrors, list.size());
154 // The duplicate error should be the last reported (pointer comparison)...
155 ASSERT_EQ(weak_error, list.back());
156 // ... and should have two reported occurrences.
157 ASSERT_EQ(2u, list.back()->occurrences());
160 } // namespace extensions