1 /*
2  * Copyright (C) 2020 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.android.tools.idea.validator;
18 
19 import com.android.tools.layoutlib.annotations.NotNull;
20 import com.android.tools.layoutlib.annotations.Nullable;
21 
22 import java.util.EnumSet;
23 import java.util.HashSet;
24 
25 import com.google.android.apps.common.testing.accessibility.framework.AccessibilityHierarchyCheck;
26 
27 /**
28  * Data used for layout validation.
29  */
30 public class ValidatorData {
31 
32     /**
33      * Category of validation
34      */
35     public enum Type {
36         ACCESSIBILITY,
37         RENDER,
38         INTERNAL_ERROR
39     }
40 
41     /**
42      * Level of importance
43      */
44     public enum Level {
45         ERROR,
46         WARNING,
47         INFO,
48         /** The test not ran or suppressed. */
49         VERBOSE,
50     }
51 
52     /**
53      * Determine what types and levels of validation to run.
54      */
55     public static class Policy {
56         @NotNull public final EnumSet<Type> mTypes;
57         @NotNull public final EnumSet<Level> mLevels;
58         @NotNull public final HashSet<AccessibilityHierarchyCheck> mChecks = new HashSet();
59 
Policy(@otNull EnumSet<Type> types, @NotNull EnumSet<Level> levels)60         public Policy(@NotNull EnumSet<Type> types, @NotNull EnumSet<Level> levels) {
61             mTypes = types;
62             mLevels = levels;
63         }
64     }
65 
66     /**
67      * Suggested fix to the user or to the studio.
68      */
69     public static class Fix {
70         @NotNull public final String mFix;
71 
Fix(String fix)72         public Fix(String fix) {
73             mFix = fix;
74         }
75     }
76 
77     /**
78      * Issue describing the layout problem.
79      */
80     public static class Issue {
81         @NotNull
82         public final String mCategory;
83         @NotNull
84         public final Type mType;
85         @NotNull
86         public final String mMsg;
87         @NotNull
88         public final Level mLevel;
89         @Nullable
90         public final Long mSrcId;
91         @Nullable
92         public final Fix mFix;
93         @NotNull
94         public final String mSourceClass;
95         @Nullable
96         public final String mHelpfulUrl;
97 
Issue( @otNull String category, @NotNull Type type, @NotNull String msg, @NotNull Level level, @Nullable Long srcId, @Nullable Fix fix, @NotNull String sourceClass, @Nullable String helpfulUrl)98         private Issue(
99                 @NotNull String category,
100                 @NotNull Type type,
101                 @NotNull String msg,
102                 @NotNull Level level,
103                 @Nullable Long srcId,
104                 @Nullable Fix fix,
105                 @NotNull String sourceClass,
106                 @Nullable String helpfulUrl) {
107             mCategory = category;
108             mType = type;
109             mMsg = msg;
110             mLevel = level;
111             mSrcId = srcId;
112             mFix = fix;
113             mSourceClass = sourceClass;
114             mHelpfulUrl = helpfulUrl;
115         }
116 
117         public static class IssueBuilder {
118             private String mCategory;
119             private Type mType = Type.ACCESSIBILITY;
120             private String mMsg;
121             private Level mLevel;
122             private Long mSrcId;
123             private Fix mFix;
124             private String mSourceClass;
125             private String mHelpfulUrl;
126 
setCategory(String category)127             public IssueBuilder setCategory(String category) {
128                 mCategory = category;
129                 return this;
130             }
131 
setType(Type type)132             public IssueBuilder setType(Type type) {
133                 mType = type;
134                 return this;
135             }
136 
setMsg(String msg)137             public IssueBuilder setMsg(String msg) {
138                 mMsg = msg;
139                 return this;
140             }
141 
setLevel(Level level)142             public IssueBuilder setLevel(Level level) {
143                 mLevel = level;
144                 return this;
145             }
146 
setSrcId(Long srcId)147             public IssueBuilder setSrcId(Long srcId) {
148                 mSrcId = srcId;
149                 return this;
150             }
151 
setFix(Fix fix)152             public IssueBuilder setFix(Fix fix) {
153                 mFix = fix;
154                 return this;
155             }
156 
setSourceClass(String sourceClass)157             public IssueBuilder setSourceClass(String sourceClass) {
158                 mSourceClass = sourceClass;
159                 return this;
160             }
161 
setHelpfulUrl(String url)162             public IssueBuilder setHelpfulUrl(String url) {
163                 mHelpfulUrl = url;
164                 return this;
165             }
166 
build()167             public Issue build() {
168                 assert(mCategory != null);
169                 assert(mType != null);
170                 assert(mMsg != null);
171                 assert(mLevel != null);
172                 assert(mSourceClass != null);
173                 return new Issue(mCategory,
174                         mType,
175                         mMsg,
176                         mLevel,
177                         mSrcId,
178                         mFix,
179                         mSourceClass,
180                         mHelpfulUrl);
181             }
182         }
183     }
184 }
185