public static void TestSearchAvailablePackagesAll(
            IntegrationTester.TestCase testCase,
            Action <IntegrationTester.TestCaseResult> testCaseComplete)
        {
            var testCaseResult = new IntegrationTester.TestCaseResult(testCase);

            UnityPackageManagerClient.SearchAvailablePackages(
                (result) => {
                // Make sure common optional Unity packages are returned in the search result.
                if (UpmSearchAllAvailable)
                {
                    CheckPackageNamesInPackageInfos(
                        new List <string>()
                    {
                        "com.unity.2d.animation",
                        "com.unity.entities"
                    },
                        result.Packages, testCaseResult,
                        "SearchAvailablePackages returned an unexpected set of packages");
                }

                var message = String.Format(
                    "Error: '{0}', PackageInfos:\n{1}\n", result.Error,
                    String.Join("\n", PackageInfoListToStringList(result.Packages).ToArray()));
                if (!String.IsNullOrEmpty(result.Error.ToString()))
                {
                    testCaseResult.ErrorMessages.Add(message);
                }
                else
                {
                    UnityEngine.Debug.Log(message);
                }
                testCaseComplete(testCaseResult);
            });
        }
        public static void TestAddAndRemovePackage(
            IntegrationTester.TestCase testCase,
            Action <IntegrationTester.TestCaseResult> testCaseComplete)
        {
            var          testCaseResult  = new IntegrationTester.TestCaseResult(testCase);
            const string packageToModify = "com.unity.ads";

            UnityPackageManagerClient.AddPackage(
                packageToModify,
                (result) => {
                if (UpmAvailable)
                {
                    CheckChangeResult(result.Error,
                                      result.Package != null ? result.Package.Name : null,
                                      packageToModify, testCaseResult);
                }
            });

            UnityPackageManagerClient.RemovePackage(
                packageToModify,
                (result) => {
                var message = UpmAvailable ?
                              CheckChangeResult(result.Error, result.PackageId, packageToModify,
                                                testCaseResult) : "";
                if (testCaseResult.ErrorMessages.Count == 0)
                {
                    UnityEngine.Debug.Log(message);
                }
                testCaseComplete(testCaseResult);
            });
        }
        public static void TestListInstalledPackages(
            IntegrationTester.TestCase testCase,
            Action <IntegrationTester.TestCaseResult> testCaseComplete)
        {
            var testCaseResult = new IntegrationTester.TestCaseResult(testCase);

            UnityPackageManagerClient.ListInstalledPackages((result) => {
                // Unity 2017.x doesn't install any default packages.
                if (ExecutionEnvironment.VersionMajorMinor >= 2018.0)
                {
                    // Make sure a subset of the default packages installed in all a newly created
                    // Unity project are present in the returned list.
                    CheckPackageNamesInPackageInfos(
                        new List <string>()
                    {
                        "com.unity.modules.audio",
                        "com.unity.modules.physics"
                    },
                        result.Packages, testCaseResult, "Found an unexpected set of packages");
                }
                var message = String.Format(
                    "Error: '{0}', PackageInfos:\n{1}\n",
                    result.Error,
                    String.Join("\n", PackageInfoListToStringList(result.Packages).ToArray()));
                if (!String.IsNullOrEmpty(result.Error.ToString()))
                {
                    testCaseResult.ErrorMessages.Add(message);
                }
                else
                {
                    UnityEngine.Debug.Log(message);
                }
                testCaseComplete(testCaseResult);
            });
        }
        public static void TestAvailable(IntegrationTester.TestCase testCase,
                                         Action <IntegrationTester.TestCaseResult> testCaseComplete)
        {
            var testCaseResult = new IntegrationTester.TestCaseResult(testCase);

            if (UpmAvailable != UnityPackageManagerClient.Available)
            {
                testCaseResult.ErrorMessages.Add(String.Format("UnityPackageManagerClient.Available " +
                                                               "returned {0}, expected {1}",
                                                               UnityPackageManagerClient.Available,
                                                               UpmAvailable));
            }
            testCaseComplete(testCaseResult);
        }
        public static void TestAddPackage(
            IntegrationTester.TestCase testCase,
            Action <IntegrationTester.TestCaseResult> testCaseComplete)
        {
            var          testCaseResult = new IntegrationTester.TestCaseResult(testCase);
            const string installPackage = "com.unity.analytics";

            UnityPackageManagerClient.AddPackage(
                installPackage,
                (result) => {
                var message = UpmAvailable ? CheckChangeResult(
                    result.Error, result.Package != null ? result.Package.Name : null,
                    installPackage, testCaseResult) : "";
                if (testCaseResult.ErrorMessages.Count == 0)
                {
                    UnityEngine.Debug.Log(message);
                }
                testCaseComplete(testCaseResult);
            });
        }
        public static void TestSearchAvailablePackages(
            IntegrationTester.TestCase testCase,
            Action <IntegrationTester.TestCaseResult> testCaseComplete)
        {
            var testCaseResult = new IntegrationTester.TestCaseResult(testCase);
            var progressLines  = new List <string>();

            UnityPackageManagerClient.SearchAvailablePackages(
                new [] {
                "com.unity.ads",
                "[email protected]"
            },
                (result) => {
                var expectedPackageNameByQuery = UpmAvailable ?
                                                 new Dictionary <string, string>()
                {
                    { "com.unity.ads", "com.unity.ads" },
                    { "[email protected]", "com.unity.analytics" },
                } : new Dictionary <string, string>();

                // Make sure all expected queries were performed.
                var queriesPerformed = new HashSet <string>(result.Keys);
                if (!queriesPerformed.SetEquals(expectedPackageNameByQuery.Keys))
                {
                    testCaseResult.ErrorMessages.Add(
                        String.Format(
                            "Search returned a subset of queries [{0}] vs. expected [{1}]",
                            String.Join(", ", (new List <string>(queriesPerformed)).ToArray()),
                            String.Join(", ", (new List <string>(
                                                   expectedPackageNameByQuery.Keys)).ToArray())));
                }

                var packageResults = new List <string>();
                foreach (var kv in result)
                {
                    var searchQuery  = kv.Key;
                    var searchResult = kv.Value;
                    packageResults.Add(String.Format(
                                           "{0}, Error: '{1}':\n{2}\n", searchQuery, searchResult.Error,
                                           String.Join("\n",
                                                       PackageInfoListToStringList(searchResult.Packages).ToArray())));

                    if (!String.IsNullOrEmpty(searchResult.Error.ToString()))
                    {
                        testCaseResult.ErrorMessages.Add(
                            String.Format("Failed when searching for '{0}', Error '{1}'",
                                          searchQuery, searchResult.Error.ToString()));
                    }

                    // Make sure returned packages match the search pattern.
                    string expectedPackageName;
                    if (expectedPackageNameByQuery.TryGetValue(searchQuery,
                                                               out expectedPackageName))
                    {
                        CheckPackageNamesInPackageInfos(
                            new List <string>()
                        {
                            expectedPackageName
                        }, searchResult.Packages,
                            testCaseResult,
                            String.Format("Returned an unexpected list of for search query '{0}'",
                                          searchQuery));
                    }
                    else
                    {
                        testCaseResult.ErrorMessages.Add(
                            String.Format("Unexpected search result returned '{0}'", searchQuery));
                    }
                }

                // Make sure progress was reported.
                if (progressLines.Count == 0)
                {
                    testCaseResult.ErrorMessages.Add("No progress reported");
                }

                var message = String.Format(String.Join("\n", packageResults.ToArray()));
                if (testCaseResult.ErrorMessages.Count == 0)
                {
                    UnityEngine.Debug.Log(message);
                }
                else
                {
                    testCaseResult.ErrorMessages.Add(message);
                }
                testCaseComplete(testCaseResult);
            },
                progress: (value, item) => {
                progressLines.Add(String.Format("Progress: {0}: {1}", value, item));
            });
        }
        public static void TestFindPackagesToMigrate(
            IntegrationTester.TestCase testCase,
            Action <IntegrationTester.TestCaseResult> testCaseComplete)
        {
            var testCaseResult = new IntegrationTester.TestCaseResult(testCase);

            // Collect progress reports.
            var progressLog = new List <KeyValuePair <float, string> >();

            PackageMigrator.PackageMap.FindPackagesProgressDelegate reportFindProgress =
                (progressValue, description) => {
                UnityEngine.Debug.Log(String.Format("Find progress {0}, {1}", progressValue,
                                                    description));
                progressLog.Add(new KeyValuePair <float, string>(progressValue, description));
            };

            PackageMigrator.PackageMap.FindPackagesToMigrate((error, packageMaps) => {
                if (CompleteIfNotAvailable(error, testCaseResult, testCaseComplete))
                {
                    return;
                }

                if (!String.IsNullOrEmpty(error))
                {
                    testCaseResult.ErrorMessages.Add(String.Format("Failed with error {0}", error));
                }

                var packageMapStrings    = new List <string>();
                var packageMapsByUpmName = new Dictionary <string, PackageMigrator.PackageMap>();
                foreach (var packageMap in packageMaps)
                {
                    packageMapsByUpmName[packageMap.AvailablePackageManagerPackageInfo.Name] =
                        packageMap;
                    packageMapStrings.Add(packageMap.ToString());
                }

                // Version-less mapping of UPM package name to Version Handler package names.
                var expectedVhNameAndUpmNames = new KeyValuePair <string, string>[] {
                    new KeyValuePair <string, string>("External Dependency Manager",
                                                      "com.google.external-dependency-manager"),
                    new KeyValuePair <string, string>("Firebase Authentication",
                                                      "com.google.firebase.auth")
                };
                foreach (var vhNameAndUpmName in expectedVhNameAndUpmNames)
                {
                    string expectedVhName  = vhNameAndUpmName.Key;
                    string expectedUpmName = vhNameAndUpmName.Value;
                    PackageMigrator.PackageMap packageMap;
                    if (packageMapsByUpmName.TryGetValue(expectedUpmName, out packageMap))
                    {
                        if (packageMap.VersionHandlerPackageName != expectedVhName)
                        {
                            testCaseResult.ErrorMessages.Add(String.Format(
                                                                 "Unexpected Version Handler package name '{0}' vs. '{1}' for '{2}'",
                                                                 packageMap.VersionHandlerPackageName, expectedVhName,
                                                                 expectedUpmName));
                        }
                        if (packageMap.AvailablePackageManagerPackageInfo.CalculateVersion() <
                            packageMap.VersionHandlerPackageCalculatedVersion)
                        {
                            testCaseResult.ErrorMessages.Add(String.Format(
                                                                 "Returned older UPM package {0} than VH package {1} for " +
                                                                 "{2} --> {3}",
                                                                 packageMap.AvailablePackageManagerPackageInfo.Version,
                                                                 packageMap.VersionHandlerPackageVersion, expectedVhName,
                                                                 expectedUpmName));
                        }
                    }
                    else
                    {
                        testCaseResult.ErrorMessages.Add(String.Format(
                                                             "Package map {0} --> {1} not found.", expectedVhName, expectedUpmName));
                    }
                }

                if (packageMaps.Count != expectedVhNameAndUpmNames.Length)
                {
                    testCaseResult.ErrorMessages.Add(
                        String.Format("Migrator returned unexpected package maps:\n{0}",
                                      String.Join("\n", packageMapStrings.ToArray())));
                }

                if (progressLog.Count == 0)
                {
                    testCaseResult.ErrorMessages.Add("No progress updates");
                }
                testCaseComplete(testCaseResult);
            }, reportFindProgress);
        }
        public static void TestMigration(
            IntegrationTester.TestCase testCase,
            Action <IntegrationTester.TestCaseResult> testCaseComplete)
        {
#if EDM_WITH_MIGRATION_NOT_AVAILABLE_ON_UPM
            testCaseComplete(new IntegrationTester.TestCaseResult(testCase)
            {
                Skipped = true
            });
            return;
#endif
            var testCaseResult = new IntegrationTester.TestCaseResult(testCase);
            PackageMigrator.TryMigration((error) => {
                if (CompleteIfNotAvailable(error, testCaseResult, testCaseComplete))
                {
                    return;
                }
                if (!String.IsNullOrEmpty(error))
                {
                    testCaseResult.ErrorMessages.Add(String.Format(
                                                         "Migration failed with error {0}", error));
                }

                // Make sure only expected version handler packages are still installed.
                var expectedVersionHandlerPackages = new HashSet <string>()
                {
                    "Firebase Realtime Database"
                };
                var manifestsByPackageName = new HashSet <string>(
                    VersionHandlerImpl.ManifestReferences.
                    FindAndReadManifestsInAssetsFolderByPackageName().Keys);
                manifestsByPackageName.ExceptWith(expectedVersionHandlerPackages);
                if (manifestsByPackageName.Count > 0)
                {
                    testCaseResult.ErrorMessages.Add(String.Format(
                                                         "Unexpected version handler packages found in the project:\n{0}",
                                                         (new List <string>(manifestsByPackageName)).ToArray()));
                }

                // Make sure the expected UPM packages are installed.
                PackageManagerClient.ListInstalledPackages((listResult) => {
                    var installedPackageNames = new HashSet <string>();
                    foreach (var pkg in listResult.Packages)
                    {
                        installedPackageNames.Add(pkg.Name);
                    }

                    // Make sure expected UPM packages are installed.
                    var expectedPackageNames = new List <string>()
                    {
                        "com.google.external-dependency-manager",
                        "com.google.firebase.auth"
                    };
                    if (installedPackageNames.IsSupersetOf(expectedPackageNames))
                    {
                        testCaseResult.ErrorMessages.Add(String.Format(
                                                             "Expected packages [{0}] not installed",
                                                             String.Join(", ", expectedPackageNames.ToArray())));
                    }

                    testCaseComplete(testCaseResult);
                });
            });
        }