16
16
17
17
package com .example .google .lint ;
18
18
19
- import com .android .annotations .NonNull ;
20
19
import com .android .tools .lint .checks .infrastructure .LintDetectorTest ;
21
- import com .android .tools .lint .client .api .LintClient ;
22
20
import com .android .tools .lint .detector .api .Detector ;
23
21
import com .android .tools .lint .detector .api .Issue ;
24
- import com .android .tools .lint .detector .api .Project ;
25
22
26
23
import java .util .Collections ;
27
- import java .util .HashSet ;
28
24
import java .util .List ;
29
- import java .util .Set ;
30
25
31
26
import static com .android .SdkConstants .FN_ANDROID_MANIFEST_XML ;
32
27
35
30
* to adjust your code for the next tools release.</b>
36
31
*/
37
32
public class MainActivityDetectorTest extends LintDetectorTest {
38
- /**
39
- * The set of enabled issues for a given test.
40
- */
41
- private Set <Issue > mEnabled = new HashSet <Issue >();
42
-
43
33
@ Override
44
34
protected Detector getDetector () {
45
35
return new MainActivityDetector ();
@@ -50,103 +40,91 @@ protected List<Issue> getIssues() {
50
40
return Collections .singletonList (MainActivityDetector .ISSUE );
51
41
}
52
42
53
- /**
54
- * Gets the configuration for the test.
55
- * Each test can have a set of enabled issues by assigning the member field {@link #mEnabled}.
56
- */
57
- @ Override
58
- protected TestConfiguration getConfiguration (LintClient client , Project project ) {
59
- return new TestConfiguration (client , project , null ) {
60
- @ Override
61
- public boolean isEnabled (@ NonNull Issue issue ) {
62
- return super .isEnabled (issue ) && mEnabled .contains (issue );
63
- }
64
- };
65
- }
66
-
67
43
/**
68
44
* Test that a manifest with an activity with a launcher intent has no warnings.
69
45
*/
70
- public void testHasMainActivity () throws Exception {
71
- mEnabled = Collections . singleton ( MainActivityDetector . ISSUE );
72
- String expected = "No warnings." ;
73
- String result = lintProject ( xml ( FN_ANDROID_MANIFEST_XML , " " +
74
- "<?xml version =\" 1.0 \" encoding= \" utf-8 \" ?> \n " +
75
- "<manifest package =\" com.example. android.custom-lint-rules \" \n " +
76
- " xmlns:android= \" http://schemas.android.com/apk/res/android \" >\n " +
77
- " <application> \n " +
78
- " <activity android:name= \" com.example.android.custom-lint-rules " +
79
- ".OtherActivity \" >\n " +
80
- " </activity> \n " +
81
- " \n " +
82
- " <activity android:name= \" com.example.android.custom-lint-rules " +
83
- ".MainActivity \" >\n " +
84
- " < intent-filter >\n " +
85
- " <action android:name=\" android.intent.action.MAIN \" />\n " +
86
- " <category android:name= \" android.intent.category.LAUNCHER \" / >\n " +
87
- " </intent-filter >\n " +
88
- " </activity >\n " +
89
- " </application> \n " +
90
- "</manifest>" ));
91
- assertEquals ( expected , result );
46
+ public void testHasMainActivity () {
47
+ lint (). files (
48
+ xml ( FN_ANDROID_MANIFEST_XML , "" +
49
+ "<? xml version= \" 1.0 \" encoding= \" utf-8 \" ?> \n " +
50
+ "<manifest package =\" com.example.android.custom-lint-rules \" \n " +
51
+ " xmlns:android =\" http://schemas. android.com/apk/res/android \" > \n " +
52
+ " <application >\n " +
53
+ " <activity android:name= \" com.example.android.custom-lint-rules " +
54
+ ".OtherActivity \" > \n " +
55
+ " </activity >\n " +
56
+ " \n " +
57
+ " <activity android:name= \" com.example.android.custom-lint-rules " +
58
+ ".MainActivity \" > \n " +
59
+ " <intent-filter >\n " +
60
+ " <action android:name= \" android. intent.action.MAIN \" / >\n " +
61
+ " <category android:name=\" android.intent.category.LAUNCHER \" />\n " +
62
+ " </intent-filter >\n " +
63
+ " </activity >\n " +
64
+ " </application >\n " +
65
+ "</manifest>" ))
66
+ . run ()
67
+ . expectClean ( );
92
68
}
93
69
94
70
/**
95
71
* Test that a manifest <em>without</em> an activity with a launcher intent reports an error.
96
72
*/
97
- public void testMissingMainActivity () throws Exception {
98
- mEnabled = Collections .singleton (MainActivityDetector .ISSUE );
73
+ public void testMissingMainActivity () {
99
74
String expected = "AndroidManifest.xml: Error: Expecting AndroidManifest.xml to have an " +
100
75
"activity with a launcher intent. [MainActivityDetector]\n " +
101
76
"1 errors, 0 warnings\n " ;
102
- String result = lintProject (xml (FN_ANDROID_MANIFEST_XML , "" +
103
- "<?xml version=\" 1.0\" encoding=\" utf-8\" ?>\n " +
104
- "<manifest package=\" com.example.android.custom-lint-rules\" \n " +
105
- " xmlns:android=\" http://schemas.android.com/apk/res/android\" >\n " +
106
- " <application>\n " +
107
- " <activity android:name=\" com.example.android.custom-lint-rules" +
108
- ".Activity1\" >\n " +
109
- " <intent-filter>\n " +
110
- " <action android:name=\" android.intent.action.VIEW\" />\n " +
111
- "\n " +
112
- " <category android:name=\" android.intent.category.HOME\" />\n " +
113
- " <category android:name=\" android.intent.category.LAUNCHER\" />\n " +
114
- " <category android:name=\" android.intent.category.DEFAULT\" />\n " +
115
- " <category android:name=\" android.intent.category.BROWSABLE\" " +
116
- "/>\n " +
117
- " </intent-filter>\n " +
118
- " </activity>\n " +
119
- "\n " +
120
- " <activity android:name=\" com.example.android.custom-lint-rules" +
121
- ".Activity2\" >\n " +
122
- " </activity>\n " +
123
- "\n " +
124
- " <activity android:name=\" com.example.android.custom-lint-rules" +
125
- ".Activity3\" >\n " +
126
- " <intent-filter>\n " +
127
- " <action android:name=\" android.intent.action.SEND\" />\n " +
128
- " <category android:name=\" android.intent.category.DEFAULT\" />\n " +
129
- " <data android:mimeType=\" text/plain\" />\n " +
130
- " </intent-filter>\n " +
131
- " </activity>\n " +
132
- " </application>\n " +
133
- "</manifest>" ));
134
- assertEquals (expected , result );
77
+ lint ().files (
78
+ xml (FN_ANDROID_MANIFEST_XML , "" +
79
+ "<?xml version=\" 1.0\" encoding=\" utf-8\" ?>\n " +
80
+ "<manifest package=\" com.example.android.custom-lint-rules\" \n " +
81
+ " xmlns:android=\" http://schemas.android.com/apk/res/android\" >\n " +
82
+ " <application>\n " +
83
+ " <activity android:name=\" com.example.android.custom-lint-rules" +
84
+ ".Activity1\" >\n " +
85
+ " <intent-filter>\n " +
86
+ " <action android:name=\" android.intent.action.VIEW\" />\n " +
87
+ "\n " +
88
+ " <category android:name=\" android.intent.category.HOME\" />\n " +
89
+ " <category android:name=\" android.intent.category.LAUNCHER\" />\n " +
90
+ " <category android:name=\" android.intent.category.DEFAULT\" />\n " +
91
+ " <category android:name=\" android.intent.category.BROWSABLE\" " +
92
+ "/>\n " +
93
+ " </intent-filter>\n " +
94
+ " </activity>\n " +
95
+ "\n " +
96
+ " <activity android:name=\" com.example.android.custom-lint-rules" +
97
+ ".Activity2\" >\n " +
98
+ " </activity>\n " +
99
+ "\n " +
100
+ " <activity android:name=\" com.example.android.custom-lint-rules" +
101
+ ".Activity3\" >\n " +
102
+ " <intent-filter>\n " +
103
+ " <action android:name=\" android.intent.action.SEND\" />\n " +
104
+ " <category android:name=\" android.intent.category.DEFAULT\" />\n " +
105
+ " <data android:mimeType=\" text/plain\" />\n " +
106
+ " </intent-filter>\n " +
107
+ " </activity>\n " +
108
+ " </application>\n " +
109
+ "</manifest>" ))
110
+ .run ()
111
+ .expect (expected );
135
112
}
136
113
137
114
/**
138
115
* Test that a manifest without an <code><application></code> tag reports an error.
139
116
*/
140
- public void testMissingApplication () throws Exception {
141
- mEnabled = Collections .singleton (MainActivityDetector .ISSUE );
117
+ public void testMissingApplication () {
142
118
String expected = "AndroidManifest.xml: Error: Expecting AndroidManifest.xml to have an " +
143
119
"<activity> tag. [MainActivityDetector]\n " +
144
120
"1 errors, 0 warnings\n " ;
145
- String result = lintProject (xml (FN_ANDROID_MANIFEST_XML , "" +
146
- "<?xml version=\" 1.0\" encoding=\" utf-8\" ?>\n " +
147
- "<manifest package=\" com.example.android.custom-lint-rules\" \n " +
148
- " xmlns:android=\" http://schemas.android.com/apk/res/android\" >\n " +
149
- "</manifest>" ));
150
- assertEquals (expected , result );
121
+ lint ().files (
122
+ xml (FN_ANDROID_MANIFEST_XML , "" +
123
+ "<?xml version=\" 1.0\" encoding=\" utf-8\" ?>\n " +
124
+ "<manifest package=\" com.example.android.custom-lint-rules\" \n " +
125
+ " xmlns:android=\" http://schemas.android.com/apk/res/android\" >\n " +
126
+ "</manifest>" ))
127
+ .run ()
128
+ .expect (expected );
151
129
}
152
130
}
0 commit comments