Index: chrome/browser/extensions/extension_message_bubble_controller_unittest.cc |
diff --git a/chrome/browser/extensions/extension_message_bubble_controller_unittest.cc b/chrome/browser/extensions/extension_message_bubble_controller_unittest.cc |
index 656ae7788f24dbb0006b2cddbbd1b3d9978d9306..669a3b923e12edcecdbf905f812e36b3918ee247 100644 |
--- a/chrome/browser/extensions/extension_message_bubble_controller_unittest.cc |
+++ b/chrome/browser/extensions/extension_message_bubble_controller_unittest.cc |
@@ -9,6 +9,7 @@ |
#include "chrome/browser/extensions/extension_function_test_utils.h" |
#include "chrome/browser/extensions/extension_message_bubble.h" |
#include "chrome/browser/extensions/extension_service.h" |
+#include "chrome/browser/extensions/settings_api_bubble_controller.h" |
#include "chrome/browser/extensions/suspicious_extension_bubble_controller.h" |
#include "chrome/browser/extensions/test_extension_system.h" |
#include "chrome/test/base/testing_profile.h" |
@@ -97,6 +98,32 @@ class TestDevModeBubbleController |
} |
}; |
+// A test class for the SettingsApiBubbleController. |
+class TestSettingsApiBubbleController |
+ : public SettingsApiBubbleController, |
+ public TestDelegate { |
+ public: |
+ TestSettingsApiBubbleController(Profile* profile, |
+ SettingsApiOverrideType type) |
+ : SettingsApiBubbleController(profile, type) { |
+ } |
+ |
+ virtual void OnBubbleAction() OVERRIDE { |
+ ++action_button_callback_count_; |
+ SettingsApiBubbleController::OnBubbleAction(); |
+ } |
+ |
+ virtual void OnBubbleDismiss() OVERRIDE { |
+ ++dismiss_button_callback_count_; |
+ SettingsApiBubbleController::OnBubbleDismiss(); |
+ } |
+ |
+ virtual void OnLinkClicked() OVERRIDE { |
+ ++link_click_callback_count_; |
+ SettingsApiBubbleController::OnLinkClicked(); |
+ } |
+}; |
+ |
// A fake bubble used for testing the controller. Takes an action that specifies |
// what should happen when the bubble is "shown" (the bubble is actually not |
// shown, the corresponding action is taken immediately). |
@@ -145,62 +172,81 @@ class FakeExtensionMessageBubble : public ExtensionMessageBubble { |
class ExtensionMessageBubbleTest : public testing::Test { |
public: |
- ExtensionMessageBubbleTest() { |
- // The two lines of magical incantation required to get the extension |
- // service to work inside a unit test and access the extension prefs. |
- thread_bundle_.reset(new content::TestBrowserThreadBundle); |
- profile_.reset(new TestingProfile); |
+ ExtensionMessageBubbleTest() {} |
- static_cast<TestExtensionSystem*>( |
- ExtensionSystem::Get(profile()))->CreateExtensionService( |
- CommandLine::ForCurrentProcess(), |
- base::FilePath(), |
- false); |
- service_ = profile_->GetExtensionService(); |
- service_->Init(); |
+ std::string LoadExtensionWithManifest(const std::string& manifest_template, |
+ const std::string& index, |
+ Manifest::Location location) { |
+ std::string extension_data; |
+ base::ReplaceChars(manifest_template, "#", index, &extension_data); |
+ scoped_refptr<Extension> my_test_extension( |
+ CreateExtension( |
Yoyo Zhou
2014/04/02 20:36:26
Can you use ExtensionBuilder and DictionaryBuilder
|
+ location, |
+ extension_data, |
+ std::string("Autogenerated ") + index)); |
+ service_->AddExtension(my_test_extension); |
+ return my_test_extension->id(); |
+ } |
- std::string basic_extension = |
+ std::string LoadGenericExtension(const std::string& index, |
+ Manifest::Location location) { |
+ std::string manifest_template = |
"{\"name\": \"Extension #\"," |
"\"version\": \"1.0\"," |
"\"manifest_version\": 2}"; |
- std::string basic_extension_with_action = |
+ return LoadExtensionWithManifest(manifest_template, index, location); |
+ } |
+ |
+ std::string LoadExtensionWithAction(const std::string& index, |
+ Manifest::Location location) { |
+ std::string manifest_template = |
"{\"name\": \"Extension #\"," |
"\"version\": \"1.0\"," |
"\"browser_action\": {" |
" \"default_title\": \"Default title\"" |
"}," |
"\"manifest_version\": 2}"; |
+ return LoadExtensionWithManifest(manifest_template, index, location); |
+ } |
- std::string extension_data; |
- base::ReplaceChars(basic_extension_with_action, "#", "1", &extension_data); |
- scoped_refptr<Extension> my_test_extension1( |
- CreateExtension( |
- Manifest::COMMAND_LINE, |
- extension_data, |
- "Autogenerated 1")); |
- |
- base::ReplaceChars(basic_extension, "#", "2", &extension_data); |
- scoped_refptr<Extension> my_test_extension2( |
- CreateExtension( |
- Manifest::UNPACKED, |
- extension_data, |
- "Autogenerated 2")); |
- |
- base::ReplaceChars(basic_extension, "#", "3", &extension_data); |
- scoped_refptr<Extension> regular_extension( |
- CreateExtension( |
- Manifest::EXTERNAL_POLICY, |
- extension_data, |
- "Autogenerated 3")); |
+ std::string LoadExtensionOverridingHome(const std::string& index, |
+ Manifest::Location location) { |
+ std::string manifest_template = |
+ "{\"name\": \"Extension #\"," |
+ "\"version\": \"1.0\"," |
+ "\"manifest_version\": 2," |
+ "\"chrome_settings_overrides\": {" |
+ "\"homepage\": \"http://www.google.com\"" |
+ "}}"; |
+ return LoadExtensionWithManifest(manifest_template, index, location); |
+ } |
- extension_id1_ = my_test_extension1->id(); |
- extension_id2_ = my_test_extension2->id(); |
- extension_id3_ = regular_extension->id(); |
+ std::string LoadExtensionOverridingStart(const std::string& index, |
+ Manifest::Location location) { |
+ std::string manifest_template = |
+ "{\"name\": \"Extension #\"," |
+ "\"version\": \"1.0\"," |
+ "\"manifest_version\": 2," |
+ "\"chrome_settings_overrides\": {" |
+ "\"startup_pages\": [\"http://www.google.com/\"]" |
+ "}}"; |
+ return LoadExtensionWithManifest(manifest_template, index, location); |
+ } |
- service_->AddExtension(regular_extension); |
- service_->AddExtension(my_test_extension1); |
- service_->AddExtension(my_test_extension2); |
+ void Init() { |
+ // The two lines of magical incantation required to get the extension |
+ // service to work inside a unit test and access the extension prefs. |
+ thread_bundle_.reset(new content::TestBrowserThreadBundle); |
+ profile_.reset(new TestingProfile); |
+ static_cast<TestExtensionSystem*>( |
+ ExtensionSystem::Get(profile()))->CreateExtensionService( |
+ CommandLine::ForCurrentProcess(), |
+ base::FilePath(), |
+ false); |
+ service_ = profile_->GetExtensionService(); |
+ service_->Init(); |
} |
+ |
virtual ~ExtensionMessageBubbleTest() { |
// Make sure the profile is destroyed before the thread bundle. |
profile_.reset(NULL); |
@@ -246,8 +292,13 @@ class ExtensionMessageBubbleTest : public testing::Test { |
#endif |
TEST_F(ExtensionMessageBubbleTest, MAYBE_WipeoutControllerTest) { |
- // The test base class adds three extensions, and we control two of them in |
- // this test (ids are: extension_id1_ and extension_id2_). |
+ Init(); |
+ // Add three extensions, and control two of them in this test (extension 1 |
+ // and 2). |
+ extension_id1_ = LoadExtensionWithAction("1", Manifest::COMMAND_LINE); |
+ extension_id2_ = LoadGenericExtension("2", Manifest::UNPACKED); |
+ extension_id3_ = LoadGenericExtension("3", Manifest::EXTERNAL_POLICY); |
+ |
scoped_ptr<TestSuspiciousExtensionBubbleController> controller( |
new TestSuspiciousExtensionBubbleController(profile())); |
FakeExtensionMessageBubble bubble; |
@@ -319,10 +370,14 @@ TEST_F(ExtensionMessageBubbleTest, MAYBE_WipeoutControllerTest) { |
TEST_F(ExtensionMessageBubbleTest, MAYBE_DevModeControllerTest) { |
FeatureSwitch::ScopedOverride force_dev_mode_highlighting( |
FeatureSwitch::force_dev_mode_highlighting(), true); |
- // The test base class adds three extensions, and we control two of them in |
- // this test (ids are: extension_id1_ and extension_id2_). Extension 1 is a |
- // regular extension, Extension 2 is UNPACKED so it counts as a DevMode |
- // extension. |
+ Init(); |
+ // Add three extensions, and control two of them in this test (extension 1 |
+ // and 2). Extension 1 is a regular extension, Extension 2 is UNPACKED so it |
+ // counts as a DevMode extension. |
+ extension_id1_ = LoadExtensionWithAction("1", Manifest::COMMAND_LINE); |
+ extension_id2_ = LoadGenericExtension("2", Manifest::UNPACKED); |
+ extension_id3_ = LoadGenericExtension("3", Manifest::EXTERNAL_POLICY); |
+ |
scoped_ptr<TestDevModeBubbleController> controller( |
new TestDevModeBubbleController(profile())); |
@@ -396,4 +451,125 @@ TEST_F(ExtensionMessageBubbleTest, MAYBE_DevModeControllerTest) { |
EXPECT_EQ(0U, dev_mode_extensions.size()); |
} |
+// The feature this is meant to test is only implemented on Windows. |
+#if defined(OS_WIN) |
+#define MAYBE_SettingsApiControllerTest SettingsApiControllerTest |
+#else |
+#define MAYBE_SettingsApiControllerTest DISABLED_SettingsApiControllerTest |
+#endif |
+ |
+TEST_F(ExtensionMessageBubbleTest, MAYBE_SettingsApiControllerTest) { |
+ Init(); |
+ extensions::ExtensionPrefs* prefs = |
+ extensions::ExtensionPrefs::Get(profile()); |
+ |
+ // We deliberately skip testing the search engine since it relies on |
Yoyo Zhou
2014/04/02 20:36:26
IMHO this would be easier to read if
- the for loo
|
+ // TemplateURLServiceFactory that isn't available while unit testing. This |
+ // test is only simulating the bubble interaction with the user and that is |
+ // more or less the same for the search engine as it is for the others. |
+ for (int i = 0; i < 3; i += 2) { |
+ switch (static_cast<SettingsApiOverrideType>(i)) { |
+ case BUBBLE_TYPE_HOME_PAGE: |
+ // Load two extensions overriding home page and one overriding something |
+ // unrelated (to check for interference). Extension 2 should still win |
+ // on the home page setting. |
+ extension_id1_ = LoadExtensionOverridingHome("1", Manifest::UNPACKED); |
+ extension_id2_ = LoadExtensionOverridingHome("2", Manifest::UNPACKED); |
+ extension_id3_ = LoadExtensionOverridingStart("3", Manifest::UNPACKED); |
+ break; |
+ case BUBBLE_TYPE_STARTUP_PAGES: |
+ // Load two extensions overriding start page and one overriding |
+ // something unrelated (to check for interference). Extension 2 should |
+ // still win on the startup page setting. |
+ extension_id1_ = LoadExtensionOverridingStart("1", Manifest::UNPACKED); |
+ extension_id2_ = LoadExtensionOverridingStart("2", Manifest::UNPACKED); |
+ extension_id3_ = LoadExtensionOverridingHome("3", Manifest::UNPACKED); |
+ break; |
+ default: |
+ NOTREACHED(); |
+ break; |
+ } |
+ |
+ scoped_ptr<TestSettingsApiBubbleController> controller( |
+ new TestSettingsApiBubbleController( |
+ profile(), static_cast<SettingsApiOverrideType>(i))); |
+ |
+ // The list will contain one enabled unpacked extension (ext 2). |
+ EXPECT_TRUE(controller->ShouldShow(extension_id2_)); |
+ std::vector<base::string16> override_extensions = |
+ controller->GetExtensionList(); |
+ ASSERT_EQ(1U, override_extensions.size()); |
+ EXPECT_TRUE(base::ASCIIToUTF16("Extension 2") == |
+ override_extensions[0].c_str()); |
+ EXPECT_EQ(0U, controller->link_click_count()); |
+ EXPECT_EQ(0U, controller->dismiss_click_count()); |
+ EXPECT_EQ(0U, controller->action_click_count()); |
+ |
+ // Simulate showing the bubble and dismissing it. |
+ FakeExtensionMessageBubble bubble; |
+ bubble.set_action_on_show( |
+ FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_DISMISS_BUTTON); |
+ controller->Show(&bubble); |
+ EXPECT_EQ(0U, controller->link_click_count()); |
+ EXPECT_EQ(0U, controller->action_click_count()); |
+ EXPECT_EQ(1U, controller->dismiss_click_count()); |
+ // No extension should have become disabled. |
+ EXPECT_TRUE(service_->GetExtensionById(extension_id1_, false) != NULL); |
+ EXPECT_TRUE(service_->GetExtensionById(extension_id2_, false) != NULL); |
+ EXPECT_TRUE(service_->GetExtensionById(extension_id3_, false) != NULL); |
+ // Only extension 2 should have been acknowledged. |
+ EXPECT_FALSE(prefs->HasSettingsApiBubbleBeenAcknowledged(extension_id1_)); |
+ EXPECT_TRUE(prefs->HasSettingsApiBubbleBeenAcknowledged(extension_id2_)); |
+ EXPECT_FALSE(prefs->HasSettingsApiBubbleBeenAcknowledged(extension_id3_)); |
+ // Clean up after ourselves. |
+ prefs->SetSettingsApiBubbleBeenAcknowledged(extension_id2_, false); |
+ |
+ // Simulate clicking the learn more link to dismiss it. |
+ bubble.set_action_on_show( |
+ FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_LINK); |
+ controller.reset(new TestSettingsApiBubbleController( |
+ profile(), static_cast<SettingsApiOverrideType>(i))); |
+ controller->Show(&bubble); |
+ EXPECT_EQ(1U, controller->link_click_count()); |
+ EXPECT_EQ(0U, controller->action_click_count()); |
+ EXPECT_EQ(0U, controller->dismiss_click_count()); |
+ // No extension should have become disabled. |
+ EXPECT_TRUE(service_->GetExtensionById(extension_id1_, false) != NULL); |
+ EXPECT_TRUE(service_->GetExtensionById(extension_id2_, false) != NULL); |
+ EXPECT_TRUE(service_->GetExtensionById(extension_id3_, false) != NULL); |
+ // Only extension 2 should have been acknowledged. |
+ EXPECT_FALSE(prefs->HasSettingsApiBubbleBeenAcknowledged(extension_id1_)); |
+ EXPECT_TRUE(prefs->HasSettingsApiBubbleBeenAcknowledged(extension_id2_)); |
+ EXPECT_FALSE(prefs->HasSettingsApiBubbleBeenAcknowledged(extension_id3_)); |
+ // Clean up after ourselves. |
+ prefs->SetSettingsApiBubbleBeenAcknowledged(extension_id2_, false); |
+ |
+ // Do it again, but now opt to disable the extension. |
+ bubble.set_action_on_show( |
+ FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_ACTION_BUTTON); |
+ controller.reset(new TestSettingsApiBubbleController( |
+ profile(), static_cast<SettingsApiOverrideType>(i))); |
+ EXPECT_TRUE(controller->ShouldShow(extension_id2_)); |
+ override_extensions = controller->GetExtensionList(); |
+ EXPECT_EQ(1U, override_extensions.size()); |
+ controller->Show(&bubble); // Simulate showing the bubble. |
+ EXPECT_EQ(0U, controller->link_click_count()); |
+ EXPECT_EQ(1U, controller->action_click_count()); |
+ EXPECT_EQ(0U, controller->dismiss_click_count()); |
+ // Only extension 2 should have become disabled. |
+ EXPECT_TRUE(service_->GetExtensionById(extension_id1_, false) != NULL); |
+ EXPECT_TRUE(service_->GetExtensionById(extension_id2_, false) == NULL); |
+ EXPECT_TRUE(service_->GetExtensionById(extension_id3_, false) != NULL); |
+ // No extension should have been acknowledged (it got disabled). |
+ EXPECT_FALSE(prefs->HasSettingsApiBubbleBeenAcknowledged(extension_id1_)); |
+ EXPECT_FALSE(prefs->HasSettingsApiBubbleBeenAcknowledged(extension_id2_)); |
+ EXPECT_FALSE(prefs->HasSettingsApiBubbleBeenAcknowledged(extension_id3_)); |
+ |
+ // Clean up after ourselves. |
+ service_->UninstallExtension(extension_id1_, false, NULL); |
+ service_->UninstallExtension(extension_id2_, false, NULL); |
+ service_->UninstallExtension(extension_id3_, false, NULL); |
+ } |
+} |
+ |
} // namespace extensions |