@Test
public void testImportMenus() {
// Create test objects
ContextAwareMenu thisMenu = new ThisImportMenu();
ContextAwareMenu objsMenu = new ObjectsImportMenu();
thisMenu.initialize();
objsMenu.initialize();
// Name is specified in code, so verify it
Assert.assertEquals(thisMenu.getText(), "Import");
Assert.assertEquals(objsMenu.getText(), "Import");
// Verify that expected extension points are published
String[] thisExts = thisMenu.getExtensionMenubarPaths();
String[] objsExts = objsMenu.getExtensionMenubarPaths();
Assert.assertEquals(thisExts[0], "/this/import.ext");
Assert.assertEquals(objsExts[0], "/objects/import.ext");
// Should only handle single, writable targets...
// Set up mocks
ActionContext mockContext = Mockito.mock(ActionContext.class);
PolicyManager mockPolicy = Mockito.mock(PolicyManager.class);
AbstractComponent mockComponent = Mockito.mock(AbstractComponent.class);
View mockView = Mockito.mock(View.class);
String compositionKey = PolicyInfo.CategoryType.COMPOSITION_POLICY_CATEGORY.getKey();
Mockito.when(mockPlatform.getPolicyManager()).thenReturn(mockPolicy);
// First, This's Import menu
// No window active - should disallow
Mockito.when(mockContext.getWindowManifestation()).thenReturn(null);
Assert.assertFalse(thisMenu.canHandle(mockContext));
// Active window but component is null - should disallow
Mockito.when(mockContext.getWindowManifestation()).thenReturn(mockView);
Mockito.when(mockView.getManifestedComponent()).thenReturn(null);
Assert.assertFalse(thisMenu.canHandle(mockContext));
// Has a window, but policy says no - should disallow
Mockito.when(mockContext.getWindowManifestation()).thenReturn(mockView);
Mockito.when(mockView.getManifestedComponent()).thenReturn(mockComponent);
Mockito.when(mockPolicy.execute(Mockito.eq(compositionKey), Mockito.<PolicyContext>any()))
.thenReturn(new ExecutionResult(null, false, ""));
Assert.assertFalse(thisMenu.canHandle(mockContext));
// If policy allows, then canHandle should be true
Mockito.when(mockPolicy.execute(Mockito.eq(compositionKey), Mockito.<PolicyContext>any()))
.thenReturn(new ExecutionResult(null, true, ""));
Assert.assertTrue(thisMenu.canHandle(mockContext));
// Second, Objects's Import menu
// Null selections - should disallow
Mockito.when(mockContext.getSelectedManifestations()).thenReturn(null);
Assert.assertFalse(objsMenu.canHandle(mockContext));
// Empty selections - should disallow
Mockito.when(mockContext.getSelectedManifestations())
.thenReturn(Collections.<View>emptyList());
Assert.assertFalse(objsMenu.canHandle(mockContext));
// One selection but policy says no - should disallow
Mockito.when(mockContext.getSelectedManifestations())
.thenReturn(Arrays.asList(mockView));
Mockito.when(mockView.getManifestedComponent()).thenReturn(mockComponent);
Mockito.when(mockPolicy.execute(Mockito.eq(compositionKey), Mockito.<PolicyContext>any()))
.thenReturn(new ExecutionResult(null, false, ""));
Assert.assertFalse(objsMenu.canHandle(mockContext));
// One selection but policy says yes - should allow!
Mockito.when(mockContext.getSelectedManifestations())
.thenReturn(Arrays.asList(mockView));
Mockito.when(mockView.getManifestedComponent()).thenReturn(mockComponent);
Mockito.when(mockPolicy.execute(Mockito.eq(compositionKey), Mockito.<PolicyContext>any()))
.thenReturn(new ExecutionResult(null, true, ""));
Assert.assertTrue(objsMenu.canHandle(mockContext));
// Multiple selections, even though policy says yes - should disallow
Mockito.when(mockContext.getSelectedManifestations())
.thenReturn(Arrays.asList(mockView, mockView));
Mockito.when(mockView.getManifestedComponent()).thenReturn(mockComponent);
Mockito.when(mockPolicy.execute(Mockito.eq(compositionKey), Mockito.<PolicyContext>any()))
.thenReturn(new ExecutionResult(null, true, ""));
Assert.assertFalse(objsMenu.canHandle(mockContext));
}