Merge "Ensure test/runtime order of singletons/pre-singletons is consistent"
diff --git a/android/testing.go b/android/testing.go
index dab7e92..042fa2b 100644
--- a/android/testing.go
+++ b/android/testing.go
@@ -101,8 +101,9 @@
 	// 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
+	// The order in which the pre-singletons, mutators and singletons will be run in this test
+	// context; for debugging.
+	preSingletonOrder, mutatorOrder, singletonOrder []string
 }
 
 func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) {
@@ -315,8 +316,14 @@
 	// Used to ensure that this is only created once.
 	once sync.Once
 
+	// The order of pre-singletons
+	preSingletonOrder registeredComponentOrder
+
 	// The order of mutators
 	mutatorOrder registeredComponentOrder
+
+	// The order of singletons
+	singletonOrder registeredComponentOrder
 }
 
 // populate initializes this structure from globally registered build components.
@@ -324,9 +331,16 @@
 // Only the first call has any effect.
 func (s *registrationSorter) populate() {
 	s.once.Do(func() {
+		// Create an ordering from the globally registered pre-singletons.
+		s.preSingletonOrder = registeredComponentOrderFromExistingOrder("pre-singleton", preSingletons)
+
 		// Created an ordering from the globally registered mutators.
 		globallyRegisteredMutators := collateGloballyRegisteredMutators()
 		s.mutatorOrder = registeredComponentOrderFromExistingOrder("mutator", globallyRegisteredMutators)
+
+		// Create an ordering from the globally registered singletons.
+		globallyRegisteredSingletons := collateGloballyRegisteredSingletons()
+		s.singletonOrder = registeredComponentOrderFromExistingOrder("singleton", globallyRegisteredSingletons)
 	})
 }
 
@@ -346,6 +360,9 @@
 func (ctx *TestContext) Register() {
 	globalOrder := globallyRegisteredComponentsOrder()
 
+	// Ensure that the pre-singletons used in the test are in the same order as they are used at
+	// runtime.
+	globalOrder.preSingletonOrder.enforceOrdering(ctx.preSingletons)
 	ctx.preSingletons.registerAll(ctx.Context)
 
 	mutators := collateRegisteredMutators(ctx.preArch, ctx.preDeps, ctx.postDeps, ctx.finalDeps)
@@ -356,10 +373,14 @@
 	// Register the env singleton with this context before sorting.
 	ctx.RegisterSingletonType("env", EnvSingleton)
 
+	// Ensure that the singletons used in the test are in the same order as they are used at runtime.
+	globalOrder.singletonOrder.enforceOrdering(ctx.singletons)
 	ctx.singletons.registerAll(ctx.Context)
 
-	// Save the mutator order away to make it easy to access while debugging.
+	// Save the sorted components order away to make them easy to access while debugging.
+	ctx.preSingletonOrder = globalOrder.preSingletonOrder.namesInOrder
 	ctx.mutatorOrder = globalOrder.mutatorOrder.namesInOrder
+	ctx.singletonOrder = globalOrder.singletonOrder.namesInOrder
 }
 
 // RegisterForBazelConversion prepares a test context for bp2build conversion.