AppStateManager implements WindowActionHandler, ProjectActionHandler { public void start(boolean openRecentProject) { eventBus.addHandler(WindowActionEvent.TYPE, this); eventBus.addHandler(ProjectActionEvent.TYPE, this); readStateFromPreferences(); openRecentProject(openRecentProject); } @Inject  AppStateManager(Set<PersistenceComponent> persistenceComponents,
                           Map<String, PersistenceComponent> projectTreePersistenceComponents,
                           EventBus eventBus,
                           PreferencesManager preferencesManager,
                           AppContext appContext,
                           DtoFactory dtoFactory,
                           ActionManager actionManager,
                           PresentationFactory presentationFactory,
                           ProjectServiceClient projectServiceClient); @Override void onWindowClosing(WindowActionEvent event); @Override void onWindowClosed(WindowActionEvent event); @Override void onProjectReady(ProjectActionEvent event); @Override void onProjectClosing(ProjectActionEvent event); @Override void onProjectClosed(ProjectActionEvent event); @Override void onProjectOpened(ProjectActionEvent event); void start(boolean openRecentProject);  }
@Test public void shouldAddEventHandlers() { appStateManager.start(false); verify(eventBus).addHandler(eq(WindowActionEvent.TYPE), eq(appStateManager)); verify(eventBus).addHandler(eq(ProjectActionEvent.TYPE), eq(appStateManager)); verify(eventBus).addHandler(eq(PersistProjectTreeStateEvent.TYPE), (PersistProjectTreeStateHandler) anyObject()); verify(eventBus).addHandler(eq(RestoreProjectTreeStateEvent.TYPE), (RestoreProjectTreeStateHandler) anyObject()); }
@Test public void shouldDeserializeStateOnStart() { appStateManager.start(false); verify(dtoFactory).createDtoFromJson(eq(SERIALIZED_STATE), Matchers.<Class<AppState>>anyObject()); }
@Test public void shouldOpenRecentlyProjectOnStart() throws Exception { appStateManager.start(true); ProjectDescriptor result = mock(ProjectDescriptor.class); verify(projectServiceClient).getProject(anyString(), projectDescriptorCaptor.capture()); AsyncRequestCallback<ProjectDescriptor> asyncRequestCallback = projectDescriptorCaptor.getValue(); Method method = asyncRequestCallback.getClass().getDeclaredMethod("onSuccess", Object.class); method.setAccessible(true); method.invoke(asyncRequestCallback, result); verify(eventBus).fireEvent(any(OpenProjectEvent.class)); }
@Test public void userPreferenceShouldBeCleanedUp() throws Exception { appStateManager.start(true); Throwable exception = mock(Throwable.class); verify(projectServiceClient).getProject(anyString(), projectDescriptorCaptor.capture()); AsyncRequestCallback<ProjectDescriptor> asyncRequestCallback = projectDescriptorCaptor.getValue(); Method method = asyncRequestCallback.getClass().getDeclaredMethod("onFailure", Throwable.class); method.setAccessible(true); method.invoke(asyncRequestCallback, exception); verify(appState).getProjects(); verify(appState, times(2)).getRecentProject(); verify(recentProject).setPath(""); verify(dtoFactory).toJson(appState); preferencesManager.setValue(PREFERENCE_PROPERTY_NAME, SERIALIZED_STATE); verify(preferencesManager).flushPreferences(Matchers.<AsyncCallback<Map<String, String>>>anyObject()); }
@Test public void projectTreeShouldBePersisted() { appStateManager.start(false); PersistProjectTreeStateEvent projectTreeStateEvent = mock(PersistProjectTreeStateEvent.class); verify(eventBus).addHandler(eq(PersistProjectTreeStateEvent.TYPE), projectTreeStateHandlerArgCaptor.capture()); projectTreeStateHandlerArgCaptor.getValue().onPersist(projectTreeStateEvent); verify(appContext).getCurrentProject(); verify(currentProject).getRootProject(); verify(rootProject).getPath(); verify(rootProject).getWorkspaceName(); verify(dtoFactory).createDto(ProjectState.class); verify(appState).getProjects(); verify(projectState).getActions(); verify(persistenceComponent).getActions(PROJECT_PATH); }
@Test public void projectTreeStateShouldBeRestored() { RestoreProjectTreeStateEvent event = mock(RestoreProjectTreeStateEvent.class); when(event.getProjectPath()).thenReturn("/" + WORKSPACE_NAME + PROJECT_PATH); appStateManager.start(false); verify(eventBus).addHandler(eq(RestoreProjectTreeStateEvent.TYPE), treeStateHandlerArgumentCaptor.capture()); treeStateHandlerArgumentCaptor.getValue().onRestore(event); verify(event).getProjectPath(); verify(appState).getProjects(); verify(projectState).getActions(); verify(actionManager).getAction(SERIALIZED_STATE); verify(presentationFactory).getPresentation(action); verify(actionDescriptor).getParameters(); verify(actionManager).performActions(Matchers.<List<Pair<Action, ActionEvent>>>anyObject(), eq(false)); verify(promise).catchError(Matchers.<Operation<PromiseError>>anyObject()); }
@Test public void preferenceShouldBeCleanBecauseAppStateJsonIsNull() { when(preferencesManager.getValue(PREFERENCE_PROPERTY_NAME)).thenReturn(null); appStateManager.start(false); verify(eventBus).addHandler(WindowActionEvent.TYPE, appStateManager); verify(eventBus).addHandler(ProjectActionEvent.TYPE, appStateManager); verify(preferencesManager).getValue(PREFERENCE_PROPERTY_NAME); verify(dtoFactory).createDto(AppState.class); verify(dtoFactory).createDto(RecentProject.class); verify(recentProject).setPath(""); verify(recentProject).setWorkspaceId(""); verify(appState).setRecentProject(recentProject); }
@Test public void preferenceShouldBeCleanBecauseAppStateJsonIsNull2() { when(appState.getRecentProject()).thenReturn(null); appStateManager.start(false); verify(eventBus).addHandler(WindowActionEvent.TYPE, appStateManager); verify(eventBus).addHandler(ProjectActionEvent.TYPE, appStateManager); verify(preferencesManager).getValue(PREFERENCE_PROPERTY_NAME); verify(dtoFactory).createDtoFromJson(anyString(), eq(AppState.class)); verify(dtoFactory).createDto(RecentProject.class); verify(appState).getRecentProject(); verify(recentProject).setPath(""); verify(recentProject).setWorkspaceId(""); verify(appState).setRecentProject(recentProject); }