Commit 9361aa0a authored by Richard Knoll's avatar Richard Knoll Committed by Commit Bot

[Nearby] Handle ConnectionRequest notification actions

This hooks up notification actions from ConnectionRequest notifications
to the NearbySharingService.

Bug: 1102348
Change-Id: I652449568893ca215925562e3cd50b7bd79fb901
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2335174
Commit-Queue: Richard Knoll <knollr@chromium.org>
Reviewed-by: default avatarAlex Chau <alexchau@chromium.org>
Cr-Commit-Position: refs/heads/master@{#794279}
parent b9b876c3
......@@ -4,6 +4,7 @@
#include "chrome/browser/nearby_sharing/nearby_notification_manager.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
......@@ -198,6 +199,49 @@ class ProgressNotificationDelegate : public NearbyNotificationDelegate {
NearbyNotificationManager* manager_;
};
class ConnectionRequestNotificationDelegate
: public NearbyNotificationDelegate {
public:
ConnectionRequestNotificationDelegate(NearbyNotificationManager* manager,
bool has_accept_button)
: manager_(manager), has_accept_button_(has_accept_button) {}
~ConnectionRequestNotificationDelegate() override = default;
// NearbyNotificationDelegate:
void OnClick(const std::string& notification_id,
const base::Optional<int>& action_index) override {
// Clicking on the notification is a noop.
if (!action_index)
return;
if (!has_accept_button_) {
DCHECK_EQ(0, *action_index);
manager_->RejectTransfer();
return;
}
switch (*action_index) {
case 0:
manager_->AcceptTransfer();
break;
case 1:
manager_->RejectTransfer();
break;
default:
NOTREACHED();
break;
}
}
void OnClose(const std::string& notification_id) override {
manager_->RejectTransfer();
}
private:
NearbyNotificationManager* manager_;
bool has_accept_button_;
};
} // namespace
NearbyNotificationManager::NearbyNotificationManager(
......@@ -312,9 +356,12 @@ void NearbyNotificationManager::ShowConnectionRequest(
notification.set_icon(GetImageFromShareTarget(share_target));
notification.set_never_timeout(true);
bool show_accept_button =
transfer_metadata.status() ==
TransferMetadata::Status::kAwaitingLocalConfirmation;
std::vector<message_center::ButtonInfo> notification_actions;
if (transfer_metadata.status() ==
TransferMetadata::Status::kAwaitingLocalConfirmation) {
if (show_accept_button) {
notification_actions.emplace_back(
l10n_util::GetStringUTF16(IDS_NEARBY_NOTIFICATION_RECEIVE_ACTION));
}
......@@ -322,8 +369,9 @@ void NearbyNotificationManager::ShowConnectionRequest(
l10n_util::GetStringUTF16(IDS_NEARBY_NOTIFICATION_DECLINE_ACTION));
notification.set_buttons(notification_actions);
// TODO(crbug.com/1102348): Handle ConnectionRequest actions.
delegate_map_.erase(kNearbyNotificationId);
delegate_map_[notification.id()] =
std::make_unique<ConnectionRequestNotificationDelegate>(
this, show_accept_button);
notification_display_service_->Display(
NotificationHandler::Type::NEARBY_SHARE, notification,
......@@ -393,3 +441,13 @@ void NearbyNotificationManager::CancelTransfer() {
CloseTransfer();
nearby_service_->Cancel(*share_target_, base::DoNothing());
}
void NearbyNotificationManager::RejectTransfer() {
CloseTransfer();
nearby_service_->Reject(*share_target_, base::DoNothing());
}
void NearbyNotificationManager::AcceptTransfer() {
// Do not close the notification as it will be replaced soon.
nearby_service_->Accept(*share_target_, base::DoNothing());
}
......@@ -70,6 +70,12 @@ class NearbyNotificationManager : public TransferUpdateCallback,
// Cancels the currently in progress transfer.
void CancelTransfer();
// Rejects the currently in progress transfer.
void RejectTransfer();
// Accepts the currently in progress transfer.
void AcceptTransfer();
private:
NotificationDisplayService* notification_display_service_;
NearbySharingService* nearby_service_;
......
......@@ -619,3 +619,118 @@ TEST_F(NearbyNotificationManagerTest, ProgressNotification_Cancelled) {
// Notification should be closed.
EXPECT_EQ(0u, GetDisplayedNotifications().size());
}
TEST_F(NearbyNotificationManagerTest, ConnectionRequest_Accept) {
ShareTarget share_target;
share_target.is_incoming = true;
TransferMetadata transfer_metadata =
TransferMetadataBuilder()
.set_status(TransferMetadata::Status::kAwaitingLocalConfirmation)
.build();
// Simulate incoming connection request waiting for local confirmation.
manager()->OnTransferUpdate(share_target, transfer_metadata);
// Expect a notification with an accept button.
std::vector<message_center::Notification> notifications =
GetDisplayedNotifications();
ASSERT_EQ(1u, notifications.size());
ASSERT_EQ(2u, notifications[0].buttons().size());
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_NEARBY_NOTIFICATION_RECEIVE_ACTION),
notifications[0].buttons()[0].title);
// Expect call to Accept on button click.
EXPECT_CALL(*nearby_service_,
Accept(MatchesTarget(share_target), testing::_));
notification_tester_->SimulateClick(NotificationHandler::Type::NEARBY_SHARE,
notifications[0].id(), /*action_index=*/0,
/*reply=*/base::nullopt);
// Notification should still be present as it will soon be replaced.
EXPECT_EQ(1u, GetDisplayedNotifications().size());
}
TEST_F(NearbyNotificationManagerTest, ConnectionRequest_Reject_Local) {
ShareTarget share_target;
share_target.is_incoming = true;
TransferMetadata transfer_metadata =
TransferMetadataBuilder()
.set_status(TransferMetadata::Status::kAwaitingLocalConfirmation)
.build();
// Simulate incoming connection request waiting for local confirmation.
manager()->OnTransferUpdate(share_target, transfer_metadata);
// Expect a notification with a reject button.
std::vector<message_center::Notification> notifications =
GetDisplayedNotifications();
ASSERT_EQ(1u, notifications.size());
ASSERT_EQ(2u, notifications[0].buttons().size());
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_NEARBY_NOTIFICATION_DECLINE_ACTION),
notifications[0].buttons()[1].title);
// Expect call to Reject on button click.
EXPECT_CALL(*nearby_service_,
Reject(MatchesTarget(share_target), testing::_));
notification_tester_->SimulateClick(NotificationHandler::Type::NEARBY_SHARE,
notifications[0].id(), /*action_index=*/1,
/*reply=*/base::nullopt);
// Notification should be closed on button click.
EXPECT_EQ(0u, GetDisplayedNotifications().size());
}
TEST_F(NearbyNotificationManagerTest, ConnectionRequest_Reject_Remote) {
ShareTarget share_target;
share_target.is_incoming = true;
TransferMetadata transfer_metadata =
TransferMetadataBuilder()
.set_status(TransferMetadata::Status::kAwaitingRemoteAcceptance)
.build();
// Simulate incoming connection request waiting for remote acceptance.
manager()->OnTransferUpdate(share_target, transfer_metadata);
// Expect a notification with only the reject button.
std::vector<message_center::Notification> notifications =
GetDisplayedNotifications();
ASSERT_EQ(1u, notifications.size());
ASSERT_EQ(1u, notifications[0].buttons().size());
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_NEARBY_NOTIFICATION_DECLINE_ACTION),
notifications[0].buttons()[0].title);
// Expect call to Reject on button click.
EXPECT_CALL(*nearby_service_,
Reject(MatchesTarget(share_target), testing::_));
notification_tester_->SimulateClick(NotificationHandler::Type::NEARBY_SHARE,
notifications[0].id(), /*action_index=*/0,
/*reply=*/base::nullopt);
// Notification should be closed on button click.
EXPECT_EQ(0u, GetDisplayedNotifications().size());
}
TEST_F(NearbyNotificationManagerTest, ConnectionRequest_Close) {
ShareTarget share_target;
share_target.is_incoming = true;
TransferMetadata transfer_metadata =
TransferMetadataBuilder()
.set_status(TransferMetadata::Status::kAwaitingLocalConfirmation)
.build();
// Simulate incoming connection request waiting for local confirmation.
manager()->OnTransferUpdate(share_target, transfer_metadata);
std::vector<message_center::Notification> notifications =
GetDisplayedNotifications();
ASSERT_EQ(1u, notifications.size());
// Expect call to Reject on notification close.
EXPECT_CALL(*nearby_service_,
Reject(MatchesTarget(share_target), testing::_));
notification_tester_->RemoveNotification(
NotificationHandler::Type::NEARBY_SHARE, notifications[0].id(),
/*by_user=*/true);
// Notification should be closed.
EXPECT_EQ(0u, GetDisplayedNotifications().size());
}
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