Commit ac88234c authored by Kurt Horimoto's avatar Kurt Horimoto Committed by Commit Bot

[iOS] Add InfoBarIOS::infobar_type()

InfobarUIDelegate will not be necessary once we move to the overlay-
based implementation.  The only functionality needed from the UI
delegate necessary with overlays is the InfobarType.  This CL adds
InfoBarIOS::infobar_type() to access this type, as well as a constructor
that does not take an InfobarUIDelegate.

This CL also updates the password infobar to be created with this new
constructor if the overlay feature is enabled.

Bug: 1033228
Change-Id: I3080bc2e5bb9e2e3db23dfe437ada90b70bb1661
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2150125
Commit-Queue: Kurt Horimoto <kkhorimoto@chromium.org>
Reviewed-by: default avatarSergio Collazos <sczs@chromium.org>
Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Reviewed-by: default avatarChris Lu <thegreenfrog@chromium.org>
Cr-Commit-Position: refs/heads/master@{#759741}
parent 21fa04a9
......@@ -61,6 +61,7 @@ source_set("badge") {
"//ios/chrome/browser/overlays",
"//ios/chrome/browser/overlays/public/common/infobars",
"//ios/chrome/browser/ui/badges:public",
"//ios/chrome/browser/ui/badges:util",
"//ios/chrome/browser/ui/infobars:feature_flags",
"//ios/chrome/browser/ui/infobars:infobars_ui",
"//ios/chrome/browser/web_state_list",
......
......@@ -7,6 +7,7 @@
#include "ios/chrome/browser/infobars/infobar_badge_model.h"
#include "ios/chrome/browser/infobars/infobar_badge_tab_helper_delegate.h"
#include "ios/chrome/browser/infobars/infobar_manager_impl.h"
#include "ios/chrome/browser/ui/badges/badge_type_util.h"
#import "ios/chrome/browser/ui/infobars/infobar_feature.h"
#import "ios/chrome/browser/ui/infobars/infobar_ui_delegate.h"
......@@ -17,11 +18,12 @@
namespace {
// Returns |infobar|'s InfobarType.
InfobarType GetInfobarType(infobars::InfoBar* infobar) {
return static_cast<InfoBarIOS*>(infobar)->InfobarUIDelegate().infobarType;
return static_cast<InfoBarIOS*>(infobar)->infobar_type();
}
// Returns whether |infobar| supports badges.
bool SupportsBadges(infobars::InfoBar* infobar) {
return static_cast<InfoBarIOS*>(infobar)->InfobarUIDelegate().hasBadge;
return BadgeTypeForInfobarType(GetInfobarType(infobar)) !=
BadgeType::kBadgeTypeNone;
}
} // namespace
......
......@@ -53,10 +53,8 @@ class InfobarBadgeTabHelperTest : public PlatformTest {
// ignored. Returns the added infobar.
FakeInfobarIOS* AddInfobar(bool has_badge, bool replace_existing = false) {
std::unique_ptr<FakeInfobarIOS> added_infobar =
std::make_unique<FakeInfobarIOS>();
added_infobar->fake_ui_delegate().infobarType =
has_badge ? kInfobarTypeWithBadge : kInfobarTypeNoBadge;
added_infobar->fake_ui_delegate().hasBadge = has_badge;
std::make_unique<FakeInfobarIOS>(has_badge ? kInfobarTypeWithBadge
: kInfobarTypeNoBadge);
FakeInfobarIOS* infobar = added_infobar.get();
InfoBarManagerImpl::FromWebState(&web_state_)
->AddInfoBar(std::move(added_infobar), replace_existing);
......@@ -79,7 +77,7 @@ TEST_F(InfobarBadgeTabHelperTest, TestInfobarBadgeState) {
// Add a badge-supporting infobar to the InfobarManager to create the badge
// item.
FakeInfobarIOS* infobar = AddInfobar(/*has_badge=*/true);
InfobarType added_type = infobar->fake_ui_delegate().infobarType;
InfobarType added_type = infobar->infobar_type();
// Simulate presenting the banner UI and verify that the badge state is sent
// to the delegate.
tab_helper()->UpdateBadgeForInfobarBannerPresented(added_type);
......@@ -103,8 +101,7 @@ TEST_F(InfobarBadgeTabHelperTest, TestInfobarBadgeState) {
TEST_F(InfobarBadgeTabHelperTest, TestInfobarBadgeStateDeprecated) {
// Add a badge-supporting infobar to the InfobarManager to create the badge
// item.
InfobarType added_type =
AddInfobar(/*has_badge=*/true)->fake_ui_delegate().infobarType;
InfobarType added_type = AddInfobar(/*has_badge=*/true)->infobar_type();
// Simulate presenting the banner UI and verify that the badge state is sent
// to the delegate.
tab_helper()->UpdateBadgeForInfobarBannerPresented(added_type);
......@@ -126,8 +123,7 @@ TEST_F(InfobarBadgeTabHelperTest, TestInfobarBadgeStateDeprecated) {
// Tests that adding an infobar that doesn't support badges does not notify the
// delegate of BadgeItem creation.
TEST_F(InfobarBadgeTabHelperTest, TestInfobarBadgeStateNoBadge) {
InfobarType added_type =
AddInfobar(/*has_badge=*/false)->fake_ui_delegate().infobarType;
InfobarType added_type = AddInfobar(/*has_badge=*/false)->infobar_type();
EXPECT_FALSE(
[delegate_ itemForBadgeType:BadgeTypeForInfobarType(added_type)]);
}
......@@ -137,8 +133,7 @@ TEST_F(InfobarBadgeTabHelperTest, TestInfobarBadgeStateNoBadge) {
TEST_F(InfobarBadgeTabHelperTest, TestInfobarBadgeOnBannerDismissal) {
// Add a badge-supporting infobar to the InfobarManager to create the badge
// item, then simulate presentation and dismissal.
InfobarType added_type =
AddInfobar(/*has_badge=*/true)->fake_ui_delegate().infobarType;
InfobarType added_type = AddInfobar(/*has_badge=*/true)->infobar_type();
tab_helper()->UpdateBadgeForInfobarBannerPresented(added_type);
tab_helper()->UpdateBadgeForInfobarBannerDismissed(added_type);
// Verify that the BadgeItem was not removed and that its state is dismissed.
......@@ -154,7 +149,7 @@ TEST_F(InfobarBadgeTabHelperTest, TestInfobarBadgeOnBannerAccepted) {
// Add a badge-supporting infobar to the InfobarManager to create the badge
// item, then simulate presentation, acceptance, and dismissal.
FakeInfobarIOS* infobar = AddInfobar(/*has_badge=*/true);
InfobarType added_type = infobar->fake_ui_delegate().infobarType;
InfobarType added_type = infobar->infobar_type();
tab_helper()->UpdateBadgeForInfobarBannerPresented(added_type);
infobar->set_accepted(true);
tab_helper()->UpdateBadgeForInfobarBannerDismissed(added_type);
......@@ -170,8 +165,7 @@ TEST_F(InfobarBadgeTabHelperTest, TestInfobarBadgeOnBannerAccepted) {
TEST_F(InfobarBadgeTabHelperTest, TestInfobarBadgeOnBannerAcceptedDeprecated) {
// Add a badge-supporting infobar to the InfobarManager to create the badge
// item, then simulate presentation, acceptance, and dismissal.
InfobarType added_type =
AddInfobar(/*has_badge=*/true)->fake_ui_delegate().infobarType;
InfobarType added_type = AddInfobar(/*has_badge=*/true)->infobar_type();
tab_helper()->UpdateBadgeForInfobarBannerPresented(added_type);
tab_helper()->UpdateBadgeForInfobarAccepted(added_type);
tab_helper()->UpdateBadgeForInfobarBannerDismissed(added_type);
......@@ -187,7 +181,7 @@ TEST_F(InfobarBadgeTabHelperTest, TestInfobarBadgeOnInfobarDestruction) {
// Add a badge-supporting infobar to the InfobarManager to create the badge
// item, then simulate presentation and dismissal.
FakeInfobarIOS* added_infobar = AddInfobar(/*has_badge=*/true);
InfobarType added_type = added_infobar->fake_ui_delegate().infobarType;
InfobarType added_type = added_infobar->infobar_type();
tab_helper()->UpdateBadgeForInfobarBannerPresented(added_type);
tab_helper()->UpdateBadgeForInfobarBannerDismissed(added_type);
ASSERT_TRUE([delegate_ itemForBadgeType:BadgeTypeForInfobarType(added_type)]);
......
......@@ -14,6 +14,7 @@
#include "base/observer_list_types.h"
#include "components/infobars/core/infobar.h"
#import "ios/chrome/browser/infobars/infobar_controller_delegate.h"
#import "ios/chrome/browser/infobars/infobar_type.h"
@protocol InfobarUIDelegate;
namespace infobars {
......@@ -23,7 +24,12 @@ class InfoBarDelegate;
// The iOS version of infobars::InfoBar.
class InfoBarIOS : public infobars::InfoBar, public InfoBarControllerDelegate {
public:
InfoBarIOS(id<InfobarUIDelegate> controller,
InfoBarIOS(InfobarType infobar_type,
std::unique_ptr<infobars::InfoBarDelegate> delegate,
bool skip_banner = false);
// TODO(crbug.com/1030357): InfobarUIDelegate and this constructor can be
// removed once overlay-based infobar UI is fully supported.
InfoBarIOS(id<InfobarUIDelegate> ui_delegate,
std::unique_ptr<infobars::InfoBarDelegate> delegate,
bool skip_banner = false);
~InfoBarIOS() override;
......@@ -44,6 +50,9 @@ class InfoBarIOS : public infobars::InfoBar, public InfoBarControllerDelegate {
void AddObserver(Observer* obs) { observers_.AddObserver(obs); }
void RemoveObserver(Observer* obs) { observers_.RemoveObserver(obs); }
// Returns the infobar type.
InfobarType infobar_type() const { return infobar_type_; }
// Whether or not the infobar has been accepted. Set to true when the
// associated action has been executed (e.g. page translation finished), and
// false if the action has not been executed or has been reverted.
......@@ -69,7 +78,8 @@ class InfoBarIOS : public infobars::InfoBar, public InfoBarControllerDelegate {
void RemoveInfoBar() override;
base::ObserverList<Observer, /*check_empty=*/true> observers_;
id<InfobarUIDelegate> controller_ = nil;
id<InfobarUIDelegate> ui_delegate_ = nil;
InfobarType infobar_type_;
bool accepted_ = false;
bool skip_banner_ = false;
base::WeakPtrFactory<InfoBarIOS> weak_factory_{this};
......
......@@ -15,20 +15,27 @@
using infobars::InfoBar;
using infobars::InfoBarDelegate;
InfoBarIOS::InfoBarIOS(id<InfobarUIDelegate> controller,
InfoBarIOS::InfoBarIOS(InfobarType infobar_type,
std::unique_ptr<InfoBarDelegate> delegate,
bool skip_banner)
: InfoBar(std::move(delegate)),
controller_(controller),
infobar_type_(infobar_type),
skip_banner_(skip_banner) {}
InfoBarIOS::InfoBarIOS(id<InfobarUIDelegate> ui_delegate,
std::unique_ptr<InfoBarDelegate> delegate,
bool skip_banner)
: InfoBar(std::move(delegate)),
ui_delegate_(ui_delegate),
infobar_type_(ui_delegate_.infobarType),
skip_banner_(skip_banner) {
DCHECK(controller_);
[controller_ setDelegate:this];
DCHECK(ui_delegate_);
[ui_delegate_ setDelegate:this];
}
InfoBarIOS::~InfoBarIOS() {
DCHECK(controller_);
[controller_ detachView];
controller_ = nil;
[ui_delegate_ detachView];
ui_delegate_ = nil;
for (auto& observer : observers_) {
observer.InfobarDestroyed(this);
}
......@@ -44,13 +51,13 @@ void InfoBarIOS::set_accepted(bool accepted) {
}
id<InfobarUIDelegate> InfoBarIOS::InfobarUIDelegate() {
DCHECK(controller_);
return controller_;
DCHECK(ui_delegate_);
return ui_delegate_;
}
void InfoBarIOS::RemoveView() {
DCHECK(controller_);
[controller_ removeView];
DCHECK(ui_delegate_);
[ui_delegate_ removeView];
}
base::WeakPtr<InfoBarIOS> InfoBarIOS::GetWeakPtr() {
......
......@@ -31,7 +31,6 @@ source_set("overlays") {
"//ios/chrome/browser/overlays/public/infobar_banner",
"//ios/chrome/browser/overlays/public/infobar_modal",
"//ios/chrome/browser/passwords:infobar_delegates",
"//ios/chrome/browser/ui/infobars:infobars_ui",
"//ios/web/public",
]
}
......
......@@ -7,7 +7,6 @@
#import "ios/chrome/browser/infobars/infobar_ios.h"
#import "ios/chrome/browser/overlays/public/infobar_banner/save_password_infobar_banner_overlay.h"
#import "ios/chrome/browser/overlays/public/infobar_modal/password_infobar_modal_overlay_request_config.h"
#import "ios/chrome/browser/ui/infobars/infobar_ui_delegate.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
......@@ -37,8 +36,7 @@ InfobarOverlayRequestFactoryImpl::CreateInfobarRequest(
// non-null after all existing infobars have been converted to using overlays.
// Early return in the interim to prevent crashing while the remaining
// infobars are being converted.
FactoryHelper* factory =
factory_storages_[infobar_ios->InfobarUIDelegate().infobarType][type];
FactoryHelper* factory = factory_storages_[infobar_ios->infobar_type()][type];
return factory ? factory->CreateInfobarRequest(infobar_ios) : nullptr;
}
......
......@@ -62,8 +62,8 @@ class InfobarOverlayRequestInserterTest : public PlatformTest {
// pointer to the added InfoBar. If |message_text| matches an infobar already
// added, then it the new one will be ignored.
InfoBar* CreateInfobar(base::string16 message_text) {
std::unique_ptr<InfoBar> added_infobar =
std::make_unique<FakeInfobarIOS>(message_text);
std::unique_ptr<InfoBar> added_infobar = std::make_unique<FakeInfobarIOS>(
InfobarType::kInfobarTypeConfirm, message_text);
InfoBar* infobar = added_infobar.get();
manager()->AddInfoBar(std::move(added_infobar));
return infobar;
......
......@@ -12,32 +12,20 @@
#include "base/strings/utf_string_conversions.h"
@class FakeInfobarUIDelegate;
class FakeInfobarDelegate;
// Fake version of InfoBarIOS set up with fake delegates to use in tests.
class FakeInfobarIOS : public InfoBarIOS {
public:
// Creates a FakeInfobarIOS with |type| that has a delegate that uses
// |message_text| as its message.
FakeInfobarIOS(
InfobarType type = InfobarType::kInfobarTypeConfirm,
base::string16 message_text = base::ASCIIToUTF16("FakeInfobar"));
// Creates a FakeInfobarIOS with |fake_delegate|. Uses
// InfobarType::kInfobarTypeConfirm as a default type value.}
FakeInfobarIOS(std::unique_ptr<FakeInfobarDelegate> fake_delegate);
~FakeInfobarIOS() override;
// Creates a FakeInfobarIOS whose FakeInfobarUIDelegate's infobar type is
// |type|, optionally with badge support.
static std::unique_ptr<FakeInfobarIOS> Create(
InfobarType type,
bool has_badge = false,
base::string16 message_text = base::ASCIIToUTF16("FakeInfobar"));
// The fake UI delegate.
FakeInfobarUIDelegate* fake_ui_delegate() const { return fake_ui_delegate_; }
// The fake InfoBarDelegate.
FakeInfobarDelegate* fake_delegate() const { return fake_delegate_; }
private:
FakeInfobarUIDelegate* fake_ui_delegate_ = nil;
FakeInfobarDelegate* fake_delegate_ = nullptr;
};
#endif // IOS_CHROME_BROWSER_INFOBARS_TEST_FAKE_INFOBAR_IOS_H_
......@@ -6,43 +6,16 @@
#include "base/logging.h"
#include "ios/chrome/browser/infobars/test/fake_infobar_delegate.h"
#import "ios/chrome/browser/ui/infobars/test/fake_infobar_ui_delegate.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
FakeInfobarIOS::FakeInfobarIOS(base::string16 message_text)
: InfoBarIOS([[FakeInfobarUIDelegate alloc] init],
std::make_unique<FakeInfobarDelegate>(message_text)),
fake_ui_delegate_(
static_cast<FakeInfobarUIDelegate*>(InfobarUIDelegate())),
fake_delegate_(static_cast<FakeInfobarDelegate*>(delegate())) {
DCHECK([fake_ui_delegate_ isKindOfClass:[FakeInfobarUIDelegate class]]);
DCHECK(fake_delegate_);
}
FakeInfobarIOS::FakeInfobarIOS(InfobarType type, base::string16 message_text)
: InfoBarIOS(type, std::make_unique<FakeInfobarDelegate>(message_text)) {}
FakeInfobarIOS::FakeInfobarIOS(
std::unique_ptr<FakeInfobarDelegate> fake_delegate)
: InfoBarIOS([[FakeInfobarUIDelegate alloc] init],
std::move(fake_delegate)),
fake_ui_delegate_(
static_cast<FakeInfobarUIDelegate*>(InfobarUIDelegate())),
fake_delegate_(static_cast<FakeInfobarDelegate*>(delegate())) {
DCHECK([fake_ui_delegate_ isKindOfClass:[FakeInfobarUIDelegate class]]);
DCHECK(fake_delegate_);
}
: InfoBarIOS(InfobarType::kInfobarTypeConfirm, std::move(fake_delegate)) {}
FakeInfobarIOS::~FakeInfobarIOS() = default;
// static
std::unique_ptr<FakeInfobarIOS> FakeInfobarIOS::Create(
InfobarType type,
bool has_badge,
base::string16 message_text) {
std::unique_ptr<FakeInfobarIOS> infobar =
std::make_unique<FakeInfobarIOS>(message_text);
infobar->fake_ui_delegate().infobarType = type;
infobar->fake_ui_delegate().hasBadge = has_badge;
return infobar;
}
......@@ -17,6 +17,7 @@ source_set("infobars") {
"//ios/chrome/browser/infobars:public",
"//ios/chrome/browser/infobars/overlays:overlay_type",
"//ios/chrome/browser/overlays",
"//ios/chrome/browser/ui/badges:util",
"//ios/chrome/browser/ui/infobars:infobars_ui",
]
}
......@@ -6,7 +6,7 @@
#include "base/logging.h"
#import "ios/chrome/browser/infobars/infobar_ios.h"
#import "ios/chrome/browser/ui/infobars/infobar_ui_delegate.h"
#include "ios/chrome/browser/ui/badges/badge_type_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
......@@ -20,8 +20,9 @@ InfobarOverlayRequestConfig::InfobarOverlayRequestConfig(
InfoBarIOS* infobar,
InfobarOverlayType overlay_type)
: infobar_(infobar->GetWeakPtr()),
infobar_type_(infobar->InfobarUIDelegate().infobarType),
has_badge_(infobar->InfobarUIDelegate().hasBadge),
infobar_type_(infobar->infobar_type()),
has_badge_(BadgeTypeForInfobarType(infobar_type_) !=
BadgeType::kBadgeTypeNone),
overlay_type_(overlay_type) {}
InfobarOverlayRequestConfig::~InfobarOverlayRequestConfig() = default;
......@@ -764,13 +764,21 @@ NSString* const kSuggestionSuffix = @" ••••••••";
delegate->set_dispatcher(self.dispatcher);
if (IsInfobarUIRebootEnabled()) {
InfobarPasswordCoordinator* coordinator =
[[InfobarPasswordCoordinator alloc]
std::unique_ptr<InfoBarIOS> infobar;
// If manual save, skip showing banner.
bool skip_banner = manual;
if (IsInfobarOverlayUIEnabled()) {
infobar = std::make_unique<InfoBarIOS>(
InfobarType::kInfobarTypePasswordSave, std::move(delegate),
skip_banner);
} else {
InfobarPasswordCoordinator* coordinator = [[InfobarPasswordCoordinator
alloc]
initWithInfoBarDelegate:delegate.get()
type:InfobarType::kInfobarTypePasswordSave];
// If manual save, skip showing banner.
std::unique_ptr<InfoBarIOS> infobar = std::make_unique<InfoBarIOS>(
coordinator, std::move(delegate), /*skip_banner=*/manual);
infobar = std::make_unique<InfoBarIOS>(
coordinator, std::move(delegate), skip_banner);
}
infoBarManager->AddInfoBar(std::move(infobar),
/*replace_existing=*/true);
} else if (!manual) {
......
......@@ -117,7 +117,7 @@ class BadgeMediatorTest : public testing::TestWithParam<TestParam> {
// Pass in different |message_text| to avoid replacing existing infobar.
InfoBarIOS* AddInfobar(InfobarType type, base::string16 message_text) {
std::unique_ptr<InfoBarIOS> added_infobar =
FakeInfobarIOS::Create(type, /*has_badge=*/true, message_text);
std::make_unique<FakeInfobarIOS>(type, message_text);
InfoBarIOS* infobar = added_infobar.get();
infobar_manager()->AddInfoBar(std::move(added_infobar));
return infobar;
......@@ -240,8 +240,8 @@ TEST_P(BadgeMediatorTest,
EXPECT_EQ(badge_consumer_.displayedBadge.badgeType,
BadgeType::kBadgeTypePasswordSave);
InsertActivatedWebState(/*index=*/1);
std::unique_ptr<InfoBarIOS> added_infobar = FakeInfobarIOS::Create(
kSecondInfobarType, /*has_badge=*/true, kSecondInfobarMessageText);
std::unique_ptr<InfoBarIOS> added_infobar = std::make_unique<FakeInfobarIOS>(
kSecondInfobarType, kSecondInfobarMessageText);
InfoBarManagerImpl::FromWebState(web_state_list_.GetWebStateAt(0))
->AddInfoBar(std::move(added_infobar));
EXPECT_FALSE(badge_consumer_.displayedBadge);
......@@ -253,8 +253,8 @@ TEST_P(BadgeMediatorTest, BadgeMediatorTestDoNotAddInfobarIfWebStateListGone) {
InsertActivatedWebState(/*index=*/0);
ASSERT_FALSE(badge_consumer_.displayedBadge);
[badge_mediator_ disconnect];
std::unique_ptr<InfoBarIOS> added_infobar = FakeInfobarIOS::Create(
kSecondInfobarType, /*has_badge=*/true, kSecondInfobarMessageText);
std::unique_ptr<InfoBarIOS> added_infobar = std::make_unique<FakeInfobarIOS>(
kSecondInfobarType, kSecondInfobarMessageText);
InfoBarManagerImpl::FromWebState(web_state_list_.GetActiveWebState())
->AddInfoBar(std::move(added_infobar));
EXPECT_FALSE(badge_consumer_.displayedBadge);
......
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