Index: chrome/browser/content_settings/permission_observer_unittest.cc |
diff --git a/chrome/browser/content_settings/permission_observer_unittest.cc b/chrome/browser/content_settings/permission_observer_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..584dac6fa903cd7bbadcbea8a0bf7a418dae506c |
--- /dev/null |
+++ b/chrome/browser/content_settings/permission_observer_unittest.cc |
@@ -0,0 +1,277 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/content_settings/permission_observer.h" |
+ |
+#include "chrome/browser/content_settings/permission_observer_factory.h" |
+#include "chrome/test/base/chrome_render_view_host_test_harness.h" |
+#include "chrome/test/base/testing_profile.h" |
+#include "components/content_settings/core/browser/host_content_settings_map.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+class PermissionObserverTest : public ChromeRenderViewHostTestHarness { |
+ public: |
+ void OnPermissionChange(ContentSetting content_setting) { |
+ callback_called_ = true; |
+ content_setting_ = content_setting; |
+ } |
+ |
+ protected: |
+ PermissionObserverTest() |
+ : ChromeRenderViewHostTestHarness(), |
+ url_("https://example.com"), |
+ other_url_("https://foo.com"), |
+ callback_called_(false), |
+ content_setting_(CONTENT_SETTING_DEFAULT) { |
+ } |
+ |
+ PermissionObserver* GetPermissionObserver() { |
+ return PermissionObserverFactory::GetForProfile(profile()); |
+ } |
+ |
+ HostContentSettingsMap* GetHostContentSettingsMap() { |
+ return profile()->GetHostContentSettingsMap(); |
+ } |
+ |
+ const GURL& url() const { |
+ return url_; |
+ } |
+ |
+ const GURL& other_url() const { |
+ return other_url_; |
+ } |
+ |
+ bool callback_called() const { |
+ return callback_called_; |
+ } |
+ |
+ ContentSetting content_setting() const { |
+ return content_setting_; |
+ } |
+ |
+ void Reset() { |
+ callback_called_ = false; |
+ content_setting_ = CONTENT_SETTING_DEFAULT; |
+ } |
+ |
+ private: |
+ const GURL url_; |
+ const GURL other_url_; |
+ bool callback_called_; |
+ ContentSetting content_setting_; |
+}; |
+ |
+TEST_F(PermissionObserverTest, SameTypeChangeNotifies) { |
+ int subscription_id = GetPermissionObserver()->Subscribe( |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), |
+ base::Bind(&PermissionObserverTest::OnPermissionChange, |
+ base::Unretained(this))); |
+ |
+ GetHostContentSettingsMap()->SetContentSetting( |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, |
+ std::string(), |
+ CONTENT_SETTING_ALLOW); |
+ |
+ EXPECT_TRUE(callback_called()); |
+ EXPECT_EQ(CONTENT_SETTING_ALLOW, content_setting()); |
+ |
+ GetPermissionObserver()->Unsubscribe(subscription_id); |
+} |
+ |
+TEST_F(PermissionObserverTest, DifferentTypeChangeDoesNotNotify) { |
+ int subscription_id = GetPermissionObserver()->Subscribe( |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), |
+ base::Bind(&PermissionObserverTest::OnPermissionChange, |
+ base::Unretained(this))); |
+ |
+ GetHostContentSettingsMap()->SetContentSetting( |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
+ std::string(), |
+ CONTENT_SETTING_ALLOW); |
+ |
+ EXPECT_FALSE(callback_called()); |
+ |
+ GetPermissionObserver()->Unsubscribe(subscription_id); |
+} |
+ |
+TEST_F(PermissionObserverTest, ChangeAfterUnsubscribeDoesNotNotify) { |
+ int subscription_id = GetPermissionObserver()->Subscribe( |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), |
+ base::Bind(&PermissionObserverTest::OnPermissionChange, |
+ base::Unretained(this))); |
+ |
+ GetPermissionObserver()->Unsubscribe(subscription_id); |
+ |
+ GetHostContentSettingsMap()->SetContentSetting( |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, |
+ std::string(), |
+ CONTENT_SETTING_ALLOW); |
+ |
+ EXPECT_FALSE(callback_called()); |
+} |
+ |
+TEST_F(PermissionObserverTest, DifferentPrimaryPatternDoesNotNotify) { |
+ int subscription_id = GetPermissionObserver()->Subscribe( |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), |
+ base::Bind(&PermissionObserverTest::OnPermissionChange, |
+ base::Unretained(this))); |
+ |
+ GetHostContentSettingsMap()->SetContentSetting( |
+ ContentSettingsPattern::FromURLNoWildcard(other_url()), |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, |
+ std::string(), |
+ CONTENT_SETTING_ALLOW); |
+ |
+ EXPECT_FALSE(callback_called()); |
+ |
+ GetPermissionObserver()->Unsubscribe(subscription_id); |
+} |
+ |
+TEST_F(PermissionObserverTest, DifferentSecondaryPatternDoesNotNotify) { |
+ int subscription_id = GetPermissionObserver()->Subscribe( |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), |
+ base::Bind(&PermissionObserverTest::OnPermissionChange, |
+ base::Unretained(this))); |
+ |
+ GetHostContentSettingsMap()->SetContentSetting( |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ ContentSettingsPattern::FromURLNoWildcard(other_url()), |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, |
+ std::string(), |
+ CONTENT_SETTING_ALLOW); |
+ |
+ EXPECT_FALSE(callback_called()); |
+ |
+ GetPermissionObserver()->Unsubscribe(subscription_id); |
+} |
+ |
+TEST_F(PermissionObserverTest, WildCardPatternNotifies) { |
+ int subscription_id = GetPermissionObserver()->Subscribe( |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), |
+ base::Bind(&PermissionObserverTest::OnPermissionChange, |
+ base::Unretained(this))); |
+ |
+ GetHostContentSettingsMap()->SetContentSetting( |
+ ContentSettingsPattern::Wildcard(), |
+ ContentSettingsPattern::Wildcard(), |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, |
+ std::string(), |
+ CONTENT_SETTING_ALLOW); |
+ |
+ EXPECT_TRUE(callback_called()); |
+ EXPECT_EQ(CONTENT_SETTING_ALLOW, content_setting()); |
+ |
+ GetPermissionObserver()->Unsubscribe(subscription_id); |
+} |
+ |
+TEST_F(PermissionObserverTest, ClearSettingsNotifies) { |
+ GetHostContentSettingsMap()->SetContentSetting( |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, |
+ std::string(), |
+ CONTENT_SETTING_ALLOW); |
+ |
+ int subscription_id = GetPermissionObserver()->Subscribe( |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), |
+ base::Bind(&PermissionObserverTest::OnPermissionChange, |
+ base::Unretained(this))); |
+ |
+ GetHostContentSettingsMap()->ClearSettingsForOneType( |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION); |
+ |
+ EXPECT_TRUE(callback_called()); |
+ EXPECT_EQ(CONTENT_SETTING_ASK, content_setting()); |
+ |
+ GetPermissionObserver()->Unsubscribe(subscription_id); |
+} |
+ |
+TEST_F(PermissionObserverTest, NewValueCorrectlyPassed) { |
+ int subscription_id = GetPermissionObserver()->Subscribe( |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), |
+ base::Bind(&PermissionObserverTest::OnPermissionChange, |
+ base::Unretained(this))); |
+ |
+ GetHostContentSettingsMap()->SetContentSetting( |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, |
+ std::string(), |
+ CONTENT_SETTING_BLOCK); |
+ |
+ EXPECT_TRUE(callback_called()); |
+ EXPECT_EQ(CONTENT_SETTING_BLOCK, content_setting()); |
+ |
+ GetPermissionObserver()->Unsubscribe(subscription_id); |
+} |
+ |
+TEST_F(PermissionObserverTest, ChangeWithoutPermissionChangeDoesNotNotify) { |
+ GetHostContentSettingsMap()->SetContentSetting( |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, |
+ std::string(), |
+ CONTENT_SETTING_ALLOW); |
+ |
+ int subscription_id = GetPermissionObserver()->Subscribe( |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), |
+ base::Bind(&PermissionObserverTest::OnPermissionChange, |
+ base::Unretained(this))); |
+ |
+ GetHostContentSettingsMap()->SetContentSetting( |
+ ContentSettingsPattern::Wildcard(), |
+ ContentSettingsPattern::Wildcard(), |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, |
+ std::string(), |
+ CONTENT_SETTING_ALLOW); |
+ |
+ EXPECT_FALSE(callback_called()); |
+ |
+ GetPermissionObserver()->Unsubscribe(subscription_id); |
+} |
+ |
+TEST_F(PermissionObserverTest, ChangesBackAndForth) { |
+ GetHostContentSettingsMap()->SetContentSetting( |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, |
+ std::string(), |
+ CONTENT_SETTING_ASK); |
+ |
+ int subscription_id = GetPermissionObserver()->Subscribe( |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), |
+ base::Bind(&PermissionObserverTest::OnPermissionChange, |
+ base::Unretained(this))); |
+ |
+ GetHostContentSettingsMap()->SetContentSetting( |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, |
+ std::string(), |
+ CONTENT_SETTING_ALLOW); |
+ |
+ EXPECT_TRUE(callback_called()); |
+ EXPECT_EQ(CONTENT_SETTING_ALLOW, content_setting()); |
+ |
+ Reset(); |
+ |
+ GetHostContentSettingsMap()->SetContentSetting( |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ ContentSettingsPattern::FromURLNoWildcard(url()), |
+ CONTENT_SETTINGS_TYPE_GEOLOCATION, |
+ std::string(), |
+ CONTENT_SETTING_ASK); |
+ |
+ EXPECT_TRUE(callback_called()); |
+ EXPECT_EQ(CONTENT_SETTING_ASK, content_setting()); |
+ |
+ GetPermissionObserver()->Unsubscribe(subscription_id); |
+} |