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/content_settings/permission_queue_controller.h"
7 #include "base/synchronization/waitable_event.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/content_settings/permission_request_id.h"
10 #include "chrome/browser/infobars/infobar_service.h"
11 #include "chrome/common/content_settings_types.h"
12 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/test/mock_render_process_host.h"
16 #include "content/public/test/test_browser_thread.h"
17 #include "testing/gtest/include/gtest/gtest.h"
20 // PermissionQueueControllerTests ---------------------------------------------
22 class PermissionQueueControllerTests
: public ChromeRenderViewHostTestHarness
{
24 PermissionQueueControllerTests() {}
25 virtual ~PermissionQueueControllerTests() {}
27 PermissionRequestID
RequestID(int bridge_id
) {
28 return PermissionRequestID(
29 web_contents()->GetRenderProcessHost()->GetID(),
30 web_contents()->GetRenderViewHost()->GetRoutingID(),
36 // ChromeRenderViewHostTestHarness:
37 virtual 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 virtual ~ObservationCountingQueueController();
53 int call_count() const { return call_count_
; }
58 // PermissionQueueController:
59 virtual 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(bool)> callback
;
100 queue_controller
.CreateInfoBarRequest(RequestID(0), url
, url
, callback
);
101 queue_controller
.CreateInfoBarRequest(RequestID(1), url
, url
, callback
);
102 queue_controller
.CancelInfoBarRequest(RequestID(0));
103 EXPECT_EQ(1, queue_controller
.call_count());