| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
| 6 | 6 |
| 7 #include "app/app_paths.h" | 7 #include "app/app_paths.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/scoped_nsautorelease_pool.h" |
| 9 #import "chrome/browser/cocoa/blocked_popup_container_controller.h" | 10 #import "chrome/browser/cocoa/blocked_popup_container_controller.h" |
| 10 #include "chrome/browser/cocoa/browser_test_helper.h" | 11 #include "chrome/browser/cocoa/browser_test_helper.h" |
| 11 #import "chrome/browser/cocoa/cocoa_test_helper.h" | 12 #import "chrome/browser/cocoa/cocoa_test_helper.h" |
| 12 #include "chrome/browser/renderer_host/test_render_view_host.h" | 13 #include "chrome/browser/renderer_host/test_render_view_host.h" |
| 13 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 const std::string host1 = "host1"; | 19 const std::string host1 = "host1"; |
| 19 } // namespace | 20 } // namespace |
| 20 | 21 |
| 21 class BlockedPopupContainerControllerTest : public RenderViewHostTestHarness { | 22 class BlockedPopupContainerControllerTest : public RenderViewHostTestHarness { |
| 22 public: | 23 public: |
| 23 virtual void SetUp() { | 24 virtual void SetUp() { |
| 25 // This is all a bit convoluted because the standard factory Create() call |
| 26 // doesn't give us access to the cocoa controller for testing (since it's |
| 27 // an internal implementation detail). As a result, we need to create one |
| 28 // separately and inject the bridge with a test-only Create() call. |
| 29 // Unfortunate, but no way around it. |
| 24 RenderViewHostTestHarness::SetUp(); | 30 RenderViewHostTestHarness::SetUp(); |
| 25 container_ = BlockedPopupContainer::Create(contents(), profile()); | |
| 26 cocoa_controller_ = [[BlockedPopupContainerController alloc] | 31 cocoa_controller_ = [[BlockedPopupContainerController alloc] |
| 27 initWithContainer:container_]; | 32 initWithContainer:nil]; |
| 28 EXPECT_TRUE([cocoa_controller_ bridge]); | 33 EXPECT_TRUE([cocoa_controller_ bridge]); |
| 29 container_->set_view([cocoa_controller_ bridge]); | 34 container_ = BlockedPopupContainer::Create(contents(), profile(), |
| 35 [cocoa_controller_ bridge]); |
| 36 [cocoa_controller_ setContainer:container_]; |
| 30 contents_->set_blocked_popup_container(container_); | 37 contents_->set_blocked_popup_container(container_); |
| 31 } | 38 } |
| 32 | 39 |
| 33 virtual void TearDown() { | 40 virtual void TearDown() { |
| 34 // This will also signal the Cocoa controller to delete itself with a | 41 // This will also signal the Cocoa controller to delete itself with a |
| 35 // Destroy() mesage to the bridge. It also clears out the association with | 42 // Destroy() mesage to the bridge. It also clears out the association with |
| 36 // |contents_|. | 43 // |contents_|. |
| 37 container_->Destroy(); | 44 container_->Destroy(); |
| 38 RenderViewHostTestHarness::TearDown(); | 45 RenderViewHostTestHarness::TearDown(); |
| 39 } | 46 } |
| 40 | 47 |
| 41 TabContents* BuildTabContents() { | 48 TabContents* BuildTabContents() { |
| 42 // This will be deleted when the TabContents goes away. | 49 // This will be deleted when the TabContents goes away. |
| 43 SiteInstance* instance = SiteInstance::CreateSiteInstance(profile_.get()); | 50 SiteInstance* instance = SiteInstance::CreateSiteInstance(profile_.get()); |
| 44 | 51 |
| 45 // Set up and use TestTabContents here. | 52 // Set up and use TestTabContents here. |
| 46 return new TestTabContents(profile_.get(), instance); | 53 return new TestTabContents(profile_.get(), instance); |
| 47 } | 54 } |
| 48 | 55 |
| 49 GURL GetTestCase(const std::string& file) { | 56 GURL GetTestCase(const std::string& file) { |
| 50 FilePath filename; | 57 FilePath filename; |
| 51 PathService::Get(app::DIR_TEST_DATA, &filename); | 58 PathService::Get(app::DIR_TEST_DATA, &filename); |
| 52 filename = filename.AppendASCII("constrained_files"); | 59 filename = filename.AppendASCII("constrained_files"); |
| 53 filename = filename.AppendASCII(file); | 60 filename = filename.AppendASCII(file); |
| 54 return net::FilePathToFileURL(filename); | 61 return net::FilePathToFileURL(filename); |
| 55 } | 62 } |
| 56 | 63 |
| 64 base::ScopedNSAutoreleasePool pool; |
| 57 BlockedPopupContainer* container_; | 65 BlockedPopupContainer* container_; |
| 58 BlockedPopupContainerController* cocoa_controller_; | 66 BlockedPopupContainerController* cocoa_controller_; |
| 59 }; | 67 }; |
| 60 | 68 |
| 61 TEST_F(BlockedPopupContainerControllerTest, BasicPopupBlock) { | 69 TEST_F(BlockedPopupContainerControllerTest, BasicPopupBlock) { |
| 62 // This is taken from the popup blocker unit test. | 70 // This is taken from the popup blocker unit test. |
| 63 TabContents* popup = BuildTabContents(); | 71 TabContents* popup = BuildTabContents(); |
| 64 popup->controller().LoadURLLazily(GetTestCase("error"), GURL(), | 72 popup->controller().LoadURLLazily(GetTestCase("error"), GURL(), |
| 65 PageTransition::LINK, | 73 PageTransition::LINK, |
| 66 L"", NULL); | 74 L"", NULL); |
| 67 container_->AddTabContents(popup, gfx::Rect(), host1); | 75 container_->AddTabContents(popup, gfx::Rect(), host1); |
| 68 EXPECT_EQ(container_->GetBlockedPopupCount(), static_cast<size_t>(1)); | 76 EXPECT_EQ(container_->GetBlockedPopupCount(), static_cast<size_t>(1)); |
| 69 EXPECT_EQ(container_->GetTabContentsAt(0), popup); | 77 EXPECT_EQ(container_->GetTabContentsAt(0), popup); |
| 70 EXPECT_FALSE(container_->IsHostWhitelisted(0)); | 78 EXPECT_FALSE(container_->IsHostWhitelisted(0)); |
| 71 | 79 |
| 72 // Ensure the view has been displayed. If it has a superview, then ShowView() | 80 // Ensure the view has been displayed. If it has a superview, then ShowView() |
| 73 // has been called on the bridge. If the label has a string, then | 81 // has been called on the bridge. If the button has a string, then |
| 74 // UpdateLabel() has been called. | 82 // UpdateLabel() has been called. |
| 75 EXPECT_TRUE([cocoa_controller_ view]); | 83 EXPECT_TRUE([cocoa_controller_ view]); |
| 76 EXPECT_TRUE([[cocoa_controller_ view] superview]); | 84 EXPECT_TRUE([[cocoa_controller_ view] superview]); |
| 77 EXPECT_TRUE([[(NSTextField*)[cocoa_controller_ label] | 85 EXPECT_TRUE([[[cocoa_controller_ popupButton] title] length] > 0); |
| 78 stringValue] length] > 0); | 86 |
| 87 // Validate the menu. It should have 4 items (the dummy title item, 1 poupup, |
| 88 // a separator, 1 host). |
| 89 NSMenu* menu = [cocoa_controller_ buildMenu]; |
| 90 EXPECT_TRUE(menu); |
| 91 EXPECT_EQ([menu numberOfItems], 4); |
| 92 |
| 93 // Change the whitelisting and make sure the host is checked. |
| 94 container_->ToggleWhitelistingForHost(0); |
| 95 menu = [cocoa_controller_ buildMenu]; |
| 96 EXPECT_TRUE(menu); |
| 97 EXPECT_EQ([menu numberOfItems], 2); |
| 98 EXPECT_EQ([[menu itemAtIndex:1] state], NSOnState); |
| 79 | 99 |
| 80 // Close the popup and verify it's no longer in the view hierarchy. This | 100 // Close the popup and verify it's no longer in the view hierarchy. This |
| 81 // means HideView() has been called. | 101 // means HideView() has been called. |
| 82 [cocoa_controller_ closePopup:nil]; | 102 [cocoa_controller_ closePopup:nil]; |
| 83 EXPECT_FALSE([[cocoa_controller_ view] superview]); | 103 EXPECT_FALSE([[cocoa_controller_ view] superview]); |
| 84 } | 104 } |
| OLD | NEW |