1
+ package com .mapbox .mapboxsdk .maps .attribution ;
2
+
3
+ import android .content .Context ;
4
+ import android .graphics .Bitmap ;
5
+ import android .graphics .PointF ;
6
+ import android .support .test .rule .ActivityTestRule ;
7
+ import android .view .View ;
8
+ import android .view .ViewGroup ;
9
+ import android .widget .TextView ;
10
+ import com .mapbox .mapboxsdk .attribution .AttributionParser ;
11
+ import com .mapbox .mapboxsdk .attribution .AttributionPlacement ;
12
+ import com .mapbox .mapboxsdk .attribution .AttributionProvider ;
13
+ import com .mapbox .mapboxsdk .testapp .activity .FeatureOverviewActivity ;
14
+ import org .junit .Before ;
15
+ import org .junit .Rule ;
16
+ import org .junit .Test ;
17
+
18
+ import static junit .framework .Assert .assertEquals ;
19
+
20
+ public class AttributionLayoutTest {
21
+
22
+ @ Rule
23
+ public ActivityTestRule <FeatureOverviewActivity > rule = new ActivityTestRule <>(FeatureOverviewActivity .class );
24
+
25
+ private static final String SATELLITE_ATTRIBUTION = "<a href=\" https://www.mapbox.com/about/maps/\" target=\" _blank\" >© Mapbox</a> <a href=\" http://www.openstreetmap.org/about/\" target=\" _blank\" >© OpenStreetMap</a> <a class=\" mapbox-improve-map\" href=\" https://www.mapbox.com/map-feedback/\" target=\" _blank\" >Improve this map</a> <a href=\" https://www.digitalglobe.com/\" target=\" _blank\" >© DigitalGlobe</a>\n " ;
26
+ private static final Bitmap LOGO = Bitmap .createBitmap (128 , 56 , Bitmap .Config .ARGB_8888 );
27
+ private static final Bitmap LOGO_SMALL = Bitmap .createBitmap (56 , 56 , Bitmap .Config .ARGB_8888 );
28
+ private static final String ATTRIBUTION = new AttributionParser .Options ()
29
+ .withAttributionData (SATELLITE_ATTRIBUTION )
30
+ .withImproveMap (false )
31
+ .withCopyrightSign (false )
32
+ .build ().getAttributionString ();
33
+
34
+ private Context context ;
35
+ private TextView textView ;
36
+ private float margin ;
37
+
38
+ @ Before
39
+ public void setUp () throws Exception {
40
+ context = rule .getActivity ();
41
+ margin = context .getResources ().getDisplayMetrics ().density * 4 ;
42
+
43
+ textView = new TextView (context );
44
+ textView .setLayoutParams (new ViewGroup .LayoutParams (
45
+ ViewGroup .LayoutParams .WRAP_CONTENT ,
46
+ ViewGroup .LayoutParams .WRAP_CONTENT )
47
+ );
48
+ textView .setSingleLine (true );
49
+ textView .setTextSize (8 );
50
+ textView .setText (ATTRIBUTION );
51
+ }
52
+
53
+ @ Test
54
+ public void testLogoLayout () throws Exception {
55
+ Bitmap snapshot = Bitmap .createBitmap (650 , 600 , Bitmap .Config .ARGB_8888 );
56
+ int widthMeasureSpec = View .MeasureSpec .makeMeasureSpec (snapshot .getWidth (), View .MeasureSpec .AT_MOST );
57
+ int heightMeasureSpec = View .MeasureSpec .makeMeasureSpec (0 , View .MeasureSpec .UNSPECIFIED );
58
+ textView .measure (widthMeasureSpec , heightMeasureSpec );
59
+
60
+ AttributionProvider attributionProvider = new AttributionProvider (snapshot , LOGO , LOGO_SMALL , textView , margin );
61
+
62
+ PointF expectedPoint = new PointF (snapshot .getWidth () - textView .getMeasuredWidth () - margin , snapshot .getHeight () - textView .getMeasuredHeight () - margin );
63
+ AttributionPlacement expected = new AttributionPlacement (LOGO , expectedPoint );
64
+ AttributionPlacement actual = attributionProvider .calculateAttributionPlacement ();
65
+ assertEquals ("Calculated placement should match: " , expected , actual );
66
+ }
67
+
68
+ @ Test
69
+ public void testSmallLogoLayout () {
70
+ Bitmap snapshot = Bitmap .createBitmap (512 , 512 , Bitmap .Config .ARGB_8888 );
71
+ int widthMeasureSpec = View .MeasureSpec .makeMeasureSpec (snapshot .getWidth (), View .MeasureSpec .AT_MOST );
72
+ int heightMeasureSpec = View .MeasureSpec .makeMeasureSpec (0 , View .MeasureSpec .UNSPECIFIED );
73
+ textView .measure (widthMeasureSpec , heightMeasureSpec );
74
+
75
+ AttributionProvider attributionProvider = new AttributionProvider (snapshot , LOGO , LOGO_SMALL , textView , margin );
76
+
77
+ PointF expectedPoint = new PointF (snapshot .getWidth () - textView .getMeasuredWidth () - margin , snapshot .getHeight () - textView .getMeasuredHeight () - margin );
78
+ AttributionPlacement expected = new AttributionPlacement (LOGO_SMALL , expectedPoint );
79
+ AttributionPlacement actual = attributionProvider .calculateAttributionPlacement ();
80
+ assertEquals ("Calculated placement should match: " , expected , actual );
81
+ }
82
+
83
+ @ Test
84
+ public void testNoLogoLayout () {
85
+ Bitmap snapshot = Bitmap .createBitmap (415 , 415 , Bitmap .Config .ARGB_8888 );
86
+ int widthMeasureSpec = View .MeasureSpec .makeMeasureSpec (0 , View .MeasureSpec .UNSPECIFIED );
87
+ int heightMeasureSpec = View .MeasureSpec .makeMeasureSpec (0 , View .MeasureSpec .UNSPECIFIED );
88
+ textView .measure (widthMeasureSpec , heightMeasureSpec );
89
+
90
+ AttributionProvider attributionProvider = new AttributionProvider (snapshot , LOGO , LOGO_SMALL , textView , margin );
91
+
92
+ PointF expectedPoint = new PointF (snapshot .getWidth () - textView .getMeasuredWidth () - margin , snapshot .getHeight () - textView .getMeasuredHeight () - margin );
93
+ AttributionPlacement expected = new AttributionPlacement (null , expectedPoint );
94
+ AttributionPlacement actual = attributionProvider .calculateAttributionPlacement ();
95
+ assertEquals ("Calculated placement should match: " , expected , actual );
96
+ }
97
+
98
+ @ Test
99
+ public void testNoAttributionLayout () {
100
+ Bitmap snapshot = Bitmap .createBitmap (25 , 25 , Bitmap .Config .ARGB_8888 );
101
+ int widthMeasureSpec = View .MeasureSpec .makeMeasureSpec (snapshot .getWidth (), View .MeasureSpec .AT_MOST );
102
+ int heightMeasureSpec = View .MeasureSpec .makeMeasureSpec (0 , View .MeasureSpec .UNSPECIFIED );
103
+ textView .measure (widthMeasureSpec , heightMeasureSpec );
104
+
105
+ AttributionProvider attributionProvider = new AttributionProvider (snapshot , LOGO , LOGO_SMALL , textView , margin );
106
+
107
+ AttributionPlacement expected = new AttributionPlacement (null , null );
108
+ AttributionPlacement actual = attributionProvider .calculateAttributionPlacement ();
109
+ assertEquals ("Calculated placement should match: " , expected , actual );
110
+ }
111
+ }
0 commit comments