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

Clean up SharingDialogView and avoid needing friend decls for testing.

Some of this cleanup will facilitate replacing ButtonListener with
callbacks in a future CL.

Bug: none
Change-Id: Ieefc3be0238bdaa95fc171b7ee7c55e4f8e41a90
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2472899
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarRichard Knoll <knollr@chromium.org>
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#818987}
parent f36eb8bd
...@@ -146,8 +146,8 @@ void ClickToCallContextMenuObserver::SendClickToCallMessage( ...@@ -146,8 +146,8 @@ void ClickToCallContextMenuObserver::SendClickToCallMessage(
if (size_t{chosen_device_index} >= devices_.size()) if (size_t{chosen_device_index} >= devices_.size())
return; return;
LogSharingSelectedDeviceIndex(controller_->GetFeatureMetricsPrefix(), LogSharingSelectedIndex(controller_->GetFeatureMetricsPrefix(),
kSharingUiContextMenu, chosen_device_index); kSharingUiContextMenu, chosen_device_index);
controller_->OnDeviceSelected(phone_number_, *devices_[chosen_device_index], controller_->OnDeviceSelected(phone_number_, *devices_[chosen_device_index],
*entry_point_); *entry_point_);
......
...@@ -141,8 +141,8 @@ void SharedClipboardContextMenuObserver::SendSharedClipboardMessage( ...@@ -141,8 +141,8 @@ void SharedClipboardContextMenuObserver::SendSharedClipboardMessage(
int chosen_device_index) { int chosen_device_index) {
if (size_t{chosen_device_index} >= devices_.size()) if (size_t{chosen_device_index} >= devices_.size())
return; return;
LogSharingSelectedDeviceIndex(controller_->GetFeatureMetricsPrefix(), LogSharingSelectedIndex(controller_->GetFeatureMetricsPrefix(),
nullptr /* No suffix */, chosen_device_index); nullptr /* No suffix */, chosen_device_index);
controller_->OnDeviceSelected(text_, *devices_[chosen_device_index]); controller_->OnDeviceSelected(text_, *devices_[chosen_device_index]);
LogSharedClipboardSelectedTextSize(text_.size()); LogSharedClipboardSelectedTextSize(text_.size());
......
...@@ -211,40 +211,22 @@ void LogSharingAppsToShow(SharingFeatureName feature, ...@@ -211,40 +211,22 @@ void LogSharingAppsToShow(SharingFeatureName feature,
/*value_max=*/20); /*value_max=*/20);
} }
void LogSharingSelectedDeviceIndex(SharingFeatureName feature, void LogSharingSelectedIndex(SharingFeatureName feature,
const char* histogram_suffix, const char* histogram_suffix,
int index) { int index,
auto* feature_str = GetEnumStringValue(feature); SharingIndexType index_type) {
// Explicitly log both the base and the suffixed histogram because the base
// aggregation is not automatically generated.
base::UmaHistogramExactLinear(
base::StrCat({"Sharing.", feature_str, "SelectedDeviceIndex"}), index,
/*value_max=*/20);
if (!histogram_suffix)
return;
base::UmaHistogramExactLinear(
base::StrCat(
{"Sharing.", feature_str, "SelectedDeviceIndex.", histogram_suffix}),
index,
/*value_max=*/20);
}
void LogSharingSelectedAppIndex(SharingFeatureName feature,
const char* histogram_suffix,
int index) {
auto* feature_str = GetEnumStringValue(feature); auto* feature_str = GetEnumStringValue(feature);
// Explicitly log both the base and the suffixed histogram because the base // Explicitly log both the base and the suffixed histogram because the base
// aggregation is not automatically generated. // aggregation is not automatically generated.
base::UmaHistogramExactLinear( std::string name = base::StrCat(
base::StrCat({"Sharing.", feature_str, "SelectedAppIndex"}), index, {"Sharing.", feature_str, "Selected",
/*value_max=*/20); (index_type == SharingIndexType::kDevice) ? "Device" : "App", "Index"});
base::UmaHistogramExactLinear(name, index, /*value_max=*/20);
if (!histogram_suffix) if (!histogram_suffix)
return; return;
base::UmaHistogramExactLinear( base::UmaHistogramExactLinear(base::StrCat({name, ".", histogram_suffix}),
base::StrCat( index,
{"Sharing.", feature_str, "SelectedAppIndex.", histogram_suffix}), /*value_max=*/20);
index,
/*value_max=*/20);
} }
void LogSharingMessageAckTime(chrome_browser_sharing::MessageType message_type, void LogSharingMessageAckTime(chrome_browser_sharing::MessageType message_type,
......
...@@ -79,21 +79,20 @@ void LogSharingAppsToShow(SharingFeatureName feature, ...@@ -79,21 +79,20 @@ void LogSharingAppsToShow(SharingFeatureName feature,
const char* histogram_suffix, const char* histogram_suffix,
int count); int count);
// Logs the |index| of the device selected by the user for sharing feature. The // Logs the |index| of the user selection for sharing feature. |index_type| is
// |histogram_suffix| indicates in which UI this event happened and must match // the type of selection made, either "Device" or "App". The |histogram_suffix|
// one from Sharing{feature}Ui defined in histograms.xml - use the // indicates in which UI this event happened and must match one from
// constants defined in this file for that. // Sharing{feature}Ui defined in histograms.xml - use the constants defined in
void LogSharingSelectedDeviceIndex(SharingFeatureName feature, // this file for that.
const char* histogram_suffix, enum class SharingIndexType {
int index); kDevice,
kApp,
// Logs the |index| of the app selected by the user for sharing feature. The };
// |histogram_suffix| indicates in which UI this event happened and must match void LogSharingSelectedIndex(
// one from Sharing{feature}Ui defined in histograms.xml - use the SharingFeatureName feature,
// constants defined in this file for that. const char* histogram_suffix,
void LogSharingSelectedAppIndex(SharingFeatureName feature, int index,
const char* histogram_suffix, SharingIndexType index_type = SharingIndexType::kDevice);
int index);
// Logs to UMA the time from sending a FCM message from the Sharing service // Logs to UMA the time from sending a FCM message from the Sharing service
// until an ack message is received for it. // until an ack message is received for it.
......
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "ui/events/base_event_utils.h" #include "ui/events/base_event_utils.h"
#include "ui/views/layout/grid_layout.h" #include "ui/views/layout/grid_layout.h"
#include "ui/views/test/button_test_api.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace { namespace {
...@@ -402,15 +403,13 @@ IN_PROC_BROWSER_TEST_F(ClickToCallBrowserTest, LeftClick_ChooseDevice) { ...@@ -402,15 +403,13 @@ IN_PROC_BROWSER_TEST_F(ClickToCallBrowserTest, LeftClick_ChooseDevice) {
static_cast<SharingDialogView*>(controller->dialog()); static_cast<SharingDialogView*>(controller->dialog());
EXPECT_EQ(SharingDialogType::kDialogWithDevicesMaybeApps, EXPECT_EQ(SharingDialogType::kDialogWithDevicesMaybeApps,
dialog->GetDialogType()); dialog->GetDialogType());
EXPECT_EQ(1u, dialog->data_.devices.size());
EXPECT_EQ(dialog->data_.devices.size() + dialog->data_.apps.size(),
dialog->dialog_buttons_.size());
const ui::MouseEvent event(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(),
ui::EventTimeForNow(), 0, 0);
// Choose first device. // Choose first device.
dialog->ButtonPressed(dialog->dialog_buttons_[0], event); const auto& buttons = dialog->button_list_for_testing()->children();
ASSERT_GT(buttons.size(), 0u);
views::test::ButtonTestApi(static_cast<views::Button*>(buttons[0]))
.NotifyClick(ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(),
gfx::Point(), ui::EventTimeForNow(), 0, 0));
CheckLastReceiver(*devices[0]); CheckLastReceiver(*devices[0]);
// Defined in tel.html // Defined in tel.html
......
...@@ -175,26 +175,21 @@ SharingDialogType SharingDialogView::GetDialogType() const { ...@@ -175,26 +175,21 @@ SharingDialogType SharingDialogView::GetDialogType() const {
void SharingDialogView::ButtonPressed(views::Button* sender, void SharingDialogView::ButtonPressed(views::Button* sender,
const ui::Event& event) { const ui::Event& event) {
DCHECK(data_.device_callback); DCHECK(sender);
DCHECK(data_.app_callback); size_t index = sender->tag();
if (!sender || sender->tag() < 0) DCHECK_LT(index, data_.devices.size() + data_.apps.size());
return;
size_t index{sender->tag()}; const bool is_device = index < data_.devices.size();
if (!is_device)
if (index < data_.devices.size()) { index -= data_.devices.size();
LogSharingSelectedDeviceIndex(data_.prefix, kSharingUiDialog, index); LogSharingSelectedIndex(
data_.prefix, kSharingUiDialog, index,
is_device ? SharingIndexType::kDevice : SharingIndexType::kApp);
if (is_device)
std::move(data_.device_callback).Run(*data_.devices[index]); std::move(data_.device_callback).Run(*data_.devices[index]);
CloseBubble(); else
return;
}
index -= data_.devices.size();
if (index < data_.apps.size()) {
LogSharingSelectedAppIndex(data_.prefix, kSharingUiDialog, index);
std::move(data_.app_callback).Run(data_.apps[index]); std::move(data_.app_callback).Run(data_.apps[index]);
CloseBubble(); CloseBubble();
}
} }
// static // static
...@@ -271,14 +266,13 @@ void SharingDialogView::InitListView() { ...@@ -271,14 +266,13 @@ void SharingDialogView::InitListView() {
: kHardwareSmartphoneIcon, : kHardwareSmartphoneIcon,
kPrimaryIconSize); kPrimaryIconSize);
auto dialog_button = std::make_unique<HoverButton>( auto* dialog_button =
this, std::move(icon), base::UTF8ToUTF16(device->client_name()), button_list->AddChildView(std::make_unique<HoverButton>(
GetLastUpdatedTimeInDays(device->last_updated_timestamp())); this, std::move(icon), base::UTF8ToUTF16(device->client_name()),
GetLastUpdatedTimeInDays(device->last_updated_timestamp())));
dialog_button->SetEnabled(true); dialog_button->SetEnabled(true);
dialog_button->set_tag(tag++); dialog_button->set_tag(tag++);
dialog_button->SetBorder(views::CreateEmptyBorder(device_border)); dialog_button->SetBorder(views::CreateEmptyBorder(device_border));
dialog_buttons_.push_back(
button_list->AddChildView(std::move(dialog_button)));
} }
// Apps: // Apps:
...@@ -293,32 +287,32 @@ void SharingDialogView::InitListView() { ...@@ -293,32 +287,32 @@ void SharingDialogView::InitListView() {
icon->SetImage(app.image.AsImageSkia()); icon->SetImage(app.image.AsImageSkia());
} }
auto dialog_button = auto* dialog_button = button_list->AddChildView(
std::make_unique<HoverButton>(this, std::move(icon), app.name, std::make_unique<HoverButton>(this, std::move(icon), app.name,
/* subtitle= */ base::string16()); /* subtitle= */ base::string16()));
dialog_button->SetEnabled(true); dialog_button->SetEnabled(true);
dialog_button->set_tag(tag++); dialog_button->set_tag(tag++);
dialog_button->SetBorder(views::CreateEmptyBorder(app_border)); dialog_button->SetBorder(views::CreateEmptyBorder(app_border));
dialog_buttons_.push_back(
button_list->AddChildView(std::move(dialog_button)));
} }
// Allow up to 5 buttons in the list and let the rest scroll. // Allow up to 5 buttons in the list and let the rest scroll.
constexpr size_t kMaxDialogButtons = 5; constexpr size_t kMaxDialogButtons = 5;
if (dialog_buttons_.size() > kMaxDialogButtons) { if (button_list->children().size() > kMaxDialogButtons) {
const int bubble_width = ChromeLayoutProvider::Get()->GetDistanceMetric( const int bubble_width = ChromeLayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_BUBBLE_PREFERRED_WIDTH); views::DISTANCE_BUBBLE_PREFERRED_WIDTH);
int max_list_height = 0; int max_list_height = 0;
for (size_t i = 0; i < kMaxDialogButtons; ++i) for (size_t i = 0; i < kMaxDialogButtons; ++i) {
max_list_height += dialog_buttons_[i]->GetHeightForWidth(bubble_width); max_list_height +=
button_list->children()[i]->GetHeightForWidth(bubble_width);
}
DCHECK_GT(max_list_height, 0); DCHECK_GT(max_list_height, 0);
auto* scroll_view = AddChildView(std::make_unique<views::ScrollView>()); auto* scroll_view = AddChildView(std::make_unique<views::ScrollView>());
scroll_view->ClipHeightTo(0, max_list_height); scroll_view->ClipHeightTo(0, max_list_height);
scroll_view->SetContents(std::move(button_list)); button_list_ = scroll_view->SetContents(std::move(button_list));
} else { } else {
AddChildView(std::move(button_list)); button_list_ = AddChildView(std::move(button_list));
} }
} }
......
...@@ -18,7 +18,6 @@ class StyledLabel; ...@@ -18,7 +18,6 @@ class StyledLabel;
class View; class View;
} // namespace views } // namespace views
class HoverButton;
enum class SharingDialogType; enum class SharingDialogType;
class SharingDialogView : public SharingDialog, class SharingDialogView : public SharingDialog,
...@@ -46,22 +45,15 @@ class SharingDialogView : public SharingDialog, ...@@ -46,22 +45,15 @@ class SharingDialogView : public SharingDialog,
void ButtonPressed(views::Button* sender, const ui::Event& event) override; void ButtonPressed(views::Button* sender, const ui::Event& event) override;
static views::BubbleDialogDelegateView* GetAsBubble(SharingDialog* dialog); static views::BubbleDialogDelegateView* GetAsBubble(SharingDialog* dialog);
static views::BubbleDialogDelegateView* GetAsBubbleForClickToCall( static views::BubbleDialogDelegateView* GetAsBubbleForClickToCall(
SharingDialog* dialog); SharingDialog* dialog);
private: SharingDialogType GetDialogType() const;
friend class SharingDialogViewTest;
FRIEND_TEST_ALL_PREFIXES(SharingDialogViewTest, PopulateDialogView);
FRIEND_TEST_ALL_PREFIXES(SharingDialogViewTest, DevicePressed);
FRIEND_TEST_ALL_PREFIXES(SharingDialogViewTest, AppPressed);
FRIEND_TEST_ALL_PREFIXES(SharingDialogViewTest, HelpTextClickedEmpty);
FRIEND_TEST_ALL_PREFIXES(SharingDialogViewTest, HelpTextClickedOnlyApps);
FRIEND_TEST_ALL_PREFIXES(SharingDialogViewTest, ThemeChangedEmptyList);
FRIEND_TEST_ALL_PREFIXES(ClickToCallBrowserTest, LeftClick_ChooseDevice); const View* button_list_for_testing() const { return button_list_; }
SharingDialogType GetDialogType() const; private:
friend class SharingDialogViewTest;
// LocationBarBubbleDelegateView: // LocationBarBubbleDelegateView:
void Init() override; void Init() override;
...@@ -76,7 +68,7 @@ class SharingDialogView : public SharingDialog, ...@@ -76,7 +68,7 @@ class SharingDialogView : public SharingDialog,
SharingDialogData data_; SharingDialogData data_;
// References to device and app buttons views. // References to device and app buttons views.
std::vector<HoverButton*> dialog_buttons_; View* button_list_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(SharingDialogView); DISALLOW_COPY_AND_ASSIGN(SharingDialogView);
}; };
......
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