| 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" |
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
| 14 #include "content/public/common/url_constants.h" | 16 #include "content/public/common/url_constants.h" |
| 15 #include "extensions/browser/extension_error.h" | 17 #include "extensions/browser/extension_error.h" |
| 16 #include "extensions/common/constants.h" | 18 #include "extensions/common/constants.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 20 |
| 19 using base::string16; | 21 using base::string16; |
| 20 using base::UTF8ToUTF16; | 22 using base::UTF8ToUTF16; |
| 21 | 23 |
| 22 namespace extensions { | 24 namespace extensions { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 } // namespace | 56 } // namespace |
| 55 | 57 |
| 56 class ErrorConsoleUnitTest : public testing::Test { | 58 class ErrorConsoleUnitTest : public testing::Test { |
| 57 public: | 59 public: |
| 58 ErrorConsoleUnitTest() : | 60 ErrorConsoleUnitTest() : |
| 59 profile_(new TestingProfile), | 61 profile_(new TestingProfile), |
| 60 error_console_(ErrorConsole::Get(profile_.get())) { | 62 error_console_(ErrorConsole::Get(profile_.get())) { |
| 61 } | 63 } |
| 62 virtual ~ErrorConsoleUnitTest() { } | 64 virtual ~ErrorConsoleUnitTest() { } |
| 63 | 65 |
| 66 virtual void SetUp() OVERRIDE { |
| 67 testing::Test::SetUp(); |
| 68 // Errors are only kept if we have Developer Mode enabled. |
| 69 profile_->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode, true); |
| 70 } |
| 71 |
| 64 protected: | 72 protected: |
| 65 scoped_ptr<TestingProfile> profile_; | 73 scoped_ptr<TestingProfile> profile_; |
| 66 ErrorConsole* error_console_; | 74 ErrorConsole* error_console_; |
| 67 }; | 75 }; |
| 68 | 76 |
| 69 // Test adding errors, and removing them by reference, by incognito status, | 77 // Test adding errors, and removing them by reference, by incognito status, |
| 70 // and in bulk. | 78 // and in bulk. |
| 71 TEST_F(ErrorConsoleUnitTest, AddAndRemoveErrors) { | 79 TEST_F(ErrorConsoleUnitTest, AddAndRemoveErrors) { |
| 72 ASSERT_EQ(0u, error_console_->errors().size()); | 80 ASSERT_EQ(0u, error_console_->errors().size()); |
| 73 | 81 |
| 74 const size_t kNumTotalErrors = 6; | 82 const size_t kNumTotalErrors = 6; |
| 75 const size_t kNumNonIncognitoErrors = 3; | 83 const size_t kNumNonIncognitoErrors = 3; |
| 76 const char kId[] = "id"; | 84 const char kId[] = "id"; |
| 77 // Populate with both incognito and non-incognito errors (evenly distributed). | 85 // Populate with both incognito and non-incognito errors (evenly distributed). |
| 78 for (size_t i = 0; i < kNumTotalErrors; ++i) { | 86 for (size_t i = 0; i < kNumTotalErrors; ++i) { |
| 79 error_console_->ReportError( | 87 error_console_->ReportError( |
| 80 CreateNewRuntimeError(i % 2 == 0, kId, string16())); | 88 CreateNewRuntimeError(i % 2 == 0, kId, base::UintToString16(i))); |
| 81 } | 89 } |
| 82 | 90 |
| 83 // There should only be one entry in the map, since errors are stored in lists | 91 // There should only be one entry in the map, since errors are stored in lists |
| 84 // keyed by extension id. | 92 // keyed by extension id. |
| 85 ASSERT_EQ(1u, error_console_->errors().size()); | 93 ASSERT_EQ(1u, error_console_->errors().size()); |
| 86 | 94 |
| 87 ASSERT_EQ(kNumTotalErrors, error_console_->GetErrorsForExtension(kId).size()); | 95 ASSERT_EQ(kNumTotalErrors, error_console_->GetErrorsForExtension(kId).size()); |
| 88 | 96 |
| 89 // Remove the incognito errors; three errors should remain, and all should | 97 // Remove the incognito errors; three errors should remain, and all should |
| 90 // be from non-incognito contexts. | 98 // be from non-incognito contexts. |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 // We should have popped off errors in the order they arrived, so the | 152 // We should have popped off errors in the order they arrived, so the |
| 145 // first stored error should be the 6th reported (zero-based)... | 153 // first stored error should be the 6th reported (zero-based)... |
| 146 ASSERT_EQ(errors.front()->message(), | 154 ASSERT_EQ(errors.front()->message(), |
| 147 base::UintToString16(kNumExtraErrors)); | 155 base::UintToString16(kNumExtraErrors)); |
| 148 // ..and the last stored should be the 105th reported. | 156 // ..and the last stored should be the 105th reported. |
| 149 ASSERT_EQ(errors.back()->message(), | 157 ASSERT_EQ(errors.back()->message(), |
| 150 base::UintToString16(kMaxErrorsPerExtension + kNumExtraErrors - 1)); | 158 base::UintToString16(kMaxErrorsPerExtension + kNumExtraErrors - 1)); |
| 151 } | 159 } |
| 152 | 160 |
| 153 } // namespace extensions | 161 } // namespace extensions |
| OLD | NEW |