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 android.dynamicmime.testapp.assertions; 18 19 import android.content.Context; 20 import android.util.ArraySet; 21 22 import java.util.Collections; 23 import java.util.Set; 24 25 public abstract class MimeGroupAssertions { assertMatchedByMimeGroup(String mimeGroup, String mimeType)26 public abstract void assertMatchedByMimeGroup(String mimeGroup, String mimeType); 27 assertNotMatchedByMimeGroup(String mimeGroup, String mimeType)28 public abstract void assertNotMatchedByMimeGroup(String mimeGroup, String mimeType); 29 assertMimeGroupInternal(String mimeGroup, Set<String> mimeTypes)30 public abstract void assertMimeGroupInternal(String mimeGroup, Set<String> mimeTypes); 31 assertMimeGroup(String mimeGroup, String... mimeTypes)32 public final void assertMimeGroup(String mimeGroup, String... mimeTypes) { 33 assertMimeGroupInternal(mimeGroup, new ArraySet<>(mimeTypes)); 34 } 35 assertMimeGroupIsEmpty(String mimeGroup)36 public final void assertMimeGroupIsEmpty(String mimeGroup) { 37 assertMimeGroupInternal(mimeGroup, Collections.emptySet()); 38 } 39 assertMimeGroupUndefined(String mimeGroup)40 public final void assertMimeGroupUndefined(String mimeGroup) { 41 assertMimeGroupInternal(mimeGroup, null); 42 } 43 assertMatchedByMimeGroup(String mimeGroup, String... mimeTypes)44 public final void assertMatchedByMimeGroup(String mimeGroup, String... mimeTypes) { 45 for (String mimeType: mimeTypes) { 46 assertMatchedByMimeGroup(mimeGroup, mimeType); 47 } 48 } 49 assertNotMatchedByMimeGroup(String mimeGroup, String... mimeTypes)50 public final void assertNotMatchedByMimeGroup(String mimeGroup, String... mimeTypes) { 51 for (String mimeType: mimeTypes) { 52 assertNotMatchedByMimeGroup(mimeGroup, mimeType); 53 } 54 } 55 testApp(Context context)56 public static MimeGroupAssertions testApp(Context context) { 57 return AssertionsByGroupData.testApp(context) 58 .mergeWith(AssertionsByIntentResolution.testApp(context)); 59 } 60 helperApp(Context context)61 public static MimeGroupAssertions helperApp(Context context) { 62 return AssertionsByGroupData.helperApp(context) 63 .mergeWith(AssertionsByIntentResolution.helperApp(context)); 64 } 65 appWithUpdates(Context context)66 public static MimeGroupAssertions appWithUpdates(Context context) { 67 return AssertionsByGroupData.appWithUpdates(context) 68 .mergeWith(AssertionsByIntentResolution.appWithUpdates(context)); 69 } 70 noOp()71 public static MimeGroupAssertions noOp() { 72 return new MimeGroupAssertions() { 73 @Override 74 public void assertMatchedByMimeGroup(String mimeGroup, String mimeType) { 75 } 76 77 @Override 78 public void assertNotMatchedByMimeGroup(String mimeGroup, String mimeType) { 79 } 80 81 @Override 82 public void assertMimeGroupInternal(String mimeGroup, Set<String> mimeTypes) { 83 } 84 }; 85 } 86 notUsed()87 public static MimeGroupAssertions notUsed() { 88 return new MimeGroupAssertions() { 89 @Override 90 public void assertMatchedByMimeGroup(String group, String type) { 91 throw new UnsupportedOperationException(); 92 } 93 94 @Override 95 public void assertNotMatchedByMimeGroup(String group, String type) { 96 throw new UnsupportedOperationException(); 97 } 98 99 @Override 100 public void assertMimeGroupInternal(String group, Set<String> types) { 101 throw new UnsupportedOperationException(); 102 } 103 }; 104 } 105 106 protected final MimeGroupAssertions mergeWith(MimeGroupAssertions other) { 107 return new MimeGroupAssertions() { 108 @Override 109 public void assertMatchedByMimeGroup(String mimeGroup, String mimeType) { 110 other.assertMatchedByMimeGroup(mimeGroup, mimeType); 111 MimeGroupAssertions.this.assertMatchedByMimeGroup(mimeGroup, mimeType); 112 } 113 114 @Override 115 public void assertNotMatchedByMimeGroup(String mimeGroup, String mimeType) { 116 other.assertNotMatchedByMimeGroup(mimeGroup, mimeType); 117 MimeGroupAssertions.this.assertNotMatchedByMimeGroup(mimeGroup, mimeType); 118 } 119 120 @Override 121 public void assertMimeGroupInternal(String mimeGroup, Set<String> mimeTypes) { 122 other.assertMimeGroupInternal(mimeGroup, mimeTypes); 123 MimeGroupAssertions.this.assertMimeGroupInternal(mimeGroup, mimeTypes); 124 } 125 }; 126 } 127 } 128