From 898a6a8d8d98ea4a0de253bbcdbbdf7dac124dbe Mon Sep 17 00:00:00 2001
From: Kyle Kirbatski <kkirbatski@gmrmarketing.com>
Date: Thu, 8 Nov 2018 12:23:29 -0600
Subject: [PATCH 1/6] =?UTF-8?q?Add=20a=20unit=20test=20to=20test=20source-?=
 =?UTF-8?q?file=20target-dir=20/app/src/main/=E2=80=A6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 spec/fixtures/org.test.plugins.dummyplugin/plugin.xml       | 2 ++
 .../src/android/DummyPlugin2.java                           | 1 +
 spec/unit/pluginHandlers/handlers.spec.js                   | 6 ++++++
 3 files changed, 9 insertions(+)
 create mode 100644 spec/fixtures/org.test.plugins.dummyplugin/src/android/DummyPlugin2.java

diff --git a/spec/fixtures/org.test.plugins.dummyplugin/plugin.xml b/spec/fixtures/org.test.plugins.dummyplugin/plugin.xml
index a40df2b2..5451ef8d 100644
--- a/spec/fixtures/org.test.plugins.dummyplugin/plugin.xml
+++ b/spec/fixtures/org.test.plugins.dummyplugin/plugin.xml
@@ -70,6 +70,8 @@
 
         <source-file src="src/android/DummyPlugin.java"
                 target-dir="src/com/phonegap/plugins/dummyplugin" />
+        <source-file src="src/android/DummyPlugin2.java"
+                target-dir="app/src/main/src/com/phonegap/plugins/dummyplugin" />
         <lib-file src="src/android/TestLib.jar" />
     </platform>
 </plugin>
diff --git a/spec/fixtures/org.test.plugins.dummyplugin/src/android/DummyPlugin2.java b/spec/fixtures/org.test.plugins.dummyplugin/src/android/DummyPlugin2.java
new file mode 100644
index 00000000..c2dd0f73
--- /dev/null
+++ b/spec/fixtures/org.test.plugins.dummyplugin/src/android/DummyPlugin2.java
@@ -0,0 +1 @@
+./org.test.plugins.dummyplugin/src/android/DummyPlugin2.java
diff --git a/spec/unit/pluginHandlers/handlers.spec.js b/spec/unit/pluginHandlers/handlers.spec.js
index cc6a39dd..1e2f1ea8 100644
--- a/spec/unit/pluginHandlers/handlers.spec.js
+++ b/spec/unit/pluginHandlers/handlers.spec.js
@@ -102,6 +102,12 @@ describe('android project handler', function () {
                     android['source-file'].install(valid_source[0], dummyPluginInfo, dummyProject);
                 }).toThrow(new Error('"' + target + '" already exists!'));
             });
+
+            it('Test#007 : should allow installing sources using proper path', function () {
+                android['source-file'].install(valid_source[1], dummyPluginInfo, dummyProject, {android_studio: true});
+                expect(copyFileSpy)
+                    .toHaveBeenCalledWith(dummyplugin, 'src/android/DummyPlugin2.java', temp, path.join('app/src/main/src/com/phonegap/plugins/dummyplugin/DummyPlugin2.java'), false);
+            });
         });
 
         describe('of <framework> elements', function () {

From fb1dfb27df1f61aedcb497d872d336e582fc301f Mon Sep 17 00:00:00 2001
From: "Christopher J. Brody" <chris.brody@gmail.com>
Date: Sun, 11 Nov 2018 13:36:31 -0500
Subject: [PATCH 2/6] unit test uninstall of <source-file> with app dest

for Java source only (GH-539)

Co-Authored-By: Christopher J. Brody <chris.brody@gmail.com>
Co-Authored-By: Kyle Kirbatski <kkirbatski@gmrmarketing.com>
---
 spec/unit/pluginHandlers/handlers.spec.js | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/spec/unit/pluginHandlers/handlers.spec.js b/spec/unit/pluginHandlers/handlers.spec.js
index 1e2f1ea8..48303b48 100644
--- a/spec/unit/pluginHandlers/handlers.spec.js
+++ b/spec/unit/pluginHandlers/handlers.spec.js
@@ -257,6 +257,10 @@ describe('android project handler', function () {
             });
         });
 
+        // TODO:
+        // - merge tests of <source-file> elements into single describe block
+        //   (with proper beforeEach/afterEach)
+        // - renumber the tests after Test#019
         describe('of <source-file> elements', function () {
             it('Test#019 : should remove stuff by calling common.deleteJava for Android Studio projects', function () {
                 android['source-file'].install(valid_source[0], dummyPluginInfo, dummyProject);
@@ -265,6 +269,14 @@ describe('android project handler', function () {
             });
         });
 
+        describe('of <source-file> element, with specific app target-dir', function () {
+            it('Test#019a : should remove stuff by calling common.deleteJava for Android Studio projects, with specific app target-dir', function () {
+                android['source-file'].install(valid_source[1], dummyPluginInfo, dummyProject, {android_studio: true});
+                android['source-file'].uninstall(valid_source[1], dummyPluginInfo, dummyProject, {android_studio: true});
+                expect(deleteJavaSpy).toHaveBeenCalledWith(temp, path.join('app/src/main/src/com/phonegap/plugins/dummyplugin/DummyPlugin2.java'));
+            });
+        });
+
         describe('of <framework> elements', function () {
 
             var someString = jasmine.any(String);

From c2f6631f910abd69a81c9abffeab46ee4e981ccf Mon Sep 17 00:00:00 2001
From: "Christopher J. Brody" <chris.brody@gmail.com>
Date: Sun, 11 Nov 2018 15:07:01 -0500
Subject: [PATCH 3/6] GH-539 fix destination path fallback

Fallback to old path mapping if no Android Studio path mapping exists

Co-authored-by: Christopher J. Brody <chris.brody@gmail.com>
Co-authored-by: Kyle Kirbatski <kkirbatski@gmrmarketing.com>
---
 bin/templates/cordova/lib/pluginHandlers.js | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/bin/templates/cordova/lib/pluginHandlers.js b/bin/templates/cordova/lib/pluginHandlers.js
index bee2841f..8e2155ea 100644
--- a/bin/templates/cordova/lib/pluginHandlers.js
+++ b/bin/templates/cordova/lib/pluginHandlers.js
@@ -26,7 +26,7 @@ var handlers = {
             if (!obj.src) throw new CordovaError(generateAttributeError('src', 'source-file', plugin.id));
             if (!obj.targetDir) throw new CordovaError(generateAttributeError('target-dir', 'source-file', plugin.id));
 
-            var dest = studioPathRemap(obj);
+            var dest = getInstallDestination(obj);
 
             if (options && options.force) {
                 copyFile(plugin.dir, obj.src, project.projectDir, dest, !!(options && options.link));
@@ -35,7 +35,7 @@ var handlers = {
             }
         },
         uninstall: function (obj, plugin, project, options) {
-            var dest = studioPathRemap(obj);
+            var dest = getInstallDestination(obj);
 
             // TODO: Add Koltin extension to uninstall, since they are handled like Java files
             if (obj.src.endsWith('java')) {
@@ -292,6 +292,11 @@ function generateAttributeError (attribute, element, id) {
     return 'Required attribute "' + attribute + '" not specified in <' + element + '> element from plugin: ' + id;
 }
 
+function getInstallDestination (obj) {
+    return studioPathRemap(obj) ||
+        path.join(obj.targetDir, path.basename(obj.src));
+}
+
 function studioPathRemap (obj) {
     // If a Java file is using the new directory structure, don't penalize it
     if (!obj.targetDir.includes('app/src/main')) {

From 3caefcae49f6dbd808489ff5c69b9915a68dca96 Mon Sep 17 00:00:00 2001
From: "Christopher J. Brody" <chris.brody@gmail.com>
Date: Fri, 9 Nov 2018 14:43:47 -0500
Subject: [PATCH 4/6] unit test source-file with custom lib target-dir

for JAR and AAR

(GH-540)

Co-Authored-By: Kyle Kirbatski <kkirbatski@gmrmarketing.com>
Co-Authored-By: Christopher J. Brody <chris.brody@gmail.com>
Co-Authored-By: @afdev82 (Antonio Facciolo)
---
 .../org.test.plugins.dummyplugin/plugin.xml         |  4 ++++
 .../src/android/TestAar.aar                         |  1 +
 spec/unit/pluginHandlers/handlers.spec.js           | 13 +++++++++++++
 3 files changed, 18 insertions(+)
 create mode 100644 spec/fixtures/org.test.plugins.dummyplugin/src/android/TestAar.aar

diff --git a/spec/fixtures/org.test.plugins.dummyplugin/plugin.xml b/spec/fixtures/org.test.plugins.dummyplugin/plugin.xml
index 5451ef8d..b299b645 100644
--- a/spec/fixtures/org.test.plugins.dummyplugin/plugin.xml
+++ b/spec/fixtures/org.test.plugins.dummyplugin/plugin.xml
@@ -72,6 +72,10 @@
                 target-dir="src/com/phonegap/plugins/dummyplugin" />
         <source-file src="src/android/DummyPlugin2.java"
                 target-dir="app/src/main/src/com/phonegap/plugins/dummyplugin" />
+        <source-file src="src/android/TestLib.jar"
+                target-dir="app/libs" />
+        <source-file src="src/android/TestAar.aar"
+                target-dir="app/libs" />
         <lib-file src="src/android/TestLib.jar" />
     </platform>
 </plugin>
diff --git a/spec/fixtures/org.test.plugins.dummyplugin/src/android/TestAar.aar b/spec/fixtures/org.test.plugins.dummyplugin/src/android/TestAar.aar
new file mode 100644
index 00000000..ce401543
--- /dev/null
+++ b/spec/fixtures/org.test.plugins.dummyplugin/src/android/TestAar.aar
@@ -0,0 +1 @@
+./org.test.plugins.dummyplugin/src/android/TestAar.aar
diff --git a/spec/unit/pluginHandlers/handlers.spec.js b/spec/unit/pluginHandlers/handlers.spec.js
index 48303b48..0ea4765f 100644
--- a/spec/unit/pluginHandlers/handlers.spec.js
+++ b/spec/unit/pluginHandlers/handlers.spec.js
@@ -108,6 +108,19 @@ describe('android project handler', function () {
                 expect(copyFileSpy)
                     .toHaveBeenCalledWith(dummyplugin, 'src/android/DummyPlugin2.java', temp, path.join('app/src/main/src/com/phonegap/plugins/dummyplugin/DummyPlugin2.java'), false);
             });
+
+            // TODO: renumber these tests and other tests below
+            it('Test#007a : should allow installing lib file from sources using proper path', function () {
+                android['source-file'].install(valid_source[2], dummyPluginInfo, dummyProject, {android_studio: true});
+                expect(copyFileSpy)
+                    .toHaveBeenCalledWith(dummyplugin, 'src/android/TestLib.jar', temp, path.join('app/libs/TestLib.jar'), false);
+            });
+
+            it('Test#007b : should allow installing aar file from sources using proper path', function () {
+                android['source-file'].install(valid_source[3], dummyPluginInfo, dummyProject, {android_studio: true});
+                expect(copyFileSpy)
+                    .toHaveBeenCalledWith(dummyplugin, 'src/android/TestAar.aar', temp, path.join('app/libs/TestAar.aar'), false);
+            });
         });
 
         describe('of <framework> elements', function () {

From c15312ee70a8eb274963653a88cff17d0e06774b Mon Sep 17 00:00:00 2001
From: "Christopher J. Brody" <chris.brody@gmail.com>
Date: Sun, 11 Nov 2018 13:36:31 -0500
Subject: [PATCH 5/6] unit test uninstall of <source-file> with app dest

for Java source, JAR, and AAR

Co-authored-by: Christopher J. Brody <chris.brody@gmail.com>
Co-authored-by: Kyle Kirbatski <kkirbatski@gmrmarketing.com>
Co-authored-by: Antonio Facciolo <afdev82@users.noreply.github.com>
---
 spec/unit/pluginHandlers/handlers.spec.js | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/spec/unit/pluginHandlers/handlers.spec.js b/spec/unit/pluginHandlers/handlers.spec.js
index 0ea4765f..4c398eab 100644
--- a/spec/unit/pluginHandlers/handlers.spec.js
+++ b/spec/unit/pluginHandlers/handlers.spec.js
@@ -290,6 +290,22 @@ describe('android project handler', function () {
             });
         });
 
+        describe('of <source-file> element, with JAR in specific app target-dir', function () {
+            it('Test#019b : should remove stuff by calling common.deleteJava for Android Studio projects, with JAR into specific app target-dir', function () {
+                android['source-file'].install(valid_source[2], dummyPluginInfo, dummyProject, {android_studio: true});
+                android['source-file'].uninstall(valid_source[2], dummyPluginInfo, dummyProject, {android_studio: true});
+                expect(removeFileSpy).toHaveBeenCalledWith(temp, path.join('app/libs/TestLib.jar'));
+            });
+        });
+
+        describe('of <source-file> element, with AAR in specific app target-dir', function () {
+            it('Test#019c : should remove stuff by calling common.deleteJava for Android Studio projects, with JAR into specific app target-dir', function () {
+                android['source-file'].install(valid_source[3], dummyPluginInfo, dummyProject, {android_studio: true});
+                android['source-file'].uninstall(valid_source[3], dummyPluginInfo, dummyProject, {android_studio: true});
+                expect(removeFileSpy).toHaveBeenCalledWith(temp, path.join('app/libs/TestAar.aar'));
+            });
+        });
+
         describe('of <framework> elements', function () {
 
             var someString = jasmine.any(String);

From ef493b4c0f941812dd791eb85ae2d9fb0b2ecb2a Mon Sep 17 00:00:00 2001
From: "Christopher J. Brody" <chris.brody@gmail.com>
Date: Sun, 11 Nov 2018 15:26:04 -0500
Subject: [PATCH 6/6] GH-540 fix for source-file with app target-dir

---
 bin/templates/cordova/lib/pluginHandlers.js | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/bin/templates/cordova/lib/pluginHandlers.js b/bin/templates/cordova/lib/pluginHandlers.js
index 8e2155ea..f1b0dba0 100644
--- a/bin/templates/cordova/lib/pluginHandlers.js
+++ b/bin/templates/cordova/lib/pluginHandlers.js
@@ -298,8 +298,9 @@ function getInstallDestination (obj) {
 }
 
 function studioPathRemap (obj) {
-    // If a Java file is using the new directory structure, don't penalize it
-    if (!obj.targetDir.includes('app/src/main')) {
+    // If any source file is using the app new directory structure,
+    // don't penalize it
+    if (!obj.targetDir.includes('app')) {
         if (obj.src.endsWith('.java')) {
             return path.join('app/src/main/java', obj.targetDir.substring(4), path.basename(obj.src));
         } else {