Skip to content

Commit

Permalink
Fix user profile picture URL (#3571)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanseokoh authored Dec 6, 2019
1 parent d120329 commit 690b964
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,34 @@
package com.google.cloud.tools.eclipse.login.ui;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.cloud.tools.eclipse.login.IGoogleLoginService;
import com.google.cloud.tools.eclipse.test.util.ui.ShellTestResource;
import com.google.cloud.tools.login.Account;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

Expand All @@ -61,6 +69,7 @@ public void setUp() {
when(account1.getName()).thenReturn("Alice");
when(account2.getName()).thenReturn(null);
when(account3.getName()).thenReturn("Charlie");
when(account1.getAvatarUrl()).thenReturn("https://avatar.url/account1");
}

@Test
Expand Down Expand Up @@ -125,6 +134,23 @@ public void testAccountsArea_accountWithNullName() {
assertTrue(namesEmails.names.get(0).isEmpty());
}

@Test
public void testAccountsArea_avatarImageUrl() throws MalformedURLException {
setUpLoginService(Arrays.asList(account1));

AccountsPanel panel = new AccountsPanel(null, loginService, imageLoader);
panel.createDialogArea(shell);

ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(imageLoader).loadImage(captor.capture(), any());
assertEquals(1, captor.getAllValues().size());

Pattern urlPattern = Pattern.compile("^https://avatar.url/account1=s([0-9]+)$");
Matcher matcher = urlPattern.matcher(captor.getValue());
assertTrue(matcher.find());
assertThat(Integer.valueOf(matcher.group(1)), Matchers.greaterThan(0));
}

@Test
public void testAccountsArea_threeAccounts() {
setUpLoginService(Arrays.asList(account1, account2, account3));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public AccountsPanel(Shell parent, IGoogleLoginService loginService) {
this.imageLoader = imageLoader;
}


@Override
protected Color getBackground() {
return getShell().getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
Expand Down Expand Up @@ -113,7 +112,7 @@ void createAccountsPane(Composite accountArea) {

if (account.getAvatarUrl() != null) {
try {
imageLoader.loadImage(account.getAvatarUrl() + "?sz=" + avatarSize, avatar);
imageLoader.loadImage(account.getAvatarUrl() + "=s" + avatarSize, avatar);
} catch (MalformedURLException ex) {
logger.log(Level.WARNING, "malformed avatar image URL", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public class LabelImageLoader {
*
* Must be called in the UI context.
*/
void loadImage(String imageUrl, Label label) throws MalformedURLException {
@VisibleForTesting
public void loadImage(String imageUrl, Label label) throws MalformedURLException {
Preconditions.checkNotNull(imageUrl);

ImageData imageData = cache.get(imageUrl);
Expand Down

0 comments on commit 690b964

Please sign in to comment.