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/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "chrome/common/extensions/features/feature_channel.h" |
11 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
12 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" |
13 #include "extensions/browser/extension_error.h" | 14 #include "extensions/browser/extension_error.h" |
14 #include "extensions/browser/extension_error_test_util.h" | 15 #include "extensions/browser/extension_error_test_util.h" |
| 16 #include "extensions/browser/extension_registry.h" |
15 #include "extensions/common/constants.h" | 17 #include "extensions/common/constants.h" |
| 18 #include "extensions/common/extension_builder.h" |
16 #include "extensions/common/feature_switch.h" | 19 #include "extensions/common/feature_switch.h" |
17 #include "extensions/common/id_util.h" | 20 #include "extensions/common/id_util.h" |
| 21 #include "extensions/common/value_builder.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
19 | 23 |
20 namespace extensions { | 24 namespace extensions { |
21 | 25 |
22 using error_test_util::CreateNewManifestError; | 26 using error_test_util::CreateNewManifestError; |
23 using error_test_util::CreateNewRuntimeError; | 27 using error_test_util::CreateNewRuntimeError; |
24 | 28 |
25 class ErrorConsoleUnitTest : public testing::Test { | 29 class ErrorConsoleUnitTest : public testing::Test { |
26 public: | 30 public: |
27 ErrorConsoleUnitTest() : error_console_(NULL) { } | 31 ErrorConsoleUnitTest() : error_console_(NULL) { } |
28 virtual ~ErrorConsoleUnitTest() { } | 32 virtual ~ErrorConsoleUnitTest() { } |
29 | 33 |
30 virtual void SetUp() OVERRIDE { | 34 virtual void SetUp() OVERRIDE { |
31 testing::Test::SetUp(); | 35 testing::Test::SetUp(); |
32 | 36 |
33 // Errors are only kept if we have the FeatureSwitch and have Developer Mode | 37 // Errors are only kept if we have the FeatureSwitch and have Developer Mode |
34 // enabled. | 38 // enabled. |
35 FeatureSwitch::error_console()->SetOverrideValue( | 39 FeatureSwitch::error_console()->SetOverrideValue( |
36 FeatureSwitch::OVERRIDE_ENABLED); | 40 FeatureSwitch::OVERRIDE_ENABLED); |
37 profile_.reset(new TestingProfile); | 41 profile_.reset(new TestingProfile); |
38 profile_->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode, true); | 42 profile_->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode, true); |
39 error_console_ = ErrorConsole::Get(profile_.get()); | 43 error_console_ = ErrorConsole::Get(profile_.get()); |
40 } | 44 } |
41 | 45 |
42 protected: | 46 protected: |
43 scoped_ptr<TestingProfile> profile_; | 47 scoped_ptr<TestingProfile> profile_; |
44 ErrorConsole* error_console_; | 48 ErrorConsole* error_console_; |
45 }; | 49 }; |
46 | 50 |
| 51 // Test that the error console is enabled/disabled appropriately. |
| 52 TEST_F(ErrorConsoleUnitTest, EnableAndDisableErrorConsole) { |
| 53 // Start in Dev Channel, without the feature switch. |
| 54 scoped_ptr<ScopedCurrentChannel> channel_override( |
| 55 new ScopedCurrentChannel(chrome::VersionInfo::CHANNEL_DEV)); |
| 56 ASSERT_EQ(chrome::VersionInfo::CHANNEL_DEV, GetCurrentChannel()); |
| 57 FeatureSwitch::error_console()->SetOverrideValue( |
| 58 FeatureSwitch::OVERRIDE_DISABLED); |
| 59 |
| 60 // At the start, the error console should be enabled, and specifically |
| 61 // enabled for the chrome:extensions page. |
| 62 EXPECT_TRUE(error_console_->enabled()); |
| 63 EXPECT_TRUE(error_console_->IsEnabledForChromeExtensionsPage()); |
| 64 EXPECT_FALSE(error_console_->IsEnabledForAppsDeveloperTools()); |
| 65 |
| 66 // If we disable developer mode, we should disable error console. |
| 67 profile_->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode, false); |
| 68 EXPECT_FALSE(error_console_->enabled()); |
| 69 EXPECT_FALSE(error_console_->IsEnabledForChromeExtensionsPage()); |
| 70 EXPECT_FALSE(error_console_->IsEnabledForAppsDeveloperTools()); |
| 71 |
| 72 // Similarly, if we change the current to less fun than Dev, ErrorConsole |
| 73 // should be disabled. |
| 74 channel_override.reset(); |
| 75 channel_override.reset( |
| 76 new ScopedCurrentChannel(chrome::VersionInfo::CHANNEL_BETA)); |
| 77 profile_->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode, true); |
| 78 EXPECT_FALSE(error_console_->enabled()); |
| 79 EXPECT_FALSE(error_console_->IsEnabledForChromeExtensionsPage()); |
| 80 EXPECT_FALSE(error_console_->IsEnabledForAppsDeveloperTools()); |
| 81 |
| 82 // But if we add the feature switch, that should override the channel. |
| 83 FeatureSwitch::error_console()->SetOverrideValue( |
| 84 FeatureSwitch::OVERRIDE_ENABLED); |
| 85 ASSERT_TRUE(FeatureSwitch::error_console()->IsEnabled()); |
| 86 // We use a pref mod to "poke" the ErrorConsole, because it needs an |
| 87 // indication that something changed (FeatureSwitches don't change in a real |
| 88 // environment, so ErrorConsole doesn't listen for them). |
| 89 profile_->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode, false); |
| 90 profile_->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode, true); |
| 91 EXPECT_TRUE(error_console_->enabled()); |
| 92 EXPECT_TRUE(error_console_->IsEnabledForChromeExtensionsPage()); |
| 93 EXPECT_FALSE(error_console_->IsEnabledForAppsDeveloperTools()); |
| 94 |
| 95 // Next, remove the feature switch (turning error console off), and install |
| 96 // the Apps Developer Tools. If we have Apps Developer Tools, Error Console |
| 97 // should be enabled by default. |
| 98 FeatureSwitch::error_console()->SetOverrideValue( |
| 99 FeatureSwitch::OVERRIDE_DISABLED); |
| 100 const char kAppsDeveloperToolsExtensionId[] = |
| 101 "ohmmkhmmmpcnpikjeljgnaoabkaalbgc"; |
| 102 scoped_refptr<Extension> adt = |
| 103 ExtensionBuilder() |
| 104 .SetManifest( |
| 105 DictionaryBuilder().Set("name", "apps dev tools") |
| 106 .Set("version", "0.2.0") |
| 107 .Set("manifest_version", 2) |
| 108 .Build()) |
| 109 .SetID(kAppsDeveloperToolsExtensionId) |
| 110 .Build(); |
| 111 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get()); |
| 112 registry->AddEnabled(adt); |
| 113 registry->TriggerOnLoaded(adt); |
| 114 EXPECT_TRUE(error_console_->enabled()); |
| 115 EXPECT_FALSE(error_console_->IsEnabledForChromeExtensionsPage()); |
| 116 EXPECT_TRUE(error_console_->IsEnabledForAppsDeveloperTools()); |
| 117 |
| 118 // Unloading the Apps Developer Tools should disable error console. |
| 119 registry->RemoveEnabled(adt->id()); |
| 120 registry->TriggerOnUnloaded(adt); |
| 121 EXPECT_FALSE(error_console_->enabled()); |
| 122 EXPECT_FALSE(error_console_->IsEnabledForChromeExtensionsPage()); |
| 123 EXPECT_FALSE(error_console_->IsEnabledForAppsDeveloperTools()); |
| 124 } |
| 125 |
47 // Test that errors are successfully reported. This is a simple test, since it | 126 // Test that errors are successfully reported. This is a simple test, since it |
48 // is tested more thoroughly in extensions/browser/error_map_unittest.cc | 127 // is tested more thoroughly in extensions/browser/error_map_unittest.cc |
49 TEST_F(ErrorConsoleUnitTest, ReportErrors) { | 128 TEST_F(ErrorConsoleUnitTest, ReportErrors) { |
50 const size_t kNumTotalErrors = 6; | 129 const size_t kNumTotalErrors = 6; |
51 const std::string kId = id_util::GenerateId("id"); | 130 const std::string kId = id_util::GenerateId("id"); |
52 ASSERT_EQ(0u, error_console_->GetErrorsForExtension(kId).size()); | 131 ASSERT_EQ(0u, error_console_->GetErrorsForExtension(kId).size()); |
53 | 132 |
54 for (size_t i = 0; i < kNumTotalErrors; ++i) { | 133 for (size_t i = 0; i < kNumTotalErrors; ++i) { |
55 error_console_->ReportError( | 134 error_console_->ReportError( |
56 CreateNewManifestError(kId, base::UintToString(i))); | 135 CreateNewManifestError(kId, base::UintToString(i))); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 // By reverting to default reporting, we should again allow manifest errors, | 179 // By reverting to default reporting, we should again allow manifest errors, |
101 // but not runtime errors. | 180 // but not runtime errors. |
102 error_console_->UseDefaultReportingForExtension(kId); | 181 error_console_->UseDefaultReportingForExtension(kId); |
103 error_console_->ReportError(CreateNewManifestError(kId, "h")); | 182 error_console_->ReportError(CreateNewManifestError(kId, "h")); |
104 ASSERT_EQ(4u, error_console_->GetErrorsForExtension(kId).size()); | 183 ASSERT_EQ(4u, error_console_->GetErrorsForExtension(kId).size()); |
105 error_console_->ReportError(CreateNewRuntimeError(kId, "i")); | 184 error_console_->ReportError(CreateNewRuntimeError(kId, "i")); |
106 ASSERT_EQ(4u, error_console_->GetErrorsForExtension(kId).size()); | 185 ASSERT_EQ(4u, error_console_->GetErrorsForExtension(kId).size()); |
107 } | 186 } |
108 | 187 |
109 } // namespace extensions | 188 } // namespace extensions |
OLD | NEW |