| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 #import "ios/chrome/browser/web/blocked_popup_tab_helper.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "components/infobars/core/infobar.h" |
| 9 #include "components/infobars/core/infobar_manager.h" |
| 10 #include "ios/chrome/browser/infobars/infobar_manager_impl.h" |
| 11 #import "ios/web/public/test/fakes/test_navigation_manager.h" |
| 12 #import "ios/web/public/test/fakes/test_web_state.h" |
| 13 #include "ios/web/web_state/blocked_popup_info.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "testing/platform_test.h" |
| 16 #include "url/gurl.h" |
| 17 |
| 18 // Test fixture for BlockedPopupTabHelper class. |
| 19 class BlockedPopupTabHelperTest : public PlatformTest { |
| 20 protected: |
| 21 BlockedPopupTabHelperTest() : web_state_(new web::TestWebState()) { |
| 22 web_state_->SetNavigationManager( |
| 23 base::MakeUnique<web::TestNavigationManager>()); |
| 24 |
| 25 BlockedPopupTabHelper::CreateForWebState(web_state_.get()); |
| 26 InfoBarManagerImpl::CreateForWebState(web_state_.get()); |
| 27 } |
| 28 |
| 29 protected: |
| 30 // Expose ivars for testing. |
| 31 bool IsObservingSources() { |
| 32 BlockedPopupTabHelper* helper = |
| 33 BlockedPopupTabHelper::FromWebState(web_state_.get()); |
| 34 return helper->scoped_observer_.IsObservingSources(); |
| 35 } |
| 36 |
| 37 std::unique_ptr<web::TestWebState> web_state_; |
| 38 |
| 39 private: |
| 40 DISALLOW_COPY_AND_ASSIGN(BlockedPopupTabHelperTest); |
| 41 }; |
| 42 |
| 43 // Tests that an infobar is added to the infobar manager when |
| 44 // BlockedPopupTabHelper::HandlePopup() is called. |
| 45 TEST_F(BlockedPopupTabHelperTest, ShowAndDismissInfoBar) { |
| 46 const GURL test_url("https://popups.example.com"); |
| 47 web::BlockedPopupInfo popup_info(test_url, web::Referrer(), nil, nil); |
| 48 |
| 49 // Check that there are no infobars showing and no registered observers. |
| 50 infobars::InfoBarManager* infobar_manager = |
| 51 InfoBarManagerImpl::FromWebState(web_state_.get()); |
| 52 EXPECT_EQ(0U, infobar_manager->infobar_count()); |
| 53 EXPECT_FALSE(IsObservingSources()); |
| 54 |
| 55 // Call |HandlePopup| to show an infobar. |
| 56 BlockedPopupTabHelper* tab_helper = |
| 57 BlockedPopupTabHelper::FromWebState(web_state_.get()); |
| 58 tab_helper->HandlePopup(popup_info); |
| 59 ASSERT_EQ(1U, infobar_manager->infobar_count()); |
| 60 EXPECT_TRUE(IsObservingSources()); |
| 61 |
| 62 // Dismiss the infobar and check that the tab helper no longer has any |
| 63 // registered observers. |
| 64 infobar_manager->infobar_at(0)->RemoveSelf(); |
| 65 EXPECT_EQ(0U, infobar_manager->infobar_count()); |
| 66 EXPECT_FALSE(IsObservingSources()); |
| 67 } |
| OLD | NEW |