1 /** 2 * Copyright (C) 2014 Google Inc. 3 * Licensed to The Android Open Source Project. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.mail.utils; 19 20 import android.test.AndroidTestCase; 21 import android.test.suitebuilder.annotation.SmallTest; 22 23 import com.google.android.mail.common.base.Function; 24 25 @SmallTest 26 public class RankedComparatorTest extends AndroidTestCase { 27 28 private static final String RANK1 = "rank1"; 29 private static final String RANK2 = "rank2"; 30 private static final String[] RANKS = new String[]{RANK1, RANK2}; 31 private static final String UNKNOWN_RANK1 = "unknown_rank_1"; 32 private static final String UNKNOWN_RANK2 = "unknown_rank_2"; 33 private static final String NULL_RANK = null; 34 35 private RankedComparator<DummyObject, String> comparator; 36 37 @Override setUp()38 protected void setUp() throws Exception { 39 super.setUp(); 40 comparator = 41 new RankedComparator<DummyObject, String>(RANKS, DUMMY_OBJECT_TO_RANK_FUNCTION); 42 } 43 testSimple()44 public void testSimple() { 45 DummyObject rank1_1 = new DummyObject(RANK1); 46 DummyObject rank1_2 = new DummyObject(RANK1); 47 DummyObject rank2 = new DummyObject(RANK2); 48 49 assertTrue("Same object should be equal to itself.", 50 comparator.compare(rank1_1, rank1_1) == 0); 51 assertTrue("Different objects with same rank should be equal.", 52 comparator.compare(rank1_1, rank1_2) == 0); 53 54 // Testing different ranks and with different order of the parameters 55 assertTrue(comparator.compare(rank1_1, rank2) < 0); 56 assertTrue(comparator.compare(rank2, rank1_1) > 0); 57 } 58 testUnknownRank()59 public void testUnknownRank() { 60 DummyObject knownRank = new DummyObject(RANK1); 61 DummyObject unknownRank1 = new DummyObject(UNKNOWN_RANK1); 62 DummyObject unknownRank2 = new DummyObject(UNKNOWN_RANK2); 63 64 assertTrue("Known rank should be smaller than unknown rank.", 65 comparator.compare(knownRank, unknownRank1) < 0); 66 assertTrue("Unknown rank should be larger than known rank.", 67 comparator.compare(unknownRank1, knownRank) > 0); 68 assertTrue("Two different unknown ranks should be equal.", 69 comparator.compare(unknownRank1, unknownRank2) == 0); 70 } 71 testNullRank()72 public void testNullRank() { 73 DummyObject knownRank = new DummyObject(RANK1); 74 DummyObject unknownRank = new DummyObject(UNKNOWN_RANK1); 75 DummyObject nullRank = new DummyObject(NULL_RANK); 76 77 assertTrue("Known rank should be smaller than null rank.", 78 comparator.compare(knownRank, nullRank) < 0); 79 assertTrue("null rank should be larger than known rank.", 80 comparator.compare(nullRank, knownRank) > 0); 81 assertTrue("Unknown and null rank should be equal.", 82 comparator.compare(unknownRank, nullRank) == 0); 83 } 84 85 private static final Function<DummyObject, String> DUMMY_OBJECT_TO_RANK_FUNCTION = 86 new Function<DummyObject, String>() { 87 @Override 88 public String apply(DummyObject dummyObject) { 89 return dummyObject.rank; 90 } 91 }; 92 93 private class DummyObject { 94 private final String rank; 95 DummyObject(String rank)96 private DummyObject(String rank) { 97 this.rank = rank; 98 } 99 } 100 } 101