| 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/command_line.h" |
| 7 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 8 #include "base/logging.h" | 9 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/prefs/pref_service.h" |
| 10 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 11 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 14 #include "content/public/common/url_constants.h" | 18 #include "content/public/common/url_constants.h" |
| 15 #include "extensions/browser/extension_error.h" | 19 #include "extensions/browser/extension_error.h" |
| 16 #include "extensions/common/constants.h" | 20 #include "extensions/common/constants.h" |
| 21 #include "extensions/common/id_util.h" |
| 22 #include "extensions/common/switches.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 24 |
| 19 using base::string16; | 25 using base::string16; |
| 20 using base::UTF8ToUTF16; | 26 using base::UTF8ToUTF16; |
| 21 | 27 |
| 22 namespace extensions { | 28 namespace extensions { |
| 23 | 29 |
| 24 namespace { | 30 namespace { |
| 25 | 31 |
| 26 const char kExecutionContextURLKey[] = "executionContextURL"; | 32 const char kExecutionContextURLKey[] = "executionContextURL"; |
| 27 const char kStackTraceKey[] = "stackTrace"; | 33 const char kStackTraceKey[] = "stackTrace"; |
| 28 | 34 |
| 29 string16 CreateErrorDetails(const std::string& extension_id) { | 35 string16 CreateErrorDetails(const std::string& extension_id) { |
| 30 base::DictionaryValue value; | 36 base::DictionaryValue value; |
| 31 value.SetString( | 37 value.SetString( |
| 32 kExecutionContextURLKey, | 38 kExecutionContextURLKey, |
| 33 std::string(kExtensionScheme) + | 39 std::string(kExtensionScheme) + |
| 34 content::kStandardSchemeSeparator + | 40 content::kStandardSchemeSeparator + |
| 35 extension_id); | 41 extension_id); |
| 36 value.Set(kStackTraceKey, new ListValue); | 42 value.Set(kStackTraceKey, new ListValue); |
| 37 std::string json_utf8; | 43 std::string json_utf8; |
| 38 base::JSONWriter::Write(&value, &json_utf8); | 44 base::JSONWriter::Write(&value, &json_utf8); |
| 39 return UTF8ToUTF16(json_utf8); | 45 return UTF8ToUTF16(json_utf8); |
| 40 } | 46 } |
| 41 | 47 |
| 42 scoped_ptr<const ExtensionError> CreateNewRuntimeError( | 48 scoped_ptr<const ExtensionError> CreateNewRuntimeError( |
| 43 bool from_incognito, | 49 bool from_incognito, |
| 44 const std::string& extension_id, | 50 const std::string& extension_id, |
| 45 const string16& message) { | 51 const string16& message) { |
| 46 return scoped_ptr<const ExtensionError>(new JavascriptRuntimeError( | 52 return scoped_ptr<const ExtensionError>(new RuntimeError( |
| 47 from_incognito, | 53 from_incognito, |
| 48 UTF8ToUTF16("source"), | 54 UTF8ToUTF16("source"), |
| 49 message, | 55 message, |
| 50 logging::LOG_INFO, | 56 logging::LOG_INFO, |
| 51 CreateErrorDetails(extension_id))); | 57 CreateErrorDetails(extension_id))); |
| 52 } | 58 } |
| 53 | 59 |
| 54 } // namespace | 60 } // namespace |
| 55 | 61 |
| 56 class ErrorConsoleUnitTest : public testing::Test { | 62 class ErrorConsoleUnitTest : public testing::Test { |
| 57 public: | 63 public: |
| 58 ErrorConsoleUnitTest() : | 64 ErrorConsoleUnitTest() : error_console_(NULL) { } |
| 59 profile_(new TestingProfile), | 65 virtual ~ErrorConsoleUnitTest() { } |
| 60 error_console_(ErrorConsole::Get(profile_.get())) { | 66 |
| 67 virtual void SetUp() OVERRIDE { |
| 68 testing::Test::SetUp(); |
| 69 |
| 70 // Errors are only kept if we have the necessary command line switch and |
| 71 // have Developer Mode enabled. |
| 72 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 73 switches::kEnableErrorConsole); |
| 74 profile_.reset(new TestingProfile); |
| 75 profile_->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode, true); |
| 76 error_console_ = ErrorConsole::Get(profile_.get()); |
| 61 } | 77 } |
| 62 virtual ~ErrorConsoleUnitTest() { } | |
| 63 | 78 |
| 64 protected: | 79 protected: |
| 65 scoped_ptr<TestingProfile> profile_; | 80 scoped_ptr<TestingProfile> profile_; |
| 66 ErrorConsole* error_console_; | 81 ErrorConsole* error_console_; |
| 67 }; | 82 }; |
| 68 | 83 |
| 69 // Test adding errors, and removing them by reference, by incognito status, | 84 // Test adding errors, and removing them by reference, by incognito status, |
| 70 // and in bulk. | 85 // and in bulk. |
| 71 TEST_F(ErrorConsoleUnitTest, AddAndRemoveErrors) { | 86 TEST_F(ErrorConsoleUnitTest, AddAndRemoveErrors) { |
| 72 ASSERT_EQ(0u, error_console_->errors().size()); | 87 ASSERT_EQ(0u, error_console_->errors().size()); |
| 73 | 88 |
| 74 const size_t kNumTotalErrors = 6; | 89 const size_t kNumTotalErrors = 6; |
| 75 const size_t kNumNonIncognitoErrors = 3; | 90 const size_t kNumNonIncognitoErrors = 3; |
| 76 const char kId[] = "id"; | 91 const std::string kId = id_util::GenerateId("id"); |
| 77 // Populate with both incognito and non-incognito errors (evenly distributed). | 92 // Populate with both incognito and non-incognito errors (evenly distributed). |
| 78 for (size_t i = 0; i < kNumTotalErrors; ++i) { | 93 for (size_t i = 0; i < kNumTotalErrors; ++i) { |
| 79 error_console_->ReportError( | 94 error_console_->ReportError( |
| 80 CreateNewRuntimeError(i % 2 == 0, kId, string16())); | 95 CreateNewRuntimeError(i % 2 == 0, kId, base::UintToString16(i))); |
| 81 } | 96 } |
| 82 | 97 |
| 83 // There should only be one entry in the map, since errors are stored in lists | 98 // There should only be one entry in the map, since errors are stored in lists |
| 84 // keyed by extension id. | 99 // keyed by extension id. |
| 85 ASSERT_EQ(1u, error_console_->errors().size()); | 100 ASSERT_EQ(1u, error_console_->errors().size()); |
| 86 | 101 |
| 87 ASSERT_EQ(kNumTotalErrors, error_console_->GetErrorsForExtension(kId).size()); | 102 ASSERT_EQ(kNumTotalErrors, error_console_->GetErrorsForExtension(kId).size()); |
| 88 | 103 |
| 89 // Remove the incognito errors; three errors should remain, and all should | 104 // Remove the incognito errors; three errors should remain, and all should |
| 90 // be from non-incognito contexts. | 105 // be from non-incognito contexts. |
| 91 error_console_->RemoveIncognitoErrors(); | 106 error_console_->RemoveIncognitoErrors(); |
| 92 const ErrorConsole::ErrorList& errors = | 107 const ErrorConsole::ErrorList& errors = |
| 93 error_console_->GetErrorsForExtension(kId); | 108 error_console_->GetErrorsForExtension(kId); |
| 94 ASSERT_EQ(kNumNonIncognitoErrors, errors.size()); | 109 ASSERT_EQ(kNumNonIncognitoErrors, errors.size()); |
| 95 for (size_t i = 0; i < errors.size(); ++i) | 110 for (size_t i = 0; i < errors.size(); ++i) |
| 96 ASSERT_FALSE(errors[i]->from_incognito()); | 111 ASSERT_FALSE(errors[i]->from_incognito()); |
| 97 | 112 |
| 98 // Add another error for a different extension id. | 113 // Add another error for a different extension id. |
| 99 const char kSecondId[] = "id2"; | 114 const std::string kSecondId = id_util::GenerateId("id2"); |
| 100 error_console_->ReportError( | 115 error_console_->ReportError( |
| 101 CreateNewRuntimeError(false, kSecondId, string16())); | 116 CreateNewRuntimeError(false, kSecondId, string16())); |
| 102 | 117 |
| 103 // There should be two entries now, one for each id, and there should be one | 118 // There should be two entries now, one for each id, and there should be one |
| 104 // error for the second extension. | 119 // error for the second extension. |
| 105 ASSERT_EQ(2u, error_console_->errors().size()); | 120 ASSERT_EQ(2u, error_console_->errors().size()); |
| 106 ASSERT_EQ(1u, error_console_->GetErrorsForExtension(kSecondId).size()); | 121 ASSERT_EQ(1u, error_console_->GetErrorsForExtension(kSecondId).size()); |
| 107 | 122 |
| 108 // Remove all errors for the second id. | 123 // Remove all errors for the second id. |
| 109 error_console_->RemoveErrorsForExtension(kSecondId); | 124 error_console_->RemoveErrorsForExtension(kSecondId); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 120 } | 135 } |
| 121 | 136 |
| 122 // Test that if we add enough errors, only the most recent | 137 // Test that if we add enough errors, only the most recent |
| 123 // kMaxErrorsPerExtension are kept. | 138 // kMaxErrorsPerExtension are kept. |
| 124 TEST_F(ErrorConsoleUnitTest, ExcessiveErrorsGetCropped) { | 139 TEST_F(ErrorConsoleUnitTest, ExcessiveErrorsGetCropped) { |
| 125 ASSERT_EQ(0u, error_console_->errors().size()); | 140 ASSERT_EQ(0u, error_console_->errors().size()); |
| 126 | 141 |
| 127 // This constant matches one of the same name in error_console.cc. | 142 // This constant matches one of the same name in error_console.cc. |
| 128 const size_t kMaxErrorsPerExtension = 100; | 143 const size_t kMaxErrorsPerExtension = 100; |
| 129 const size_t kNumExtraErrors = 5; | 144 const size_t kNumExtraErrors = 5; |
| 130 const char kId[] = "id"; | 145 const std::string kId = id_util::GenerateId("id"); |
| 131 | 146 |
| 132 // Add new errors, with each error's message set to its number. | 147 // Add new errors, with each error's message set to its number. |
| 133 for (size_t i = 0; i < kMaxErrorsPerExtension + kNumExtraErrors; ++i) { | 148 for (size_t i = 0; i < kMaxErrorsPerExtension + kNumExtraErrors; ++i) { |
| 134 error_console_->ReportError( | 149 error_console_->ReportError( |
| 135 CreateNewRuntimeError(false, kId, base::UintToString16(i))); | 150 CreateNewRuntimeError(false, kId, base::UintToString16(i))); |
| 136 } | 151 } |
| 137 | 152 |
| 138 ASSERT_EQ(1u, error_console_->errors().size()); | 153 ASSERT_EQ(1u, error_console_->errors().size()); |
| 139 | 154 |
| 140 const ErrorConsole::ErrorList& errors = | 155 const ErrorConsole::ErrorList& errors = |
| 141 error_console_->GetErrorsForExtension(kId); | 156 error_console_->GetErrorsForExtension(kId); |
| 142 ASSERT_EQ(kMaxErrorsPerExtension, errors.size()); | 157 ASSERT_EQ(kMaxErrorsPerExtension, errors.size()); |
| 143 | 158 |
| 144 // We should have popped off errors in the order they arrived, so the | 159 // We should have popped off errors in the order they arrived, so the |
| 145 // first stored error should be the 6th reported (zero-based)... | 160 // first stored error should be the 6th reported (zero-based)... |
| 146 ASSERT_EQ(errors.front()->message(), | 161 ASSERT_EQ(base::UintToString16(kNumExtraErrors), |
| 147 base::UintToString16(kNumExtraErrors)); | 162 errors.front()->message()); |
| 148 // ..and the last stored should be the 105th reported. | 163 // ..and the last stored should be the 105th reported. |
| 149 ASSERT_EQ(errors.back()->message(), | 164 ASSERT_EQ(base::UintToString16(kMaxErrorsPerExtension + kNumExtraErrors - 1), |
| 150 base::UintToString16(kMaxErrorsPerExtension + kNumExtraErrors - 1)); | 165 errors.back()->message()); |
| 166 } |
| 167 |
| 168 // Test to ensure that the error console will not add duplicate errors, but will |
| 169 // keep the latest version of an error. |
| 170 TEST_F(ErrorConsoleUnitTest, DuplicateErrorsAreReplaced) { |
| 171 ASSERT_EQ(0u, error_console_->errors().size()); |
| 172 |
| 173 const std::string kId = id_util::GenerateId("id"); |
| 174 const size_t kNumErrors = 3u; |
| 175 |
| 176 // Report three errors. |
| 177 for (size_t i = 0; i < kNumErrors; ++i) { |
| 178 error_console_->ReportError( |
| 179 CreateNewRuntimeError(false, kId, base::UintToString16(i))); |
| 180 } |
| 181 |
| 182 // Create an error identical to the second error reported, and save its |
| 183 // location. |
| 184 const size_t kDuplicateError = 1u; |
| 185 scoped_ptr<const ExtensionError> runtime_error2 = |
| 186 CreateNewRuntimeError(false, kId, base::UintToString16(kDuplicateError)); |
| 187 const ExtensionError* weak_error = runtime_error2.get(); |
| 188 |
| 189 // Add it to the error console. |
| 190 error_console_->ReportError(runtime_error2.Pass()); |
| 191 |
| 192 // We should only have three errors stored, since two of the four reported |
| 193 // were identical, and the older should have been replaced. |
| 194 ASSERT_EQ(1u, error_console_->errors().size()); |
| 195 const ErrorConsole::ErrorList& errors = |
| 196 error_console_->GetErrorsForExtension(kId); |
| 197 ASSERT_EQ(kNumErrors, errors.size()); |
| 198 |
| 199 // The more-recently reported error should be in the slot of the error it |
| 200 // replaced. |
| 201 // Pointer comparison. |
| 202 ASSERT_EQ(weak_error, errors[kDuplicateError]); |
| 151 } | 203 } |
| 152 | 204 |
| 153 } // namespace extensions | 205 } // namespace extensions |
| OLD | NEW |