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,7 +146,7 @@ void ClickToCallContextMenuObserver::SendClickToCallMessage( ...@@ -146,7 +146,7 @@ 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],
......
...@@ -141,7 +141,7 @@ void SharedClipboardContextMenuObserver::SendSharedClipboardMessage( ...@@ -141,7 +141,7 @@ 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]);
......
...@@ -211,38 +211,20 @@ void LogSharingAppsToShow(SharingFeatureName feature, ...@@ -211,38 +211,20 @@ 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,
SharingIndexType index_type) {
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, "SelectedDeviceIndex"}), index, {"Sharing.", feature_str, "Selected",
/*value_max=*/20); (index_type == SharingIndexType::kDevice) ? "Device" : "App", "Index"});
if (!histogram_suffix) base::UmaHistogramExactLinear(name, index, /*value_max=*/20);
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);
// Explicitly log both the base and the suffixed histogram because the base
// aggregation is not automatically generated.
base::UmaHistogramExactLinear(
base::StrCat({"Sharing.", feature_str, "SelectedAppIndex"}), index,
/*value_max=*/20);
if (!histogram_suffix) if (!histogram_suffix)
return; return;
base::UmaHistogramExactLinear( base::UmaHistogramExactLinear(base::StrCat({name, ".", histogram_suffix}),
base::StrCat(
{"Sharing.", feature_str, "SelectedAppIndex.", histogram_suffix}),
index, index,
/*value_max=*/20); /*value_max=*/20);
} }
......
...@@ -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.
void LogSharingSelectedAppIndex(SharingFeatureName feature,
const char* histogram_suffix, const char* histogram_suffix,
int index); int index,
SharingIndexType index_type = SharingIndexType::kDevice);
// 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()};
if (index < data_.devices.size()) {
LogSharingSelectedDeviceIndex(data_.prefix, kSharingUiDialog, index);
std::move(data_.device_callback).Run(*data_.devices[index]);
CloseBubble();
return;
}
const bool is_device = index < data_.devices.size();
if (!is_device)
index -= data_.devices.size(); index -= data_.devices.size();
LogSharingSelectedIndex(
if (index < data_.apps.size()) { data_.prefix, kSharingUiDialog, index,
LogSharingSelectedAppIndex(data_.prefix, kSharingUiDialog, index); is_device ? SharingIndexType::kDevice : SharingIndexType::kApp);
if (is_device)
std::move(data_.device_callback).Run(*data_.devices[index]);
else
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 =
button_list->AddChildView(std::make_unique<HoverButton>(
this, std::move(icon), base::UTF8ToUTF16(device->client_name()), this, std::move(icon), base::UTF8ToUTF16(device->client_name()),
GetLastUpdatedTimeInDays(device->last_updated_timestamp())); 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);
}; };
......
...@@ -7,12 +7,13 @@ ...@@ -7,12 +7,13 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/test/bind_test_util.h" #include "base/test/bind_test_util.h"
#include "chrome/browser/sharing/fake_device_info.h" #include "chrome/browser/sharing/fake_device_info.h"
#include "chrome/browser/sharing/sharing_app.h" #include "chrome/browser/sharing/sharing_app.h"
#include "chrome/browser/sharing/sharing_metrics.h" #include "chrome/browser/sharing/sharing_metrics.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/test_with_browser_view.h"
#include "chrome/browser/ui/views/hover_button.h" #include "chrome/browser/ui/views/hover_button.h"
#include "chrome/grit/chromium_strings.h" #include "chrome/grit/chromium_strings.h"
#include "chrome/test/base/browser_with_test_window_test.h" #include "chrome/test/base/browser_with_test_window_test.h"
...@@ -28,74 +29,59 @@ ...@@ -28,74 +29,59 @@
#include "ui/views/bubble/bubble_dialog_delegate_view.h" #include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/bubble/bubble_frame_view.h" #include "ui/views/bubble/bubble_frame_view.h"
#include "ui/views/controls/styled_label.h" #include "ui/views/controls/styled_label.h"
#include "ui/views/test/button_test_api.h"
#include "url/gurl.h" #include "url/gurl.h"
#include "url/origin.h" #include "url/origin.h"
using ::testing::Property; using ::testing::Property;
namespace {
class SharingDialogViewFake : public SharingDialogView {
public:
SharingDialogViewFake(views::View* anchor_view,
content::WebContents* web_contents,
SharingDialogData data)
: SharingDialogView(anchor_view, web_contents, std::move(data)) {}
~SharingDialogViewFake() override = default;
// The delegate cannot find widget since it is created from a null profile.
// This method will be called inside ButtonPressed(). Unit tests will
// crash without mocking.
void CloseBubble() override {}
};
} // namespace
MATCHER_P(AppEquals, app, "") { MATCHER_P(AppEquals, app, "") {
return app->name == arg.name; return app->name == arg.name;
} }
class SharingDialogViewTest : public BrowserWithTestWindowTest { class SharingDialogViewTest : public TestWithBrowserView {
protected: protected:
void SetUp() override { void SetUp() override {
BrowserWithTestWindowTest::SetUp(); TestWithBrowserView::SetUp();
// We create |web_contents| to have a valid commited page origin to check // We create |web_contents_| to have a valid committed page origin to check
// against when showing the origin view. // against when showing the origin view.
GURL url("https://google.com");
web_contents_ = browser()->OpenURL(content::OpenURLParams( web_contents_ = browser()->OpenURL(content::OpenURLParams(
url, content::Referrer(), WindowOpenDisposition::CURRENT_TAB, GURL("https://google.com"), content::Referrer(),
ui::PAGE_TRANSITION_TYPED, false)); WindowOpenDisposition::CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false));
CommitPendingLoad(&web_contents_->GetController()); CommitPendingLoad(&web_contents_->GetController());
} }
void TearDown() override {
if (dialog_)
dialog_->GetWidget()->CloseNow();
TestWithBrowserView::TearDown();
}
std::vector<std::unique_ptr<syncer::DeviceInfo>> CreateDevices(int count) { std::vector<std::unique_ptr<syncer::DeviceInfo>> CreateDevices(int count) {
std::vector<std::unique_ptr<syncer::DeviceInfo>> devices; std::vector<std::unique_ptr<syncer::DeviceInfo>> devices;
for (int i = 0; i < count; i++) { for (int i = 0; i < count; ++i) {
devices.emplace_back(CreateFakeDeviceInfo( devices.push_back(
base::StrCat({"guid_", base::NumberToString(i)}), CreateFakeDeviceInfo("guid_" + base::NumberToString(i),
base::StrCat({"name_", base::NumberToString(i)}))); "name_" + base::NumberToString(i)));
} }
return devices; return devices;
} }
std::vector<SharingApp> CreateApps(int count) { std::vector<SharingApp> CreateApps(int count) {
std::vector<SharingApp> apps; std::vector<SharingApp> apps;
for (int i = 0; i < count; i++) { for (int i = 0; i < count; ++i) {
apps.emplace_back( apps.emplace_back(&vector_icons::kOpenInNewIcon, gfx::Image(),
&vector_icons::kOpenInNewIcon, gfx::Image(), base::UTF8ToUTF16("app" + base::NumberToString(i)),
base::UTF8ToUTF16(base::StrCat({"app", base::NumberToString(i)})), "app_id_" + base::NumberToString(i));
base::StrCat({"app_id_", base::NumberToString(i)}));
} }
return apps; return apps;
} }
std::unique_ptr<SharingDialogView> CreateDialogView( void CreateDialogView(SharingDialogData dialog_data) {
SharingDialogData dialog_data) { dialog_ = new SharingDialogView(browser_view(), web_contents_,
auto dialog = std::make_unique<SharingDialogViewFake>( std::move(dialog_data));
/*anchor_view=*/nullptr, web_contents_, std::move(dialog_data)); views::BubbleDialogDelegateView::CreateBubble(dialog_);
dialog->Init();
return dialog;
} }
SharingDialogData CreateDialogData(int devices, int apps) { SharingDialogData CreateDialogData(int devices, int apps) {
...@@ -129,16 +115,21 @@ class SharingDialogViewTest : public BrowserWithTestWindowTest { ...@@ -129,16 +115,21 @@ class SharingDialogViewTest : public BrowserWithTestWindowTest {
return data; return data;
} }
SharingDialogView* dialog() { return dialog_; }
testing::MockFunction<void(const syncer::DeviceInfo&)> device_callback_; testing::MockFunction<void(const syncer::DeviceInfo&)> device_callback_;
testing::MockFunction<void(const SharingApp&)> app_callback_; testing::MockFunction<void(const SharingApp&)> app_callback_;
private:
content::WebContents* web_contents_ = nullptr; content::WebContents* web_contents_ = nullptr;
SharingDialogView* dialog_ = nullptr;
}; };
TEST_F(SharingDialogViewTest, PopulateDialogView) { TEST_F(SharingDialogViewTest, PopulateDialogView) {
auto dialog_data = CreateDialogData(/*devices=*/3, /*apps=*/2); auto dialog_data = CreateDialogData(/*devices=*/3, /*apps=*/2);
auto dialog = CreateDialogView(std::move(dialog_data)); CreateDialogView(std::move(dialog_data));
EXPECT_EQ(5UL, dialog->dialog_buttons_.size()); EXPECT_EQ(5U, dialog()->button_list_for_testing()->children().size());
} }
TEST_F(SharingDialogViewTest, DevicePressed) { TEST_F(SharingDialogViewTest, DevicePressed) {
...@@ -146,13 +137,14 @@ TEST_F(SharingDialogViewTest, DevicePressed) { ...@@ -146,13 +137,14 @@ TEST_F(SharingDialogViewTest, DevicePressed) {
Call(Property(&syncer::DeviceInfo::guid, "guid_1"))); Call(Property(&syncer::DeviceInfo::guid, "guid_1")));
auto dialog_data = CreateDialogData(/*devices=*/3, /*apps=*/2); auto dialog_data = CreateDialogData(/*devices=*/3, /*apps=*/2);
auto dialog = CreateDialogView(std::move(dialog_data)); CreateDialogView(std::move(dialog_data));
const ui::MouseEvent event(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(),
ui::EventTimeForNow(), 0, 0);
// Choose second device: device0(tag=0), device1(tag=1) // Choose second device: device0(tag=0), device1(tag=1)
dialog->ButtonPressed(dialog->dialog_buttons_[1], event); const auto& buttons = dialog()->button_list_for_testing()->children();
ASSERT_EQ(5U, buttons.size());
views::test::ButtonTestApi(static_cast<views::Button*>(buttons[1]))
.NotifyClick(ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(),
gfx::Point(), ui::EventTimeForNow(), 0, 0));
} }
TEST_F(SharingDialogViewTest, AppPressed) { TEST_F(SharingDialogViewTest, AppPressed) {
...@@ -161,83 +153,95 @@ TEST_F(SharingDialogViewTest, AppPressed) { ...@@ -161,83 +153,95 @@ TEST_F(SharingDialogViewTest, AppPressed) {
EXPECT_CALL(app_callback_, Call(AppEquals(&app))); EXPECT_CALL(app_callback_, Call(AppEquals(&app)));
auto dialog_data = CreateDialogData(/*devices=*/3, /*apps=*/2); auto dialog_data = CreateDialogData(/*devices=*/3, /*apps=*/2);
auto dialog = CreateDialogView(std::move(dialog_data)); CreateDialogView(std::move(dialog_data));
const ui::MouseEvent event(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(),
ui::EventTimeForNow(), 0, 0);
// Choose first app: device0(tag=0), device1(tag=1), device2(tag=2), // Choose first app: device0(tag=0), device1(tag=1), device2(tag=2),
// app0(tag=3) // app0(tag=3)
dialog->ButtonPressed(dialog->dialog_buttons_[3], event); const auto& buttons = dialog()->button_list_for_testing()->children();
ASSERT_EQ(5U, buttons.size());
views::test::ButtonTestApi(static_cast<views::Button*>(buttons[3]))
.NotifyClick(ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(),
gfx::Point(), ui::EventTimeForNow(), 0, 0));
} }
TEST_F(SharingDialogViewTest, ThemeChangedEmptyList) { TEST_F(SharingDialogViewTest, ThemeChangedEmptyList) {
auto dialog_data = CreateDialogData(/*devices=*/1, /*apps=*/1); auto dialog_data = CreateDialogData(/*devices=*/1, /*apps=*/1);
dialog_data.type = SharingDialogType::kErrorDialog; dialog_data.type = SharingDialogType::kErrorDialog;
auto dialog = CreateDialogView(std::move(dialog_data)); CreateDialogView(std::move(dialog_data));
EXPECT_EQ(SharingDialogType::kErrorDialog, dialog->GetDialogType()); EXPECT_EQ(SharingDialogType::kErrorDialog, dialog()->GetDialogType());
// Regression test for crbug.com/1001112 // Regression test for crbug.com/1001112
dialog->OnThemeChanged(); dialog()->GetWidget()->ThemeChanged();
} }
TEST_F(SharingDialogViewTest, OriginView) { TEST_F(SharingDialogViewTest, FootnoteNoOrigin) {
auto dialog_data = CreateDialogData(/*devices=*/1, /*apps=*/1); auto dialog_data = CreateDialogData(/*devices=*/1, /*apps=*/1);
auto dialog = CreateDialogView(std::move(dialog_data)); CreateDialogView(std::move(dialog_data));
// No footnote by default if there is no initiating origin set. // No footnote by default if there is no initiating origin set.
EXPECT_EQ(nullptr, dialog->GetFootnoteViewForTesting()); EXPECT_EQ(nullptr, dialog()->GetFootnoteViewForTesting());
}
TEST_F(SharingDialogViewTest, FootnoteCurrentOrigin) {
auto dialog_data = CreateDialogData(/*devices=*/1, /*apps=*/1);
dialog_data.initiating_origin =
url::Origin::Create(GURL("https://google.com"));
CreateDialogView(std::move(dialog_data));
// No footnote if the initiating origin matches the main frame origin.
EXPECT_EQ(nullptr, dialog()->GetFootnoteViewForTesting());
}
dialog_data = CreateDialogData(/*devices=*/1, /*apps=*/1); TEST_F(SharingDialogViewTest, FootnoteOtherOrigin) {
auto dialog_data = CreateDialogData(/*devices=*/1, /*apps=*/1);
dialog_data.initiating_origin = dialog_data.initiating_origin =
url::Origin::Create(GURL("https://example.com")); url::Origin::Create(GURL("https://example.com"));
dialog = CreateDialogView(std::move(dialog_data)); CreateDialogView(std::move(dialog_data));
// Origin should be shown in the footnote if the initiating origin does not // Origin should be shown in the footnote if the initiating origin does not
// match the main frame origin. // match the main frame origin.
EXPECT_NE(nullptr, dialog->GetFootnoteViewForTesting()); EXPECT_NE(nullptr, dialog()->GetFootnoteViewForTesting());
}
dialog_data = CreateDialogData(/*devices=*/1, /*apps=*/1); TEST_F(SharingDialogViewTest, HelpTextNoOrigin) {
base::string16 expected_default = l10n_util::GetStringUTF16(
IDS_BROWSER_SHARING_CLICK_TO_CALL_DIALOG_HELP_TEXT_NO_DEVICES);
// Expect default help text if no initiating origin is set.
auto dialog_data = CreateDialogData(/*devices=*/0, /*apps=*/1);
CreateDialogView(std::move(dialog_data));
EXPECT_EQ(expected_default, static_cast<views::StyledLabel*>(
dialog()->GetFootnoteViewForTesting())
->GetText());
}
TEST_F(SharingDialogViewTest, HelpTextCurrentOrigin) {
base::string16 expected_default = l10n_util::GetStringUTF16(
IDS_BROWSER_SHARING_CLICK_TO_CALL_DIALOG_HELP_TEXT_NO_DEVICES);
// Expect default help text if the initiating origin matches the main frame
// origin.
auto dialog_data = CreateDialogData(/*devices=*/0, /*apps=*/1);
dialog_data.initiating_origin = dialog_data.initiating_origin =
url::Origin::Create(GURL("https://google.com")); url::Origin::Create(GURL("https://google.com"));
dialog = CreateDialogView(std::move(dialog_data)); CreateDialogView(std::move(dialog_data));
// Origin should not be shown in the footnote if the initiating origin does EXPECT_EQ(expected_default, static_cast<views::StyledLabel*>(
// match the main frame origin. dialog()->GetFootnoteViewForTesting())
EXPECT_EQ(nullptr, dialog->GetFootnoteViewForTesting()); ->GetText());
} }
TEST_F(SharingDialogViewTest, HelpTextContent) { TEST_F(SharingDialogViewTest, HelpTextOtherOrigin) {
url::Origin current_origin = url::Origin::Create(GURL("https://google.com"));
url::Origin other_origin = url::Origin::Create(GURL("https://example.com")); url::Origin other_origin = url::Origin::Create(GURL("https://example.com"));
base::string16 origin_text = url_formatter::FormatOriginForSecurityDisplay( base::string16 origin_text = url_formatter::FormatOriginForSecurityDisplay(
other_origin, url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS); other_origin, url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
base::string16 expected_default = l10n_util::GetStringUTF16(
IDS_BROWSER_SHARING_CLICK_TO_CALL_DIALOG_HELP_TEXT_NO_DEVICES);
base::string16 expected_origin = l10n_util::GetStringFUTF16( base::string16 expected_origin = l10n_util::GetStringFUTF16(
IDS_BROWSER_SHARING_CLICK_TO_CALL_DIALOG_HELP_TEXT_NO_DEVICES_ORIGIN, IDS_BROWSER_SHARING_CLICK_TO_CALL_DIALOG_HELP_TEXT_NO_DEVICES_ORIGIN,
origin_text); origin_text);
// Expect default help text if no initiating origin is set.
auto dialog_data = CreateDialogData(/*devices=*/0, /*apps=*/1);
auto dialog = CreateDialogView(std::move(dialog_data));
views::View* footnote_view = dialog->GetFootnoteViewForTesting();
EXPECT_EQ(expected_default,
static_cast<views::StyledLabel*>(footnote_view)->GetText());
// Still expect the default help text if the initiating origin matches the
// main frame origin.
dialog_data = CreateDialogData(/*devices=*/0, /*apps=*/1);
dialog_data.initiating_origin = current_origin;
dialog = CreateDialogView(std::move(dialog_data));
footnote_view = dialog->GetFootnoteViewForTesting();
EXPECT_EQ(expected_default,
static_cast<views::StyledLabel*>(footnote_view)->GetText());
// Expect the origin to be included in the help text if it does not match the // Expect the origin to be included in the help text if it does not match the
// main frame origin. // main frame origin.
dialog_data = CreateDialogData(/*devices=*/0, /*apps=*/1); auto dialog_data = CreateDialogData(/*devices=*/0, /*apps=*/1);
dialog_data.initiating_origin = other_origin; dialog_data.initiating_origin = other_origin;
dialog = CreateDialogView(std::move(dialog_data)); CreateDialogView(std::move(dialog_data));
footnote_view = dialog->GetFootnoteViewForTesting(); EXPECT_EQ(expected_origin, static_cast<views::StyledLabel*>(
EXPECT_EQ(expected_origin, dialog()->GetFootnoteViewForTesting())
static_cast<views::StyledLabel*>(footnote_view)->GetText()); ->GetText());
} }
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