summaryrefslogtreecommitdiffstats
path: root/python/mozbuild/mozbuild/test/backend/test_android_eclipse.py
blob: c4e9221c9e758cb74c767f89a2a495c65a1db512 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from __future__ import unicode_literals

import json
import os
import unittest

from mozbuild.backend.android_eclipse import AndroidEclipseBackend
from mozbuild.frontend.emitter import TreeMetadataEmitter
from mozbuild.frontend.reader import BuildReader
from mozbuild.test.backend.common import BackendTester
from mozpack.manifests import InstallManifest
from mozunit import main

import mozpack.path as mozpath

class TestAndroidEclipseBackend(BackendTester):
    def __init__(self, *args, **kwargs):
        BackendTester.__init__(self, *args, **kwargs)
        self.env = None

    def assertExists(self, *args):
        p = mozpath.join(self.env.topobjdir, 'android_eclipse', *args)
        self.assertTrue(os.path.exists(p), "Path %s exists" % p)

    def assertNotExists(self, *args):
        p = mozpath.join(self.env.topobjdir, 'android_eclipse', *args)
        self.assertFalse(os.path.exists(p), "Path %s does not exist" % p)

    def test_library_project_files(self):
        """Ensure we generate reasonable files for library projects."""
        self.env = self._consume('android_eclipse', AndroidEclipseBackend)
        for f in ['.classpath',
                  '.project',
                  '.settings',
                  'AndroidManifest.xml',
                  'project.properties']:
            self.assertExists('library1', f)

    def test_main_project_files(self):
        """Ensure we generate reasonable files for main (non-library) projects."""
        self.env = self._consume('android_eclipse', AndroidEclipseBackend)
        for f in ['.classpath',
                  '.project',
                  '.settings',
                  'gen',
                  'lint.xml',
                  'project.properties']:
            self.assertExists('main1', f)

    def test_library_manifest(self):
        """Ensure we generate manifest for library projects."""
        self.env = self._consume('android_eclipse', AndroidEclipseBackend)
        self.assertExists('library1', 'AndroidManifest.xml')

    def test_classpathentries(self):
        """Ensure we produce reasonable classpathentries."""
        self.env = self._consume('android_eclipse', AndroidEclipseBackend)
        self.assertExists('main3', '.classpath')
        # This is brittle but simple.
        with open(mozpath.join(self.env.topobjdir, 'android_eclipse', 'main3', '.classpath'), 'rt') as fh:
            lines = fh.readlines()
        lines = [line.strip() for line in lines]
        self.assertIn('<classpathentry including="**/*.java" kind="src" path="a" />', lines)
        self.assertIn('<classpathentry excluding="b/Excludes.java|b/Excludes2.java" including="**/*.java" kind="src" path="b" />', lines)
        self.assertIn('<classpathentry including="**/*.java" kind="src" path="c"><attributes><attribute name="ignore_optional_problems" value="true" /></attributes></classpathentry>', lines)

    def test_library_project_setting(self):
        """Ensure we declare a library project correctly."""
        self.env = self._consume('android_eclipse', AndroidEclipseBackend)

        self.assertExists('library1', 'project.properties')
        with open(mozpath.join(self.env.topobjdir, 'android_eclipse', 'library1', 'project.properties'), 'rt') as fh:
            lines = fh.readlines()
        lines = [line.strip() for line in lines]
        self.assertIn('android.library=true', lines)

        self.assertExists('main1', 'project.properties')
        with open(mozpath.join(self.env.topobjdir, 'android_eclipse', 'main1', 'project.properties'), 'rt') as fh:
            lines = fh.readlines()
        lines = [line.strip() for line in lines]
        self.assertNotIn('android.library=true', lines)

    def test_referenced_projects(self):
        """Ensure we reference another project correctly."""
        self.env = self._consume('android_eclipse', AndroidEclipseBackend)
        self.assertExists('main4', '.classpath')
        # This is brittle but simple.
        with open(mozpath.join(self.env.topobjdir, 'android_eclipse', 'main4', '.classpath'), 'rt') as fh:
            lines = fh.readlines()
        lines = [line.strip() for line in lines]
        self.assertIn('<classpathentry combineaccessrules="false" kind="src" path="/library1" />', lines)

    def test_extra_jars(self):
        """Ensure we add class path entries to extra jars iff asked to."""
        self.env = self._consume('android_eclipse', AndroidEclipseBackend)
        self.assertExists('main2', '.classpath')
        # This is brittle but simple.
        with open(mozpath.join(self.env.topobjdir, 'android_eclipse', 'main2', '.classpath'), 'rt') as fh:
            lines = fh.readlines()
        lines = [line.strip() for line in lines]
        self.assertIn('<classpathentry exported="true" kind="lib" path="%s/main2/extra.jar" />' % self.env.topsrcdir, lines)

    def test_included_projects(self):
        """Ensure we include another project correctly."""
        self.env = self._consume('android_eclipse', AndroidEclipseBackend)
        self.assertExists('main4', 'project.properties')
        # This is brittle but simple.
        with open(mozpath.join(self.env.topobjdir, 'android_eclipse', 'main4', 'project.properties'), 'rt') as fh:
            lines = fh.readlines()
        lines = [line.strip() for line in lines]
        self.assertIn('android.library.reference.1=library2', lines)

    def assertInManifest(self, project_name, *args):
        manifest_path = mozpath.join(self.env.topobjdir, 'android_eclipse', '%s.manifest' % project_name)
        manifest = InstallManifest(manifest_path)
        for arg in args:
            self.assertIn(arg, manifest, '%s in manifest for project %s' % (arg, project_name))

    def assertNotInManifest(self, project_name, *args):
        manifest_path = mozpath.join(self.env.topobjdir, 'android_eclipse', '%s.manifest' % project_name)
        manifest = InstallManifest(manifest_path)
        for arg in args:
            self.assertNotIn(arg, manifest, '%s not in manifest for project %s' % (arg, project_name))

    def test_manifest_main_manifest(self):
        """Ensure we symlink manifest if asked to for main projects."""
        self.env = self._consume('android_eclipse', AndroidEclipseBackend)
        self.assertInManifest('main1', 'AndroidManifest.xml')

    def test_manifest_res(self):
        """Ensure we symlink res/ iff asked to."""
        self.env = self._consume('android_eclipse', AndroidEclipseBackend)
        self.assertInManifest('library1', 'res')
        self.assertNotInManifest('library2', 'res')

    def test_manifest_classpathentries(self):
        """Ensure we symlink classpathentries correctly."""
        self.env = self._consume('android_eclipse', AndroidEclipseBackend)
        self.assertInManifest('main3', 'a/a', 'b', 'd/e')

    def test_manifest_assets(self):
        """Ensure we symlink assets/ iff asked to."""
        self.env = self._consume('android_eclipse', AndroidEclipseBackend)
        self.assertNotInManifest('main1', 'assets')
        self.assertInManifest('main2', 'assets')


if __name__ == '__main__':
    main()