Skip to content

Commit bdd47c1

Browse files
authored
Uncheck "Stop previous version" when not promoting in deploy preferences panel (#1504)
* Bind observables in a correct way * Restore buttons to be private
1 parent ad5335a commit bdd47c1

3 files changed

Lines changed: 98 additions & 10 deletions

File tree

plugins/com.google.cloud.tools.eclipse.appengine.deploy.ui.test/META-INF/MANIFEST.MF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Import-Package: com.google.cloud.tools.eclipse.appengine.facets,
1515
com.google.common.base;version="[20.0.0,21.0.0)",
1616
org.eclipse.core.expressions,
1717
org.eclipse.jface.viewers,
18+
org.eclipse.swtbot.swt.finder.widgets,
1819
org.eclipse.wst.common.project.facet.core,
1920
org.mockito;provider=google;version="1.10.19",
2021
org.mockito.runners;provider=google;version="1.10.19",

plugins/com.google.cloud.tools.eclipse.appengine.deploy.ui.test/src/com/google/cloud/tools/eclipse/appengine/deploy/ui/StandardDeployPreferencesPanelTest.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.tools.eclipse.appengine.deploy.ui;
1818

1919
import static org.hamcrest.CoreMatchers.is;
20+
import static org.junit.Assert.assertFalse;
2021
import static org.junit.Assert.assertThat;
2122
import static org.junit.Assert.assertTrue;
2223
import static org.junit.Assert.fail;
@@ -44,8 +45,10 @@
4445
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
4546
import org.eclipse.jface.viewers.IStructuredSelection;
4647
import org.eclipse.swt.SWT;
48+
import org.eclipse.swt.widgets.Button;
4749
import org.eclipse.swt.widgets.Composite;
4850
import org.eclipse.swt.widgets.Control;
51+
import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;
4952
import org.junit.Before;
5053
import org.junit.Rule;
5154
import org.junit.Test;
@@ -117,6 +120,61 @@ public void testValidationMessageWhenSignedIn() {
117120
assertThat(status.getMessage(), is("Select an account."));
118121
}
119122

123+
private static Button getButtonWithText(Composite parent, String text) {
124+
for (Control control : parent.getChildren()) {
125+
if (control instanceof Button) {
126+
Button button = (Button) control;
127+
if (button.getText().equals(text)) {
128+
return button;
129+
}
130+
}
131+
}
132+
return null;
133+
}
134+
135+
// https://github.com/GoogleCloudPlatform/google-cloud-eclipse/issues/1229
136+
@Test
137+
public void testUncheckStopPreviousVersionButtonWhenDisabled() {
138+
StandardDeployPreferencesPanel panel = new StandardDeployPreferencesPanel(
139+
parent, project, loginService, layoutChangedHandler, true, projectRepository);
140+
141+
Button promoteButton =
142+
getButtonWithText(panel, "Promote the deployed version to receive all traffic");
143+
Button stopButton = getButtonWithText(panel, "Stop previous version");
144+
SWTBotCheckBox promote = new SWTBotCheckBox(promoteButton);
145+
SWTBotCheckBox stop = new SWTBotCheckBox(stopButton);
146+
147+
// Initially, everything is checked and enabled.
148+
assertTrue(promoteButton.getSelection());
149+
assertTrue(stopButton.getSelection());
150+
assertTrue(stopButton.getEnabled());
151+
152+
promote.click();
153+
assertFalse(promoteButton.getSelection());
154+
assertFalse(stopButton.getSelection());
155+
assertFalse(stopButton.getEnabled());
156+
157+
promote.click();
158+
assertTrue(promoteButton.getSelection());
159+
assertTrue(stopButton.getSelection());
160+
assertTrue(stopButton.getEnabled());
161+
162+
stop.click();
163+
assertTrue(promoteButton.getSelection());
164+
assertFalse(stopButton.getSelection());
165+
assertTrue(stopButton.getEnabled());
166+
167+
promote.click();
168+
assertFalse(promoteButton.getSelection());
169+
assertFalse(stopButton.getSelection());
170+
assertFalse(stopButton.getEnabled());
171+
172+
promote.click();
173+
assertTrue(promoteButton.getSelection());
174+
assertFalse(stopButton.getSelection());
175+
assertTrue(stopButton.getEnabled());
176+
}
177+
120178
@Test
121179
public void testProjectSavedInPreferencesSelected() throws ProjectRepositoryException {
122180
IEclipsePreferences node =
@@ -170,5 +228,4 @@ private IStatus getAccountSelectorValidationStatus(StandardDeployPreferencesPane
170228
}
171229
return status;
172230
}
173-
174231
}

plugins/com.google.cloud.tools.eclipse.appengine.deploy.ui/src/com/google/cloud/tools/eclipse/appengine/deploy/ui/StandardDeployPreferencesPanel.java

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.eclipse.core.databinding.conversion.Converter;
4646
import org.eclipse.core.databinding.observable.Observables;
4747
import org.eclipse.core.databinding.observable.list.IObservableList;
48+
import org.eclipse.core.databinding.observable.value.ComputedValue;
4849
import org.eclipse.core.databinding.observable.value.IObservableValue;
4950
import org.eclipse.core.databinding.observable.value.WritableValue;
5051
import org.eclipse.core.databinding.validation.IValidator;
@@ -213,24 +214,53 @@ private void setupProjectVersionDataBinding(DataBindingContext context) {
213214
}
214215

215216
private void setupAutoPromoteDataBinding(DataBindingContext context) {
216-
ISWTObservableValue promoteButton = WidgetProperties.selection().observe(autoPromoteButton);
217-
ISWTObservableValue stopPreviousVersion =
217+
ISWTObservableValue promoteButton =
218+
WidgetProperties.selection().observe(autoPromoteButton);
219+
final ISWTObservableValue stopPreviousVersion =
218220
WidgetProperties.selection().observe(stopPreviousVersionButton);
219-
ISWTObservableValue stopPreviousVersionEnablement =
221+
final ISWTObservableValue stopPreviousVersionEnablement =
220222
WidgetProperties.enabled().observe(stopPreviousVersionButton);
221223

222-
// use an intermediary value to control the enabled state of stopPreviousVersionButton
223-
// based on the promote checkbox's state
224-
WritableValue enablement = new WritableValue();
225-
context.bindValue(promoteButton, enablement);
226-
context.bindValue(stopPreviousVersionEnablement, enablement);
224+
context.bindValue(stopPreviousVersionEnablement, promoteButton);
227225

228226
IObservableValue promoteModel = PojoProperties.value("autoPromote").observe(model);
229227
IObservableValue stopPreviousVersionModel =
230228
PojoProperties.value("stopPreviousVersion").observe(model);
231229

232230
context.bindValue(promoteButton, promoteModel);
233-
context.bindValue(stopPreviousVersion, stopPreviousVersionModel);
231+
232+
// Intermediary model necessary for "Restore Defaults" to work.
233+
final IObservableValue currentStopPreviousVersionChoice = new WritableValue();
234+
context.bindValue(currentStopPreviousVersionChoice, stopPreviousVersionModel);
235+
236+
// One-way update: button selection <-- latest user choice
237+
// Update the button (to match the user choice), if enabled; if not, force unchecking.
238+
context.bindValue(stopPreviousVersion, new ComputedValue() {
239+
@Override
240+
protected Object calculate() {
241+
boolean buttonEnabled = (boolean) stopPreviousVersionEnablement.getValue();
242+
boolean currentValue = (boolean) currentStopPreviousVersionChoice.getValue();
243+
if (!buttonEnabled) {
244+
return Boolean.FALSE; // Force unchecking the stop previous button if it is disabled.
245+
}
246+
return currentValue; // Otherwise, check the button according to the latest user choice.
247+
}
248+
}, new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), null);
249+
250+
// One-way update: button selection --> latest user choice
251+
// Update the user choice (to match the button selection), only when the button is enabled.
252+
context.bindValue(new ComputedValue() {
253+
@Override
254+
protected Object calculate() {
255+
boolean buttonEnabled = (boolean) stopPreviousVersionEnablement.getValue();
256+
boolean buttonValue = (boolean) stopPreviousVersion.getValue();
257+
boolean currentValue = (boolean) currentStopPreviousVersionChoice.getValue();
258+
if (buttonEnabled) {
259+
return buttonValue; // Remember the button state as the latest choice if it is enabled.
260+
}
261+
return currentValue; // Otherwise, retain the latest (current) user choice.
262+
}
263+
}, stopPreviousVersionModel, null, new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER));
234264
}
235265

236266
private void setupBucketDataBinding(DataBindingContext context) {

0 commit comments

Comments
 (0)