Index: chrome/browser/permissions/permission_context_base_unittest.cc |
diff --git a/chrome/browser/permissions/permission_context_base_unittest.cc b/chrome/browser/permissions/permission_context_base_unittest.cc |
index 19d713500b7fcf263c4ae564eba6653f0e0c7343..fb996b54d7a7ec3913f9acf0f7a5431f81191dd6 100644 |
--- a/chrome/browser/permissions/permission_context_base_unittest.cc |
+++ b/chrome/browser/permissions/permission_context_base_unittest.cc |
@@ -7,8 +7,11 @@ |
#include "base/bind.h" |
#include "base/command_line.h" |
#include "base/macros.h" |
+#include "base/message_loop/message_loop.h" |
#include "base/metrics/field_trial.h" |
#include "base/test/mock_entropy_provider.h" |
+#include "base/test/test_mock_time_task_runner.h" |
+#include "base/time/time.h" |
#include "build/build_config.h" |
#include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
#include "chrome/browser/infobars/infobar_service.h" |
@@ -88,6 +91,12 @@ class TestPermissionContext : public PermissionContextBase { |
variations::testing::ClearAllVariationParams(); |
} |
+ ContentSetting GetContentSettingFromMap(GURL url_a, GURL url_b) { |
+ return HostContentSettingsMapFactory::GetForProfile(profile()) |
+ ->GetContentSetting(url_a.GetOrigin(), url_b.GetOrigin(), |
+ content_settings_type(), std::string()); |
+ } |
+ |
protected: |
void UpdateTabContext(const PermissionRequestID& id, |
const GURL& requesting_origin, |
@@ -149,14 +158,8 @@ class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness { |
EXPECT_TRUE(permission_context.permission_set()); |
EXPECT_TRUE(permission_context.permission_granted()); |
EXPECT_TRUE(permission_context.tab_context_updated()); |
- |
- ContentSetting setting = |
- HostContentSettingsMapFactory::GetForProfile(profile()) |
- ->GetContentSetting(url.GetOrigin(), |
- url.GetOrigin(), |
- CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
- std::string()); |
- EXPECT_EQ(CONTENT_SETTING_ALLOW, setting); |
+ EXPECT_EQ(CONTENT_SETTING_ALLOW, |
+ permission_context.GetContentSettingFromMap(url, url)); |
} |
void TestAskAndDismiss_TestContent() { |
@@ -180,14 +183,8 @@ class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness { |
EXPECT_TRUE(permission_context.permission_set()); |
EXPECT_FALSE(permission_context.permission_granted()); |
EXPECT_TRUE(permission_context.tab_context_updated()); |
- |
- ContentSetting setting = |
- HostContentSettingsMapFactory::GetForProfile(profile()) |
- ->GetContentSetting(url.GetOrigin(), |
- url.GetOrigin(), |
- CONTENT_SETTINGS_TYPE_MIDI_SYSEX, |
- std::string()); |
- EXPECT_EQ(CONTENT_SETTING_ASK, setting); |
+ EXPECT_EQ(CONTENT_SETTING_ASK, |
+ permission_context.GetContentSettingFromMap(url, url)); |
} |
void TestRequestPermissionInvalidUrl( |
@@ -212,12 +209,8 @@ class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness { |
EXPECT_TRUE(permission_context.permission_set()); |
EXPECT_FALSE(permission_context.permission_granted()); |
EXPECT_TRUE(permission_context.tab_context_updated()); |
- |
- ContentSetting setting = |
- HostContentSettingsMapFactory::GetForProfile(profile()) |
- ->GetContentSetting(url.GetOrigin(), url.GetOrigin(), |
- content_settings_type, std::string()); |
- EXPECT_EQ(CONTENT_SETTING_ASK, setting); |
+ EXPECT_EQ(CONTENT_SETTING_ASK, |
+ permission_context.GetContentSettingFromMap(url, url)); |
} |
void TestGrantAndRevoke_TestContent(content::PermissionType permission_type, |
@@ -242,19 +235,13 @@ class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness { |
EXPECT_TRUE(permission_context.permission_set()); |
EXPECT_TRUE(permission_context.permission_granted()); |
EXPECT_TRUE(permission_context.tab_context_updated()); |
- |
- ContentSetting setting = |
- HostContentSettingsMapFactory::GetForProfile(profile()) |
- ->GetContentSetting(url.GetOrigin(), url.GetOrigin(), |
- content_settings_type, std::string()); |
- EXPECT_EQ(CONTENT_SETTING_ALLOW, setting); |
+ EXPECT_EQ(CONTENT_SETTING_ALLOW, |
+ permission_context.GetContentSettingFromMap(url, url)); |
// Try to reset permission. |
permission_context.ResetPermission(url.GetOrigin(), url.GetOrigin()); |
ContentSetting setting_after_reset = |
- HostContentSettingsMapFactory::GetForProfile(profile()) |
- ->GetContentSetting(url.GetOrigin(), url.GetOrigin(), |
- content_settings_type, std::string()); |
+ permission_context.GetContentSettingFromMap(url, url); |
ContentSetting default_setting = |
HostContentSettingsMapFactory::GetForProfile(profile()) |
->GetDefaultContentSetting(content_settings_type, nullptr); |
@@ -366,6 +353,81 @@ TEST_F(PermissionContextBaseTests, TestGrantAndRevokeWithBubbles) { |
} |
#endif |
+// Tests content_settings::ContentSettingsInfo::DENY_IN_INCOGNITO_AFTER_DELAY. |
+#if defined(ENABLE_NOTIFICATIONS) |
+TEST_F(PermissionContextBaseTests, TestDenyInIncognitoAfterDelay) { |
+ TestPermissionContext permission_context( |
+ profile()->GetOffTheRecordProfile(), |
+ content::PermissionType::NOTIFICATIONS, |
+ CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
+ GURL url("http://www.google.com"); |
+ content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); |
+ |
+ const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
+ web_contents()->GetMainFrame()->GetRoutingID(), |
+ -1); |
+ |
+ scoped_refptr<base::SingleThreadTaskRunner> old_task_runner( |
+ base::MessageLoop::current()->task_runner()); |
+ scoped_refptr<base::TestMockTimeTaskRunner> task_runner( |
+ new base::TestMockTimeTaskRunner(base::Time::Now(), |
+ base::TimeTicks::Now())); |
+ base::MessageLoop::current()->SetTaskRunner(task_runner); |
+ |
+ permission_context.RequestPermission( |
+ web_contents(), id, url, true /* user_gesture */, |
+ base::Bind(&TestPermissionContext::TrackPermissionDecision, |
+ base::Unretained(&permission_context))); |
+ |
+ // Should be blocked after 1-2 seconds, but the timer is reset whenever the |
+ // tab is not visible, so these 500ms never add up to >= 1 second. |
+ for (int n = 0; n < 10; n++) { |
+ web_contents()->WasShown(); |
+ task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); |
+ web_contents()->WasHidden(); |
+ } |
+ |
+ EXPECT_FALSE(permission_context.permission_set()); |
+ EXPECT_FALSE(permission_context.tab_context_updated()); |
+ EXPECT_EQ(CONTENT_SETTING_ASK, |
+ permission_context.GetContentSettingFromMap(url, url)); |
+ |
+ // Time elapsed whilst hidden is not counted. |
+ // n.b. This line also clears out any old scheduled timer tasks. This is |
+ // important, because otherwise Timer::Reset (triggered by |
+ // VisibilityTimerTabHelper::WasShown) may choose to re-use an existing |
+ // scheduled task, and when it fires Timer::RunScheduledTask will call |
+ // TimeTicks::Now() (which unlike task_runner->NowTicks(), we can't fake), |
+ // and miscalculate the remaining delay at which to fire the timer. |
+ task_runner->FastForwardBy(base::TimeDelta::FromDays(1)); |
+ |
+ EXPECT_FALSE(permission_context.permission_set()); |
+ EXPECT_FALSE(permission_context.tab_context_updated()); |
+ EXPECT_EQ(CONTENT_SETTING_ASK, |
+ permission_context.GetContentSettingFromMap(url, url)); |
+ |
+ // Should be blocked after 1-2 seconds. So 500ms is not enough. |
+ web_contents()->WasShown(); |
+ task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); |
+ |
+ EXPECT_FALSE(permission_context.permission_set()); |
+ EXPECT_FALSE(permission_context.tab_context_updated()); |
+ EXPECT_EQ(CONTENT_SETTING_ASK, |
+ permission_context.GetContentSettingFromMap(url, url)); |
+ |
+ // But 5*500ms > 2 seconds, so it should now be blocked. |
+ for (int n = 0; n < 4; n++) |
+ task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); |
+ |
+ EXPECT_TRUE(permission_context.permission_set()); |
+ EXPECT_TRUE(permission_context.tab_context_updated()); |
+ EXPECT_EQ(CONTENT_SETTING_BLOCK, |
+ permission_context.GetContentSettingFromMap(url, url)); |
+ |
+ base::MessageLoop::current()->SetTaskRunner(old_task_runner); |
+} |
+#endif |
+ |
// Tests the global kill switch by enabling/disabling the Field Trials. |
TEST_F(PermissionContextBaseTests, TestGlobalKillSwitch) { |
TestGlobalPermissionsKillSwitch(content::PermissionType::GEOLOCATION, |