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