1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/permissions/permission_queue_controller.h"
7 #include "base/synchronization/waitable_event.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/infobars/infobar_service.h"
10 #include "chrome/browser/permissions/permission_request_id.h"
11 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "components/content_settings/core/common/content_settings_types.h"
14 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/test/mock_render_process_host.h"
17 #include "testing/gtest/include/gtest/gtest.h"
20 // PermissionQueueControllerTests ---------------------------------------------
22 class PermissionQueueControllerTests
: public ChromeRenderViewHostTestHarness
{
24 PermissionQueueControllerTests() {}
25 ~PermissionQueueControllerTests() override
{}
27 PermissionRequestID
RequestID(int bridge_id
) {
28 return PermissionRequestID(
29 web_contents()->GetRenderProcessHost()->GetID(),
30 web_contents()->GetMainFrame()->GetRoutingID(),
36 // ChromeRenderViewHostTestHarness:
37 void SetUp() override
{
38 ChromeRenderViewHostTestHarness::SetUp();
39 InfoBarService::CreateForWebContents(web_contents());
42 DISALLOW_COPY_AND_ASSIGN(PermissionQueueControllerTests
);
46 // ObservationCountingQueueController -----------------------------------------
48 class ObservationCountingQueueController
: public PermissionQueueController
{
50 explicit ObservationCountingQueueController(Profile
* profile
);
51 ~ObservationCountingQueueController() override
;
53 int call_count() const { return call_count_
; }
58 // PermissionQueueController:
59 void Observe(int type
,
60 const content::NotificationSource
& source
,
61 const content::NotificationDetails
& details
) override
;
63 DISALLOW_COPY_AND_ASSIGN(ObservationCountingQueueController
);
66 ObservationCountingQueueController::ObservationCountingQueueController(
68 : PermissionQueueController(profile
, CONTENT_SETTINGS_TYPE_GEOLOCATION
),
72 ObservationCountingQueueController::~ObservationCountingQueueController() {
75 void ObservationCountingQueueController::Observe(
77 const content::NotificationSource
& source
,
78 const content::NotificationDetails
& details
) {
79 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED
, type
);
81 PermissionQueueController::Observe(type
, source
, details
);
85 // Actual tests ---------------------------------------------------------------
87 TEST_F(PermissionQueueControllerTests
, OneObservationPerInfoBarCancelled
) {
88 // When an infobar is cancelled, the infobar helper sends a notification to
89 // the controller. If the controller has another infobar queued, it should
90 // maintain its registration for notifications with the helper, but on the
91 // last infobar cancellation it should unregister for notifications.
93 // What we don't want is for the controller to unregister and then re-register
94 // for notifications, which can lead to getting notified multiple times. This
95 // test checks that in the case where the controller should remain registered
96 // for notifications, it gets notified exactly once."
97 ObservationCountingQueueController
queue_controller(profile());
98 GURL
url("http://www.example.com/geolocation");
99 base::Callback
<void(ContentSetting
)> callback
;
100 queue_controller
.CreateInfoBarRequest(
101 RequestID(0), url
, url
, callback
);
102 queue_controller
.CreateInfoBarRequest(
103 RequestID(1), url
, url
, callback
);
104 queue_controller
.CancelInfoBarRequest(RequestID(0));
105 EXPECT_EQ(1, queue_controller
.call_count());
108 TEST_F(PermissionQueueControllerTests
, FailOnBadPattern
) {
109 ObservationCountingQueueController
queue_controller(profile());
110 GURL
url("chrome://settings");
111 base::Callback
<void(ContentSetting
)> callback
;
112 queue_controller
.CreateInfoBarRequest(
113 RequestID(0), url
, url
, callback
);
114 queue_controller
.CancelInfoBarRequest(RequestID(0));
115 EXPECT_EQ(0, queue_controller
.call_count());