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