- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / content_settings / permission_queue_controller_unittest.cc
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.
4
5 #include "chrome/browser/content_settings/permission_queue_controller.h"
6
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"
21
22
23 // PermissionQueueControllerTests ---------------------------------------------
24
25 class PermissionQueueControllerTests : public ChromeRenderViewHostTestHarness,
26                                        public content::NotificationObserver {
27  protected:
28   PermissionQueueControllerTests() {
29     registrar_.Add(this,
30                    chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
31                    content::NotificationService::AllSources());
32   }
33   virtual ~PermissionQueueControllerTests() {}
34
35   PermissionRequestID RequestID(int bridge_id) {
36     return PermissionRequestID(
37         web_contents()->GetRenderProcessHost()->GetID(),
38         web_contents()->GetRenderViewHost()->GetRoutingID(),
39         bridge_id);
40   }
41
42  private:
43   // ChromeRenderViewHostTestHarness:
44   virtual void SetUp() OVERRIDE {
45     ChromeRenderViewHostTestHarness::SetUp();
46     InfoBarService::CreateForWebContents(web_contents());
47   }
48
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;
61   }
62
63   content::NotificationRegistrar registrar_;
64
65   DISALLOW_COPY_AND_ASSIGN(PermissionQueueControllerTests);
66 };
67
68
69 // ObservationCountingQueueController -----------------------------------------
70
71 class ObservationCountingQueueController : public PermissionQueueController {
72  public:
73   explicit ObservationCountingQueueController(Profile* profile);
74   virtual ~ObservationCountingQueueController();
75
76   int call_count() const { return call_count_; }
77
78  private:
79   int call_count_;
80
81   // PermissionQueueController:
82   virtual void Observe(int type,
83                        const content::NotificationSource& source,
84                        const content::NotificationDetails& details) OVERRIDE;
85
86   DISALLOW_COPY_AND_ASSIGN(ObservationCountingQueueController);
87 };
88
89 ObservationCountingQueueController::ObservationCountingQueueController(
90     Profile* profile)
91     : PermissionQueueController(profile, CONTENT_SETTINGS_TYPE_GEOLOCATION),
92       call_count_(0) {
93 }
94
95 ObservationCountingQueueController::~ObservationCountingQueueController() {
96 }
97
98 void ObservationCountingQueueController::Observe(
99     int type,
100     const content::NotificationSource& source,
101     const content::NotificationDetails& details) {
102   DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type);
103   ++call_count_;
104   PermissionQueueController::Observe(type, source, details);
105 }
106
107
108 // Actual tests ---------------------------------------------------------------
109
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.
115   //
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());
127 };