From 690b96415790420e21e5db562fb695566846c851 Mon Sep 17 00:00:00 2001 From: Chanseok Oh Date: Fri, 6 Dec 2019 15:43:46 -0500 Subject: [PATCH] Fix user profile picture URL (#3571) --- .../eclipse/login/ui/AccountsPanelTest.java | 26 +++++++++++++++++++ .../tools/eclipse/login/ui/AccountsPanel.java | 3 +-- .../eclipse/login/ui/LabelImageLoader.java | 3 ++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/plugins/com.google.cloud.tools.eclipse.login.test/src/com/google/cloud/tools/eclipse/login/ui/AccountsPanelTest.java b/plugins/com.google.cloud.tools.eclipse.login.test/src/com/google/cloud/tools/eclipse/login/ui/AccountsPanelTest.java index c65272528f..43599d0ce7 100644 --- a/plugins/com.google.cloud.tools.eclipse.login.test/src/com/google/cloud/tools/eclipse/login/ui/AccountsPanelTest.java +++ b/plugins/com.google.cloud.tools.eclipse.login.test/src/com/google/cloud/tools/eclipse/login/ui/AccountsPanelTest.java @@ -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; @@ -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 @@ -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 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)); diff --git a/plugins/com.google.cloud.tools.eclipse.login/src/com/google/cloud/tools/eclipse/login/ui/AccountsPanel.java b/plugins/com.google.cloud.tools.eclipse.login/src/com/google/cloud/tools/eclipse/login/ui/AccountsPanel.java index 9bb47c6cef..921f5a47a6 100644 --- a/plugins/com.google.cloud.tools.eclipse.login/src/com/google/cloud/tools/eclipse/login/ui/AccountsPanel.java +++ b/plugins/com.google.cloud.tools.eclipse.login/src/com/google/cloud/tools/eclipse/login/ui/AccountsPanel.java @@ -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); @@ -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); } diff --git a/plugins/com.google.cloud.tools.eclipse.login/src/com/google/cloud/tools/eclipse/login/ui/LabelImageLoader.java b/plugins/com.google.cloud.tools.eclipse.login/src/com/google/cloud/tools/eclipse/login/ui/LabelImageLoader.java index f33cdd207a..dfe4a9cc2e 100644 --- a/plugins/com.google.cloud.tools.eclipse.login/src/com/google/cloud/tools/eclipse/login/ui/LabelImageLoader.java +++ b/plugins/com.google.cloud.tools.eclipse.login/src/com/google/cloud/tools/eclipse/login/ui/LabelImageLoader.java @@ -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);