| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/command_line.h" | |
| 6 #include "base/strings/string_util.h" | |
| 7 #include "base/strings/utf_string_conversions.h" | |
| 8 #include "chrome/browser/extensions/extension_function_test_utils.h" | |
| 9 #include "chrome/browser/extensions/extension_service.h" | |
| 10 #include "chrome/browser/extensions/suspicious_extension_bubble.h" | |
| 11 #include "chrome/browser/extensions/suspicious_extension_bubble_controller.h" | |
| 12 #include "chrome/browser/extensions/test_extension_system.h" | |
| 13 #include "chrome/test/base/testing_profile.h" | |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | |
| 15 #include "extensions/common/extension.h" | |
| 16 | |
| 17 namespace extensions { | |
| 18 | |
| 19 // A test class for the SuspiciousExtensionBubbleController. | |
| 20 class TestSuspiciousExtensionBubbleController | |
| 21 : public SuspiciousExtensionBubbleController { | |
| 22 public: | |
| 23 explicit TestSuspiciousExtensionBubbleController(Profile* profile) | |
| 24 : SuspiciousExtensionBubbleController(profile), | |
| 25 dismiss_button_callback_count_(0), | |
| 26 link_click_callback_count_(0) { | |
| 27 } | |
| 28 | |
| 29 // Returns how often the dismiss button has been called. | |
| 30 size_t dismiss_click_count() { | |
| 31 return dismiss_button_callback_count_; | |
| 32 } | |
| 33 | |
| 34 // Returns how often the link has been clicked. | |
| 35 size_t link_click_count() { | |
| 36 return link_click_callback_count_; | |
| 37 } | |
| 38 | |
| 39 // Callbacks from bubble. | |
| 40 virtual void OnBubbleDismiss() OVERRIDE { | |
| 41 ++dismiss_button_callback_count_; | |
| 42 SuspiciousExtensionBubbleController::OnBubbleDismiss(); | |
| 43 } | |
| 44 virtual void OnLinkClicked() OVERRIDE { | |
| 45 ++link_click_callback_count_; | |
| 46 SuspiciousExtensionBubbleController::OnLinkClicked(); | |
| 47 } | |
| 48 | |
| 49 private: | |
| 50 size_t dismiss_button_callback_count_; | |
| 51 size_t link_click_callback_count_; | |
| 52 }; | |
| 53 | |
| 54 // A fake bubble used for testing the controller. Takes an action that specifies | |
| 55 // what should happen when the bubble is "shown" (the bubble is actually not | |
| 56 // shown, the corresponding action is taken immediately). | |
| 57 class FakeSuspiciousExtensionBubble : public SuspiciousExtensionBubble { | |
| 58 public: | |
| 59 enum SuspiciousExtensionBubbleAction { | |
| 60 BUBBLE_ACTION_CLICK_BUTTON = 0, | |
| 61 BUBBLE_ACTION_CLICK_LINK | |
| 62 }; | |
| 63 | |
| 64 FakeSuspiciousExtensionBubble() {} | |
| 65 | |
| 66 void set_action_on_show(SuspiciousExtensionBubbleAction action) { | |
| 67 action_ = action; | |
| 68 } | |
| 69 | |
| 70 virtual void Show() OVERRIDE { | |
| 71 if (action_ == BUBBLE_ACTION_CLICK_BUTTON) | |
| 72 button_callback_.Run(); | |
| 73 else if (action_ == BUBBLE_ACTION_CLICK_LINK) | |
| 74 link_callback_.Run(); | |
| 75 } | |
| 76 | |
| 77 virtual void OnButtonClicked(const base::Closure& callback) OVERRIDE { | |
| 78 button_callback_ = callback; | |
| 79 } | |
| 80 | |
| 81 virtual void OnLinkClicked(const base::Closure& callback) OVERRIDE { | |
| 82 link_callback_ = callback; | |
| 83 } | |
| 84 | |
| 85 private: | |
| 86 SuspiciousExtensionBubbleAction action_; | |
| 87 | |
| 88 base::Closure button_callback_; | |
| 89 base::Closure link_callback_; | |
| 90 }; | |
| 91 | |
| 92 class SuspiciousExtensionBubbleTest : public testing::Test { | |
| 93 public: | |
| 94 SuspiciousExtensionBubbleTest() { | |
| 95 // The two lines of magical incantation required to get the extension | |
| 96 // service to work inside a unit test and access the extension prefs. | |
| 97 thread_bundle_.reset(new content::TestBrowserThreadBundle); | |
| 98 profile_.reset(new TestingProfile); | |
| 99 } | |
| 100 virtual ~SuspiciousExtensionBubbleTest() {} | |
| 101 virtual void SetUp() { | |
| 102 command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM)); | |
| 103 } | |
| 104 scoped_ptr<CommandLine> command_line_; | |
| 105 | |
| 106 protected: | |
| 107 scoped_refptr<Extension> CreateExtension( | |
| 108 Manifest::Location location, | |
| 109 const std::string& data, | |
| 110 const std::string& id) { | |
| 111 scoped_ptr<base::DictionaryValue> parsed_manifest( | |
| 112 extension_function_test_utils::ParseDictionary(data)); | |
| 113 return extension_function_test_utils::CreateExtension( | |
| 114 location, | |
| 115 parsed_manifest.get(), | |
| 116 id); | |
| 117 } | |
| 118 | |
| 119 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_; | |
| 120 scoped_ptr<TestingProfile> profile_; | |
| 121 | |
| 122 private: | |
| 123 DISALLOW_COPY_AND_ASSIGN(SuspiciousExtensionBubbleTest); | |
| 124 }; | |
| 125 | |
| 126 // The feature this is meant to test is only implemented on Windows. | |
| 127 #if defined(OS_WIN) | |
| 128 #define MAYBE_ControllerTest ControllerTest | |
| 129 #else | |
| 130 #define MAYBE_ControllerTest DISABLED_ControllerTest | |
| 131 #endif | |
| 132 | |
| 133 TEST_F(SuspiciousExtensionBubbleTest, MAYBE_ControllerTest) { | |
| 134 std::string basic_extension = | |
| 135 "{\"name\": \"Extension #\"," | |
| 136 "\"version\": \"1.0\"," | |
| 137 "\"manifest_version\": 2}"; | |
| 138 | |
| 139 std::string extension_data; | |
| 140 base::ReplaceChars(basic_extension, "#", "1", &extension_data); | |
| 141 scoped_refptr<Extension> my_test_extension1( | |
| 142 CreateExtension( | |
| 143 Manifest::COMMAND_LINE, | |
| 144 extension_data, | |
| 145 "Autogenerated 1")); | |
| 146 | |
| 147 base::ReplaceChars(basic_extension, "#", "2", &extension_data); | |
| 148 scoped_refptr<Extension> my_test_extension2( | |
| 149 CreateExtension( | |
| 150 Manifest::COMMAND_LINE, | |
| 151 extension_data, | |
| 152 "Autogenerated 2")); | |
| 153 | |
| 154 base::ReplaceChars(basic_extension, "#", "3", &extension_data); | |
| 155 scoped_refptr<Extension> regular_extension( | |
| 156 CreateExtension( | |
| 157 Manifest::COMMAND_LINE, | |
| 158 extension_data, | |
| 159 "UnimportantId")); | |
| 160 | |
| 161 std::string extension_id1 = my_test_extension1->id(); | |
| 162 std::string extension_id2 = my_test_extension2->id(); | |
| 163 | |
| 164 static_cast<TestExtensionSystem*>( | |
| 165 ExtensionSystem::Get(profile_.get()))->CreateExtensionService( | |
| 166 CommandLine::ForCurrentProcess(), | |
| 167 base::FilePath(), | |
| 168 false); | |
| 169 ExtensionService* service = profile_->GetExtensionService(); | |
| 170 service->Init(); | |
| 171 | |
| 172 service->AddExtension(regular_extension); | |
| 173 service->AddExtension(my_test_extension1); | |
| 174 service->AddExtension(my_test_extension2); | |
| 175 | |
| 176 scoped_ptr<TestSuspiciousExtensionBubbleController> controller( | |
| 177 new TestSuspiciousExtensionBubbleController(profile_.get())); | |
| 178 FakeSuspiciousExtensionBubble bubble; | |
| 179 bubble.set_action_on_show( | |
| 180 FakeSuspiciousExtensionBubble::BUBBLE_ACTION_CLICK_BUTTON); | |
| 181 | |
| 182 // Validate that we don't have a suppress value for the extensions. | |
| 183 ExtensionPrefs* prefs = service->extension_prefs(); | |
| 184 EXPECT_FALSE(prefs->HasWipeoutBeenAcknowledged(extension_id1)); | |
| 185 EXPECT_FALSE(prefs->HasWipeoutBeenAcknowledged(extension_id2)); | |
| 186 | |
| 187 EXPECT_FALSE(controller->HasSuspiciousExtensions()); | |
| 188 std::vector<string16> suspicious_extensions = | |
| 189 controller->GetSuspiciousExtensionNames(); | |
| 190 EXPECT_EQ(0U, suspicious_extensions.size()); | |
| 191 EXPECT_EQ(0U, controller->link_click_count()); | |
| 192 EXPECT_EQ(0U, controller->dismiss_click_count()); | |
| 193 | |
| 194 // Now disable an extension, specifying the wipeout flag. | |
| 195 service->DisableExtension(extension_id1, | |
| 196 Extension::DISABLE_NOT_VERIFIED); | |
| 197 | |
| 198 EXPECT_FALSE(prefs->HasWipeoutBeenAcknowledged(extension_id1)); | |
| 199 EXPECT_FALSE(prefs->HasWipeoutBeenAcknowledged(extension_id2)); | |
| 200 controller.reset(new TestSuspiciousExtensionBubbleController(profile_.get())); | |
| 201 EXPECT_TRUE(controller->HasSuspiciousExtensions()); | |
| 202 suspicious_extensions = controller->GetSuspiciousExtensionNames(); | |
| 203 ASSERT_EQ(1U, suspicious_extensions.size()); | |
| 204 EXPECT_TRUE(ASCIIToUTF16("Extension 1") == suspicious_extensions[0]); | |
| 205 controller->Show(&bubble); // Simulate showing the bubble. | |
| 206 EXPECT_EQ(0U, controller->link_click_count()); | |
| 207 EXPECT_EQ(1U, controller->dismiss_click_count()); | |
| 208 // Now the acknowledge flag should be set only for the first extension. | |
| 209 EXPECT_TRUE(prefs->HasWipeoutBeenAcknowledged(extension_id1)); | |
| 210 EXPECT_FALSE(prefs->HasWipeoutBeenAcknowledged(extension_id2)); | |
| 211 // Clear the flag. | |
| 212 prefs->SetWipeoutAcknowledged(extension_id1, false); | |
| 213 EXPECT_FALSE(prefs->HasWipeoutBeenAcknowledged(extension_id1)); | |
| 214 | |
| 215 // Now disable the other extension and exercise the link click code path. | |
| 216 service->DisableExtension(extension_id2, | |
| 217 Extension::DISABLE_NOT_VERIFIED); | |
| 218 | |
| 219 bubble.set_action_on_show( | |
| 220 FakeSuspiciousExtensionBubble::BUBBLE_ACTION_CLICK_LINK); | |
| 221 controller.reset(new TestSuspiciousExtensionBubbleController(profile_.get())); | |
| 222 EXPECT_TRUE(controller->HasSuspiciousExtensions()); | |
| 223 suspicious_extensions = controller->GetSuspiciousExtensionNames(); | |
| 224 ASSERT_EQ(2U, suspicious_extensions.size()); | |
| 225 EXPECT_TRUE(ASCIIToUTF16("Extension 1") == suspicious_extensions[1]); | |
| 226 EXPECT_TRUE(ASCIIToUTF16("Extension 2") == suspicious_extensions[0]); | |
| 227 controller->Show(&bubble); // Simulate showing the bubble. | |
| 228 EXPECT_EQ(1U, controller->link_click_count()); | |
| 229 EXPECT_EQ(0U, controller->dismiss_click_count()); | |
| 230 } | |
| 231 | |
| 232 } // namespace extensions | |
| OLD | NEW |