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.h"
11 #include "chrome/browser/infobars/infobar_service.h"
12 #include "chrome/common/content_settings_types.h"
13 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "content/public/browser/notification_details.h"
16 #include "content/public/browser/notification_service.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/test/mock_render_process_host.h"
19 #include "content/public/test/test_browser_thread.h"
20 #include "testing/gtest/include/gtest/gtest.h"
23 // PermissionQueueControllerTests ---------------------------------------------
25 class PermissionQueueControllerTests : public ChromeRenderViewHostTestHarness,
26 public content::NotificationObserver {
28 PermissionQueueControllerTests() {
30 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
31 content::NotificationService::AllSources());
33 virtual ~PermissionQueueControllerTests() {}
35 PermissionRequestID RequestID(int bridge_id) {
36 return PermissionRequestID(
37 web_contents()->GetRenderProcessHost()->GetID(),
38 web_contents()->GetRenderViewHost()->GetRoutingID(),
43 // ChromeRenderViewHostTestHarness:
44 virtual void SetUp() OVERRIDE {
45 ChromeRenderViewHostTestHarness::SetUp();
46 InfoBarService::CreateForWebContents(web_contents());
49 // content::NotificationObserver:
50 virtual void Observe(int type,
51 const content::NotificationSource& source,
52 const content::NotificationDetails& details) OVERRIDE {
53 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type);
54 // Delete the removed infobar. In normal Chrome code, this would be handled
55 // by the InfoBarContainer. It's safe to do this even if the queue
56 // controller Observe() function has not yet been called; see comments in
57 // PermissionQueueController::Observe().
58 // TODO(pkasting): This will no longer be necessary once the InfoBarService
59 // truly owns infobars.
60 delete content::Details<InfoBarRemovedDetails>(details)->first;
63 content::NotificationRegistrar registrar_;
65 DISALLOW_COPY_AND_ASSIGN(PermissionQueueControllerTests);
69 // ObservationCountingQueueController -----------------------------------------
71 class ObservationCountingQueueController : public PermissionQueueController {
73 explicit ObservationCountingQueueController(Profile* profile);
74 virtual ~ObservationCountingQueueController();
76 int call_count() const { return call_count_; }
81 // PermissionQueueController:
82 virtual void Observe(int type,
83 const content::NotificationSource& source,
84 const content::NotificationDetails& details) OVERRIDE;
86 DISALLOW_COPY_AND_ASSIGN(ObservationCountingQueueController);
89 ObservationCountingQueueController::ObservationCountingQueueController(
91 : PermissionQueueController(profile, CONTENT_SETTINGS_TYPE_GEOLOCATION),
95 ObservationCountingQueueController::~ObservationCountingQueueController() {
98 void ObservationCountingQueueController::Observe(
100 const content::NotificationSource& source,
101 const content::NotificationDetails& details) {
102 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type);
104 PermissionQueueController::Observe(type, source, details);
108 // Actual tests ---------------------------------------------------------------
110 TEST_F(PermissionQueueControllerTests, OneObservationPerInfoBarCancelled) {
111 // When an infobar is cancelled, the infobar helper sends a notification to
112 // the controller. If the controller has another infobar queued, it should
113 // maintain its registration for notifications with the helper, but on the
114 // last infobar cancellation it should unregister for notifications.
116 // What we don't want is for the controller to unregister and then re-register
117 // for notifications, which can lead to getting notified multiple times. This
118 // test checks that in the case where the controller should remain registered
119 // for notifications, it gets notified exactly once."
120 ObservationCountingQueueController queue_controller(profile());
121 GURL url("http://www.example.com/geolocation");
122 base::Callback<void(bool)> callback;
123 queue_controller.CreateInfoBarRequest(RequestID(0), url, url, callback);
124 queue_controller.CreateInfoBarRequest(RequestID(1), url, url, callback);
125 queue_controller.CancelInfoBarRequest(RequestID(0));
126 EXPECT_EQ(1, queue_controller.call_count());