Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1435)

Side by Side Diff: chrome/browser/extensions/error_console/error_console_unittest.cc

Issue 22647007: Resubmit 21609003: Move ExtensionError to extensions/, add error limits (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Latest master Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
8 #include "base/logging.h"
7 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string16.h" 10 #include "base/strings/string16.h"
11 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/extensions/error_console/extension_error.h"
11 #include "chrome/test/base/testing_profile.h" 13 #include "chrome/test/base/testing_profile.h"
12 #include "extensions/common/id_util.h" 14 #include "content/public/common/url_constants.h"
15 #include "extensions/browser/extension_error.h"
16 #include "extensions/common/constants.h"
13 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
14 18
15 using base::string16; 19 using base::string16;
16 using base::UTF8ToUTF16; 20 using base::UTF8ToUTF16;
17 21
18 namespace extensions { 22 namespace extensions {
19 23
20 namespace { 24 namespace {
21 25
22 scoped_ptr<ExtensionError> CreateNewManifestError(bool from_incognito) { 26 const char kExecutionContextURLKey[] = "executionContextURL";
23 return scoped_ptr<ExtensionError>( 27 const char kStackTraceKey[] = "stackTrace";
24 new ManifestParsingError(from_incognito, 28
25 UTF8ToUTF16("source"), 29 string16 CreateErrorDetails(const std::string& extension_id) {
26 UTF8ToUTF16("message"), 30 base::DictionaryValue value;
27 0u /* line number */ )); 31 value.SetString(
32 kExecutionContextURLKey,
33 std::string(kExtensionScheme) +
34 content::kStandardSchemeSeparator +
35 extension_id);
36 value.Set(kStackTraceKey, new ListValue);
37 std::string json_utf8;
38 base::JSONWriter::Write(&value, &json_utf8);
39 return UTF8ToUTF16(json_utf8);
40 }
41
42 scoped_ptr<const ExtensionError> CreateNewRuntimeError(
43 bool from_incognito,
44 const std::string& extension_id,
45 const string16& message) {
46 return scoped_ptr<const ExtensionError>(new JavascriptRuntimeError(
47 from_incognito,
48 UTF8ToUTF16("source"),
49 message,
50 logging::LOG_INFO,
51 CreateErrorDetails(extension_id)));
28 } 52 }
29 53
30 } // namespace 54 } // namespace
31 55
32 class ErrorConsoleUnitTest : public testing::Test { 56 class ErrorConsoleUnitTest : public testing::Test {
33 public: 57 public:
34 ErrorConsoleUnitTest() : 58 ErrorConsoleUnitTest() :
35 profile_(new TestingProfile), 59 profile_(new TestingProfile),
36 error_console_(ErrorConsole::Get(profile_.get())) { 60 error_console_(ErrorConsole::Get(profile_.get())) {
37 } 61 }
38 virtual ~ErrorConsoleUnitTest() { } 62 virtual ~ErrorConsoleUnitTest() { }
39 63
40 protected: 64 protected:
41 scoped_ptr<TestingProfile> profile_; 65 scoped_ptr<TestingProfile> profile_;
42 ErrorConsole* error_console_; 66 ErrorConsole* error_console_;
43 }; 67 };
44 68
45 // Test adding errors, and removing them by reference, by incognito status, 69 // Test adding errors, and removing them by reference, by incognito status,
46 // and in bulk. 70 // and in bulk.
47 TEST_F(ErrorConsoleUnitTest, AddAndRemoveErrors) { 71 TEST_F(ErrorConsoleUnitTest, AddAndRemoveErrors) {
48 ASSERT_EQ(0u, error_console_->errors().size()); 72 ASSERT_EQ(0u, error_console_->errors().size());
49 73
50 const size_t kNumTotalErrors = 6; 74 const size_t kNumTotalErrors = 6;
51 const size_t kNumNonIncognitoErrors = 3; 75 const size_t kNumNonIncognitoErrors = 3;
76 const char kId[] = "id";
52 // Populate with both incognito and non-incognito errors (evenly distributed). 77 // Populate with both incognito and non-incognito errors (evenly distributed).
53 for (size_t i = 0; i < kNumTotalErrors; ++i) 78 for (size_t i = 0; i < kNumTotalErrors; ++i) {
54 error_console_->ReportError(CreateNewManifestError(i % 2 == 0)); 79 error_console_->ReportError(
80 CreateNewRuntimeError(i % 2 == 0, kId, string16()));
81 }
55 82
56 ASSERT_EQ(kNumTotalErrors, error_console_->errors().size()); 83 // There should only be one entry in the map, since errors are stored in lists
84 // keyed by extension id.
85 ASSERT_EQ(1u, error_console_->errors().size());
86
87 ASSERT_EQ(kNumTotalErrors, error_console_->GetErrorsForExtension(kId).size());
57 88
58 // Remove the incognito errors; three errors should remain, and all should 89 // Remove the incognito errors; three errors should remain, and all should
59 // be from non-incognito contexts. 90 // be from non-incognito contexts.
60 error_console_->RemoveIncognitoErrors(); 91 error_console_->RemoveIncognitoErrors();
61 ASSERT_EQ(kNumNonIncognitoErrors, error_console_->errors().size()); 92 const ErrorConsole::ErrorList& errors =
62 for (size_t i = 0; i < error_console_->errors().size(); ++i) 93 error_console_->GetErrorsForExtension(kId);
63 ASSERT_FALSE(error_console_->errors()[i]->from_incognito()); 94 ASSERT_EQ(kNumNonIncognitoErrors, errors.size());
95 for (size_t i = 0; i < errors.size(); ++i)
96 ASSERT_FALSE(errors[i]->from_incognito());
64 97
65 // Remove an error by address. 98 // Add another error for a different extension id.
66 error_console_->RemoveError(error_console_->errors()[1]); 99 const char kSecondId[] = "id2";
67 ASSERT_EQ(kNumNonIncognitoErrors - 1, error_console_->errors().size()); 100 error_console_->ReportError(
101 CreateNewRuntimeError(false, kSecondId, string16()));
68 102
69 // Remove all remaining errors. 103 // There should be two entries now, one for each id, and there should be one
104 // error for the second extension.
105 ASSERT_EQ(2u, error_console_->errors().size());
106 ASSERT_EQ(1u, error_console_->GetErrorsForExtension(kSecondId).size());
107
108 // Remove all errors for the second id.
109 error_console_->RemoveErrorsForExtension(kSecondId);
110 ASSERT_EQ(1u, error_console_->errors().size());
111 ASSERT_EQ(0u, error_console_->GetErrorsForExtension(kSecondId).size());
112 // First extension should be unaffected.
113 ASSERT_EQ(kNumNonIncognitoErrors,
114 error_console_->GetErrorsForExtension(kId).size());
115
116 // Remove remaining errors.
70 error_console_->RemoveAllErrors(); 117 error_console_->RemoveAllErrors();
71 ASSERT_EQ(0u, error_console_->errors().size()); 118 ASSERT_EQ(0u, error_console_->errors().size());
119 ASSERT_EQ(0u, error_console_->GetErrorsForExtension(kId).size());
120 }
121
122 // Test that if we add enough errors, only the most recent
123 // kMaxErrorsPerExtension are kept.
124 TEST_F(ErrorConsoleUnitTest, ExcessiveErrorsGetCropped) {
125 ASSERT_EQ(0u, error_console_->errors().size());
126
127 // This constant matches one of the same name in error_console.cc.
128 const size_t kMaxErrorsPerExtension = 100;
129 const size_t kNumExtraErrors = 5;
130 const char kId[] = "id";
131
132 // Add new errors, with each error's message set to its number.
133 for (size_t i = 0; i < kMaxErrorsPerExtension + kNumExtraErrors; ++i) {
134 error_console_->ReportError(
135 CreateNewRuntimeError(false, kId, base::UintToString16(i)));
136 }
137
138 ASSERT_EQ(1u, error_console_->errors().size());
139
140 const ErrorConsole::ErrorList& errors =
141 error_console_->GetErrorsForExtension(kId);
142 ASSERT_EQ(kMaxErrorsPerExtension, errors.size());
143
144 // We should have popped off errors in the order they arrived, so the
145 // first stored error should be the 6th reported (zero-based)...
146 ASSERT_EQ(errors.front()->message(),
147 base::UintToString16(kNumExtraErrors));
148 // ..and the last stored should be the 105th reported.
149 ASSERT_EQ(errors.back()->message(),
150 base::UintToString16(kMaxErrorsPerExtension + kNumExtraErrors - 1));
72 } 151 }
73 152
74 } // namespace extensions 153 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/error_console/error_console.cc ('k') | chrome/browser/extensions/error_console/extension_error.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698