Bug 262446 -  [ui] Check that nested categories still work
diff --git a/bundles/org.eclipse.equinox.p2.tests.ui/src/org/eclipse/equinox/p2/tests/ui/query/AllTests.java b/bundles/org.eclipse.equinox.p2.tests.ui/src/org/eclipse/equinox/p2/tests/ui/query/AllTests.java
index 0fd24d7..60b4705 100644
--- a/bundles/org.eclipse.equinox.p2.tests.ui/src/org/eclipse/equinox/p2/tests/ui/query/AllTests.java
+++ b/bundles/org.eclipse.equinox.p2.tests.ui/src/org/eclipse/equinox/p2/tests/ui/query/AllTests.java
@@ -25,6 +25,7 @@
 		suite.addTestSuite(TranslationSupportTests.class);
 		suite.addTestSuite(LatestIUVersionElementWrapperTest.class);
 		suite.addTestSuite(QueryDescriptorTest.class);
+		suite.addTestSuite(QueryProviderTests.class);
 		suite.addTestSuite(QueryableMetadataRepositoryManagerTest.class);
 		// This must come after QueryableMetadataRepositoryManager or it causes side-effects in those tests.
 		suite.addTestSuite(QueryableArtifactRepositoryManagerTest.class);
diff --git a/bundles/org.eclipse.equinox.p2.tests.ui/src/org/eclipse/equinox/p2/tests/ui/query/QueryProviderTests.java b/bundles/org.eclipse.equinox.p2.tests.ui/src/org/eclipse/equinox/p2/tests/ui/query/QueryProviderTests.java
new file mode 100644
index 0000000..064f85a
--- /dev/null
+++ b/bundles/org.eclipse.equinox.p2.tests.ui/src/org/eclipse/equinox/p2/tests/ui/query/QueryProviderTests.java
@@ -0,0 +1,79 @@
+/*******************************************************************************
+ * Copyright (c) 2009 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * 
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.equinox.p2.tests.ui.query;
+
+import java.util.HashMap;
+import org.eclipse.equinox.internal.p2.ui.model.*;
+import org.eclipse.equinox.p2.core.ProvisionException;
+import org.eclipse.equinox.p2.metadata.IInstallableUnit;
+import org.eclipse.equinox.p2.metadata.Version;
+import org.eclipse.equinox.p2.operations.InstallOperation;
+import org.eclipse.equinox.p2.query.IQueryable;
+import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;
+import org.eclipse.equinox.p2.tests.ui.AbstractProvisioningUITest;
+
+public class QueryProviderTests extends AbstractProvisioningUITest {
+	IInstallableUnit category, nestedCategory;
+	IInstallableUnit a, b, c;
+	static final String CAT = "Category";
+	static final String NESTED = "NestedCategory";
+	static final String A = "A";
+	static final String B = "B";
+	static final String C = "C";
+	IMetadataRepository testRepo;
+
+	protected void setUp() throws Exception {
+		super.setUp();
+		HashMap categoryProperties = new HashMap();
+		categoryProperties.put("org.eclipse.equinox.p2.type.category", "true");
+		HashMap groupProperties = new HashMap();
+		groupProperties.put("org.eclipse.equinox.p2.type.group", "true");
+		category = createIU(CAT, Version.create("1.0.0"), createRequiredCapabilities(IInstallableUnit.NAMESPACE_IU_ID, NESTED), categoryProperties, true);
+		nestedCategory = createIU(NESTED, Version.create("1.0.0"), createRequiredCapabilities(IInstallableUnit.NAMESPACE_IU_ID, A), categoryProperties, true);
+		a = createIU(A, Version.create("1.0.0"), createRequiredCapabilities(IInstallableUnit.NAMESPACE_IU_ID, B), groupProperties, true);
+		b = createIU(B, Version.create("1.0.0"), createRequiredCapabilities(IInstallableUnit.NAMESPACE_IU_ID, C), groupProperties, true);
+		c = createIU(C, Version.create("1.0.0"), NO_REQUIRES, NO_PROPERTIES, true);
+		testRepo = createTestMetdataRepository(new IInstallableUnit[] {category, nestedCategory, a, b, c});
+	}
+
+	public void testNestedCategories() throws ProvisionException {
+		MetadataRepositoryElement element = new MetadataRepositoryElement(null, testRepo.getLocation(), true);
+		Object[] children = element.getChildren(element);
+		assertEquals("1.1", 1, children.length); // the nested category should get removed from the list
+		assertTrue("1.2", children[0] instanceof CategoryElement);
+		CategoryElement cat = (CategoryElement) children[0];
+		children = cat.getChildren(cat);
+		boolean foundNestedCategory = false;
+		for (int i = 0; i < children.length; i++) {
+			if (children[i] instanceof CategoryElement) {
+				if (((CategoryElement) children[i]).getIU().equals(nestedCategory)) {
+					foundNestedCategory = true;
+					break;
+				}
+			}
+		}
+		assertTrue("1.3", foundNestedCategory);
+	}
+
+	public void testInstallDrilldown() throws ProvisionException {
+		IUElementListRoot root = new IUElementListRoot();
+		AvailableIUElement element = new AvailableIUElement(root, a, TESTPROFILE, getPolicy().getShowDrilldownRequirements());
+		root.setChildren(new Object[] {element});
+		InstallOperation op = new InstallOperation(getSession(), new IInstallableUnit[] {a});
+		op.setProfileId(TESTPROFILE);
+		op.resolveModal(getMonitor());
+		IQueryable queryable = op.getProvisioningPlan().getAdditions();
+		element.setQueryable(queryable);
+		Object[] children = element.getChildren(element);
+		assertTrue("1.1", children.length == 1);
+	}
+
+}