| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/synchronization/waitable_event.h" | 5 #include "base/synchronization/waitable_event.h" |
| 6 #include "chrome/browser/geolocation/geolocation_infobar_queue_controller.h" | 6 #include "chrome/browser/geolocation/geolocation_infobar_queue_controller.h" |
| 7 #include "chrome/browser/geolocation/geolocation_permission_request_id.h" |
| 7 #include "chrome/browser/infobars/infobar_tab_helper.h" | 8 #include "chrome/browser/infobars/infobar_tab_helper.h" |
| 8 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 9 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 9 #include "chrome/test/base/testing_profile.h" | 10 #include "chrome/test/base/testing_profile.h" |
| 10 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
| 11 #include "content/public/test/mock_render_process_host.h" | 12 #include "content/public/test/mock_render_process_host.h" |
| 12 #include "content/public/test/test_browser_thread.h" | 13 #include "content/public/test/test_browser_thread.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 class GeolocationInfoBarQueueControllerTests | 16 class GeolocationInfoBarQueueControllerTests |
| 16 : public ChromeRenderViewHostTestHarness { | 17 : public ChromeRenderViewHostTestHarness { |
| 17 public: | 18 public: |
| 18 GeolocationInfoBarQueueControllerTests(); | 19 GeolocationInfoBarQueueControllerTests(); |
| 19 | 20 |
| 20 protected: | 21 protected: |
| 21 int ProcessId(); | 22 GeolocationPermissionRequestID RequestID(int bridge_id); |
| 22 int RenderId(); | |
| 23 | 23 |
| 24 private: | 24 private: |
| 25 // ChromeRenderViewHostTestHarness: | 25 // ChromeRenderViewHostTestHarness: |
| 26 virtual void SetUp() OVERRIDE; | 26 virtual void SetUp() OVERRIDE; |
| 27 | 27 |
| 28 content::TestBrowserThread ui_thread_; | 28 content::TestBrowserThread ui_thread_; |
| 29 | 29 |
| 30 DISALLOW_COPY_AND_ASSIGN(GeolocationInfoBarQueueControllerTests); | 30 DISALLOW_COPY_AND_ASSIGN(GeolocationInfoBarQueueControllerTests); |
| 31 }; | 31 }; |
| 32 | 32 |
| 33 GeolocationInfoBarQueueControllerTests::GeolocationInfoBarQueueControllerTests() | 33 GeolocationInfoBarQueueControllerTests::GeolocationInfoBarQueueControllerTests() |
| 34 : ui_thread_(content::BrowserThread::UI, MessageLoop::current()) { | 34 : ui_thread_(content::BrowserThread::UI, MessageLoop::current()) { |
| 35 } | 35 } |
| 36 | 36 |
| 37 int GeolocationInfoBarQueueControllerTests::ProcessId() { | 37 GeolocationPermissionRequestID |
| 38 return web_contents()->GetRenderProcessHost()->GetID(); | 38 GeolocationInfoBarQueueControllerTests::RequestID(int bridge_id) { |
| 39 } | 39 return GeolocationPermissionRequestID( |
| 40 | 40 web_contents()->GetRenderProcessHost()->GetID(), |
| 41 int GeolocationInfoBarQueueControllerTests::RenderId() { | 41 web_contents()->GetRenderViewHost()->GetRoutingID(), |
| 42 return web_contents()->GetRenderViewHost()->GetRoutingID(); | 42 bridge_id); |
| 43 } | 43 } |
| 44 | 44 |
| 45 void GeolocationInfoBarQueueControllerTests::SetUp() { | 45 void GeolocationInfoBarQueueControllerTests::SetUp() { |
| 46 ChromeRenderViewHostTestHarness::SetUp(); | 46 ChromeRenderViewHostTestHarness::SetUp(); |
| 47 InfoBarTabHelper::CreateForWebContents(web_contents()); | 47 InfoBarTabHelper::CreateForWebContents(web_contents()); |
| 48 } | 48 } |
| 49 | 49 |
| 50 class ObservationCountingQueueController : | 50 class ObservationCountingQueueController : |
| 51 public GeolocationInfoBarQueueController { | 51 public GeolocationInfoBarQueueController { |
| 52 public: | 52 public: |
| 53 explicit ObservationCountingQueueController(Profile* profile); | 53 explicit ObservationCountingQueueController(Profile* profile); |
| 54 | 54 |
| 55 int call_count() const { return call_count_; } | 55 int call_count() const { return call_count_; } |
| 56 | 56 |
| 57 private: | 57 private: |
| 58 int call_count_; | 58 int call_count_; |
| 59 | 59 |
| 60 // GeolocationInfoBarQueueController | 60 // GeolocationInfoBarQueueController |
| 61 virtual void Observe(int type, | 61 virtual void Observe(int type, |
| 62 const content::NotificationSource& source, | 62 const content::NotificationSource& source, |
| 63 const content::NotificationDetails& details); | 63 const content::NotificationDetails& details); |
| 64 | 64 |
| 65 static void NotifyPermissionSet(int render_process_id, | 65 static void NotifyPermissionSet(const GeolocationPermissionRequestID& id, |
| 66 int render_view_id, | |
| 67 int bridge_id, | |
| 68 const GURL& requesting_frame, | 66 const GURL& requesting_frame, |
| 69 base::Callback<void(bool)> callback, | 67 base::Callback<void(bool)> callback, |
| 70 bool allowed); | 68 bool allowed); |
| 71 }; | 69 }; |
| 72 | 70 |
| 73 ObservationCountingQueueController::ObservationCountingQueueController( | 71 ObservationCountingQueueController::ObservationCountingQueueController( |
| 74 Profile* profile) | 72 Profile* profile) |
| 75 : GeolocationInfoBarQueueController( | 73 : GeolocationInfoBarQueueController( |
| 76 profile), | 74 profile), |
| 77 call_count_(0) { | 75 call_count_(0) { |
| 78 } | 76 } |
| 79 | 77 |
| 80 void ObservationCountingQueueController::Observe( | 78 void ObservationCountingQueueController::Observe( |
| 81 int type, | 79 int type, |
| 82 const content::NotificationSource& source, | 80 const content::NotificationSource& source, |
| 83 const content::NotificationDetails& details) { | 81 const content::NotificationDetails& details) { |
| 84 ++call_count_; | 82 ++call_count_; |
| 85 GeolocationInfoBarQueueController::Observe(type, source, details); | 83 GeolocationInfoBarQueueController::Observe(type, source, details); |
| 86 } | 84 } |
| 87 | 85 |
| 88 void ObservationCountingQueueController::NotifyPermissionSet( | 86 void ObservationCountingQueueController::NotifyPermissionSet( |
| 89 int render_process_id, | 87 const GeolocationPermissionRequestID& id, |
| 90 int render_view_id, | |
| 91 int bridge_id, | |
| 92 const GURL& requesting_frame, | 88 const GURL& requesting_frame, |
| 93 base::Callback<void(bool)> callback, | 89 base::Callback<void(bool)> callback, |
| 94 bool allowed) { | 90 bool allowed) { |
| 95 } | 91 } |
| 96 | 92 |
| 97 | 93 |
| 98 TEST_F(GeolocationInfoBarQueueControllerTests, | 94 TEST_F(GeolocationInfoBarQueueControllerTests, |
| 99 OneObservationPerInfoBarCancelled) { | 95 OneObservationPerInfoBarCancelled) { |
| 100 // When an infobar is cancelled, the infobar helper sends a notification to | 96 // When an infobar is cancelled, the infobar helper sends a notification to |
| 101 // the controller. If the controller has another infobar queued, it should | 97 // the controller. If the controller has another infobar queued, it should |
| 102 // maintain its registration for notifications with the helper, but on the | 98 // maintain its registration for notifications with the helper, but on the |
| 103 // last infobar cancellation it should unregister for notifications. | 99 // last infobar cancellation it should unregister for notifications. |
| 104 // | 100 // |
| 105 // What we don't want is for the controller to unregister and then re-register | 101 // What we don't want is for the controller to unregister and then re-register |
| 106 // for notifications, which can lead to getting notified multiple times. This | 102 // for notifications, which can lead to getting notified multiple times. This |
| 107 // test checks that in the case where the controller should remain registered | 103 // test checks that in the case where the controller should remain registered |
| 108 // for notifications, it gets notified exactly once." | 104 // for notifications, it gets notified exactly once." |
| 109 | 105 |
| 110 ObservationCountingQueueController infobar_queue_controller(profile()); | 106 ObservationCountingQueueController infobar_queue_controller(profile()); |
| 111 GURL url("http://www.example.com/geolocation"); | 107 GURL url("http://www.example.com/geolocation"); |
| 112 base::Callback<void(bool)> callback; | 108 base::Callback<void(bool)> callback; |
| 113 infobar_queue_controller.CreateInfoBarRequest(ProcessId(), RenderId(), 1, | 109 infobar_queue_controller.CreateInfoBarRequest(RequestID(0), url, url, |
| 114 url, url, callback); | 110 callback); |
| 115 infobar_queue_controller.CreateInfoBarRequest(ProcessId(), RenderId(), 2, | 111 infobar_queue_controller.CreateInfoBarRequest(RequestID(1), url, url, |
| 116 url, url, callback); | 112 callback); |
| 117 infobar_queue_controller.CancelInfoBarRequest(ProcessId(), RenderId(), 1); | 113 infobar_queue_controller.CancelInfoBarRequest(RequestID(0)); |
| 118 EXPECT_EQ(1, infobar_queue_controller.call_count()); | 114 EXPECT_EQ(1, infobar_queue_controller.call_count()); |
| 119 }; | 115 }; |
| OLD | NEW |