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

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

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

Powered by Google App Engine
This is Rietveld 408576698