error_console_unittest.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
1558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch// Copyright 2013 The Chromium Authors. All rights reserved. 2558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch// Use of this source code is governed by a BSD-style license that can be 3558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch// found in the LICENSE file. 4558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 5558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#include "chrome/browser/extensions/error_console/error_console.h" 6558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 7ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "base/logging.h" 80529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "base/memory/ref_counted.h" 9558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#include "base/memory/scoped_ptr.h" 10424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "base/prefs/pref_service.h" 11ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "base/strings/string_number_conversions.h" 12e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/common/extensions/features/feature_channel.h" 13424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "chrome/common/pref_names.h" 14558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#include "chrome/test/base/testing_profile.h" 15ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "extensions/browser/extension_error.h" 165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/browser/extension_error_test_util.h" 17e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "extensions/browser/extension_registry.h" 18ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "extensions/common/constants.h" 19e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "extensions/common/extension_builder.h" 20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/feature_switch.h" 21424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "extensions/common/id_util.h" 22e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "extensions/common/value_builder.h" 23558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#include "testing/gtest/include/gtest/gtest.h" 24558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 25558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdochnamespace extensions { 26558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using error_test_util::CreateNewManifestError; 285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using error_test_util::CreateNewRuntimeError; 29558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 30558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdochclass ErrorConsoleUnitTest : public testing::Test { 31558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch public: 32424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) ErrorConsoleUnitTest() : error_console_(NULL) { } 33558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch virtual ~ErrorConsoleUnitTest() { } 34558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 35424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) virtual void SetUp() OVERRIDE { 36424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) testing::Test::SetUp(); 37424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 38424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // Errors are only kept if we have the FeatureSwitch and have Developer Mode 39424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // enabled. 40424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) FeatureSwitch::error_console()->SetOverrideValue( 41424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) FeatureSwitch::OVERRIDE_ENABLED); 42424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) profile_.reset(new TestingProfile); 43424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) profile_->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode, true); 44424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) error_console_ = ErrorConsole::Get(profile_.get()); 45424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 46424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 47558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch protected: 48558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch scoped_ptr<TestingProfile> profile_; 49558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch ErrorConsole* error_console_; 50558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch}; 51558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 52e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// Test that the error console is enabled/disabled appropriately. 53e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochTEST_F(ErrorConsoleUnitTest, EnableAndDisableErrorConsole) { 54e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // Start in Dev Channel, without the feature switch. 55e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch scoped_ptr<ScopedCurrentChannel> channel_override( 56e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch new ScopedCurrentChannel(chrome::VersionInfo::CHANNEL_DEV)); 57e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch ASSERT_EQ(chrome::VersionInfo::CHANNEL_DEV, GetCurrentChannel()); 58e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch FeatureSwitch::error_console()->SetOverrideValue( 59e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch FeatureSwitch::OVERRIDE_DISABLED); 60e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 61e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // At the start, the error console should be enabled, and specifically 62e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // enabled for the chrome:extensions page. 63e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch EXPECT_TRUE(error_console_->enabled()); 64e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch EXPECT_TRUE(error_console_->IsEnabledForChromeExtensionsPage()); 65e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch EXPECT_FALSE(error_console_->IsEnabledForAppsDeveloperTools()); 66e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 67e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // If we disable developer mode, we should disable error console. 68e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch profile_->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode, false); 69e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch EXPECT_FALSE(error_console_->enabled()); 70e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch EXPECT_FALSE(error_console_->IsEnabledForChromeExtensionsPage()); 71e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch EXPECT_FALSE(error_console_->IsEnabledForAppsDeveloperTools()); 72e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 73e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // Similarly, if we change the current to less fun than Dev, ErrorConsole 74e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // should be disabled. 75e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch channel_override.reset(); 76e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch channel_override.reset( 77e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch new ScopedCurrentChannel(chrome::VersionInfo::CHANNEL_BETA)); 78e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch profile_->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode, true); 79e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch EXPECT_FALSE(error_console_->enabled()); 80e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch EXPECT_FALSE(error_console_->IsEnabledForChromeExtensionsPage()); 81e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch EXPECT_FALSE(error_console_->IsEnabledForAppsDeveloperTools()); 82e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 83e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // But if we add the feature switch, that should override the channel. 84e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch FeatureSwitch::error_console()->SetOverrideValue( 85e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch FeatureSwitch::OVERRIDE_ENABLED); 86e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch ASSERT_TRUE(FeatureSwitch::error_console()->IsEnabled()); 87e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // We use a pref mod to "poke" the ErrorConsole, because it needs an 88e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // indication that something changed (FeatureSwitches don't change in a real 89e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // environment, so ErrorConsole doesn't listen for them). 90e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch profile_->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode, false); 91e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch profile_->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode, true); 92e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch EXPECT_TRUE(error_console_->enabled()); 93e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch EXPECT_TRUE(error_console_->IsEnabledForChromeExtensionsPage()); 94e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch EXPECT_FALSE(error_console_->IsEnabledForAppsDeveloperTools()); 95e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 96e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // Next, remove the feature switch (turning error console off), and install 97e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // the Apps Developer Tools. If we have Apps Developer Tools, Error Console 98e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // should be enabled by default. 99e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch FeatureSwitch::error_console()->SetOverrideValue( 100e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch FeatureSwitch::OVERRIDE_DISABLED); 101e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch const char kAppsDeveloperToolsExtensionId[] = 102e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch "ohmmkhmmmpcnpikjeljgnaoabkaalbgc"; 103e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch scoped_refptr<Extension> adt = 104e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch ExtensionBuilder() 105e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch .SetManifest( 106e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch DictionaryBuilder().Set("name", "apps dev tools") 107e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch .Set("version", "0.2.0") 108e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch .Set("manifest_version", 2) 109e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch .Build()) 110e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch .SetID(kAppsDeveloperToolsExtensionId) 111e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch .Build(); 112e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get()); 113e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch registry->AddEnabled(adt); 114e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch registry->TriggerOnLoaded(adt); 115e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch EXPECT_TRUE(error_console_->enabled()); 116e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch EXPECT_FALSE(error_console_->IsEnabledForChromeExtensionsPage()); 117e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch EXPECT_TRUE(error_console_->IsEnabledForAppsDeveloperTools()); 118e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 119e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // Unloading the Apps Developer Tools should disable error console. 120e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch registry->RemoveEnabled(adt->id()); 1210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch registry->TriggerOnUnloaded(adt, UnloadedExtensionInfo::REASON_DISABLE); 122e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch EXPECT_FALSE(error_console_->enabled()); 123e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch EXPECT_FALSE(error_console_->IsEnabledForChromeExtensionsPage()); 124e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch EXPECT_FALSE(error_console_->IsEnabledForAppsDeveloperTools()); 125e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch} 126e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Test that errors are successfully reported. This is a simple test, since it 1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// is tested more thoroughly in extensions/browser/error_map_unittest.cc 1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(ErrorConsoleUnitTest, ReportErrors) { 130558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch const size_t kNumTotalErrors = 6; 131424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const std::string kId = id_util::GenerateId("id"); 1320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch error_console_->set_default_reporting_for_test(ExtensionError::MANIFEST_ERROR, 1330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch true); 134ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch ASSERT_EQ(0u, error_console_->GetErrorsForExtension(kId).size()); 135ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch 1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) for (size_t i = 0; i < kNumTotalErrors; ++i) { 137ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch error_console_->ReportError( 1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) CreateNewManifestError(kId, base::UintToString(i))); 139ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch } 140ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch 1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_EQ(kNumTotalErrors, error_console_->GetErrorsForExtension(kId).size()); 142424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 143424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(ErrorConsoleUnitTest, DontStoreErrorsWithoutEnablingType) { 1450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Disable default runtime error reporting, and enable default manifest error 1460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // reporting. 1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) error_console_->set_default_reporting_for_test(ExtensionError::RUNTIME_ERROR, 1480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch false); 1490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch error_console_->set_default_reporting_for_test(ExtensionError::MANIFEST_ERROR, 1500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch true); 151424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 152424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const std::string kId = id_util::GenerateId("id"); 153424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Try to report a runtime error - it should be ignored. 1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) error_console_->ReportError(CreateNewRuntimeError(kId, "a")); 1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_EQ(0u, error_console_->GetErrorsForExtension(kId).size()); 157424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Check that manifest errors are reported successfully. 1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) error_console_->ReportError(CreateNewManifestError(kId, "b")); 1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_EQ(1u, error_console_->GetErrorsForExtension(kId).size()); 1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // We should still ignore runtime errors. 1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) error_console_->ReportError(CreateNewRuntimeError(kId, "c")); 1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_EQ(1u, error_console_->GetErrorsForExtension(kId).size()); 1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Enable runtime errors specifically for this extension, and disable the use 1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // of the default mask. 1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) error_console_->SetReportingForExtension( 1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) kId, ExtensionError::RUNTIME_ERROR, true); 1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // We should now accept runtime and manifest errors. 1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) error_console_->ReportError(CreateNewManifestError(kId, "d")); 1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_EQ(2u, error_console_->GetErrorsForExtension(kId).size()); 1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) error_console_->ReportError(CreateNewRuntimeError(kId, "e")); 1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_EQ(3u, error_console_->GetErrorsForExtension(kId).size()); 1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // All other extensions should still use the default mask, and ignore runtime 1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // errors but report manifest errors. 1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string kId2 = id_util::GenerateId("id2"); 1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) error_console_->ReportError(CreateNewRuntimeError(kId2, "f")); 1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_EQ(0u, error_console_->GetErrorsForExtension(kId2).size()); 1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) error_console_->ReportError(CreateNewManifestError(kId2, "g")); 1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_EQ(1u, error_console_->GetErrorsForExtension(kId2).size()); 1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // By reverting to default reporting, we should again allow manifest errors, 1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // but not runtime errors. 1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) error_console_->UseDefaultReportingForExtension(kId); 1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) error_console_->ReportError(CreateNewManifestError(kId, "h")); 1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_EQ(4u, error_console_->GetErrorsForExtension(kId).size()); 1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) error_console_->ReportError(CreateNewRuntimeError(kId, "i")); 1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_EQ(4u, error_console_->GetErrorsForExtension(kId).size()); 192558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch} 193558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 1940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// Test that we only store errors by default for unpacked extensions, and that 1950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// assigning a preference to any extension overrides the defaults. 1960529e5d033099cbfc42635f6f6183833b09dff6eBen MurdochTEST_F(ErrorConsoleUnitTest, TestDefaultStoringPrefs) { 1970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // For this, we need actual extensions. 1980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch scoped_refptr<const Extension> unpacked_extension = 1990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch ExtensionBuilder() 2000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch .SetManifest(DictionaryBuilder().Set("name", "unpacked") 2010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch .Set("version", "0.0.1") 2020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch .Set("manifest_version", 2) 2030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch .Build()) 2040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch .SetLocation(Manifest::UNPACKED) 2050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch .SetID(id_util::GenerateId("unpacked")) 2060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch .Build(); 2070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch scoped_refptr<const Extension> packed_extension = 2080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch ExtensionBuilder() 2090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch .SetManifest(DictionaryBuilder().Set("name", "packed") 2100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch .Set("version", "0.0.1") 2110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch .Set("manifest_version", 2) 2120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch .Build()) 2130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch .SetLocation(Manifest::INTERNAL) 2140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch .SetID(id_util::GenerateId("packed")) 2150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch .Build(); 2160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 2170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get()); 2180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch registry->AddEnabled(unpacked_extension); 2190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch registry->AddEnabled(packed_extension); 2200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 2210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // We should start with a clean slate. 2220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_EQ(0u, error_console_->GetErrorsForExtension( 2230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch unpacked_extension->id()).size()); 2240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_EQ(0u, error_console_->GetErrorsForExtension( 2250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch packed_extension->id()).size()); 2260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 2270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Errors should be ignored by default for the packed extension. 2280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch error_console_->ReportError( 2290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch CreateNewManifestError(packed_extension->id(), "manifest error 1")); 2300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch error_console_->ReportError( 2310529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch CreateNewRuntimeError(packed_extension->id(), "runtime error 1")); 2320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_EQ(0u, error_console_->GetErrorsForExtension( 2330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch packed_extension->id()).size()); 2340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Also check that reporting settings are correctly returned. 2350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_FALSE(error_console_->IsReportingEnabledForExtension( 2360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch packed_extension->id())); 2370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 2380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Errors should be reported by default for the unpacked extension. 2390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch error_console_->ReportError( 2400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch CreateNewManifestError(unpacked_extension->id(), "manifest error 2")); 2410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch error_console_->ReportError( 2420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch CreateNewRuntimeError(unpacked_extension->id(), "runtime error 2")); 2430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_EQ(2u, error_console_->GetErrorsForExtension( 2440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch unpacked_extension->id()).size()); 2450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Also check that reporting settings are correctly returned. 2460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_TRUE(error_console_->IsReportingEnabledForExtension( 2470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch unpacked_extension->id())); 2480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 2490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Registering a preference should override this for both types of extensions 2500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // (should be able to enable errors for packed, or disable errors for 2510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // unpacked). 2520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch error_console_->SetReportingForExtension(packed_extension->id(), 2530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch ExtensionError::RUNTIME_ERROR, 2540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch true); 2550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch error_console_->ReportError( 2560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch CreateNewRuntimeError(packed_extension->id(), "runtime error 3")); 2570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_EQ(1u, error_console_->GetErrorsForExtension( 2580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch packed_extension->id()).size()); 2590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_TRUE(error_console_->IsReportingEnabledForExtension( 2600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch packed_extension->id())); 2610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 2620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch error_console_->SetReportingForExtension(unpacked_extension->id(), 2630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch ExtensionError::RUNTIME_ERROR, 2640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch false); 2650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch error_console_->ReportError( 2660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch CreateNewRuntimeError(packed_extension->id(), "runtime error 4")); 2670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_EQ(2u, // We should still have the first two errors. 2680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch error_console_->GetErrorsForExtension( 2690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch unpacked_extension->id()).size()); 2700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EXPECT_FALSE(error_console_->IsReportingEnabledForExtension( 2710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch unpacked_extension->id())); 2720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch} 2730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 274558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch} // namespace extensions 275