Commit b98fc36f authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Make use of the recently-added MockInfoBarService.

This avoids instantiating a real InfoBarService in tests that don't need it,
which will in turn avoid constructing platform-specific InfoBar instances, which
will in turn prevent crashes in an upcoming change where I make the
views-specific implementation rely on objects these tests don't create.

This also addresses my followup comments on
https://chromium-review.googlesource.com/c/chromium/src/+/953711 .

BUG=none
TEST=none

Change-Id: Iab027693b982db7b91a3be8681647a112ba86e9e
Reviewed-on: https://chromium-review.googlesource.com/989213Reviewed-by: default avatarPatti <patricialor@chromium.org>
Reviewed-by: default avatarJosh Karlin <jkarlin@chromium.org>
Reviewed-by: default avatarCharlie Harrison <csharrison@chromium.org>
Reviewed-by: default avatarTarun Bansal <tbansal@chromium.org>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#547904}
parent d4e14c81
...@@ -26,10 +26,7 @@ class PageLoadCappingInfoBarDelegateTest ...@@ -26,10 +26,7 @@ class PageLoadCappingInfoBarDelegateTest
~PageLoadCappingInfoBarDelegateTest() override = default; ~PageLoadCappingInfoBarDelegateTest() override = default;
void SetUpTest() { void SetUpTest() {
InfoBarService* mock_infobar_service = MockInfoBarService::CreateForWebContents(web_contents());
new MockInfoBarService(web_contents());
ASSERT_EQ(infobar_service(), mock_infobar_service);
ASSERT_TRUE(infobar_service());
NavigateAndCommit(GURL(kTestURL)); NavigateAndCommit(GURL(kTestURL));
} }
......
...@@ -577,11 +577,11 @@ IN_PROC_BROWSER_TEST_F(InfoBarUiTest, InvokeUi_data_reduction_proxy_preview) { ...@@ -577,11 +577,11 @@ IN_PROC_BROWSER_TEST_F(InfoBarUiTest, InvokeUi_data_reduction_proxy_preview) {
ShowAndVerifyUi(); ShowAndVerifyUi();
} }
IN_PROC_BROWSER_TEST_F(InfoBarUiTest, InvokeUi_page_load_capping) { IN_PROC_BROWSER_TEST_F(InfoBarUiTest, InvokeUi_automation) {
ShowAndVerifyUi(); ShowAndVerifyUi();
} }
IN_PROC_BROWSER_TEST_F(InfoBarUiTest, InvokeUi_automation) { IN_PROC_BROWSER_TEST_F(InfoBarUiTest, InvokeUi_page_load_capping) {
ShowAndVerifyUi(); ShowAndVerifyUi();
} }
......
...@@ -7,16 +7,20 @@ ...@@ -7,16 +7,20 @@
#include "components/infobars/core/confirm_infobar_delegate.h" #include "components/infobars/core/confirm_infobar_delegate.h"
#include "components/infobars/core/infobar.h" #include "components/infobars/core/infobar.h"
MockInfoBarService::MockInfoBarService(content::WebContents* web_contents) // static
: InfoBarService(web_contents) { void MockInfoBarService::CreateForWebContents(
content::WebContents* web_contents) {
DCHECK(web_contents);
void* user_data_key = UserDataKey(); void* user_data_key = UserDataKey();
DCHECK(!web_contents->GetUserData(user_data_key)); DCHECK(!web_contents->GetUserData(user_data_key));
web_contents->SetUserData(user_data_key, base::WrapUnique(this)); web_contents->SetUserData(
user_data_key, base::WrapUnique(new MockInfoBarService(web_contents)));
} }
MockInfoBarService::~MockInfoBarService() = default;
std::unique_ptr<infobars::InfoBar> MockInfoBarService::CreateConfirmInfoBar( std::unique_ptr<infobars::InfoBar> MockInfoBarService::CreateConfirmInfoBar(
std::unique_ptr<ConfirmInfoBarDelegate> delegate) { std::unique_ptr<ConfirmInfoBarDelegate> delegate) {
return std::make_unique<infobars::InfoBar>(std::move(delegate)); return std::make_unique<infobars::InfoBar>(std::move(delegate));
} }
MockInfoBarService::MockInfoBarService(content::WebContents* web_contents)
: InfoBarService(web_contents) {}
...@@ -13,11 +13,15 @@ ...@@ -13,11 +13,15 @@
// infobars. // infobars.
class MockInfoBarService : public InfoBarService { class MockInfoBarService : public InfoBarService {
public: public:
explicit MockInfoBarService(content::WebContents* web_contents); // Creates a MockInfoBarService and attaches it as the InfoBarService for
~MockInfoBarService() override; // |web_contents|.
static void CreateForWebContents(content::WebContents* web_contents);
std::unique_ptr<infobars::InfoBar> CreateConfirmInfoBar( std::unique_ptr<infobars::InfoBar> CreateConfirmInfoBar(
std::unique_ptr<ConfirmInfoBarDelegate> delegate) override; std::unique_ptr<ConfirmInfoBarDelegate> delegate) override;
private:
explicit MockInfoBarService(content::WebContents* web_contents);
}; };
#endif // CHROME_BROWSER_INFOBARS_MOCK_INFOBAR_SERVICE_H_ #endif // CHROME_BROWSER_INFOBARS_MOCK_INFOBAR_SERVICE_H_
...@@ -31,16 +31,9 @@ class PageCappingObserverTest ...@@ -31,16 +31,9 @@ class PageCappingObserverTest
NavigateAndCommit(GURL(kTestURL)); NavigateAndCommit(GURL(kTestURL));
} }
size_t InfoBarCount() { size_t InfoBarCount() { return infobar_service()->infobar_count(); }
// Don't show multiple infobars.
return infobar_service() ? infobar_service()->infobar_count() : 0;
}
void RemoveAllInfoBars() { void RemoveAllInfoBars() { infobar_service()->RemoveAllInfoBars(false); }
// Don't show multiple infobars.
if (infobar_service())
infobar_service()->RemoveAllInfoBars(false);
}
InfoBarService* infobar_service() { InfoBarService* infobar_service() {
return InfoBarService::FromWebContents(web_contents()); return InfoBarService::FromWebContents(web_contents());
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
#include "base/time/time.h" #include "base/time/time.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/browser/android/android_theme_resources.h" #include "chrome/browser/android/android_theme_resources.h"
#include "chrome/browser/infobars/infobar_service.h" #include "chrome/browser/infobars/mock_infobar_service.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h" #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h"
#include "chrome/browser/page_load_metrics/observers/page_load_metrics_observer_test_harness.h" #include "chrome/browser/page_load_metrics/observers/page_load_metrics_observer_test_harness.h"
...@@ -160,7 +160,7 @@ class PreviewsInfoBarDelegateUnitTest ...@@ -160,7 +160,7 @@ class PreviewsInfoBarDelegateUnitTest
void SetUp() override { void SetUp() override {
PageLoadMetricsObserverTestHarness::SetUp(); PageLoadMetricsObserverTestHarness::SetUp();
InfoBarService::CreateForWebContents(web_contents()); MockInfoBarService::CreateForWebContents(web_contents());
PreviewsInfoBarTabHelper::CreateForWebContents(web_contents()); PreviewsInfoBarTabHelper::CreateForWebContents(web_contents());
TestPreviewsWebContentsObserver::CreateForWebContents(web_contents()); TestPreviewsWebContentsObserver::CreateForWebContents(web_contents());
...@@ -220,12 +220,10 @@ class PreviewsInfoBarDelegateUnitTest ...@@ -220,12 +220,10 @@ class PreviewsInfoBarDelegateUnitTest
base::Unretained(this)), base::Unretained(this)),
previews_ui_service_.get()); previews_ui_service_.get());
InfoBarService* infobar_service = EXPECT_EQ(1U, infobar_service()->infobar_count());
InfoBarService::FromWebContents(web_contents());
EXPECT_EQ(1U, infobar_service->infobar_count());
return static_cast<PreviewsInfoBarDelegate*>( return static_cast<PreviewsInfoBarDelegate*>(
infobar_service->infobar_at(0)->delegate()); infobar_service()->infobar_at(0)->delegate());
} }
void EnableStalePreviewsTimestamp( void EnableStalePreviewsTimestamp(
...@@ -261,7 +259,7 @@ class PreviewsInfoBarDelegateUnitTest ...@@ -261,7 +259,7 @@ class PreviewsInfoBarDelegateUnitTest
tester_->ExpectBucketCount(kUMAPreviewsInfoBarTimestamp, expected_bucket, tester_->ExpectBucketCount(kUMAPreviewsInfoBarTimestamp, expected_bucket,
1); 1);
// Dismiss the infobar. // Dismiss the infobar.
InfoBarService::FromWebContents(web_contents())->RemoveAllInfoBars(false); infobar_service()->RemoveAllInfoBars(false);
PreviewsInfoBarTabHelper::FromWebContents(web_contents()) PreviewsInfoBarTabHelper::FromWebContents(web_contents())
->set_displayed_preview_infobar(false); ->set_displayed_preview_infobar(false);
} }
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/infobars/infobar_service.h" #include "chrome/browser/infobars/mock_infobar_service.h"
#include "chrome/browser/loader/chrome_navigation_data.h" #include "chrome/browser/loader/chrome_navigation_data.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h" #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h"
...@@ -52,7 +52,7 @@ class PreviewsInfoBarTabHelperUnitTest ...@@ -52,7 +52,7 @@ class PreviewsInfoBarTabHelperUnitTest
#if BUILDFLAG(ENABLE_OFFLINE_PAGES) #if BUILDFLAG(ENABLE_OFFLINE_PAGES)
offline_pages::OfflinePageTabHelper::CreateForWebContents(web_contents()); offline_pages::OfflinePageTabHelper::CreateForWebContents(web_contents());
#endif // BUILDFLAG(ENABLE_OFFLINE_PAGES) #endif // BUILDFLAG(ENABLE_OFFLINE_PAGES)
InfoBarService::CreateForWebContents(web_contents()); MockInfoBarService::CreateForWebContents(web_contents());
PreviewsInfoBarTabHelper::CreateForWebContents(web_contents()); PreviewsInfoBarTabHelper::CreateForWebContents(web_contents());
test_handle_ = content::NavigationHandle::CreateNavigationHandleForTesting( test_handle_ = content::NavigationHandle::CreateNavigationHandleForTesting(
GURL(kTestUrl), main_rfh()); GURL(kTestUrl), main_rfh());
...@@ -137,6 +137,10 @@ class PreviewsInfoBarTabHelperUnitTest ...@@ -137,6 +137,10 @@ class PreviewsInfoBarTabHelperUnitTest
->SetNavigationData(test_handle_.get(), std::move(navigation_data)); ->SetNavigationData(test_handle_.get(), std::move(navigation_data));
} }
InfoBarService* infobar_service() {
return InfoBarService::FromWebContents(web_contents());
}
protected: protected:
std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext> std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext>
drp_test_context_; drp_test_context_;
...@@ -155,9 +159,7 @@ TEST_F(PreviewsInfoBarTabHelperUnitTest, ...@@ -155,9 +159,7 @@ TEST_F(PreviewsInfoBarTabHelperUnitTest,
SimulateWillProcessResponse(); SimulateWillProcessResponse();
CallDidFinishNavigation(); CallDidFinishNavigation();
InfoBarService* infobar_service = EXPECT_EQ(1U, infobar_service()->infobar_count());
InfoBarService::FromWebContents(web_contents());
EXPECT_EQ(1U, infobar_service->infobar_count());
EXPECT_TRUE(infobar_tab_helper->displayed_preview_infobar()); EXPECT_TRUE(infobar_tab_helper->displayed_preview_infobar());
// Navigate to reset the displayed state. // Navigate to reset the displayed state.
...@@ -177,9 +179,7 @@ TEST_F(PreviewsInfoBarTabHelperUnitTest, ...@@ -177,9 +179,7 @@ TEST_F(PreviewsInfoBarTabHelperUnitTest,
SimulateWillProcessResponse(); SimulateWillProcessResponse();
CallDidFinishNavigation(); CallDidFinishNavigation();
InfoBarService* infobar_service = EXPECT_EQ(1U, infobar_service()->infobar_count());
InfoBarService::FromWebContents(web_contents());
EXPECT_EQ(1U, infobar_service->infobar_count());
EXPECT_TRUE(infobar_tab_helper->displayed_preview_infobar()); EXPECT_TRUE(infobar_tab_helper->displayed_preview_infobar());
// Navigate to reset the displayed state. // Navigate to reset the displayed state.
...@@ -199,9 +199,7 @@ TEST_F(PreviewsInfoBarTabHelperUnitTest, ...@@ -199,9 +199,7 @@ TEST_F(PreviewsInfoBarTabHelperUnitTest,
SimulateWillProcessResponse(); SimulateWillProcessResponse();
CallDidFinishNavigation(); CallDidFinishNavigation();
InfoBarService* infobar_service = EXPECT_EQ(0U, infobar_service()->infobar_count());
InfoBarService::FromWebContents(web_contents());
EXPECT_EQ(0U, infobar_service->infobar_count());
EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar());
} }
...@@ -263,9 +261,7 @@ TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateOfflineInfoBar) { ...@@ -263,9 +261,7 @@ TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateOfflineInfoBar) {
CallDidFinishNavigation(); CallDidFinishNavigation();
InfoBarService* infobar_service = EXPECT_EQ(1U, infobar_service()->infobar_count());
InfoBarService::FromWebContents(web_contents());
EXPECT_EQ(1U, infobar_service->infobar_count());
EXPECT_TRUE(infobar_tab_helper->displayed_preview_infobar()); EXPECT_TRUE(infobar_tab_helper->displayed_preview_infobar());
// Navigate to reset the displayed state. // Navigate to reset the displayed state.
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h" #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/infobars/infobar_service.h" #include "chrome/browser/infobars/mock_infobar_service.h"
#include "chrome/browser/ui/page_info/page_info_ui.h" #include "chrome/browser/ui/page_info/page_info_ui.h"
#include "chrome/browser/usb/usb_chooser_context.h" #include "chrome/browser/usb/usb_chooser_context.h"
#include "chrome/browser/usb/usb_chooser_context_factory.h" #include "chrome/browser/usb/usb_chooser_context_factory.h"
...@@ -117,7 +117,7 @@ class PageInfoTest : public ChromeRenderViewHostTestHarness { ...@@ -117,7 +117,7 @@ class PageInfoTest : public ChromeRenderViewHostTestHarness {
ASSERT_TRUE(cert_); ASSERT_TRUE(cert_);
TabSpecificContentSettings::CreateForWebContents(web_contents()); TabSpecificContentSettings::CreateForWebContents(web_contents());
InfoBarService::CreateForWebContents(web_contents()); MockInfoBarService::CreateForWebContents(web_contents());
// Setup mock ui. // Setup mock ui.
ResetMockUI(); ResetMockUI();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment