| 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/infobars/infobar_tab_helper.h" | 7 #include "chrome/browser/infobars/infobar_tab_helper.h" |
| 8 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 8 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 9 #include "chrome/test/base/testing_profile.h" | 9 #include "chrome/test/base/testing_profile.h" |
| 10 #include "content/public/browser/geolocation.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 content::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 content::GeolocationPermissionRequestID |
| 38 return web_contents()->GetRenderProcessHost()->GetID(); | 38 GeolocationInfoBarQueueControllerTests::RequestID(int bridge_id) { |
| 39 } | 39 return content::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( |
| 66 int render_view_id, | 66 const content::GeolocationPermissionRequestID& id, |
| 67 int bridge_id, | 67 const GURL& requesting_frame, |
| 68 const GURL& requesting_frame, | 68 base::Callback<void(bool)> callback, |
| 69 base::Callback<void(bool)> callback, | 69 bool allowed); |
| 70 bool allowed); | |
| 71 }; | 70 }; |
| 72 | 71 |
| 73 ObservationCountingQueueController::ObservationCountingQueueController( | 72 ObservationCountingQueueController::ObservationCountingQueueController( |
| 74 Profile* profile) | 73 Profile* profile) |
| 75 : GeolocationInfoBarQueueController( | 74 : GeolocationInfoBarQueueController( |
| 76 profile), | 75 profile), |
| 77 call_count_(0) { | 76 call_count_(0) { |
| 78 } | 77 } |
| 79 | 78 |
| 80 void ObservationCountingQueueController::Observe( | 79 void ObservationCountingQueueController::Observe( |
| 81 int type, | 80 int type, |
| 82 const content::NotificationSource& source, | 81 const content::NotificationSource& source, |
| 83 const content::NotificationDetails& details) { | 82 const content::NotificationDetails& details) { |
| 84 ++call_count_; | 83 ++call_count_; |
| 85 GeolocationInfoBarQueueController::Observe(type, source, details); | 84 GeolocationInfoBarQueueController::Observe(type, source, details); |
| 86 } | 85 } |
| 87 | 86 |
| 88 void ObservationCountingQueueController::NotifyPermissionSet( | 87 void ObservationCountingQueueController::NotifyPermissionSet( |
| 89 int render_process_id, | 88 const content::GeolocationPermissionRequestID& id, |
| 90 int render_view_id, | |
| 91 int bridge_id, | |
| 92 const GURL& requesting_frame, | 89 const GURL& requesting_frame, |
| 93 base::Callback<void(bool)> callback, | 90 base::Callback<void(bool)> callback, |
| 94 bool allowed) { | 91 bool allowed) { |
| 95 } | 92 } |
| 96 | 93 |
| 97 | 94 |
| 98 TEST_F(GeolocationInfoBarQueueControllerTests, | 95 TEST_F(GeolocationInfoBarQueueControllerTests, |
| 99 OneObservationPerInfoBarCancelled) { | 96 OneObservationPerInfoBarCancelled) { |
| 100 // When an infobar is cancelled, the infobar helper sends a notification to | 97 // When an infobar is cancelled, the infobar helper sends a notification to |
| 101 // the controller. If the controller has another infobar queued, it should | 98 // the controller. If the controller has another infobar queued, it should |
| 102 // maintain its registration for notifications with the helper, but on the | 99 // maintain its registration for notifications with the helper, but on the |
| 103 // last infobar cancellation it should unregister for notifications. | 100 // last infobar cancellation it should unregister for notifications. |
| 104 // | 101 // |
| 105 // What we don't want is for the controller to unregister and then re-register | 102 // 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 | 103 // for notifications, which can lead to getting notified multiple times. This |
| 107 // test checks that in the case where the controller should remain registered | 104 // test checks that in the case where the controller should remain registered |
| 108 // for notifications, it gets notified exactly once." | 105 // for notifications, it gets notified exactly once." |
| 109 | 106 |
| 110 ObservationCountingQueueController infobar_queue_controller(profile()); | 107 ObservationCountingQueueController infobar_queue_controller(profile()); |
| 111 GURL url("http://www.example.com/geolocation"); | 108 GURL url("http://www.example.com/geolocation"); |
| 112 base::Callback<void(bool)> callback; | 109 base::Callback<void(bool)> callback; |
| 113 infobar_queue_controller.CreateInfoBarRequest(ProcessId(), RenderId(), 1, | 110 infobar_queue_controller.CreateInfoBarRequest(RequestID(0), url, url, |
| 114 url, url, callback); | 111 callback); |
| 115 infobar_queue_controller.CreateInfoBarRequest(ProcessId(), RenderId(), 2, | 112 infobar_queue_controller.CreateInfoBarRequest(RequestID(1), url, url, |
| 116 url, url, callback); | 113 callback); |
| 117 infobar_queue_controller.CancelInfoBarRequest(ProcessId(), RenderId(), 1); | 114 infobar_queue_controller.CancelInfoBarRequest(RequestID(0)); |
| 118 EXPECT_EQ(1, infobar_queue_controller.call_count()); | 115 EXPECT_EQ(1, infobar_queue_controller.call_count()); |
| 119 }; | 116 }; |
| OLD | NEW |