Commit c6d97a24 authored by Monica Basta's avatar Monica Basta Committed by Commit Bot

[Settings]: Add generic colored avatar to mange profile.

This CL adds the generic colored avatar to the avatar icons shown on
'chrome://settings/manageProfile' that is only shown for local profile
(No GAIA account).

It also fixes the bug where no selected is shown when the user is using
the GAIA avatar as the chrome avatar.

In a future CL, |kProfileUsingDefaultAvatar, kProfileUsingGAIAAvatar|
prefs will be deleted. The |ManageProfileHandler| should not rely on
these prefs.

Fixed: 1149539
Change-Id: I9cbce03e62efb20a6c466eac9c7e32d1cbc93f18
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2542062
Commit-Queue: Monica Basta <msalama@chromium.org>
Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Reviewed-by: default avatarEsmael Elmoslimany <aee@chromium.org>
Cr-Commit-Position: refs/heads/master@{#829194}
parent 57ea9b92
......@@ -607,11 +607,6 @@ void ProfileAttributesEntry::SetIsAuthError(bool value) {
}
void ProfileAttributesEntry::SetAvatarIconIndex(size_t icon_index) {
if (!profiles::IsDefaultAvatarIconIndex(icon_index)) {
DLOG(WARNING) << "Unknown avatar icon index: " << icon_index;
// switch to generic avatar
icon_index = 0;
}
std::string default_avatar_icon_url =
profiles::GetDefaultAvatarIconUrl(icon_index);
if (default_avatar_icon_url == GetString(kAvatarIconKey)) {
......
......@@ -34,6 +34,7 @@
#include "third_party/skia/include/core/SkScalar.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/webui/web_ui_util.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/favicon_size.h"
......@@ -551,7 +552,7 @@ const IconResourceInfo* GetDefaultAvatarIconResourceInfo(size_t index) {
IDS_DEFAULT_AVATAR_LABEL_25},
#endif
// Placeholder avatar icon:
{IDR_PROFILE_AVATAR_26, NULL, -1},
{IDR_PROFILE_AVATAR_26, nullptr, IDS_DEFAULT_AVATAR_LABEL_26},
#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
// Modern avatar icons:
......@@ -663,7 +664,6 @@ std::string GetDefaultAvatarIconUrl(size_t index) {
}
int GetDefaultAvatarLabelResourceIDAtIndex(size_t index) {
CHECK_NE(index, kPlaceholderAvatarIndex);
return GetDefaultAvatarIconResourceInfo(index)->label_id;
}
......@@ -691,22 +691,49 @@ bool IsDefaultAvatarIconUrl(const std::string& url, size_t* icon_index) {
return false;
}
std::unique_ptr<base::ListValue> GetDefaultProfileAvatarIconsAndLabels(
std::unique_ptr<base::DictionaryValue> GetAvatarIconAndLabelDict(
const std::string& url,
const base::string16& label,
size_t index,
bool selected,
bool is_gaia_avatar) {
std::unique_ptr<base::DictionaryValue> avatar_info(
new base::DictionaryValue());
avatar_info->SetStringPath("url", url);
avatar_info->SetStringPath("label", label);
avatar_info->SetIntPath("index", index);
avatar_info->SetBoolPath("selected", selected);
avatar_info->SetBoolPath("isGaiaAvatar", is_gaia_avatar);
return avatar_info;
}
std::unique_ptr<base::DictionaryValue> GetDefaultProfileAvatarIconAndLabel(
SkColor fill_color,
SkColor stroke_color,
bool selected) {
std::unique_ptr<base::DictionaryValue> avatar_info(
new base::DictionaryValue());
gfx::Image icon = profiles::GetPlaceholderAvatarIconWithColors(
fill_color, stroke_color, kAvatarIconSize);
size_t index = profiles::GetPlaceholderAvatarIndex();
return GetAvatarIconAndLabelDict(
webui::GetBitmapDataUrl(icon.AsBitmap()),
l10n_util::GetStringUTF16(
profiles::GetDefaultAvatarLabelResourceIDAtIndex(index)),
index, selected, /*is_gaia_avatar=*/false);
}
std::unique_ptr<base::ListValue> GetCustomProfileAvatarIconsAndLabels(
size_t selected_avatar_idx) {
std::unique_ptr<base::ListValue> avatars(new base::ListValue());
for (size_t i = GetModernAvatarIconStartIndex();
i < GetDefaultAvatarIconCount(); ++i) {
std::unique_ptr<base::DictionaryValue> avatar_info(
new base::DictionaryValue());
avatar_info->SetString("url", profiles::GetDefaultAvatarIconUrl(i));
avatar_info->SetString(
"label", l10n_util::GetStringUTF16(
profiles::GetDefaultAvatarLabelResourceIDAtIndex(i)));
if (i == selected_avatar_idx)
avatar_info->SetBoolean("selected", true);
avatars->Append(std::move(avatar_info));
avatars->Append(GetAvatarIconAndLabelDict(
profiles::GetDefaultAvatarIconUrl(i),
l10n_util::GetStringUTF16(
profiles::GetDefaultAvatarLabelResourceIDAtIndex(i)),
i, i == selected_avatar_idx, /*is_gaia_avatar=*/false));
}
return avatars;
}
......
......@@ -150,11 +150,27 @@ bool IsDefaultAvatarIconIndex(size_t index);
// is, returns true and its index through |icon_index|. If not, returns false.
bool IsDefaultAvatarIconUrl(const std::string& icon_url, size_t *icon_index);
// Returns a list of dictionaries containing the default profile avatar icons as
// Returns Dict containing the avatar icon info in the format expected by the
// WebUI component 'cr-profile-avatar-selector'.
std::unique_ptr<base::DictionaryValue> GetAvatarIconAndLabelDict(
const std::string& url,
const base::string16& label,
size_t index,
bool selected,
bool is_gaia_avatar);
// Returns Dict containing the default generic avatar icon, label, index and
// selected state.
std::unique_ptr<base::DictionaryValue> GetDefaultProfileAvatarIconAndLabel(
SkColor fill_color,
SkColor stroke_color,
bool selected);
// Returns a list of dictionaries containing modern profile avatar icons as
// well as avatar labels used for accessibility purposes. The list is ordered
// according to the avatars' default order. If |selected_avatar_idx| is one of
// the available indices, the corresponding avatar is marked as selected.
std::unique_ptr<base::ListValue> GetDefaultProfileAvatarIconsAndLabels(
std::unique_ptr<base::ListValue> GetCustomProfileAvatarIconsAndLabels(
size_t selected_avatar_idx = SIZE_MAX);
// This method tries to find a random avatar index that is not in
......
......@@ -176,7 +176,8 @@ Polymer({
if (this.profileAvatar_.isGaiaAvatar) {
this.browserProxy_.setProfileIconToGaiaAvatar();
} else {
this.browserProxy_.setProfileIconToDefaultAvatar(this.profileAvatar_.url);
this.browserProxy_.setProfileIconToDefaultAvatar(
this.profileAvatar_.index);
}
},
......
......@@ -38,9 +38,9 @@ export class ManageProfileBrowserProxy {
/**
* Sets the profile's icon to one of the default avatars.
* @param {string} iconUrl The new profile URL.
* @param {number} index The new profile avatar index.
*/
setProfileIconToDefaultAvatar(iconUrl) {}
setProfileIconToDefaultAvatar(index) {}
/**
* Sets the profile's name.
......@@ -80,8 +80,8 @@ export class ManageProfileBrowserProxyImpl {
}
/** @override */
setProfileIconToDefaultAvatar(iconUrl) {
chrome.send('setProfileIconToDefaultAvatar', [iconUrl]);
setProfileIconToDefaultAvatar(index) {
chrome.send('setProfileIconToDefaultAvatar', [index]);
}
/** @override */
......
......@@ -23,6 +23,8 @@
#include "chrome/browser/profiles/profile_window.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/signin/profile_colors_util.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/generated_resources.h"
......@@ -94,16 +96,28 @@ void ManageProfileHandler::OnJavascriptDisallowed() {
void ManageProfileHandler::OnProfileHighResAvatarLoaded(
const base::FilePath& profile_path) {
if (profile_path != profile_->GetPath())
return;
// GAIA image is loaded asynchronously.
FireWebUIListener("available-icons-changed", *GetAvailableIcons());
}
void ManageProfileHandler::OnProfileAvatarChanged(
const base::FilePath& profile_path) {
if (profile_path != profile_->GetPath())
return;
// This is necessary to send the potentially updated GAIA photo.
FireWebUIListener("available-icons-changed", *GetAvailableIcons());
}
void ManageProfileHandler::OnProfileThemeColorsChanged(
const base::FilePath& profile_path) {
// This is necessary to send the potentially updated Generic colored avatar.
OnProfileAvatarChanged(profile_path);
}
void ManageProfileHandler::HandleGetAvailableIcons(
const base::ListValue* args) {
AllowJavascript();
......@@ -117,35 +131,47 @@ void ManageProfileHandler::HandleGetAvailableIcons(
}
std::unique_ptr<base::ListValue> ManageProfileHandler::GetAvailableIcons() {
PrefService* pref_service = profile_->GetPrefs();
bool using_gaia = pref_service->GetBoolean(prefs::kProfileUsingGAIAAvatar);
ProfileAttributesEntry* entry = nullptr;
// TODO(msalama): Convert to a DCHECK.
if (!g_browser_process->profile_manager()
->GetProfileAttributesStorage()
.GetProfileAttributesWithPath(profile_->GetPath(), &entry)) {
LOG(ERROR) << "No profile attributes entry found for profile with path: "
<< profile_->GetPath();
return std::make_unique<base::ListValue>();
}
bool using_gaia = entry->IsUsingGAIAPicture();
size_t selected_avatar_idx =
using_gaia ? SIZE_MAX
: pref_service->GetInteger(prefs::kProfileAvatarIndex);
using_gaia ? SIZE_MAX : entry->GetAvatarIconIndex();
// Obtain a list of the default avatar icons.
// Obtain a list of the modern avatar icons.
std::unique_ptr<base::ListValue> avatars(
profiles::GetDefaultProfileAvatarIconsAndLabels(selected_avatar_idx));
profiles::GetCustomProfileAvatarIconsAndLabels(selected_avatar_idx));
if (entry->GetSigninState() == SigninState::kNotSignedIn) {
if (base::FeatureList::IsEnabled(features::kNewProfilePicker) &&
base::FeatureList::IsEnabled(features::kProfilesUIRevamp)) {
ProfileThemeColors colors = entry->GetProfileThemeColors();
auto generic_avatar_info = profiles::GetDefaultProfileAvatarIconAndLabel(
colors.default_avatar_fill_color, colors.default_avatar_stroke_color,
selected_avatar_idx == profiles::GetPlaceholderAvatarIndex());
avatars->Insert(0, std::move(generic_avatar_info));
}
return avatars;
}
// Add the GAIA picture to the beginning of the list if it is available.
ProfileAttributesEntry* entry;
if (g_browser_process->profile_manager()->GetProfileAttributesStorage().
GetProfileAttributesWithPath(profile_->GetPath(), &entry)) {
const gfx::Image* icon = entry->GetGAIAPicture();
if (icon) {
auto gaia_picture_info = std::make_unique<base::DictionaryValue>();
gfx::Image avatar_icon = profiles::GetAvatarIconForWebUI(*icon, true);
gaia_picture_info->SetString(
"url", webui::GetBitmapDataUrl(avatar_icon.AsBitmap()));
gaia_picture_info->SetString(
"label",
l10n_util::GetStringUTF16(IDS_SETTINGS_CHANGE_PICTURE_PROFILE_PHOTO));
gaia_picture_info->SetBoolean("isGaiaAvatar", true);
if (using_gaia)
gaia_picture_info->SetBoolean("selected", true);
auto gaia_picture_info = profiles::GetAvatarIconAndLabelDict(
/*url=*/webui::GetBitmapDataUrl(avatar_icon.AsBitmap()),
/*label=*/
l10n_util::GetStringUTF16(IDS_SETTINGS_CHANGE_PICTURE_PROFILE_PHOTO),
/*index=*/0, using_gaia, /*is_gaia_avatar=*/true);
avatars->Insert(0, std::move(gaia_picture_info));
}
}
return avatars;
}
......@@ -173,18 +199,18 @@ void ManageProfileHandler::HandleSetProfileIconToGaiaAvatar(
void ManageProfileHandler::HandleSetProfileIconToDefaultAvatar(
const base::ListValue* args) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(args);
DCHECK_EQ(1u, args->GetSize());
CHECK(args);
CHECK_EQ(1u, args->GetSize());
CHECK(args->GetList()[0].is_int());
std::string icon_url;
CHECK(args->GetString(0, &icon_url));
size_t new_icon_index = 0;
CHECK(profiles::IsDefaultAvatarIconUrl(icon_url, &new_icon_index));
size_t new_icon_index = args->GetList()[0].GetInt();
CHECK(profiles::IsDefaultAvatarIconIndex(new_icon_index));
PrefService* pref_service = profile_->GetPrefs();
pref_service->SetInteger(prefs::kProfileAvatarIndex, new_icon_index);
pref_service->SetBoolean(prefs::kProfileUsingDefaultAvatar, false);
pref_service->SetBoolean(
prefs::kProfileUsingDefaultAvatar,
new_icon_index == profiles::GetPlaceholderAvatarIndex());
pref_service->SetBoolean(prefs::kProfileUsingGAIAAvatar, false);
ProfileMetrics::LogProfileAvatarSelection(new_icon_index);
......@@ -193,8 +219,8 @@ void ManageProfileHandler::HandleSetProfileIconToDefaultAvatar(
void ManageProfileHandler::HandleSetProfileName(const base::ListValue* args) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(args);
DCHECK_EQ(1u, args->GetSize());
CHECK(args);
CHECK_EQ(1u, args->GetSize());
if (profile_->IsLegacySupervised())
return;
......
......@@ -31,22 +31,25 @@ class ManageProfileHandler : public settings::SettingsPageUIHandler,
// ProfileAttributesStorage::Observer:
void OnProfileAvatarChanged(const base::FilePath& profile_path) override;
// ProfileAttributesStorage::Observer:
void OnProfileHighResAvatarLoaded(
const base::FilePath& profile_path) override;
void OnProfileThemeColorsChanged(const base::FilePath& profile_path) override;
private:
FRIEND_TEST_ALL_PREFIXES(ManageProfileHandlerTest,
HandleSetProfileIconToGaiaAvatar);
FRIEND_TEST_ALL_PREFIXES(ManageProfileHandlerTest,
HandleSetProfileIconToDefaultAvatar);
GetAvailableIconsSignedInProfile);
FRIEND_TEST_ALL_PREFIXES(ManageProfileHandlerTest,
GetAvailableIconsLocalProfile);
FRIEND_TEST_ALL_PREFIXES(ManageProfileHandlerTest,
HandleSetProfileIconToDefaultCustomAvatar);
FRIEND_TEST_ALL_PREFIXES(ManageProfileHandlerTest,
HandleSetProfileIconToDefaultGenericAvatar);
FRIEND_TEST_ALL_PREFIXES(ManageProfileHandlerTest, HandleSetProfileName);
FRIEND_TEST_ALL_PREFIXES(ManageProfileHandlerTest, HandleGetAvailableIcons);
FRIEND_TEST_ALL_PREFIXES(ManageProfileHandlerTest,
HandleGetAvailableIconsOldIconSelected);
FRIEND_TEST_ALL_PREFIXES(ManageProfileHandlerTest,
HandleGetAvailableIconsGaiaAvatarSelected);
// Callback for the "getAvailableIcons" message.
// Sends the array of default profile icon URLs and profile names to WebUI.
......
......@@ -5,7 +5,10 @@
#include "chrome/browser/ui/webui/settings/settings_manage_profile_handler.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "chrome/browser/ui/signin/profile_colors_util.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
......@@ -14,6 +17,8 @@
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_web_ui.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/webui/web_ui_util.h"
#include "ui/gfx/image/image_unittest_util.h"
namespace settings {
......@@ -33,12 +38,15 @@ class TestManageProfileHandler : public ManageProfileHandler {
class ManageProfileHandlerTest : public testing::Test {
public:
ManageProfileHandlerTest()
: profile_manager_(TestingBrowserProcess::GetGlobal()),
profile_(nullptr) {}
: profile_manager_(TestingBrowserProcess::GetGlobal()) {}
void SetUp() override {
ASSERT_TRUE(profile_manager_.SetUp());
profile_ = profile_manager_.CreateTestingProfile("Profile 1");
ASSERT_TRUE(
profile_manager_.profile_attributes_storage()
->GetProfileAttributesWithPath(profile_->GetPath(), &entry_));
entry_->SetAvatarIconIndex(profiles::GetPlaceholderAvatarIndex());
handler_ = std::make_unique<TestManageProfileHandler>(profile_);
handler_->set_web_ui(&web_ui_);
......@@ -46,56 +54,108 @@ class ManageProfileHandlerTest : public testing::Test {
web_ui()->ClearTrackedCalls();
}
void VerifyIconListWithNoneSelected(const base::Value* value) {
VerifyIconList(value, 0 /* ignored */, true);
void SetSignedInProfile() {
gfx::Image gaia_image(gfx::test::CreateImage(256, 256));
entry()->SetAuthInfo("gaia_id", base::UTF8ToUTF16("user@gmail.com"), false);
entry()->SetGAIAPicture("GAIA_IMAGE_URL_WITH_SIZE", gaia_image);
EXPECT_TRUE(entry()->IsUsingDefaultAvatar());
EXPECT_TRUE(entry()->IsUsingGAIAPicture());
}
void VerifyIconListWithSingleSelection(const base::Value* value,
void VerifyIconListWithOnlyCustomAvatars(const base::Value* value,
size_t selected_index) {
VerifyIconList(value, selected_index, false);
VerifyIconList(value, selected_index, false, false, false);
}
content::TestWebUI* web_ui() { return &web_ui_; }
Profile* profile() const { return profile_; }
TestManageProfileHandler* handler() const { return handler_.get(); }
private:
content::BrowserTaskEnvironment task_environment_;
TestingProfileManager profile_manager_;
content::TestWebUI web_ui_;
Profile* profile_;
std::unique_ptr<TestManageProfileHandler> handler_;
void VerifyIconList(const base::Value* value,
size_t selected_index,
bool all_not_selected) {
bool gaia_included,
bool gaia_selected,
bool default_avatar_included) {
const base::ListValue* icons = nullptr;
ASSERT_TRUE(value->GetAsList(&icons));
// Expect a non-empty list of dictionaries containing non-empty strings for
// profile avatar icon urls and labels.
EXPECT_FALSE(icons->empty());
for (size_t i = 0; i < icons->GetSize(); ++i) {
size_t i = (gaia_included || default_avatar_included) ? 1 : 0;
if (gaia_included) {
ASSERT_FALSE(default_avatar_included);
VerifyGaiaAvatar(icons, gaia_selected);
} else if (default_avatar_included) {
VerifyDefaultGenericAvatar(icons, selected_index);
}
bool selected_found =
gaia_selected ||
(selected_index == profiles::GetPlaceholderAvatarIndex());
for (; i < icons->GetSize(); ++i) {
const base::DictionaryValue* icon = nullptr;
EXPECT_TRUE(icons->GetDictionary(i, &icon));
std::string icon_url;
size_t icon_index;
EXPECT_TRUE(icon->GetString("url", &icon_url));
EXPECT_FALSE(icon_url.empty());
EXPECT_TRUE(profiles::IsDefaultAvatarIconUrl(icon_url, &icon_index));
std::string icon_label;
EXPECT_TRUE(icon->GetString("label", &icon_label));
EXPECT_FALSE(icon_label.empty());
bool icon_selected;
bool has_icon_selected = icon->GetBoolean("selected", &icon_selected);
if (all_not_selected) {
EXPECT_FALSE(has_icon_selected);
} else if (selected_index == icon_index) {
EXPECT_TRUE(has_icon_selected);
EXPECT_TRUE(icon_selected);
int icon_index_int = *icon->FindIntPath("index");
EXPECT_TRUE(profiles::IsDefaultAvatarIconIndex(icon_index_int));
size_t icon_index = static_cast<size_t>(icon_index_int);
EXPECT_NE(icon_index, profiles::GetPlaceholderAvatarIndex());
EXPECT_NE(icon_index_int, 0);
size_t url_icon_index;
EXPECT_TRUE(profiles::IsDefaultAvatarIconUrl(icon_url, &url_icon_index));
EXPECT_EQ(icon_index, url_icon_index);
EXPECT_TRUE(!icon->FindStringPath("label")->empty());
base::Optional<bool> current_selected = icon->FindBoolPath("selected");
if (selected_index == icon_index) {
EXPECT_FALSE(selected_found);
EXPECT_TRUE(current_selected.value_or(false));
selected_found = true;
} else {
EXPECT_FALSE(current_selected.value_or(false));
}
}
EXPECT_TRUE(selected_index == 0 || selected_found);
}
content::TestWebUI* web_ui() { return &web_ui_; }
Profile* profile() const { return profile_; }
ProfileAttributesEntry* entry() const { return entry_; }
TestManageProfileHandler* handler() const { return handler_.get(); }
private:
content::BrowserTaskEnvironment task_environment_;
TestingProfileManager profile_manager_;
ProfileAttributesEntry* entry_ = nullptr;
content::TestWebUI web_ui_;
Profile* profile_ = nullptr;
std::unique_ptr<TestManageProfileHandler> handler_;
void VerifyGaiaAvatar(const base::ListValue* icons, bool gaia_selected) {
const base::DictionaryValue* icon = nullptr;
EXPECT_TRUE(icons->GetDictionary(0, &icon));
EXPECT_EQ(*icon->FindIntPath("index"), 0);
const gfx::Image* avatar_icon = entry()->GetGAIAPicture();
ASSERT_TRUE(avatar_icon);
EXPECT_EQ(
*icon->FindStringPath("url"),
webui::GetBitmapDataUrl(
profiles::GetAvatarIconForWebUI(*avatar_icon, true).AsBitmap()));
EXPECT_TRUE(!icon->FindStringPath("label")->empty());
EXPECT_EQ(*icon->FindBoolPath("selected"), gaia_selected);
}
void VerifyDefaultGenericAvatar(const base::ListValue* icons,
size_t selected_index) {
const base::DictionaryValue* icon = nullptr;
EXPECT_TRUE(icons->GetDictionary(0, &icon));
EXPECT_TRUE(!icon->FindStringPath("label")->empty());
int icon_index_int = icon->FindIntPath("index").value_or(0);
EXPECT_TRUE(icon_index_int != 0);
size_t icon_index = static_cast<size_t>(icon_index_int);
EXPECT_EQ(icon_index, profiles::GetPlaceholderAvatarIndex());
EXPECT_EQ(*icon->FindBoolPath("selected"), selected_index == icon_index);
}
};
......@@ -107,9 +167,9 @@ TEST_F(ManageProfileHandlerTest, HandleSetProfileIconToGaiaAvatar) {
EXPECT_TRUE(pref_service->GetBoolean(prefs::kProfileUsingGAIAAvatar));
}
TEST_F(ManageProfileHandlerTest, HandleSetProfileIconToDefaultAvatar) {
TEST_F(ManageProfileHandlerTest, HandleSetProfileIconToDefaultCustomAvatar) {
base::ListValue list_args;
list_args.AppendString("chrome://theme/IDR_PROFILE_AVATAR_15");
list_args.AppendInteger(15);
handler()->HandleSetProfileIconToDefaultAvatar(&list_args);
PrefService* pref_service = profile()->GetPrefs();
......@@ -118,6 +178,19 @@ TEST_F(ManageProfileHandlerTest, HandleSetProfileIconToDefaultAvatar) {
EXPECT_FALSE(pref_service->GetBoolean(prefs::kProfileUsingGAIAAvatar));
}
TEST_F(ManageProfileHandlerTest, HandleSetProfileIconToDefaultGenericAvatar) {
int generic_avatar_index = profiles::GetPlaceholderAvatarIndex();
base::ListValue list_args;
list_args.AppendInteger(generic_avatar_index);
handler()->HandleSetProfileIconToDefaultAvatar(&list_args);
PrefService* pref_service = profile()->GetPrefs();
EXPECT_EQ(generic_avatar_index,
pref_service->GetInteger(prefs::kProfileAvatarIndex));
EXPECT_TRUE(pref_service->GetBoolean(prefs::kProfileUsingDefaultAvatar));
EXPECT_FALSE(pref_service->GetBoolean(prefs::kProfileUsingGAIAAvatar));
}
TEST_F(ManageProfileHandlerTest, HandleSetProfileName) {
base::ListValue list_args;
list_args.AppendString("New Profile Name");
......@@ -128,8 +201,11 @@ TEST_F(ManageProfileHandlerTest, HandleSetProfileName) {
}
TEST_F(ManageProfileHandlerTest, HandleGetAvailableIcons) {
PrefService* pref_service = profile()->GetPrefs();
pref_service->SetInteger(prefs::kProfileAvatarIndex, 27);
// Set avatar icon will trigger avatar icons updated event.
entry()->SetIsUsingDefaultAvatar(false);
entry()->SetAvatarIconIndex(27);
EXPECT_EQ(1U, web_ui()->call_data().size());
web_ui()->ClearTrackedCalls();
base::ListValue list_args_1;
list_args_1.AppendString("get-icons-callback-id");
......@@ -139,17 +215,16 @@ TEST_F(ManageProfileHandlerTest, HandleGetAvailableIcons) {
const content::TestWebUI::CallData& data_1 = *web_ui()->call_data().back();
EXPECT_EQ("cr.webUIResponse", data_1.function_name());
EXPECT_EQ("get-icons-callback-id", data_1.arg1()->GetString());
std::string callback_id_1;
ASSERT_TRUE(data_1.arg1()->GetAsString(&callback_id_1));
EXPECT_EQ("get-icons-callback-id", callback_id_1);
VerifyIconListWithSingleSelection(data_1.arg3(), 27);
VerifyIconListWithOnlyCustomAvatars(data_1.arg3(), 27);
}
TEST_F(ManageProfileHandlerTest, HandleGetAvailableIconsOldIconSelected) {
PrefService* pref_service = profile()->GetPrefs();
pref_service->SetInteger(prefs::kProfileAvatarIndex, 7);
// Set avatar icon will trigger avatar icons updated event.
entry()->SetAvatarIconIndex(7);
EXPECT_EQ(1U, web_ui()->call_data().size());
web_ui()->ClearTrackedCalls();
base::ListValue list_args;
list_args.AppendString("get-icons-callback-id");
......@@ -159,18 +234,68 @@ TEST_F(ManageProfileHandlerTest, HandleGetAvailableIconsOldIconSelected) {
const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
EXPECT_EQ("cr.webUIResponse", data.function_name());
EXPECT_EQ("get-icons-callback-id", data.arg1()->GetString());
std::string callback_id;
ASSERT_TRUE(data.arg1()->GetAsString(&callback_id));
EXPECT_EQ("get-icons-callback-id", callback_id);
VerifyIconListWithOnlyCustomAvatars(data.arg3(), 0);
}
TEST_F(ManageProfileHandlerTest, GetAvailableIconsSignedInProfile) {
SetSignedInProfile();
EXPECT_TRUE(entry()->IsUsingDefaultAvatar());
EXPECT_TRUE(entry()->IsUsingGAIAPicture());
web_ui()->ClearTrackedCalls();
base::ListValue list_args;
list_args.AppendString("get-icons-callback-id");
handler()->HandleGetAvailableIcons(&list_args);
EXPECT_EQ(1U, web_ui()->call_data().size());
const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
EXPECT_EQ("cr.webUIResponse", data.function_name());
EXPECT_EQ("get-icons-callback-id", data.arg1()->GetString());
VerifyIconList(data.arg3(), /*selected_index=*/0,
/*gaia_included=*/true,
/*gaia_selected=*/true,
/*default_avatar_included=*/false);
VerifyIconListWithNoneSelected(data.arg3());
web_ui()->ClearTrackedCalls();
// Set custom avatar
entry()->SetIsUsingDefaultAvatar(false);
entry()->SetAvatarIconIndex(30);
const content::TestWebUI::CallData& data_1 = *web_ui()->call_data().back();
EXPECT_EQ("cr.webUIListenerCallback", data_1.function_name());
EXPECT_EQ("available-icons-changed", data_1.arg1()->GetString());
VerifyIconList(data_1.arg2(), /*selected_index=*/30,
/*gaia_included=*/true,
/*gaia_selected=*/false,
/*default_avatar_included=*/false);
// Sign out.
entry()->SetAuthInfo("", base::string16(), false);
entry()->SetGAIAPicture(std::string(), gfx::Image());
const content::TestWebUI::CallData& data_2 = *web_ui()->call_data().back();
EXPECT_EQ("cr.webUIListenerCallback", data_2.function_name());
EXPECT_EQ("available-icons-changed", data_2.arg1()->GetString());
VerifyIconList(data_2.arg2(), /*selected_index=*/30,
/*gaia_included=*/false,
/*gaia_selected=*/false,
/*default_avatar_included=*/false);
}
TEST_F(ManageProfileHandlerTest, HandleGetAvailableIconsGaiaAvatarSelected) {
PrefService* pref_service = profile()->GetPrefs();
pref_service->SetInteger(prefs::kProfileAvatarIndex, 27);
pref_service->SetBoolean(prefs::kProfileUsingGAIAAvatar, true);
TEST_F(ManageProfileHandlerTest, GetAvailableIconsLocalProfile) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatures(
{features::kNewProfilePicker, features::kProfilesUIRevamp}, {});
EXPECT_FALSE(entry()->IsUsingGAIAPicture());
EXPECT_EQ(entry()->GetAvatarIconIndex(),
profiles::GetPlaceholderAvatarIndex());
base::ListValue list_args;
list_args.AppendString("get-icons-callback-id");
......@@ -181,28 +306,76 @@ TEST_F(ManageProfileHandlerTest, HandleGetAvailableIconsGaiaAvatarSelected) {
const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
EXPECT_EQ("cr.webUIResponse", data.function_name());
std::string callback_id;
ASSERT_TRUE(data.arg1()->GetAsString(&callback_id));
std::string callback_id = data.arg1()->GetString();
EXPECT_EQ("get-icons-callback-id", callback_id);
VerifyIconList(data.arg3(), /*selected_index=*/entry()->GetAvatarIconIndex(),
/*gaia_included=*/false,
/*gaia_selected=*/false,
/*default_avatar_included=*/true);
// Sign in.
SetSignedInProfile();
EXPECT_TRUE(entry()->IsUsingGAIAPicture());
const content::TestWebUI::CallData& data_1 = *web_ui()->call_data().back();
EXPECT_EQ("cr.webUIListenerCallback", data_1.function_name());
VerifyIconListWithNoneSelected(data.arg3());
EXPECT_EQ("available-icons-changed", data_1.arg1()->GetString());
VerifyIconList(data_1.arg2(), /*selected_index=*/0,
/*gaia_included=*/true,
/*gaia_selected=*/true,
/*default_avatar_included=*/false);
}
TEST_F(ManageProfileHandlerTest, ProfileAvatarChangedWebUIEvent) {
PrefService* pref_service = profile()->GetPrefs();
pref_service->SetInteger(prefs::kProfileAvatarIndex, 12);
handler()->OnProfileAvatarChanged(base::FilePath());
entry()->SetIsUsingDefaultAvatar(false);
entry()->SetAvatarIconIndex(27);
EXPECT_EQ(1U, web_ui()->call_data().size());
const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
EXPECT_EQ("cr.webUIListenerCallback", data.function_name());
std::string event_id;
ASSERT_TRUE(data.arg1()->GetAsString(&event_id));
EXPECT_EQ("available-icons-changed", event_id);
VerifyIconListWithSingleSelection(data.arg2(), 12);
EXPECT_EQ("available-icons-changed", data.arg1()->GetString());
VerifyIconListWithOnlyCustomAvatars(data.arg2(), 27);
}
TEST_F(ManageProfileHandlerTest, ProfileThemeColorsChangedWebUIEvent) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatures(
{features::kNewProfilePicker, features::kProfilesUIRevamp}, {});
ProfileThemeColors colors = {SK_ColorTRANSPARENT, SK_ColorBLACK,
SK_ColorWHITE};
entry()->SetProfileThemeColors(colors);
// The expected number of calls are two, since the profile avatar has changed
// along with the color, as the current profile avatar is the generic colored
// avatar.
EXPECT_EQ(2U, web_ui()->call_data().size());
const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
EXPECT_EQ("cr.webUIListenerCallback", data.function_name());
EXPECT_EQ("available-icons-changed", data.arg1()->GetString());
VerifyIconList(data.arg2(), /*selected_index=*/entry()->GetAvatarIconIndex(),
/*gaia_included=*/false,
/*gaia_selected=*/false,
/*default_avatar_included=*/true);
// Set custom avatar.
entry()->SetIsUsingDefaultAvatar(false);
entry()->SetAvatarIconIndex(37);
web_ui()->ClearTrackedCalls();
entry()->SetProfileThemeColors(base::nullopt);
EXPECT_EQ(1U, web_ui()->call_data().size());
const content::TestWebUI::CallData& data_1 = *web_ui()->call_data().back();
EXPECT_EQ("cr.webUIListenerCallback", data_1.function_name());
EXPECT_EQ("available-icons-changed", data_1.arg1()->GetString());
VerifyIconList(data_1.arg2(),
/*selected_index=*/entry()->GetAvatarIconIndex(),
/*gaia_included=*/false,
/*gaia_selected=*/false,
/*default_avatar_included=*/true);
}
} // namespace settings
......@@ -123,7 +123,7 @@ void SigninCreateProfileHandler::RequestDefaultProfileIcons(
const base::ListValue* args) {
web_ui()->CallJavascriptFunctionUnsafe(
"cr.webUIListenerCallback", base::Value("profile-icons-received"),
*profiles::GetDefaultProfileAvatarIconsAndLabels());
*profiles::GetCustomProfileAvatarIconsAndLabels());
}
void SigninCreateProfileHandler::CreateProfile(const base::ListValue* args) {
......
......@@ -35,8 +35,8 @@ class TestManageProfileBrowserProxy extends TestBrowserProxy {
getAvailableIcons() {
this.methodCalled('getAvailableIcons');
return Promise.resolve([
{url: 'fake-icon-1.png', label: 'fake-icon-1'},
{url: 'fake-icon-2.png', label: 'fake-icon-2', selected: true},
{url: 'fake-icon-1.png', label: 'fake-icon-1', index: 1},
{url: 'fake-icon-2.png', label: 'fake-icon-2', index: 2, selected: true},
{url: 'gaia-icon.png', label: 'gaia-icon', isGaiaAvatar: true},
]);
}
......@@ -47,8 +47,8 @@ class TestManageProfileBrowserProxy extends TestBrowserProxy {
}
/** @override */
setProfileIconToDefaultAvatar(iconUrl) {
this.methodCalled('setProfileIconToDefaultAvatar', [iconUrl]);
setProfileIconToDefaultAvatar(index) {
this.methodCalled('setProfileIconToDefaultAvatar', [index]);
}
/** @override */
......@@ -130,7 +130,7 @@ suite('ManageProfileTests', function() {
items[1].click();
const args =
await browserProxy.whenCalled('setProfileIconToDefaultAvatar');
assertEquals('fake-icon-2.png', args[0]);
assertEquals(2, args[0]);
items[2].click();
await browserProxy.whenCalled('setProfileIconToGaiaAvatar');
......
......@@ -10,8 +10,9 @@
/**
* @typedef {{url: string,
* label: string,
* isGaiaAvatar: (boolean|undefined),
* selected: (boolean|undefined)}}
* index: (number),
* isGaiaAvatar: (boolean),
* selected: (boolean)}}
*/
/* #export */ let AvatarIcon;
......
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