PageRenderTime 44ms CodeModel.GetById 31ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/sigmah/src/test/java/org/sigmah/client/offline/ui/OfflinePresenterTest.java

http://sigma-h.googlecode.com/
Java | 306 lines | 208 code | 74 blank | 24 comment | 0 complexity | b16299211c161c0d237e981d5323d173 MD5 | raw file
  1/*
  2 * All Sigmah code is released under the GNU General Public License v3
  3 * See COPYRIGHT.txt and LICENSE.txt.
  4 */
  5
  6package org.sigmah.client.offline.ui;
  7
  8import com.extjs.gxt.ui.client.event.BaseObservable;
  9import com.extjs.gxt.ui.client.event.Events;
 10import com.extjs.gxt.ui.client.event.MenuEvent;
 11import com.extjs.gxt.ui.client.event.Observable;
 12import com.google.gwt.user.client.rpc.AsyncCallback;
 13import com.google.inject.Provider;
 14import org.junit.Before;
 15import org.junit.Test;
 16import org.sigmah.client.dispatch.AsyncMonitor;
 17import org.sigmah.client.dispatch.remote.RemoteDispatcher;
 18import org.sigmah.client.mock.MockEventBus;
 19import org.sigmah.client.mock.MockStateManager;
 20import org.sigmah.client.offline.OfflineGateway;
 21import org.sigmah.client.offline.sync.SyncStatusEvent;
 22import org.sigmah.client.offline.ui.OfflinePresenter.PromptCallback;
 23import org.sigmah.shared.command.Command;
 24import org.sigmah.shared.command.result.CommandResult;
 25
 26import java.util.Date;
 27
 28import static org.easymock.EasyMock.*;
 29import static org.hamcrest.CoreMatchers.equalTo;
 30import static org.junit.Assert.assertThat;
 31
 32public class OfflinePresenterTest {
 33    private ViewStub view;
 34    private MockEventBus eventBus;
 35    private Provider<OfflineGateway> gatewayProvider;
 36    private OfflineImplStub offlineImpl;
 37    private MockStateManager stateManager;
 38	private RemoteDispatcher remoteDispatcher;
 39
 40    @Before
 41    public void setUp() {
 42        view = new ViewStub();
 43        eventBus = new MockEventBus();
 44        offlineImpl = new OfflineImplStub();
 45        gatewayProvider = new Provider<OfflineGateway>() {
 46            @Override
 47            public OfflineGateway get() {
 48                return offlineImpl;
 49            }
 50        };
 51        stateManager = new MockStateManager();
 52        remoteDispatcher = null;
 53    }
 54
 55
 56    @Test
 57    public void installationSequence() {
 58
 59        // No state is set, so the presenter should assume that offline is not yet installed
 60
 61        OfflinePresenter presenter = new OfflinePresenter(view, eventBus, remoteDispatcher,
 62        		gatewayProvider, stateManager);
 63
 64        assertThat(view.defaultButtonText, equalTo("Install"));
 65
 66        //  click the default button
 67        view.defaultButton.fireEvent(Events.Select);
 68
 69        // verify that installation has been launched
 70        assertThat(offlineImpl.lastCall, equalTo("install"));
 71
 72        // verify the UI is showing the status
 73        assertThat(view.defaultButtonText, equalTo("Installing..."));
 74        assertThat(view.progressDialogVisible, equalTo(true));
 75        assertThat(view.menuEnabled, equalTo(false));
 76
 77        // status event is fired
 78        eventBus.fireEvent(new SyncStatusEvent("sync-task", 50));
 79
 80        // verify that progress bar is updated
 81        assertThat(view.percentComplete, equalTo(50d));
 82
 83        // signal that installation is complete
 84        offlineImpl.lastCallback.onSuccess(null);
 85
 86        // verify that the UI has been updated
 87        assertThat(view.progressDialogVisible, equalTo(false));
 88        assertThat(view.defaultButtonText, equalTo("Last Sync'd: <date>"));
 89        assertThat(view.menuEnabled, equalTo(true));
 90    }
 91
 92    @Test
 93    public void loadWhenOfflineIsEnabled() {
 94
 95        // No state is set, so the presenter should assume that offline is not yet installed
 96
 97        stateManager.set(OfflinePresenter.OFFLINE_MODE_KEY, OfflinePresenter.OfflineMode.OFFLINE.toString());
 98
 99        OfflinePresenter presenter = new OfflinePresenter(view, eventBus, 
100        		remoteDispatcher, gatewayProvider, stateManager);
101
102        // offline async fragment finishes loading
103        assertThat(offlineImpl.lastCall, equalTo("goOffline"));
104        offlineImpl.lastCallback.onSuccess(null);
105
106        assertThat(view.defaultButtonText, equalTo("Last Sync'd: <date>"));
107        assertThat(offlineImpl.lastCall, equalTo("goOffline"));
108    }
109
110    @Test
111    public void reinstallWhileOffline() {
112
113        stateManager.set(OfflinePresenter.OFFLINE_MODE_KEY, OfflinePresenter.OfflineMode.OFFLINE.toString());
114
115        new OfflinePresenter(view, eventBus, remoteDispatcher,
116        		gatewayProvider, stateManager);
117
118        // offline async fragment finishes loading
119        offlineImpl.lastCallback.onSuccess(null);
120
121        // now click the reinstall menu item
122        view.reinstallItem.fireEvent(Events.Select, new MenuEvent(null));
123
124        // we should be in install mode
125        assertThat(offlineImpl.lastCall, equalTo("install"));
126        assertThat(view.defaultButtonText, equalTo("Installing..."));
127    }
128
129
130    private static class ViewStub implements OfflinePresenter.View {
131
132        public BaseObservable defaultButton = new BaseObservable();
133        public String defaultButtonText;
134        public boolean progressDialogVisible = false;
135        public String taskDescription;
136        public double percentComplete;
137        public boolean menuEnabled = false;
138
139        private BaseObservable reinstallItem = new BaseObservable();
140        private BaseObservable toggleItem = new BaseObservable();
141        private BaseObservable syncNowItem = new BaseObservable();
142
143        @Override
144        public Observable getButton() {
145            return defaultButton;
146        }
147
148        @Override
149        public Observable getReinstallItem() {
150            return reinstallItem;
151        }
152
153        @Override
154        public Observable getToggleItem() {
155            return toggleItem;
156        }
157
158        @Override
159        public Observable getSyncNowItem() {
160            return syncNowItem;
161        }
162
163        @Override
164        public void setButtonTextToInstall() {
165            this.defaultButtonText = "Install";
166        }
167
168        @Override
169        public void setButtonTextToInstalling() {
170            this.defaultButtonText = "Installing...";
171        }
172
173        @Override
174        public void setButtonTextToLoading() {
175            this.defaultButtonText = "Loading...";
176        }
177
178        @Override
179        public void setButtonTextToSyncing() {
180            this.defaultButtonText = "Syncing...";
181
182        }
183
184        @Override
185        public void setButtonTextToLastSync(Date lastSyncTime) {
186            this.defaultButtonText = "Last Sync'd: <date>";
187        }
188
189        @Override
190        public void setButtonTextToOnline() {
191            this.defaultButtonText = "Online";
192        }
193
194        @Override
195        public void updateProgress(String taskDescription, double percentComplete) {
196            this.taskDescription = taskDescription;
197            this.percentComplete = percentComplete;
198        }
199
200        @Override
201        public void showProgressDialog() {
202            progressDialogVisible = true;
203        }
204
205        @Override
206        public void hideProgressDialog() {
207            progressDialogVisible = false;
208        }
209
210        @Override
211        public void enableMenu() {
212            menuEnabled = true;
213        }
214
215        @Override
216        public void disableMenu() {
217            menuEnabled = false;
218        }
219
220		@Override
221		public void showError(String message) {
222			// TODO Auto-generated method stub
223			
224		}
225
226		@Override
227		public void promptToGoOnline(PromptCallback callback) {
228			// TODO Auto-generated method stub
229			
230		}
231
232		@Override
233		public void setConnectionDialogToFailure() {
234			// TODO Auto-generated method stub
235			
236		}
237
238		@Override
239		public void setConnectionDialogToBusy() {
240			// TODO Auto-generated method stub
241			
242		}
243
244		@Override
245		public void hideConnectionDialog() {
246			// TODO Auto-generated method stub
247			
248		}
249
250    }
251
252    private static class OfflineImplStub implements OfflineGateway {
253        AsyncCallback<Void> lastCallback;
254        String lastCall = "not yet been called";
255
256        @Override
257        public void install(AsyncCallback<Void> callback) {
258            onCalled("install", callback);
259        }
260
261        @Override
262        public void goOffline(AsyncCallback<Void> callback) {
263           onCalled("goOffline", callback);
264        }
265
266        @Override
267        public void goOnline(AsyncCallback<Void> callback) {
268            onCalled("goOnline", callback);
269        }
270
271        @Override
272        public void synchronize(AsyncCallback<Void> callback) {
273            onCalled("synchronize", callback);
274
275        }
276
277        @Override
278        public Date getLastSyncTime() {
279            return null;
280        }
281
282        private void onCalled(String method, AsyncCallback<Void> callback) {
283            lastCall = method;
284            lastCallback = callback;
285        }
286
287        @Override
288        public boolean validateOfflineInstalled() {
289            return true;
290        }
291
292
293		@Override
294		public boolean canHandle(Command command) {
295			// TODO Auto-generated method stub
296			return false;
297		}
298
299		@Override
300		public void execute(Command command, AsyncMonitor monitor,
301				AsyncCallback callback) {
302			// TODO Auto-generated method stub
303			
304		}
305    }
306}