| 1 |
/* |
| 2 |
* Copyright (C) 2014 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.manifmerger; |
| 18 |
|
| 19 |
import static org.mockito.Mockito.verify; |
| 20 |
|
| 21 |
import com.android.utils.ILogger; |
| 22 |
|
| 23 |
import junit.framework.TestCase; |
| 24 |
|
| 25 |
import org.mockito.Mock; |
| 26 |
import org.mockito.Mockito; |
| 27 |
import org.mockito.MockitoAnnotations; |
| 28 |
|
| 29 |
/** |
| 30 |
* Tests for the {@link com.android.manifmerger.AttributeModel} class |
| 31 |
*/ |
| 32 |
public class AttributeModelTest extends TestCase { |
| 33 |
|
| 34 |
@Mock |
| 35 |
AttributeModel.Validator mValidator; |
| 36 |
|
| 37 |
@Mock |
| 38 |
XmlAttribute mXmlAttribute; |
| 39 |
|
| 40 |
@Mock |
| 41 |
ILogger mMockLog; |
| 42 |
|
| 43 |
@Override |
| 44 |
protected void setUp() throws Exception { |
| 45 |
super.setUp(); |
| 46 |
MockitoAnnotations.initMocks(this); |
| 47 |
} |
| 48 |
|
| 49 |
public void testGetters() { |
| 50 |
AttributeModel attributeModel = AttributeModel.newModel("someName") |
| 51 |
.setIsPackageDependent() |
| 52 |
.setDefaultValue("default_value") |
| 53 |
.setOnReadValidator(mValidator) |
| 54 |
.build(); |
| 55 |
|
| 56 |
assertEquals(XmlNode.fromXmlName("android:someName"), attributeModel.getName()); |
| 57 |
assertTrue(attributeModel.isPackageDependent()); |
| 58 |
assertEquals("default_value", attributeModel.getDefaultValue()); |
| 59 |
|
| 60 |
attributeModel = AttributeModel.newModel("someName").build(); |
| 61 |
|
| 62 |
assertEquals(XmlNode.fromXmlName("android:someName"), attributeModel.getName()); |
| 63 |
assertFalse(attributeModel.isPackageDependent()); |
| 64 |
assertEquals(null, attributeModel.getDefaultValue()); |
| 65 |
|
| 66 |
Mockito.verifyZeroInteractions(mValidator); |
| 67 |
} |
| 68 |
|
| 69 |
public void testBooleanValidator() { |
| 70 |
|
| 71 |
AttributeModel.BooleanValidator booleanValidator = new AttributeModel.BooleanValidator(); |
| 72 |
MergingReport.Builder mergingReport = new MergingReport.Builder(mMockLog); |
| 73 |
assertTrue(booleanValidator.validates(mergingReport, mXmlAttribute, "false")); |
| 74 |
assertTrue(booleanValidator.validates(mergingReport, mXmlAttribute, "true")); |
| 75 |
assertTrue(booleanValidator.validates(mergingReport, mXmlAttribute, "FALSE")); |
| 76 |
assertTrue(booleanValidator.validates(mergingReport, mXmlAttribute, "TRUE")); |
| 77 |
assertTrue(booleanValidator.validates(mergingReport, mXmlAttribute, "False")); |
| 78 |
assertTrue(booleanValidator.validates(mergingReport, mXmlAttribute, "True")); |
| 79 |
|
| 80 |
assertFalse(booleanValidator.validates(mergingReport, mXmlAttribute, "foo")); |
| 81 |
verify(mXmlAttribute).printPosition(); |
| 82 |
} |
| 83 |
|
| 84 |
public void testMultiValuesValidator() { |
| 85 |
AttributeModel.MultiValueValidator multiValueValidator = |
| 86 |
new AttributeModel.MultiValueValidator("foo", "bar", "doh !"); |
| 87 |
MergingReport.Builder mergingReport = new MergingReport.Builder(mMockLog); |
| 88 |
assertTrue(multiValueValidator.validates(mergingReport, mXmlAttribute, "foo")); |
| 89 |
assertTrue(multiValueValidator.validates(mergingReport, mXmlAttribute, "bar")); |
| 90 |
assertTrue(multiValueValidator.validates(mergingReport, mXmlAttribute, "doh !")); |
| 91 |
|
| 92 |
assertFalse(multiValueValidator.validates(mergingReport, mXmlAttribute, "oh no !")); |
| 93 |
} |
| 94 |
|
| 95 |
public void testIntegerValueValidator() { |
| 96 |
AttributeModel.IntegerValueValidator integerValueValidator = |
| 97 |
new AttributeModel.IntegerValueValidator(); |
| 98 |
MergingReport.Builder mergingReport = new MergingReport.Builder(mMockLog); |
| 99 |
assertFalse(integerValueValidator.validates(mergingReport, mXmlAttribute, "abcd")); |
| 100 |
assertFalse(integerValueValidator.validates(mergingReport, mXmlAttribute, |
| 101 |
"123456789123456789")); |
| 102 |
assertFalse(integerValueValidator.validates(mergingReport, mXmlAttribute, |
| 103 |
"0xFFFFFFFFFFFFFFFF")); |
| 104 |
} |
| 105 |
|
| 106 |
public void testStrictMergingPolicy() { |
| 107 |
assertEquals("ok", AttributeModel.STRICT_MERGING_POLICY.merge("ok", "ok")); |
| 108 |
assertNull(AttributeModel.STRICT_MERGING_POLICY.merge("one", "two")); |
| 109 |
} |
| 110 |
|
| 111 |
public void testOrMergingPolicy() { |
| 112 |
assertEquals("true", AttributeModel.OR_MERGING_POLICY.merge("true", "true")); |
| 113 |
assertEquals("true", AttributeModel.OR_MERGING_POLICY.merge("true", "false")); |
| 114 |
assertEquals("true", AttributeModel.OR_MERGING_POLICY.merge("false", "true")); |
| 115 |
assertEquals("false", AttributeModel.OR_MERGING_POLICY.merge("false", "false")); |
| 116 |
} |
| 117 |
|
| 118 |
public void testNumericalSuperiorityPolicy() { |
| 119 |
assertEquals("5", AttributeModel.NO_MERGING_POLICY.merge("5", "10")); |
| 120 |
assertEquals("10", AttributeModel.NO_MERGING_POLICY.merge("10", "5")); |
| 121 |
} |
| 122 |
} |