• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.checkcolor.lint;
18 
19 import com.android.annotations.NonNull;
20 import com.android.tools.lint.checks.infrastructure.LintDetectorTest;
21 import com.android.tools.lint.client.api.LintClient;
22 import com.android.tools.lint.detector.api.Detector;
23 import com.android.tools.lint.detector.api.Issue;
24 import com.android.tools.lint.detector.api.Project;
25 
26 import java.util.Collections;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
30 
31 public class HardcodedColorDetectorTest extends LintDetectorTest {
32 
33     private Set<Issue> mEnabled = new HashSet<Issue>();
34 
35     @Override
getDetector()36     protected Detector getDetector() {
37         return new HardcodedColorDetector();
38     }
39 
40     @Override
getIssues()41     protected List<Issue> getIssues() {
42         return Collections.singletonList(HardcodedColorDetector.ISSUE);
43     }
44 
45     @Override
getConfiguration(LintClient client, Project project)46     protected TestConfiguration getConfiguration(LintClient client, Project project) {
47         return new TestConfiguration(client, project, null) {
48             @Override
49             public boolean isEnabled(@NonNull Issue issue) {
50                 return super.isEnabled(issue) && mEnabled.contains(issue);
51             }
52         };
53     }
54 
55     public void testHardcodeColorInSelector() throws Exception {
56         mEnabled = Collections.singleton(HardcodedColorDetector.ISSUE);
57         String expected = "res/layout/selector.xml:4: Error: Using hardcoded colors is not allowed [HardcodedColor]\n" +
58                 "        android:color=\"#ffffff\" />\n" +
59                 "        ~~~~~~~~~~~~~~~~~~~~~~~\n" +
60                 "1 errors, 0 warnings\n";
61         String result = lintProject(xml("res/layout/selector.xml", "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
62                 "<selector xmlns:android=\"http://schemas.android.com/apk/res/android\">\n" +
63                 "    <item android:state_checked=\"true\"\n" +
64                 "        android:color=\"#ffffff\" />\n" +
65                 "    <item android:color=\"@android:color/black\"\n" +
66                 "        android:alpha=\".54\" />\n" +
67                 "</selector>"));
68         assertEquals(expected, result);
69     }
70 
71     public void testHardcodeColorInLayout() throws Exception {
72         mEnabled = Collections.singleton(HardcodedColorDetector.ISSUE);
73         String expected = "res/layout/layout_battery.xml:13: Error: Using hardcoded colors is not allowed [HardcodedColor]\n" +
74                 "        android:textColor=\"#ff0\" />\n" +
75                 "        ~~~~~~~~~~~~~~~~~~~~~~~~\n" +
76                 "res/layout/layout_battery.xml:25: Error: Using hardcoded colors is not allowed [HardcodedColor]\n" +
77                 "        systemui:textColor=\"#66FFFFFF\" />\n" +
78                 "        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" +
79                 "2 errors, 0 warnings\n";
80         String result = lintProject(xml("res/layout/layout_battery.xml", "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
81                 "<LinearLayout\n" +
82                 "    xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
83                 "    xmlns:systemui=\"http://schemas.android.com/apk/res-auto\"\n" +
84                 "    android:layout_width=\"match_parent\"\n" +
85                 "    android:layout_height=\"wrap_content\"\n" +
86                 "    android:orientation=\"vertical\">\n" +
87                 "\n" +
88                 "    <TextView\n" +
89                 "        android:id=\"@+id/charge_and_estimation\"\n" +
90                 "        android:layout_width=\"match_parent\"\n" +
91                 "        android:layout_height=\"wrap_content\"\n" +
92                 "        android:textColor=\"#ff0\" />\n" +
93                 "\n" +
94                 "    <com.android.systemui.ResizingSpace\n" +
95                 "        android:layout_width=\"match_parent\"\n" +
96                 "        android:layout_height=\"@dimen/battery_detail_graph_space_top\" />\n" +
97                 "\n" +
98                 "    <com.android.settingslib.graph.UsageView\n" +
99                 "        android:id=\"@+id/battery_usage\"\n" +
100                 "        android:layout_width=\"match_parent\"\n" +
101                 "        android:layout_height=\"141dp\"\n" +
102                 "        systemui:sideLabels=\"@array/battery_labels\"\n" +
103                 "        android:colorAccent=\"?android:attr/colorAccent\"\n" +
104                 "        systemui:textColor=\"#66FFFFFF\" />\n" +
105                 "</LinearLayout>"));
106         assertEquals(expected, result);
107     }
108 
109     public void testHardcodeColorInStyle() throws Exception {
110         mEnabled = Collections.singleton(HardcodedColorDetector.ISSUE);
111         String expected = "res/values/pxsp.xml:6: Error: Using hardcoded colors is not allowed [HardcodedColor]\n" +
112                 "        <item name=\"android:textColor\">#b2ffffff</item>\n" +
113                 "                                       ^\n" +
114                 "res/values/pxsp.xml:7: Error: Using hardcoded colors is not allowed [HardcodedColor]\n" +
115                 "        <item name=\"android:background\">#b2ffffff</item>\n" +
116                 "                                        ^\n" +
117                 "2 errors, 0 warnings\n";
118         String result = lintProject(xml("res/values/pxsp.xml", "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
119                 "<resources xmlns:android=\"http://schemas.android.com/apk/res/android\">\n" +
120                 "    <style name=\"TextAppearance.StatusBar.Expanded.Date\">\n" +
121                 "        <item name=\"android:textSize\">@dimen/qs_date_collapsed_size</item>\n" +
122                 "        <item name=\"android:textStyle\">normal</item>\n" +
123                 "        <item name=\"android:textColor\">#b2ffffff</item>\n" +
124                 "        <item name=\"android:background\">#b2ffffff</item>\n" +
125                 "    </style>\n" +
126                 "</resources>"));
127         assertEquals(expected, result);
128     }
129 
130     public void testIndirectedColor() throws Exception {
131         mEnabled = Collections.singleton(HardcodedColorDetector.ISSUE);
132         String expected = "res/values/colors.xml:3: Error: Using hardcoded colors is not allowed [HardcodedColor]\n" +
133                 "    <color name=\"color_red\">#ffffff</color>\n" +
134                 "    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" +
135                 "res/layout/layout_battery.xml:13: Error: Using hardcoded colors is not allowed [HardcodedColor]\n" +
136                 "        android:textColor=\"@color/color_red\" />\n" +
137                 "        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" +
138                 "res/layout/layout_battery.xml:19: Error: Using hardcoded colors is not allowed [HardcodedColor]\n" +
139                 "        systemui:textColor=\"#b2ffffff\" />\n" +
140                 "        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" +
141                 "3 errors, 0 warnings\n";
142         String result = lintProject(xml("res/layout/layout_battery.xml", "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
143                 "<LinearLayout\n" +
144                 "    xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
145                 "    xmlns:systemui=\"http://schemas.android.com/apk/res-auto\"\n" +
146                 "    android:layout_width=\"match_parent\"\n" +
147                 "    android:layout_height=\"wrap_content\"\n" +
148                 "    android:orientation=\"vertical\">\n" +
149                 "\n" +
150                 "    <TextView\n" +
151                 "        android:id=\"@+id/charge_and_estimation\"\n" +
152                 "        android:layout_width=\"match_parent\"\n" +
153                 "        android:layout_height=\"wrap_content\"\n" +
154                 "        android:textColor=\"@color/color_red\" />\n" +
155                 "\n" +
156                 "    <com.android.settingslib.graph.UsageView\n" +
157                 "        android:id=\"@+id/battery_usage\"\n" +
158                 "        android:layout_width=\"match_parent\"\n" +
159                 "        android:layout_height=\"141dp\"\n" +
160                 "        systemui:textColor=\"#b2ffffff\" />\n" +
161                 "</LinearLayout>"),
162                 xml("res/values/colors.xml","<resources>\n" +
163                         "    <!-- Keyboard shortcuts colors -->\n" +
164                         "    <color name=\"color_red\">#ffffff</color>\n" +
165                         "    <color name=\"ksh_key_item_background\">@color/material_grey_100</color>\n" +
166                         "</resources>\n"));
167         assertEquals(expected, result);
168     }
169 
170     public void testHardcodeColorInColorElement() throws Exception {
171         mEnabled = Collections.singleton(HardcodedColorDetector.ISSUE);
172         String expected = "res/values/colors.xml:3: Error: Using hardcoded colors is not allowed [HardcodedColor]\n" +
173                 "    <color name=\"color_red\">#ffffff</color>\n" +
174                 "    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" +
175                 "1 errors, 0 warnings\n";
176         String result = lintProject(xml("res/values/colors.xml", "<resources>\n" +
177                 "    <!-- Keyboard shortcuts colors -->\n" +
178                 "    <color name=\"color_red\">#ffffff</color>\n" +
179                 "    <color name=\"ksh_key_item_background\">@color/material_grey_100</color>\n" +
180                 "</resources>\n"));
181         assertEquals(expected, result);
182     }
183 
184     public void testHardcodeColorInVectorDrawable() throws Exception {
185         mEnabled = Collections.singleton(HardcodedColorDetector.ISSUE);
186         String expected = "res/values/colors.xml:2: Error: Using hardcoded colors is not allowed [HardcodedColor]\n" +
187                 "        android:tint=\"#FFFFFF\">\n" +
188                 "        ~~~~~~~~~~~~~~~~~~~~~~\n" +
189                 "1 errors, 0 warnings\n";
190         String result = lintProject(xml("res/values/colors.xml", "<vector xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
191                 "        android:tint=\"#FFFFFF\">\n" +
192                 "    <path\n" +
193                 "        android:fillColor=\"#FFFFFF\"/>\n" +
194                 "</vector>"));
195         assertEquals(expected, result);
196     }
197 }
198