1616
1717package com .example .google .lint ;
1818
19- import com .android .annotations .NonNull ;
2019import com .android .tools .lint .checks .infrastructure .LintDetectorTest ;
21- import com .android .tools .lint .client .api .LintClient ;
2220import com .android .tools .lint .detector .api .Detector ;
2321import com .android .tools .lint .detector .api .Issue ;
24- import com .android .tools .lint .detector .api .Project ;
2522
2623import java .util .Collections ;
27- import java .util .HashSet ;
2824import java .util .List ;
29- import java .util .Set ;
3025
3126import static com .android .SdkConstants .FN_ANDROID_MANIFEST_XML ;
3227
3530 * to adjust your code for the next tools release.</b>
3631 */
3732public 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- 4333 @ Override
4434 protected Detector getDetector () {
4535 return new MainActivityDetector ();
@@ -50,103 +40,91 @@ protected List<Issue> getIssues() {
5040 return Collections .singletonList (MainActivityDetector .ISSUE );
5141 }
5242
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- 6743 /**
6844 * Test that a manifest with an activity with a launcher intent has no warnings.
6945 */
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 ( );
9268 }
9369
9470 /**
9571 * Test that a manifest <em>without</em> an activity with a launcher intent reports an error.
9672 */
97- public void testMissingMainActivity () throws Exception {
98- mEnabled = Collections .singleton (MainActivityDetector .ISSUE );
73+ public void testMissingMainActivity () {
9974 String expected = "AndroidManifest.xml: Error: Expecting AndroidManifest.xml to have an " +
10075 "activity with a launcher intent. [MainActivityDetector]\n " +
10176 "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 );
135112 }
136113
137114 /**
138115 * Test that a manifest without an <code><application></code> tag reports an error.
139116 */
140- public void testMissingApplication () throws Exception {
141- mEnabled = Collections .singleton (MainActivityDetector .ISSUE );
117+ public void testMissingApplication () {
142118 String expected = "AndroidManifest.xml: Error: Expecting AndroidManifest.xml to have an " +
143119 "<activity> tag. [MainActivityDetector]\n " +
144120 "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 );
151129 }
152130}
0 commit comments