diff options
author | erg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-13 00:17:18 +0000 |
---|---|---|
committer | erg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-13 00:17:18 +0000 |
commit | 3f45e747058ff88d56ade868918d99f830d11df9 (patch) | |
tree | 9d27f816d82e7e4210f2a0a6672549c73dd60fdc | |
parent | 6d21eb72d7333889badc108afe21dd1bc52e1135 (diff) | |
download | chromium_src-3f45e747058ff88d56ade868918d99f830d11df9.zip chromium_src-3f45e747058ff88d56ade868918d99f830d11df9.tar.gz chromium_src-3f45e747058ff88d56ade868918d99f830d11df9.tar.bz2 |
Create a basic test case for the common usage of BlockedPopupContainer.
Review URL: http://codereview.chromium.org/125083
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@18336 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/blocked_popup_container.h | 3 | ||||
-rw-r--r-- | chrome/browser/blocked_popup_container_unittest.cc | 102 | ||||
-rw-r--r-- | chrome/browser/tab_contents/tab_contents.h | 9 | ||||
-rw-r--r-- | chrome/chrome.gyp | 3 |
4 files changed, 116 insertions, 1 deletions
diff --git a/chrome/browser/blocked_popup_container.h b/chrome/browser/blocked_popup_container.h index 7833592..c7d94ea 100644 --- a/chrome/browser/blocked_popup_container.h +++ b/chrome/browser/blocked_popup_container.h @@ -207,7 +207,7 @@ class BlockedPopupContainer : public TabContentsDelegate, typedef std::map<std::string, bool> PopupHosts; // Hides the UI portion of the container. - virtual void HideSelf(); + void HideSelf(); // Helper function to convert a host index (which the view uses) into an // iterator into |popup_hosts_|. Returns popup_hosts_.end() if |index| is @@ -224,6 +224,7 @@ class BlockedPopupContainer : public TabContentsDelegate, private: friend class BlockedPopupContainerImpl; + friend class BlockedPopupContainerTest; // string is hostname. typedef std::set<std::string> Whitelist; diff --git a/chrome/browser/blocked_popup_container_unittest.cc b/chrome/browser/blocked_popup_container_unittest.cc new file mode 100644 index 0000000..6d872f3 --- /dev/null +++ b/chrome/browser/blocked_popup_container_unittest.cc @@ -0,0 +1,102 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Tests the cross platform BlockedPopupContainer model/controller object. +// +// TODO(erg): The unit tests on BlockedPopupContainer need to be greatly +// expanded. + +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" + +#include "app/app_paths.h" +#include "base/path_service.h" +#include "chrome/browser/blocked_popup_container.h" +#include "chrome/browser/tab_contents/test_web_contents.h" +#include "chrome/browser/renderer_host/test_render_view_host.h" +#include "chrome/test/testing_profile.h" +#include "net/base/net_util.h" + +namespace { +const std::string host1 = "host1"; +} // namespace + +// Mock for our view. +class MockBlockedPopupContainerView : public BlockedPopupContainerView { + public: + MOCK_METHOD0(SetPosition, void()); + MOCK_METHOD0(ShowView, void()); + MOCK_METHOD0(UpdateLabel, void()); + MOCK_METHOD0(HideView, void()); + MOCK_METHOD0(Destroy, void()); +}; + +class BlockedPopupContainerTest : public RenderViewHostTestHarness { + public: + TabContents* BuildTabContents() { + // This will be deleted when the TabContents goes away. + SiteInstance* instance = SiteInstance::CreateSiteInstance(profile_.get()); + + // Set up and use TestTabContents here. + return new TestTabContents(profile_.get(), instance); + } + + GURL GetTestCase(const std::string& file) { + FilePath filename; + PathService::Get(app::DIR_TEST_DATA, &filename); + filename = filename.AppendASCII("constrained_files"); + filename = filename.AppendASCII(file); + return net::FilePathToFileURL(filename); + } + + protected: + virtual void SetUp() { + RenderViewHostTestHarness::SetUp(); + container_ = new BlockedPopupContainer(contents(), profile()->GetPrefs()); + container_->set_view(&mock); + + contents_->set_blocked_popup_container(container_); + } + + // Our blocked popup container that we are testing. WARNING: If you are + // trying to test destruction issues, make sure to remove |container_| from + // |contents_|. + BlockedPopupContainer* container_; + + // The mock that we're using. + MockBlockedPopupContainerView mock; +}; + +// Destroying the container should tell the View to destroy itself. +TEST_F(BlockedPopupContainerTest, TestDestroy) { + EXPECT_CALL(mock, Destroy()).Times(1); +} + +// Make sure TabContents::RepositionSupressedPopupsToFit() filters to the view. +TEST_F(BlockedPopupContainerTest, TestReposition) { + EXPECT_CALL(mock, SetPosition()).Times(1); + // Always need this to shut gmock up. :-/ + EXPECT_CALL(mock, Destroy()).Times(1); + + contents_->RepositionSupressedPopupsToFit(); +} + +// Test the basic blocked popup case. +TEST_F(BlockedPopupContainerTest, BasicCase) { + EXPECT_CALL(mock, UpdateLabel()).Times(1); + EXPECT_CALL(mock, ShowView()).Times(1); + EXPECT_CALL(mock, Destroy()).Times(1); + + // Create another TabContents representing the blocked popup case. + TabContents* popup = BuildTabContents(); + popup->controller().LoadURLLazily(GetTestCase("error"), GURL(), + PageTransition::LINK, + L"", NULL); + container_->AddTabContents(popup, gfx::Rect(), host1); + + EXPECT_EQ(container_->GetBlockedPopupCount(), static_cast<size_t>(1)); + EXPECT_EQ(container_->GetTabContentsAt(0), popup); + EXPECT_FALSE(container_->IsHostWhitelisted(0)); + EXPECT_THAT(container_->GetHosts(), testing::Contains(host1)); +} diff --git a/chrome/browser/tab_contents/tab_contents.h b/chrome/browser/tab_contents/tab_contents.h index d73db16..699bf4d 100644 --- a/chrome/browser/tab_contents/tab_contents.h +++ b/chrome/browser/tab_contents/tab_contents.h @@ -558,8 +558,10 @@ class TabContents : public PageNavigator, // Used to access the child_windows_ (ConstrainedWindowList) for testing // automation purposes. friend class AutomationProvider; + friend class BlockedPopupContainerTest; FRIEND_TEST(TabContentsTest, UpdateTitle); + FRIEND_TEST(BlockedPopupContainerTest, TestReposition); // Temporary until the view/contents separation is complete. friend class TabContentsView; @@ -620,6 +622,13 @@ class TabContents : public PageNavigator, // determines whether to show itself). bool ShowingBlockedPopupNotification() const; + // Only used during unit testing; otherwise |blocked_popups_| will be created + // on demand. + void set_blocked_popup_container(BlockedPopupContainer* container) { + DCHECK(blocked_popups_ == NULL); + blocked_popups_ = container; + } + // Called by derived classes to indicate that we're no longer waiting for a // response. This won't actually update the throbber, but it will get picked // up at the next animation step if the throbber is going. diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index b474a99d..4ac0cb7 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -2834,6 +2834,7 @@ 'chrome_strings', 'theme_resources', '../skia/skia.gyp:skia', + '../testing/gmock.gyp:gmock', '../testing/gtest.gyp:gtest', ], 'include_dirs': [ @@ -3201,6 +3202,7 @@ '../webkit/webkit.gyp:webkit', '../webkit/webkit.gyp:webkit_resources', '../skia/skia.gyp:skia', + '../testing/gmock.gyp:gmock', '../testing/gtest.gyp:gtest', '../third_party/icu38/icu38.gyp:icui18n', '../third_party/icu38/icu38.gyp:icuuc', @@ -3225,6 +3227,7 @@ 'browser/autocomplete/keyword_provider_unittest.cc', 'browser/autocomplete/search_provider_unittest.cc', 'browser/back_forward_menu_model_unittest.cc', + 'browser/blocked_popup_container_unittest.cc', 'browser/bookmarks/bookmark_codec_unittest.cc', 'browser/bookmarks/bookmark_context_menu_test.cc', 'browser/bookmarks/bookmark_drag_data_unittest.cc', |