Skip to content

Commit fb49730

Browse files
authored
Handle avatar URLs with existing resize directives (#3643)
1 parent ae5fb8c commit fb49730

File tree

2 files changed

+25
-1
lines changed
  • plugins
    • com.google.cloud.tools.eclipse.login.test/src/com/google/cloud/tools/eclipse/login/ui
    • com.google.cloud.tools.eclipse.login/src/com/google/cloud/tools/eclipse/login/ui

2 files changed

+25
-1
lines changed

plugins/com.google.cloud.tools.eclipse.login.test/src/com/google/cloud/tools/eclipse/login/ui/AccountsPanelTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ public void testAccountsArea_threeAccounts() {
168168
assertTrue(namesEmails.names.contains("Charlie"));
169169
}
170170

171+
@Test
172+
public void testResizedImageUrl() {
173+
assertEquals("https://lh3/xxxx=s48", AccountsPanel.resizedImageUrl("https://lh3/xxxx", 48));
174+
assertEquals(
175+
"https://lh3/xxxx=s48", AccountsPanel.resizedImageUrl("https://lh3/xxxx=s96-c", 48));
176+
}
177+
171178
private void setUpLoginService(List<Account> accounts) {
172179
when(loginService.hasAccounts()).thenReturn(!accounts.isEmpty());
173180
when(loginService.getAccounts()).thenReturn(new HashSet<>(accounts));

plugins/com.google.cloud.tools.eclipse.login/src/com/google/cloud/tools/eclipse/login/ui/AccountsPanel.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,31 @@ void createAccountsPane(Composite accountArea) {
112112

113113
if (account.getAvatarUrl() != null) {
114114
try {
115-
imageLoader.loadImage(account.getAvatarUrl() + "=s" + avatarSize, avatar);
115+
String url = resizedImageUrl(account.getAvatarUrl(), avatarSize);
116+
imageLoader.loadImage(url, avatar);
116117
} catch (MalformedURLException ex) {
117118
logger.log(Level.WARNING, "malformed avatar image URL", ex);
118119
}
119120
}
120121
}
121122
}
122123

124+
/**
125+
* Apply a resize directive to an Google avatar URL, replacing any previous resize directives.
126+
*
127+
* @param avatarUrl the URL
128+
* @param avatarSize the desired image size
129+
* @return an amended URL
130+
*/
131+
@VisibleForTesting
132+
static String resizedImageUrl(String avatarUrl, int avatarSize) {
133+
int index = avatarUrl.lastIndexOf('=');
134+
if (index > 0) {
135+
avatarUrl = avatarUrl.substring(0, index);
136+
}
137+
return avatarUrl + "=s" + avatarSize;
138+
}
139+
123140
private void createButtons(Composite container) {
124141
Composite buttonArea = new Composite(container, SWT.NONE);
125142
GridLayoutFactory.fillDefaults().numColumns(2).applyTo(buttonArea);

0 commit comments

Comments
 (0)