| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/error_map.h" | 5 #include "extensions/browser/error_map.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "components/crx_file/id_util.h" | 10 #include "components/crx_file/id_util.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 } | 44 } |
| 45 | 45 |
| 46 // There should only be one entry in the map, since errors are stored in lists | 46 // There should only be one entry in the map, since errors are stored in lists |
| 47 // keyed by extension id. | 47 // keyed by extension id. |
| 48 EXPECT_EQ(1u, errors_.size()); | 48 EXPECT_EQ(1u, errors_.size()); |
| 49 | 49 |
| 50 EXPECT_EQ(kNumTotalErrors, errors_.GetErrorsForExtension(kId).size()); | 50 EXPECT_EQ(kNumTotalErrors, errors_.GetErrorsForExtension(kId).size()); |
| 51 | 51 |
| 52 // Remove the incognito errors; three errors should remain, and all should | 52 // Remove the incognito errors; three errors should remain, and all should |
| 53 // be from non-incognito contexts. | 53 // be from non-incognito contexts. |
| 54 errors_.RemoveErrors(ErrorMap::Filter::IncognitoErrors()); | 54 std::set<std::string> affected_ids; |
| 55 errors_.RemoveErrors(ErrorMap::Filter::IncognitoErrors(), &affected_ids); |
| 55 const ErrorList& list = errors_.GetErrorsForExtension(kId); | 56 const ErrorList& list = errors_.GetErrorsForExtension(kId); |
| 56 EXPECT_EQ(kNumNonIncognitoErrors, list.size()); | 57 EXPECT_EQ(kNumNonIncognitoErrors, list.size()); |
| 57 for (size_t i = 0; i < list.size(); ++i) | 58 for (size_t i = 0; i < list.size(); ++i) |
| 58 EXPECT_FALSE(list[i]->from_incognito()); | 59 EXPECT_FALSE(list[i]->from_incognito()); |
| 60 EXPECT_EQ(1u, affected_ids.size()); |
| 61 EXPECT_TRUE(affected_ids.count(kId)); |
| 59 | 62 |
| 60 // Add another error for a different extension id. | 63 // Add another error for a different extension id. |
| 61 const std::string kSecondId = crx_file::id_util::GenerateId("id2"); | 64 const std::string kSecondId = crx_file::id_util::GenerateId("id2"); |
| 62 EXPECT_TRUE(errors_.AddError(CreateNewRuntimeError(kSecondId, "foo"))); | 65 EXPECT_TRUE(errors_.AddError(CreateNewRuntimeError(kSecondId, "foo"))); |
| 63 | 66 |
| 64 // There should be two entries now, one for each id, and there should be one | 67 // There should be two entries now, one for each id, and there should be one |
| 65 // error for the second extension. | 68 // error for the second extension. |
| 66 EXPECT_EQ(2u, errors_.size()); | 69 EXPECT_EQ(2u, errors_.size()); |
| 67 EXPECT_EQ(1u, errors_.GetErrorsForExtension(kSecondId).size()); | 70 EXPECT_EQ(1u, errors_.GetErrorsForExtension(kSecondId).size()); |
| 68 | 71 |
| 69 // Remove all errors for the second id. | 72 // Remove all errors for the second id. |
| 70 errors_.RemoveErrors(ErrorMap::Filter::ErrorsForExtension(kSecondId)); | 73 affected_ids.clear(); |
| 74 errors_.RemoveErrors(ErrorMap::Filter::ErrorsForExtension(kSecondId), |
| 75 &affected_ids); |
| 71 EXPECT_EQ(0u, errors_.GetErrorsForExtension(kSecondId).size()); | 76 EXPECT_EQ(0u, errors_.GetErrorsForExtension(kSecondId).size()); |
| 72 // First extension should be unaffected. | 77 // First extension should be unaffected. |
| 73 EXPECT_EQ(kNumNonIncognitoErrors, errors_.GetErrorsForExtension(kId).size()); | 78 EXPECT_EQ(kNumNonIncognitoErrors, errors_.GetErrorsForExtension(kId).size()); |
| 79 EXPECT_EQ(1u, affected_ids.size()); |
| 80 EXPECT_TRUE(affected_ids.count(kSecondId)); |
| 74 | 81 |
| 75 errors_.AddError(CreateNewManifestError(kId, "manifest error")); | 82 errors_.AddError(CreateNewManifestError(kId, "manifest error")); |
| 76 EXPECT_EQ(kNumNonIncognitoErrors + 1, | 83 EXPECT_EQ(kNumNonIncognitoErrors + 1, |
| 77 errors_.GetErrorsForExtension(kId).size()); | 84 errors_.GetErrorsForExtension(kId).size()); |
| 78 errors_.RemoveErrors(ErrorMap::Filter::ErrorsForExtensionWithType( | 85 errors_.RemoveErrors(ErrorMap::Filter::ErrorsForExtensionWithType( |
| 79 kId, ExtensionError::MANIFEST_ERROR)); | 86 kId, ExtensionError::MANIFEST_ERROR), nullptr); |
| 80 EXPECT_EQ(kNumNonIncognitoErrors, errors_.GetErrorsForExtension(kId).size()); | 87 EXPECT_EQ(kNumNonIncognitoErrors, errors_.GetErrorsForExtension(kId).size()); |
| 81 | 88 |
| 82 const ExtensionError* added_error = | 89 const ExtensionError* added_error = |
| 83 errors_.AddError(CreateNewManifestError(kId, "manifest error2")); | 90 errors_.AddError(CreateNewManifestError(kId, "manifest error2")); |
| 84 EXPECT_EQ(kNumNonIncognitoErrors + 1, | 91 EXPECT_EQ(kNumNonIncognitoErrors + 1, |
| 85 errors_.GetErrorsForExtension(kId).size()); | 92 errors_.GetErrorsForExtension(kId).size()); |
| 86 std::set<int> ids; | 93 std::set<int> ids; |
| 87 ids.insert(added_error->id()); | 94 ids.insert(added_error->id()); |
| 88 errors_.RemoveErrors(ErrorMap::Filter::ErrorsForExtensionWithIds(kId, ids)); | 95 errors_.RemoveErrors(ErrorMap::Filter::ErrorsForExtensionWithIds(kId, ids), |
| 96 nullptr); |
| 89 EXPECT_EQ(kNumNonIncognitoErrors, errors_.GetErrorsForExtension(kId).size()); | 97 EXPECT_EQ(kNumNonIncognitoErrors, errors_.GetErrorsForExtension(kId).size()); |
| 90 | 98 |
| 91 // Remove remaining errors. | 99 // Remove remaining errors. |
| 92 errors_.RemoveAllErrors(); | 100 errors_.RemoveAllErrors(); |
| 93 EXPECT_EQ(0u, errors_.size()); | 101 EXPECT_EQ(0u, errors_.size()); |
| 94 EXPECT_EQ(0u, errors_.GetErrorsForExtension(kId).size()); | 102 EXPECT_EQ(0u, errors_.GetErrorsForExtension(kId).size()); |
| 95 } | 103 } |
| 96 | 104 |
| 97 // Test that if we add enough errors, only the most recent | 105 // Test that if we add enough errors, only the most recent |
| 98 // kMaxErrorsPerExtension are kept. | 106 // kMaxErrorsPerExtension are kept. |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 const ErrorList& list = errors_.GetErrorsForExtension(kId); | 159 const ErrorList& list = errors_.GetErrorsForExtension(kId); |
| 152 ASSERT_EQ(kNumErrors, list.size()); | 160 ASSERT_EQ(kNumErrors, list.size()); |
| 153 | 161 |
| 154 // The duplicate error should be the last reported (pointer comparison)... | 162 // The duplicate error should be the last reported (pointer comparison)... |
| 155 ASSERT_EQ(weak_error, list.back()); | 163 ASSERT_EQ(weak_error, list.back()); |
| 156 // ... and should have two reported occurrences. | 164 // ... and should have two reported occurrences. |
| 157 ASSERT_EQ(2u, list.back()->occurrences()); | 165 ASSERT_EQ(2u, list.back()->occurrences()); |
| 158 } | 166 } |
| 159 | 167 |
| 160 } // namespace extensions | 168 } // namespace extensions |
| OLD | NEW |