Skip to content

Commit

Permalink
Merge pull request #17006 from brave/issues/28264
Browse files Browse the repository at this point in the history
Fixed MockAdsService usage after test finished.
  • Loading branch information
aseren authored Feb 3, 2023
2 parents ee2bc66 + 5dccb78 commit 5e987c4
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions browser/brave_ads/search_result_ad/search_result_ad_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,20 @@ class SearchResultAdTest : public InProcessBrowserTest {

net::EmbeddedTestServer* https_server() { return https_server_.get(); }

MockAdsService* ads_service() { return &ads_service_; }

private:
base::test::ScopedFeatureList feature_list_;
content::ContentMockCertVerifier mock_cert_verifier_;
std::unique_ptr<net::EmbeddedTestServer> https_server_;
MockAdsService ads_service_;
};

IN_PROC_BROWSER_TEST_F(SearchResultAdTest, AdsDisabled) {
MockAdsService ads_service;
ScopedTestingAdsServiceSetter scoped_setter(&ads_service);
ScopedTestingAdsServiceSetter scoped_setter(ads_service());

EXPECT_CALL(ads_service, IsEnabled()).WillRepeatedly(Return(false));
EXPECT_CALL(ads_service, TriggerSearchResultAdEvent(_, _)).Times(0);
EXPECT_CALL(*ads_service(), IsEnabled()).WillRepeatedly(Return(false));
EXPECT_CALL(*ads_service(), TriggerSearchResultAdEvent(_, _)).Times(0);

GURL url = GetURL(kAllowedDomain, kSearchResultUrlPath);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
Expand All @@ -177,11 +179,10 @@ IN_PROC_BROWSER_TEST_F(SearchResultAdTest, AdsDisabled) {
}

IN_PROC_BROWSER_TEST_F(SearchResultAdTest, NotAllowedDomain) {
MockAdsService ads_service;
ScopedTestingAdsServiceSetter scoped_setter(&ads_service);
ScopedTestingAdsServiceSetter scoped_setter(ads_service());

EXPECT_CALL(ads_service, IsEnabled()).WillRepeatedly(Return(true));
EXPECT_CALL(ads_service, TriggerSearchResultAdEvent(_, _)).Times(0);
EXPECT_CALL(*ads_service(), IsEnabled()).WillRepeatedly(Return(true));
EXPECT_CALL(*ads_service(), TriggerSearchResultAdEvent(_, _)).Times(0);

GURL url = GetURL(kNotAllowedDomain, kSearchResultUrlPath);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
Expand All @@ -191,11 +192,10 @@ IN_PROC_BROWSER_TEST_F(SearchResultAdTest, NotAllowedDomain) {
}

IN_PROC_BROWSER_TEST_F(SearchResultAdTest, BrokenSearchAdMetadata) {
MockAdsService ads_service;
ScopedTestingAdsServiceSetter scoped_setter(&ads_service);
ScopedTestingAdsServiceSetter scoped_setter(ads_service());

EXPECT_CALL(ads_service, IsEnabled()).WillRepeatedly(Return(true));
EXPECT_CALL(ads_service, TriggerSearchResultAdEvent(_, _)).Times(0);
EXPECT_CALL(*ads_service(), IsEnabled()).WillRepeatedly(Return(true));
EXPECT_CALL(*ads_service(), TriggerSearchResultAdEvent(_, _)).Times(0);

GURL url = GetURL(kAllowedDomain, "/brave_ads/search_result_ad_broken.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
Expand Down Expand Up @@ -272,15 +272,14 @@ class SampleSearchResultAdTest : public SearchResultAdTest,
}

content::WebContents* LoadAndCheckSampleSearchResultAdWebPage(
MockAdsService* ads_service,
const GURL& url) {
auto run_loop1 = std::make_unique<base::RunLoop>();
auto run_loop2 = std::make_unique<base::RunLoop>();
EXPECT_CALL(*ads_service,
EXPECT_CALL(*ads_service(),
TriggerSearchResultAdEvent(
_, ads::mojom::SearchResultAdEventType::kServed))
.Times(2);
EXPECT_CALL(*ads_service,
EXPECT_CALL(*ads_service(),
TriggerSearchResultAdEvent(
_, ads::mojom::SearchResultAdEventType::kViewed))
.Times(2)
Expand Down Expand Up @@ -316,15 +315,14 @@ class SampleSearchResultAdTest : public SearchResultAdTest,

IN_PROC_BROWSER_TEST_P(SampleSearchResultAdTest,
SearchResultAdOpenedInSameTab) {
MockAdsService ads_service;
ScopedTestingAdsServiceSetter scoped_setter(&ads_service);
EXPECT_CALL(ads_service, IsEnabled()).WillRepeatedly(Return(true));
ScopedTestingAdsServiceSetter scoped_setter(ads_service());
EXPECT_CALL(*ads_service(), IsEnabled()).WillRepeatedly(Return(true));

content::WebContents* web_contents = LoadAndCheckSampleSearchResultAdWebPage(
&ads_service, GetSearchResultUrl());
content::WebContents* web_contents =
LoadAndCheckSampleSearchResultAdWebPage(GetSearchResultUrl());

base::RunLoop run_loop;
EXPECT_CALL(ads_service, TriggerSearchResultAdEvent(_, _))
EXPECT_CALL(*ads_service(), TriggerSearchResultAdEvent(_, _))
.WillOnce([this, &run_loop](
ads::mojom::SearchResultAdInfoPtr ad_mojom,
const ads::mojom::SearchResultAdEventType event_type) {
Expand All @@ -339,16 +337,15 @@ IN_PROC_BROWSER_TEST_P(SampleSearchResultAdTest,
}

IN_PROC_BROWSER_TEST_P(SampleSearchResultAdTest, SearchResultAdOpenedInNewTab) {
MockAdsService ads_service;
ScopedTestingAdsServiceSetter scoped_setter(&ads_service);
EXPECT_CALL(ads_service, IsEnabled()).WillRepeatedly(Return(true));
ScopedTestingAdsServiceSetter scoped_setter(ads_service());
EXPECT_CALL(*ads_service(), IsEnabled()).WillRepeatedly(Return(true));

content::WebContents* web_contents = LoadAndCheckSampleSearchResultAdWebPage(
&ads_service, GetSearchResultUrl());
content::WebContents* web_contents =
LoadAndCheckSampleSearchResultAdWebPage(GetSearchResultUrl());

EXPECT_CALL(ads_service, IsEnabled()).WillRepeatedly(Return(true));
EXPECT_CALL(*ads_service(), IsEnabled()).WillRepeatedly(Return(true));
base::RunLoop run_loop;
EXPECT_CALL(ads_service, TriggerSearchResultAdEvent(_, _))
EXPECT_CALL(*ads_service(), TriggerSearchResultAdEvent(_, _))
.WillOnce([this, &run_loop](
ads::mojom::SearchResultAdInfoPtr ad_mojom,
const ads::mojom::SearchResultAdEventType event_type) {
Expand Down

0 comments on commit 5e987c4

Please sign in to comment.