// 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));
}