Chromium Code Reviews| 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..6435e85c0d9186450b5b78322343627c45153275 |
| --- /dev/null |
| +++ b/chrome/browser/content_settings/permission_observer_unittest.cc |
| @@ -0,0 +1,271 @@ |
| +// 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 PermissionObserverTests : public ChromeRenderViewHostTestHarness { |
|
Bernhard Bauer
2015/03/11 14:06:18
The usual style is to use singular for the test fi
mlamouri (slow - plz ping)
2015/03/18 16:24:40
I will not point that permission_context_base_unit
Bernhard Bauer
2015/03/23 10:20:43
Feel free to complain about that to the person who
|
| + public: |
| + void OnPermissionChange(ContentSetting content_setting) { |
| + callback_called_ = true; |
| + content_setting_ = content_setting; |
| + } |
| + |
| + protected: |
| + PermissionObserverTests() |
| + : ChromeRenderViewHostTestHarness(), |
| + url_("https://example.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_; |
|
Bernhard Bauer
2015/03/11 14:06:18
If this URL is constant, you could just directly u
mlamouri (slow - plz ping)
2015/03/18 16:24:40
I used to do that and I stopped because it was add
Bernhard Bauer
2015/03/23 10:20:43
What additional cost is that?
Bernhard Bauer
2015/03/23 18:26:18
Ping?
|
| + } |
| + |
| + 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_; |
| + bool callback_called_; |
| + ContentSetting content_setting_; |
| +}; |
| + |
| +TEST_F(PermissionObserverTests, SameTypeChangeNotifies) { |
| + int subscription_id = GetPermissionObserver()->Subscribe( |
| + CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), |
| + base::Bind(&PermissionObserverTests::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(PermissionObserverTests, DifferentTypeChangeDoesNotNotify) { |
| + int subscription_id = GetPermissionObserver()->Subscribe( |
| + CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), |
| + base::Bind(&PermissionObserverTests::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(PermissionObserverTests, ChangeAfterUnsubscribeDoesNotNotify) { |
| + int subscription_id = GetPermissionObserver()->Subscribe( |
| + CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), |
| + base::Bind(&PermissionObserverTests::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(PermissionObserverTests, DifferentPrimaryPatternDoesNotNotify) { |
| + int subscription_id = GetPermissionObserver()->Subscribe( |
| + CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), |
| + base::Bind(&PermissionObserverTests::OnPermissionChange, |
| + base::Unretained(this))); |
| + |
| + GetHostContentSettingsMap()->SetContentSetting( |
| + ContentSettingsPattern::FromURLNoWildcard(GURL("https://foo.com")), |
| + ContentSettingsPattern::FromURLNoWildcard(url()), |
| + CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| + std::string(), |
| + CONTENT_SETTING_ALLOW); |
| + |
| + EXPECT_FALSE(callback_called()); |
| + |
| + GetPermissionObserver()->Unsubscribe(subscription_id); |
| +} |
| + |
| +TEST_F(PermissionObserverTests, DifferentSecondaryPatternDoesNotNotify) { |
| + int subscription_id = GetPermissionObserver()->Subscribe( |
| + CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), |
| + base::Bind(&PermissionObserverTests::OnPermissionChange, |
| + base::Unretained(this))); |
| + |
| + GetHostContentSettingsMap()->SetContentSetting( |
| + ContentSettingsPattern::FromURLNoWildcard(url()), |
| + ContentSettingsPattern::FromURLNoWildcard(GURL("https://foo.com")), |
| + CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| + std::string(), |
| + CONTENT_SETTING_ALLOW); |
| + |
| + EXPECT_FALSE(callback_called()); |
| + |
| + GetPermissionObserver()->Unsubscribe(subscription_id); |
| +} |
| + |
| +TEST_F(PermissionObserverTests, WildCardPatternNotifies) { |
| + int subscription_id = GetPermissionObserver()->Subscribe( |
| + CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), |
| + base::Bind(&PermissionObserverTests::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(PermissionObserverTests, 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(&PermissionObserverTests::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(PermissionObserverTests, NewValueCorrectlyPassed) { |
| + int subscription_id = GetPermissionObserver()->Subscribe( |
| + CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), |
| + base::Bind(&PermissionObserverTests::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(PermissionObserverTests, 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(&PermissionObserverTests::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(PermissionObserverTests, 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(&PermissionObserverTests::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); |
| +} |