[Smart Lock] Record a detailed UMA metric for each unlock attempt by Smart Lock users.
[chromium-blink-merge.git] / extensions / browser / error_map_unittest.cc
blobd393624f4a97392f2c701dcf0a01abfd9d5bb520
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;
20 class ErrorMapUnitTest : public testing::Test {
21 public:
22 ErrorMapUnitTest() { }
23 ~ErrorMapUnitTest() override {}
25 void SetUp() override { testing::Test::SetUp(); }
27 protected:
28 ErrorMap errors_;
31 // Test adding errors, and removing them by reference, by incognito status,
32 // and in bulk.
33 TEST_F(ErrorMapUnitTest, AddAndRemoveErrors) {
34 ASSERT_EQ(0u, errors_.size());
36 const size_t kNumTotalErrors = 6;
37 const size_t kNumNonIncognitoErrors = 3;
38 const std::string kId = crx_file::id_util::GenerateId("id");
39 // Populate with both incognito and non-incognito errors (evenly distributed).
40 for (size_t i = 0; i < kNumTotalErrors; ++i) {
41 ASSERT_TRUE(errors_.AddError(
42 CreateNewRuntimeError(kId, base::UintToString(i), i % 2 == 0)));
45 // There should only be one entry in the map, since errors are stored in lists
46 // keyed by extension id.
47 ASSERT_EQ(1u, errors_.size());
49 ASSERT_EQ(kNumTotalErrors, errors_.GetErrorsForExtension(kId).size());
51 // Remove the incognito errors; three errors should remain, and all should
52 // be from non-incognito contexts.
53 errors_.RemoveIncognitoErrors();
54 const ErrorList& list = errors_.GetErrorsForExtension(kId);
55 ASSERT_EQ(kNumNonIncognitoErrors, list.size());
56 for (size_t i = 0; i < list.size(); ++i)
57 ASSERT_FALSE(list[i]->from_incognito());
59 // Add another error for a different extension id.
60 const std::string kSecondId = crx_file::id_util::GenerateId("id2");
61 ASSERT_TRUE(errors_.AddError(CreateNewRuntimeError(kSecondId, "foo")));
63 // There should be two entries now, one for each id, and there should be one
64 // error for the second extension.
65 ASSERT_EQ(2u, errors_.size());
66 ASSERT_EQ(1u, errors_.GetErrorsForExtension(kSecondId).size());
68 // Remove all errors for the second id.
69 errors_.Remove(kSecondId);
70 ASSERT_EQ(1u, errors_.size());
71 ASSERT_EQ(0u, errors_.GetErrorsForExtension(kSecondId).size());
72 // First extension should be unaffected.
73 ASSERT_EQ(kNumNonIncognitoErrors,
74 errors_.GetErrorsForExtension(kId).size());
76 // Remove remaining errors.
77 errors_.RemoveAllErrors();
78 ASSERT_EQ(0u, errors_.size());
79 ASSERT_EQ(0u, errors_.GetErrorsForExtension(kId).size());
82 // Test that if we add enough errors, only the most recent
83 // kMaxErrorsPerExtension are kept.
84 TEST_F(ErrorMapUnitTest, ExcessiveErrorsGetCropped) {
85 ASSERT_EQ(0u, errors_.size());
87 // This constant matches one of the same name in error_console.cc.
88 const size_t kMaxErrorsPerExtension = 100;
89 const size_t kNumExtraErrors = 5;
90 const std::string kId = crx_file::id_util::GenerateId("id");
92 // Add new errors, with each error's message set to its number.
93 for (size_t i = 0; i < kMaxErrorsPerExtension + kNumExtraErrors; ++i) {
94 ASSERT_TRUE(errors_.AddError(
95 CreateNewRuntimeError(kId, base::UintToString(i))));
98 ASSERT_EQ(1u, errors_.size());
100 const ErrorList& list = errors_.GetErrorsForExtension(kId);
101 ASSERT_EQ(kMaxErrorsPerExtension, list.size());
103 // We should have popped off errors in the order they arrived, so the
104 // first stored error should be the 6th reported (zero-based)...
105 ASSERT_EQ(base::UintToString16(kNumExtraErrors),
106 list.front()->message());
107 // ..and the last stored should be the 105th reported.
108 ASSERT_EQ(base::UintToString16(kMaxErrorsPerExtension + kNumExtraErrors - 1),
109 list.back()->message());
112 // Test to ensure that the error console will not add duplicate errors, but will
113 // keep the latest version of an error.
114 TEST_F(ErrorMapUnitTest, DuplicateErrorsAreReplaced) {
115 ASSERT_EQ(0u, errors_.size());
117 const std::string kId = crx_file::id_util::GenerateId("id");
118 const size_t kNumErrors = 3u;
120 // Report three errors.
121 for (size_t i = 0; i < kNumErrors; ++i) {
122 ASSERT_TRUE(errors_.AddError(
123 CreateNewRuntimeError(kId, base::UintToString(i))));
126 // Create an error identical to the second error reported, save its
127 // location, and add it to the error map.
128 scoped_ptr<ExtensionError> runtime_error2 =
129 CreateNewRuntimeError(kId, base::UintToString(1u));
130 const ExtensionError* weak_error = runtime_error2.get();
131 ASSERT_TRUE(errors_.AddError(runtime_error2.Pass()));
133 // We should only have three errors stored, since two of the four reported
134 // were identical, and the older should have been replaced.
135 ASSERT_EQ(1u, errors_.size());
136 const ErrorList& list = errors_.GetErrorsForExtension(kId);
137 ASSERT_EQ(kNumErrors, list.size());
139 // The duplicate error should be the last reported (pointer comparison)...
140 ASSERT_EQ(weak_error, list.back());
141 // ... and should have two reported occurrences.
142 ASSERT_EQ(2u, list.back()->occurrences());
145 } // namespace extensions