| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/notifications/notification_permission_context.h" | 5 #include "chrome/browser/notifications/notification_permission_context.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/test/test_mock_time_task_runner.h" | 9 #include "base/test/test_mock_time_task_runner.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 ASSERT_FALSE(permission_context.last_permission_set_persisted()); | 159 ASSERT_FALSE(permission_context.last_permission_set_persisted()); |
| 160 ASSERT_EQ(CONTENT_SETTING_DEFAULT, | 160 ASSERT_EQ(CONTENT_SETTING_DEFAULT, |
| 161 permission_context.last_permission_set_setting()); | 161 permission_context.last_permission_set_setting()); |
| 162 | 162 |
| 163 permission_context.RequestPermission( | 163 permission_context.RequestPermission( |
| 164 web_contents(), id, url, base::Bind(&DoNothing)); | 164 web_contents(), id, url, base::Bind(&DoNothing)); |
| 165 | 165 |
| 166 // Should be blocked after 1-2 seconds, but the timer is reset whenever the | 166 // Should be blocked after 1-2 seconds, but the timer is reset whenever the |
| 167 // tab is not visible, so these 500ms never add up to >= 1 second. | 167 // tab is not visible, so these 500ms never add up to >= 1 second. |
| 168 for (int n = 0; n < 10; n++) { | 168 for (int n = 0; n < 10; n++) { |
| 169 web_contents()->WasShown(); | 169 SimulateShow(); |
| 170 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); | 170 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); |
| 171 web_contents()->WasHidden(); | 171 SimulateHide(); |
| 172 } | 172 } |
| 173 | 173 |
| 174 EXPECT_EQ(0, permission_context.permission_set_count()); | 174 EXPECT_EQ(0, permission_context.permission_set_count()); |
| 175 EXPECT_EQ(CONTENT_SETTING_ASK, | 175 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 176 permission_context.GetContentSettingFromMap(url, url)); | 176 permission_context.GetContentSettingFromMap(url, url)); |
| 177 | 177 |
| 178 // Time elapsed whilst hidden is not counted. | 178 // Time elapsed whilst hidden is not counted. |
| 179 // n.b. This line also clears out any old scheduled timer tasks. This is | 179 // n.b. This line also clears out any old scheduled timer tasks. This is |
| 180 // important, because otherwise Timer::Reset (triggered by | 180 // important, because otherwise Timer::Reset (triggered by |
| 181 // VisibilityTimerTabHelper::WasShown) may choose to re-use an existing | 181 // VisibilityTimerTabHelper::WasShown) may choose to re-use an existing |
| 182 // scheduled task, and when it fires Timer::RunScheduledTask will call | 182 // scheduled task, and when it fires Timer::RunScheduledTask will call |
| 183 // TimeTicks::Now() (which unlike task_runner->NowTicks(), we can't fake), | 183 // TimeTicks::Now() (which unlike task_runner->NowTicks(), we can't fake), |
| 184 // and miscalculate the remaining delay at which to fire the timer. | 184 // and miscalculate the remaining delay at which to fire the timer. |
| 185 task_runner->FastForwardBy(base::TimeDelta::FromDays(1)); | 185 task_runner->FastForwardBy(base::TimeDelta::FromDays(1)); |
| 186 | 186 |
| 187 EXPECT_EQ(0, permission_context.permission_set_count()); | 187 EXPECT_EQ(0, permission_context.permission_set_count()); |
| 188 EXPECT_EQ(CONTENT_SETTING_ASK, | 188 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 189 permission_context.GetContentSettingFromMap(url, url)); | 189 permission_context.GetContentSettingFromMap(url, url)); |
| 190 | 190 |
| 191 // Should be blocked after 1-2 seconds. So 500ms is not enough. | 191 // Should be blocked after 1-2 seconds. So 500ms is not enough. |
| 192 web_contents()->WasShown(); | 192 SimulateShow(); |
| 193 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); | 193 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); |
| 194 | 194 |
| 195 EXPECT_EQ(0, permission_context.permission_set_count()); | 195 EXPECT_EQ(0, permission_context.permission_set_count()); |
| 196 EXPECT_EQ(CONTENT_SETTING_ASK, | 196 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 197 permission_context.GetContentSettingFromMap(url, url)); | 197 permission_context.GetContentSettingFromMap(url, url)); |
| 198 | 198 |
| 199 // But 5*500ms > 2 seconds, so it should now be blocked. | 199 // But 5*500ms > 2 seconds, so it should now be blocked. |
| 200 for (int n = 0; n < 4; n++) | 200 for (int n = 0; n < 4; n++) |
| 201 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); | 201 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); |
| 202 | 202 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 EXPECT_EQ(CONTENT_SETTING_ASK, | 240 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 241 permission_context.GetContentSettingFromMap(url, url)); | 241 permission_context.GetContentSettingFromMap(url, url)); |
| 242 } | 242 } |
| 243 | 243 |
| 244 // Tests how multiple parallel permission requests get auto-denied in incognito. | 244 // Tests how multiple parallel permission requests get auto-denied in incognito. |
| 245 TEST_F(NotificationPermissionContextTest, TestParallelDenyInIncognito) { | 245 TEST_F(NotificationPermissionContextTest, TestParallelDenyInIncognito) { |
| 246 TestNotificationPermissionContext permission_context( | 246 TestNotificationPermissionContext permission_context( |
| 247 profile()->GetOffTheRecordProfile()); | 247 profile()->GetOffTheRecordProfile()); |
| 248 GURL url("https://www.example.com"); | 248 GURL url("https://www.example.com"); |
| 249 NavigateAndCommit(url); | 249 NavigateAndCommit(url); |
| 250 web_contents()->WasShown(); | 250 SimulateShow(); |
| 251 | 251 |
| 252 const PermissionRequestID id0(web_contents()->GetRenderProcessHost()->GetID(), | 252 const PermissionRequestID id0(web_contents()->GetRenderProcessHost()->GetID(), |
| 253 web_contents()->GetMainFrame()->GetRoutingID(), | 253 web_contents()->GetMainFrame()->GetRoutingID(), |
| 254 0); | 254 0); |
| 255 const PermissionRequestID id1(web_contents()->GetRenderProcessHost()->GetID(), | 255 const PermissionRequestID id1(web_contents()->GetRenderProcessHost()->GetID(), |
| 256 web_contents()->GetMainFrame()->GetRoutingID(), | 256 web_contents()->GetMainFrame()->GetRoutingID(), |
| 257 1); | 257 1); |
| 258 | 258 |
| 259 scoped_refptr<base::TestMockTimeTaskRunner> task_runner(SwitchToMockTime()); | 259 scoped_refptr<base::TestMockTimeTaskRunner> task_runner(SwitchToMockTime()); |
| 260 | 260 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 291 // After another 2.5 seconds, the second permission request should also have | 291 // After another 2.5 seconds, the second permission request should also have |
| 292 // received a response. | 292 // received a response. |
| 293 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(2500)); | 293 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(2500)); |
| 294 EXPECT_EQ(2, permission_context.permission_set_count()); | 294 EXPECT_EQ(2, permission_context.permission_set_count()); |
| 295 EXPECT_TRUE(permission_context.last_permission_set_persisted()); | 295 EXPECT_TRUE(permission_context.last_permission_set_persisted()); |
| 296 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 296 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 297 permission_context.last_permission_set_setting()); | 297 permission_context.last_permission_set_setting()); |
| 298 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 298 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 299 permission_context.GetContentSettingFromMap(url, url)); | 299 permission_context.GetContentSettingFromMap(url, url)); |
| 300 } | 300 } |
| OLD | NEW |