Defer registration of singletons and pre-singletons in TestContext

In preparation for allowing the TestContext to enforce an ordering on
the registration of singletons and pre-singletons in the same way as it
does for mutators this defers the registration of them into the
underlying Context.

Bug: 181953909
Test: m nothing
Change-Id: I2d9652122bb6387b6b47ca4761e811885d15c2b6
diff --git a/android/testing.go b/android/testing.go
index d68d626..dab7e92 100644
--- a/android/testing.go
+++ b/android/testing.go
@@ -98,6 +98,9 @@
 	bp2buildPreArch, bp2buildDeps, bp2buildMutators []RegisterMutatorFunc
 	NameResolver                                    *NameResolver
 
+	// The list of pre-singletons and singletons registered for the test.
+	preSingletons, singletons sortableComponents
+
 	// The order in which the mutators will be run in this test context; for debugging.
 	mutatorOrder []string
 }
@@ -343,13 +346,18 @@
 func (ctx *TestContext) Register() {
 	globalOrder := globallyRegisteredComponentsOrder()
 
+	ctx.preSingletons.registerAll(ctx.Context)
+
 	mutators := collateRegisteredMutators(ctx.preArch, ctx.preDeps, ctx.postDeps, ctx.finalDeps)
 	// Ensure that the mutators used in the test are in the same order as they are used at runtime.
 	globalOrder.mutatorOrder.enforceOrdering(mutators)
 	mutators.registerAll(ctx.Context)
 
+	// Register the env singleton with this context before sorting.
 	ctx.RegisterSingletonType("env", EnvSingleton)
 
+	ctx.singletons.registerAll(ctx.Context)
+
 	// Save the mutator order away to make it easy to access while debugging.
 	ctx.mutatorOrder = globalOrder.mutatorOrder.namesInOrder
 }
@@ -382,11 +390,11 @@
 }
 
 func (ctx *TestContext) RegisterSingletonType(name string, factory SingletonFactory) {
-	ctx.Context.RegisterSingletonType(name, SingletonFactoryAdaptor(ctx.Context, factory))
+	ctx.singletons = append(ctx.singletons, newSingleton(name, factory))
 }
 
 func (ctx *TestContext) RegisterPreSingletonType(name string, factory SingletonFactory) {
-	ctx.Context.RegisterPreSingletonType(name, SingletonFactoryAdaptor(ctx.Context, factory))
+	ctx.preSingletons = append(ctx.preSingletons, newPreSingleton(name, factory))
 }
 
 func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {