OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/extensions/error_console/error_console.h" | 5 #include "chrome/browser/extensions/error_console/error_console.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/ref_counted.h" |
8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
9 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
11 #include "chrome/common/extensions/features/feature_channel.h" | 12 #include "chrome/common/extensions/features/feature_channel.h" |
12 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
13 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
14 #include "extensions/browser/extension_error.h" | 15 #include "extensions/browser/extension_error.h" |
15 #include "extensions/browser/extension_error_test_util.h" | 16 #include "extensions/browser/extension_error_test_util.h" |
16 #include "extensions/browser/extension_registry.h" | 17 #include "extensions/browser/extension_registry.h" |
17 #include "extensions/common/constants.h" | 18 #include "extensions/common/constants.h" |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 EXPECT_FALSE(error_console_->enabled()); | 122 EXPECT_FALSE(error_console_->enabled()); |
122 EXPECT_FALSE(error_console_->IsEnabledForChromeExtensionsPage()); | 123 EXPECT_FALSE(error_console_->IsEnabledForChromeExtensionsPage()); |
123 EXPECT_FALSE(error_console_->IsEnabledForAppsDeveloperTools()); | 124 EXPECT_FALSE(error_console_->IsEnabledForAppsDeveloperTools()); |
124 } | 125 } |
125 | 126 |
126 // Test that errors are successfully reported. This is a simple test, since it | 127 // Test that errors are successfully reported. This is a simple test, since it |
127 // is tested more thoroughly in extensions/browser/error_map_unittest.cc | 128 // is tested more thoroughly in extensions/browser/error_map_unittest.cc |
128 TEST_F(ErrorConsoleUnitTest, ReportErrors) { | 129 TEST_F(ErrorConsoleUnitTest, ReportErrors) { |
129 const size_t kNumTotalErrors = 6; | 130 const size_t kNumTotalErrors = 6; |
130 const std::string kId = id_util::GenerateId("id"); | 131 const std::string kId = id_util::GenerateId("id"); |
| 132 error_console_->set_default_reporting_for_test(ExtensionError::MANIFEST_ERROR, |
| 133 true); |
131 ASSERT_EQ(0u, error_console_->GetErrorsForExtension(kId).size()); | 134 ASSERT_EQ(0u, error_console_->GetErrorsForExtension(kId).size()); |
132 | 135 |
133 for (size_t i = 0; i < kNumTotalErrors; ++i) { | 136 for (size_t i = 0; i < kNumTotalErrors; ++i) { |
134 error_console_->ReportError( | 137 error_console_->ReportError( |
135 CreateNewManifestError(kId, base::UintToString(i))); | 138 CreateNewManifestError(kId, base::UintToString(i))); |
136 } | 139 } |
137 | 140 |
138 ASSERT_EQ(kNumTotalErrors, error_console_->GetErrorsForExtension(kId).size()); | 141 ASSERT_EQ(kNumTotalErrors, error_console_->GetErrorsForExtension(kId).size()); |
139 } | 142 } |
140 | 143 |
141 TEST_F(ErrorConsoleUnitTest, DontStoreErrorsWithoutEnablingType) { | 144 TEST_F(ErrorConsoleUnitTest, DontStoreErrorsWithoutEnablingType) { |
142 // Disable default runtime error reporting. | 145 // Disable default runtime error reporting, and enable default manifest error |
| 146 // reporting. |
143 error_console_->set_default_reporting_for_test(ExtensionError::RUNTIME_ERROR, | 147 error_console_->set_default_reporting_for_test(ExtensionError::RUNTIME_ERROR, |
144 false); | 148 false); |
| 149 error_console_->set_default_reporting_for_test(ExtensionError::MANIFEST_ERROR, |
| 150 true); |
145 | 151 |
146 const std::string kId = id_util::GenerateId("id"); | 152 const std::string kId = id_util::GenerateId("id"); |
147 | 153 |
148 // Try to report a runtime error - it should be ignored. | 154 // Try to report a runtime error - it should be ignored. |
149 error_console_->ReportError(CreateNewRuntimeError(kId, "a")); | 155 error_console_->ReportError(CreateNewRuntimeError(kId, "a")); |
150 ASSERT_EQ(0u, error_console_->GetErrorsForExtension(kId).size()); | 156 ASSERT_EQ(0u, error_console_->GetErrorsForExtension(kId).size()); |
151 | 157 |
152 // Check that manifest errors are reported successfully. | 158 // Check that manifest errors are reported successfully. |
153 error_console_->ReportError(CreateNewManifestError(kId, "b")); | 159 error_console_->ReportError(CreateNewManifestError(kId, "b")); |
154 ASSERT_EQ(1u, error_console_->GetErrorsForExtension(kId).size()); | 160 ASSERT_EQ(1u, error_console_->GetErrorsForExtension(kId).size()); |
(...skipping 23 matching lines...) Expand all Loading... |
178 | 184 |
179 // By reverting to default reporting, we should again allow manifest errors, | 185 // By reverting to default reporting, we should again allow manifest errors, |
180 // but not runtime errors. | 186 // but not runtime errors. |
181 error_console_->UseDefaultReportingForExtension(kId); | 187 error_console_->UseDefaultReportingForExtension(kId); |
182 error_console_->ReportError(CreateNewManifestError(kId, "h")); | 188 error_console_->ReportError(CreateNewManifestError(kId, "h")); |
183 ASSERT_EQ(4u, error_console_->GetErrorsForExtension(kId).size()); | 189 ASSERT_EQ(4u, error_console_->GetErrorsForExtension(kId).size()); |
184 error_console_->ReportError(CreateNewRuntimeError(kId, "i")); | 190 error_console_->ReportError(CreateNewRuntimeError(kId, "i")); |
185 ASSERT_EQ(4u, error_console_->GetErrorsForExtension(kId).size()); | 191 ASSERT_EQ(4u, error_console_->GetErrorsForExtension(kId).size()); |
186 } | 192 } |
187 | 193 |
| 194 // Test that we only store errors by default for unpacked extensions, and that |
| 195 // assigning a preference to any extension overrides the defaults. |
| 196 TEST_F(ErrorConsoleUnitTest, TestDefaultStoringPrefs) { |
| 197 // For this, we need actual extensions. |
| 198 scoped_refptr<const Extension> unpacked_extension = |
| 199 ExtensionBuilder() |
| 200 .SetManifest(DictionaryBuilder().Set("name", "unpacked") |
| 201 .Set("version", "0.0.1") |
| 202 .Set("manifest_version", 2) |
| 203 .Build()) |
| 204 .SetLocation(Manifest::UNPACKED) |
| 205 .SetID(id_util::GenerateId("unpacked")) |
| 206 .Build(); |
| 207 scoped_refptr<const Extension> packed_extension = |
| 208 ExtensionBuilder() |
| 209 .SetManifest(DictionaryBuilder().Set("name", "packed") |
| 210 .Set("version", "0.0.1") |
| 211 .Set("manifest_version", 2) |
| 212 .Build()) |
| 213 .SetLocation(Manifest::INTERNAL) |
| 214 .SetID(id_util::GenerateId("packed")) |
| 215 .Build(); |
| 216 |
| 217 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get()); |
| 218 registry->AddEnabled(unpacked_extension); |
| 219 registry->AddEnabled(packed_extension); |
| 220 |
| 221 // We should start with a clean slate. |
| 222 EXPECT_EQ(0u, error_console_->GetErrorsForExtension( |
| 223 unpacked_extension->id()).size()); |
| 224 EXPECT_EQ(0u, error_console_->GetErrorsForExtension( |
| 225 packed_extension->id()).size()); |
| 226 |
| 227 // Errors should be ignored by default for the packed extension. |
| 228 error_console_->ReportError( |
| 229 CreateNewManifestError(packed_extension->id(), "manifest error 1")); |
| 230 error_console_->ReportError( |
| 231 CreateNewRuntimeError(packed_extension->id(), "runtime error 1")); |
| 232 EXPECT_EQ(0u, error_console_->GetErrorsForExtension( |
| 233 packed_extension->id()).size()); |
| 234 |
| 235 // Errors should be reported by default for the unpacked extension. |
| 236 error_console_->ReportError( |
| 237 CreateNewManifestError(unpacked_extension->id(), "manifest error 2")); |
| 238 error_console_->ReportError( |
| 239 CreateNewRuntimeError(unpacked_extension->id(), "runtime error 2")); |
| 240 EXPECT_EQ(2u, error_console_->GetErrorsForExtension( |
| 241 unpacked_extension->id()).size()); |
| 242 |
| 243 // Registering a preference should override this for both types of extensions |
| 244 // (should be able to enable errors for packed, or disable errors for |
| 245 // unpacked). |
| 246 error_console_->SetReportingForExtension(packed_extension->id(), |
| 247 ExtensionError::RUNTIME_ERROR, |
| 248 true); |
| 249 error_console_->ReportError( |
| 250 CreateNewRuntimeError(packed_extension->id(), "runtime error 3")); |
| 251 EXPECT_EQ(1u, error_console_->GetErrorsForExtension( |
| 252 packed_extension->id()).size()); |
| 253 |
| 254 error_console_->SetReportingForExtension(unpacked_extension->id(), |
| 255 ExtensionError::RUNTIME_ERROR, |
| 256 false); |
| 257 error_console_->ReportError( |
| 258 CreateNewRuntimeError(packed_extension->id(), "runtime error 4")); |
| 259 EXPECT_EQ(2u, // We should still have the first two errors. |
| 260 error_console_->GetErrorsForExtension( |
| 261 unpacked_extension->id()).size()); |
| 262 } |
| 263 |
188 } // namespace extensions | 264 } // namespace extensions |
OLD | NEW |