/// <summary>
        /// Extract a zip file.
        /// </summary>
        /// <param name="zipFile">File to extract.</param>
        /// <param name="failureMessages">List to add any failure messages to.</param>
        /// <returns>Directory containing unzipped files if successful, null otherwise.</returns>
        private static string ExtractZip(string zipFile, List <string> failureMessages)
        {
            string outputDir = Path.Combine(Path.Combine(Path.GetTempPath(),
                                                         Path.GetRandomFileName()),
                                            Path.GetFileName(zipFile));

            Directory.CreateDirectory(outputDir);
            // This uses reflection to access an internal method for testing purposes.
            // ExtractZip is not part of the public API.
            bool successful = PlayServicesResolver.ExtractZip(zipFile, null, outputDir, false);

            if (!successful)
            {
                failureMessages.Add(String.Format("Unable to extract {0} to {1}",
                                                  zipFile, outputDir));
                Directory.Delete(outputDir, true);
                return(null);
            }
            return(outputDir);
        }
예제 #2
0
        public static bool DoSetup(string appID)
        {
            // check for valid app id
            if (!GPGSUtil.LooksLikeValidAppId(appID))
            {
                GPGSUtil.Alert(GPGSStrings.Setup.AppIdError);
                return(false);
            }

            if (!GPGSUtil.HasAndroidSdk())
            {
                EditorUtility.DisplayDialog(
                    GPGSStrings.AndroidSetup.SdkNotFound,
                    GPGSStrings.AndroidSetup.SdkNotFoundBlurb,
                    GPGSStrings.Ok);
                return(false);
            }

            // Generate AndroidManifest.xml
            var destination  = GPGSUtil.SlashesToPlatformSeparator(CloudOncePaths.GooglePlayLib + "/AndroidManifest.xml");
            var manifestBody = GPGSUtil.ReadFile(manifestTemplate);

            manifestBody = manifestBody.Replace(appIdPlaceholder, appID);
#if UNITY_ANDROID
            manifestBody = manifestBody.Replace(pluginVersionPlaceholder, PluginVersion.VersionString);
#endif
            manifestBody = manifestBody.Replace(serviceIdPlaceholder, string.Empty);
            GPGSUtil.WriteFile(destination, manifestBody);

            // Resolve dependencies
            Google.VersionHandler.UpdateVersionedAssets(true);
            Google.VersionHandler.Enabled = true;
            AssetDatabase.Refresh();
#if UNITY_ANDROID
            PlayServicesResolver.MenuResolve();
#endif

            // refresh assets, and we're done
            AssetDatabase.Refresh();
            return(EditorUtility.DisplayDialog(GPGSStrings.Success, GPGSStrings.AndroidSetup.SetupComplete, GPGSStrings.Ok));
        }
 /// <summary>
 /// Validate Android libraries and repos are setup correctly.
 /// </summary>
 /// <param name="testCaseResult">TestCaseResult instance to add errors to if this method
 /// fails. </param>
 private static void ValidateDependencies(IntegrationTester.TestCaseResult testCaseResult)
 {
     // Validate set dependencies are present.
     CompareKeyValuePairLists(
         new List <KeyValuePair <string, string> >()
     {
         new KeyValuePair <string, string>(
             "com.android.support:support-annotations:26.1.0",
             "Assets/ExternalDependencyManager/Editor/TestDependencies.xml:4"),
         new KeyValuePair <string, string>(
             "com.google.firebase:firebase-app-unity:5.1.1",
             "Assets/ExternalDependencyManager/Editor/TestDependencies.xml:10"),
         new KeyValuePair <string, string>(
             "com.google.firebase:firebase-common:16.0.0",
             "Google.AndroidResolverIntegrationTests.SetupDependencies"),
         new KeyValuePair <string, string>(
             "org.test.psr:classifier:1.0.1:foo@aar",
             "Assets/ExternalDependencyManager/Editor/TestDependencies.xml:12"),
     },
         PlayServicesResolver.GetPackageSpecs(),
         "Package Specs", testCaseResult);
     // Validate configured repos are present.
     CompareKeyValuePairLists(
         new List <KeyValuePair <string, string> >()
     {
         new KeyValuePair <string, string>(
             "file:///my/nonexistant/test/repo",
             "Assets/ExternalDependencyManager/Editor/TestDependencies.xml:17"),
         new KeyValuePair <string, string>(
             "file:///" + Path.GetFullPath("project_relative_path/repo").Replace("\\", "/"),
             "Assets/ExternalDependencyManager/Editor/TestDependencies.xml:17"),
         new KeyValuePair <string, string>(
             "file:///" + Path.GetFullPath(
                 "Assets/Firebase/m2repository").Replace("\\", "/"),
             "Assets/ExternalDependencyManager/Editor/TestDependencies.xml:10")
     },
         PlayServicesResolver.GetRepos(),
         "Repos", testCaseResult);
 }
예제 #4
0
 private static void PrepareResolver()
 {
     // Force playServices Resolver
     PlayServicesResolver.Resolve(null, true);
 }
        public static void ConfigureTestCases()
        {
            // Set of files to ignore (relative to the Assets/Plugins/Android directory) in all tests
            // that do not use the Gradle template.
            var nonGradleTemplateFilesToIgnore = new HashSet <string>()
            {
                Path.GetFileName(GRADLE_TEMPLATE_DISABLED),
                Path.GetFileName(GRADLE_TEMPLATE_LIBRARY_DISABLED)
            };

            UnityEngine.Debug.Log("Setting up test cases for execution.");
            IntegrationTester.Runner.ScheduleTestCases(new [] {
                // This *must* be the first test case as other test cases depend upon it.
                new IntegrationTester.TestCase {
                    Name   = "ValidateAndroidTargetSelected",
                    Method = ValidateAndroidTargetSelected,
                },
                new IntegrationTester.TestCase {
                    Name   = "SetupDependencies",
                    Method = (testCase, testCaseComplete) => {
                        ClearAllDependencies();
                        SetupDependencies();

                        var testCaseResult = new IntegrationTester.TestCaseResult(testCase);
                        ValidateDependencies(testCaseResult);
                        testCaseComplete(testCaseResult);
                    }
                },
                new IntegrationTester.TestCase {
                    Name   = "ResolveForGradleBuildSystemWithTemplate",
                    Method = (testCase, testCaseComplete) => {
                        ClearAllDependencies();
                        SetupDependencies();

                        ResolveWithGradleTemplate(
                            GRADLE_TEMPLATE_DISABLED,
                            "ExpectedArtifacts/NoExport/GradleTemplate",
                            testCase, testCaseComplete,
                            otherExpectedFiles: new [] {
                            "Assets/Firebase/m2repository/com/google/firebase/" +
                            "firebase-app-unity/5.1.1/firebase-app-unity-5.1.1.aar"
                        },
                            filesToIgnore: new HashSet <string> {
                            Path.GetFileName(GRADLE_TEMPLATE_LIBRARY_DISABLED)
                        });
                    }
                },
                new IntegrationTester.TestCase {
                    Name   = "ResolverForGradleBuildSystemWithTemplateUsingJetifier",
                    Method = (testCase, testCaseComplete) => {
                        ClearAllDependencies();
                        SetupDependencies();
                        GooglePlayServices.SettingsDialog.UseJetifier = true;

                        ResolveWithGradleTemplate(
                            GRADLE_TEMPLATE_DISABLED,
                            "ExpectedArtifacts/NoExport/GradleTemplateJetifier",
                            testCase, testCaseComplete,
                            otherExpectedFiles: new [] {
                            "Assets/Firebase/m2repository/com/google/firebase/" +
                            "firebase-app-unity/5.1.1/firebase-app-unity-5.1.1.aar"
                        },
                            filesToIgnore: new HashSet <string> {
                            Path.GetFileName(GRADLE_TEMPLATE_LIBRARY_DISABLED)
                        });
                    }
                },
                new IntegrationTester.TestCase {
                    Name   = "ResolveForGradleBuildSystemLibraryWithTemplate",
                    Method = (testCase, testCaseComplete) => {
                        ClearAllDependencies();
                        SetupDependencies();

                        ResolveWithGradleTemplate(
                            GRADLE_TEMPLATE_LIBRARY_DISABLED,
                            "ExpectedArtifacts/NoExport/GradleTemplateLibrary",
                            testCase, testCaseComplete,
                            otherExpectedFiles: new [] {
                            "Assets/Firebase/m2repository/com/google/firebase/" +
                            "firebase-app-unity/5.1.1/firebase-app-unity-5.1.1.aar"
                        },
                            filesToIgnore: new HashSet <string> {
                            Path.GetFileName(GRADLE_TEMPLATE_DISABLED)
                        });
                    }
                },
                new IntegrationTester.TestCase {
                    Name   = "ResolveForGradleBuildSystemWithTemplateEmpty",
                    Method = (testCase, testCaseComplete) => {
                        string enabledDependencies =
                            "Assets/ExternalDependencyManager/Editor/TestDependencies.xml";
                        string disabledDependencies =
                            "Assets/ExternalDependencyManager/Editor/TestDependenciesDISABLED.xml";
                        Action enableDependencies = () => {
                            UnityEditor.AssetDatabase.MoveAsset(disabledDependencies,
                                                                enabledDependencies);
                        };
                        try {
                            // Disable all XML dependencies.
                            var error = UnityEditor.AssetDatabase.MoveAsset(enabledDependencies,
                                                                            disabledDependencies);
                            if (!String.IsNullOrEmpty(error))
                            {
                                testCaseComplete(new IntegrationTester.TestCaseResult(testCase)
                                {
                                    ErrorMessages = new List <string>()
                                    {
                                        error
                                    }
                                });
                                return;
                            }
                            ClearAllDependencies();
                            ResolveWithGradleTemplate(
                                GRADLE_TEMPLATE_DISABLED,
                                "ExpectedArtifacts/NoExport/GradleTemplateEmpty",
                                testCase, (testCaseResult) => {
                                enableDependencies();
                                testCaseComplete(testCaseResult);
                            },
                                filesToIgnore: new HashSet <string> {
                                Path.GetFileName(GRADLE_TEMPLATE_LIBRARY_DISABLED)
                            });
                        } finally {
                            enableDependencies();
                        }
                    }
                },
                new IntegrationTester.TestCase {
                    Name   = "ResolveForGradleBuildSystem",
                    Method = (testCase, testCaseComplete) => {
                        ClearAllDependencies();
                        SetupDependencies();
                        Resolve("Gradle", false, "ExpectedArtifacts/NoExport/Gradle",
                                null, nonGradleTemplateFilesToIgnore, testCase, testCaseComplete);
                    }
                },
                new IntegrationTester.TestCase {
                    Name   = "ResolveForGradleBuildSystemSync",
                    Method = (testCase, testCaseComplete) => {
                        ClearAllDependencies();
                        SetupDependencies();
                        Resolve("Gradle", false, "ExpectedArtifacts/NoExport/Gradle",
                                null, nonGradleTemplateFilesToIgnore, testCase, testCaseComplete,
                                synchronous: true);
                    }
                },
                new IntegrationTester.TestCase {
                    Name   = "ResolveForInternalBuildSystem",
                    Method = (testCase, testCaseComplete) => {
                        ClearAllDependencies();
                        SetupDependencies();
                        Resolve("Internal", false,
                                AarsWithNativeLibrariesSupported ?
                                "ExpectedArtifacts/NoExport/InternalNativeAars" :
                                "ExpectedArtifacts/NoExport/InternalNativeAarsExploded",
                                null, nonGradleTemplateFilesToIgnore, testCase, testCaseComplete);
                    }
                },
                new IntegrationTester.TestCase {
                    Name   = "ResolveForInternalBuildSystemUsingJetifier",
                    Method = (testCase, testCaseComplete) => {
                        ClearAllDependencies();
                        SetupDependencies();
                        GooglePlayServices.SettingsDialog.UseJetifier = true;
                        Resolve("Internal", false,
                                AarsWithNativeLibrariesSupported ?
                                "ExpectedArtifacts/NoExport/InternalNativeAarsJetifier" :
                                "ExpectedArtifacts/NoExport/InternalNativeAarsExplodedJetifier",
                                null, nonGradleTemplateFilesToIgnore, testCase, testCaseComplete);
                    }
                },
                new IntegrationTester.TestCase {
                    Name   = "ResolveForGradleBuildSystemAndExport",
                    Method = (testCase, testCaseComplete) => {
                        ClearAllDependencies();
                        SetupDependencies();
                        Resolve("Gradle", true, "ExpectedArtifacts/Export/Gradle",
                                null, nonGradleTemplateFilesToIgnore, testCase, testCaseComplete);
                    }
                },
                new IntegrationTester.TestCase {
                    Name   = "ResolveAddedDependencies",
                    Method = (testCase, testCaseComplete) => {
                        ClearAllDependencies();
                        SetupDependencies();
                        UpdateAdditionalDependenciesFile(true);
                        Resolve("Gradle", true, "ExpectedArtifacts/Export/GradleAddedDeps",
                                null, nonGradleTemplateFilesToIgnore, testCase, testCaseComplete);
                    }
                },
                new IntegrationTester.TestCase {
                    Name   = "ResolveRemovedDependencies",
                    Method = (testCase, testCaseComplete) => {
                        ClearAllDependencies();
                        SetupDependencies();
                        // Add the additional dependencies file then immediately remove it.
                        UpdateAdditionalDependenciesFile(true);
                        UpdateAdditionalDependenciesFile(false);
                        Resolve("Gradle", true, "ExpectedArtifacts/Export/Gradle",
                                null, nonGradleTemplateFilesToIgnore, testCase, testCaseComplete);
                    }
                },
                new IntegrationTester.TestCase {
                    Name   = "DeleteResolvedLibraries",
                    Method = (testCase, testCaseComplete) => {
                        ClearAllDependencies();
                        SetupDependencies();
                        Resolve("Gradle", true, "ExpectedArtifacts/Export/Gradle",
                                null, nonGradleTemplateFilesToIgnore,
                                testCase, (testCaseResult) => {
                            PlayServicesResolver.DeleteResolvedLibrariesSync();
                            var unexpectedFilesMessage = new List <string>();
                            var resolvedFiles          = ListFiles("Assets/Plugins/Android",
                                                                   nonGradleTemplateFilesToIgnore);
                            if (resolvedFiles.Count > 0)
                            {
                                unexpectedFilesMessage.Add("Libraries not deleted!");
                                foreach (var filename in resolvedFiles.Values)
                                {
                                    unexpectedFilesMessage.Add(filename);
                                }
                            }
                            testCaseResult.ErrorMessages.AddRange(unexpectedFilesMessage);
                            testCaseComplete(testCaseResult);
                        },
                                synchronous: true);
                    }
                },
                new IntegrationTester.TestCase {
                    Name   = "ResolveForGradleBuildSystemWithTemplateDeleteLibraries",
                    Method = (testCase, testCaseComplete) => {
                        ClearAllDependencies();
                        SetupDependencies();
                        var filesToIgnore = new HashSet <string> {
                            Path.GetFileName(GRADLE_TEMPLATE_LIBRARY_DISABLED)
                        };

                        ResolveWithGradleTemplate(
                            GRADLE_TEMPLATE_DISABLED,
                            "ExpectedArtifacts/NoExport/GradleTemplate",
                            testCase, (testCaseResult) => {
                            PlayServicesResolver.DeleteResolvedLibrariesSync();
                            testCaseResult.ErrorMessages.AddRange(CompareDirectoryContents(
                                                                      "ExpectedArtifacts/NoExport/GradleTemplateEmpty",
                                                                      "Assets/Plugins/Android", filesToIgnore));
                            if (File.Exists(GRADLE_TEMPLATE_ENABLED))
                            {
                                File.Delete(GRADLE_TEMPLATE_ENABLED);
                            }
                            testCaseComplete(testCaseResult);
                        },
                            deleteGradleTemplate: false,
                            filesToIgnore: filesToIgnore);
                    }
                },
            });

            // Test resolution with Android ABI filtering.
            if (IntegrationTester.Runner.UnityVersion >= 2018.0f)
            {
                IntegrationTester.Runner.ScheduleTestCase(
                    new IntegrationTester.TestCase {
                    Name   = "ResolverForGradleBuildSystemUsingAbisArmeabiv7aAndArm64",
                    Method = (testCase, testCaseComplete) => {
                        ClearAllDependencies();
                        Resolve("Gradle", false,
                                "ExpectedArtifacts/NoExport/GradleArmeabiv7aArm64",
                                "armeabi-v7a, arm64-v8a", nonGradleTemplateFilesToIgnore,
                                testCase, testCaseComplete);
                    }
                });
            }
            else if (IntegrationTester.Runner.UnityVersion >= 5.0f)
            {
                IntegrationTester.Runner.ScheduleTestCase(
                    new IntegrationTester.TestCase {
                    Name   = "ResolverForGradleBuildSystemUsingAbisArmeabiv7a",
                    Method = (testCase, testCaseComplete) => {
                        ClearAllDependencies();
                        Resolve("Gradle", false,
                                "ExpectedArtifacts/NoExport/GradleArmeabiv7a",
                                "armeabi-v7a", nonGradleTemplateFilesToIgnore,
                                testCase, testCaseComplete);
                    }
                });
            }
        }
        /// <summary>
        /// Asynchronously run the Android Resolver and validate the result with
        /// ValidateAndroidResolution.
        /// </summary>
        /// <param name="androidBuildSystem">Android build system to select.</param>
        /// <param name="exportProject">Whether Android project export should be enabled.</param>
        /// <param name="expectedAssetsDir">Directory that contains the assets expected from the
        /// resolution step.</param>
        /// <param name="targetAbis">String of Android ABIs to target or null if the default ABIs
        /// should be selected.</param>
        /// <param name="filesToIgnore">Set of files to relative to the generatedAssetsDir.</param>
        /// <param name="testCase">Object executing this method.</param>
        /// <param name="testCaseComplete">Called with the test result.</param>
        /// <param name="synchronous">Whether the resolution should be executed synchronously.</param>
        private static void Resolve(string androidBuildSystem, bool exportProject,
                                    string expectedAssetsDir, string targetAbis,
                                    ICollection <string> filesToIgnore,
                                    IntegrationTester.TestCase testCase,
                                    Action <IntegrationTester.TestCaseResult> testCaseComplete,
                                    bool synchronous = false)
        {
            // Set the Android target ABIs.
            GooglePlayServices.AndroidAbis.CurrentString = targetAbis;
            // Try setting the build system if this version of Unity supports it.
            if (!GradleBuildSupported && androidBuildSystem == "Gradle")
            {
                testCaseComplete(new IntegrationTester.TestCaseResult(testCase)
                {
                    Skipped       = true,
                    ErrorMessages = new List <string> {
                        "Unity version does not support Gradle builds."
                    }
                });
                return;
            }
            if (!(SetEditorUserBuildSettingsProperty(
                      ANDROID_BUILD_SYSTEM, StringToAndroidBuildSystemValue(androidBuildSystem)) &&
                  GetEditorUserBuildSettingsProperty(
                      ANDROID_BUILD_SYSTEM, androidBuildSystem).ToString() == androidBuildSystem))
            {
                testCaseComplete(new IntegrationTester.TestCaseResult(testCase)
                {
                    ErrorMessages = new List <string> {
                        String.Format("Unable to set AndroidBuildSystem to {0}.",
                                      androidBuildSystem)
                    }
                });
                return;
            }
            // Configure project export setting.
            if (!(SetEditorUserBuildSettingsProperty(EXPORT_ANDROID_PROJECT, exportProject) &&
                  (bool)GetEditorUserBuildSettingsProperty(EXPORT_ANDROID_PROJECT,
                                                           exportProject) == exportProject))
            {
                testCaseComplete(new IntegrationTester.TestCaseResult(testCase)
                {
                    ErrorMessages = new List <string> {
                        String.Format("Unable to set Android export project to {0}.",
                                      exportProject)
                    }
                });
            }

            // Resolve dependencies.
            Action <bool> completeWithResult = (bool complete) => {
                IntegrationTester.Runner.ExecuteTestCase(
                    testCase,
                    () => {
                    testCaseComplete(new IntegrationTester.TestCaseResult(testCase)
                    {
                        ErrorMessages = ValidateAndroidResolution(expectedAssetsDir, complete,
                                                                  filesToIgnore)
                    });
                }, true);
            };

            if (synchronous)
            {
                bool success = PlayServicesResolver.ResolveSync(true);
                completeWithResult(success);
            }
            else
            {
                PlayServicesResolver.Resolve(resolutionCompleteWithResult: completeWithResult);
            }
        }