1 /*
2  * Copyright (C) 2017 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 package com.android.documentsui.inspector;
17 
18 import android.media.ExifInterface;
19 import android.media.MediaMetadata;
20 import android.os.Bundle;
21 import android.provider.DocumentsContract;
22 import android.test.suitebuilder.annotation.SmallTest;
23 
24 import androidx.test.runner.AndroidJUnit4;
25 
26 import com.android.documentsui.R;
27 import com.android.documentsui.base.Shared;
28 import com.android.documentsui.testing.TestEnv;
29 import com.android.documentsui.testing.TestResources;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 
35 import java.util.function.Consumer;
36 
37 @RunWith(AndroidJUnit4.class)
38 @SmallTest
39 public class MediaViewTest {
40 
41     private TestResources mResources;
42     private TestTable mTable;
43     private Bundle mMetadata;
44     private Consumer<float[]> mGeo = (float[] coords) -> {
45         mTable.put(R.string.metadata_address, "1234 Street Street\n"
46                 + "City, State, 56789");
47     };
48 
49     @Before
setUp()50     public void setUp() {
51         mResources = TestResources.create();
52         // TODO: We should just be using the real underlying resources.
53         mResources.strings.put(R.string.metadata_dimensions_format, "%d x %d, %.1fMP");
54         mResources.strings.put(R.string.metadata_aperture_format, "f/%.1f");
55         mResources.strings.put(R.string.metadata_coordinates_format, "%.3f, %.3f");
56         mResources.strings.put(R.string.metadata_camera_format, "%s %s");
57         mTable = new TestTable();
58         mMetadata = new Bundle();
59         TestMetadata.populateExifData(mMetadata);
60         TestMetadata.populateVideoData(mMetadata);
61         TestMetadata.populateAudioData(mMetadata);
62     }
63 
64     /**
65      * Test that the updateMetadata method is printing metadata for selected items found in the
66      * bundle.
67      */
68     @Test
testShowExifData()69     public void testShowExifData() throws Exception {
70         mResources.strings.put(R.string.metadata_focal_format, "%.2f mm");
71         mResources.strings.put(R.string.metadata_iso_format, "ISO %d");
72         Bundle exif = mMetadata.getBundle(DocumentsContract.METADATA_EXIF);
73         MediaView.showExifData(mTable, mResources, TestEnv.FILE_JPG, exif, null, mGeo);
74 
75         mTable.assertHasRow(R.string.metadata_dimensions, "3840 x 2160, 8.3MP");
76         mTable.assertHasRow(R.string.metadata_date_time, "Jan 01, 1970, 12:16 AM");
77         mTable.assertHasRow(R.string.metadata_coordinates, "33.996, -118.475");
78         mTable.assertHasRow(R.string.metadata_altitude, "1244.0");
79         mTable.assertHasRow(R.string.metadata_camera, "Google Pixel");
80         mTable.assertHasRow(R.string.metadata_shutter_speed, "1/100");
81         mTable.assertHasRow(R.string.metadata_aperture, "f/2.0");
82         mTable.assertHasRow(R.string.metadata_iso_speed_ratings, "ISO 120");
83         mTable.assertHasRow(R.string.metadata_focal_length, "4.27 mm");
84         mTable.assertHasRow(R.string.metadata_address, "1234 Street Street\n"
85                 + "City, State, 56789");
86     }
87 
88     /**
89      * Bundle only supplies half of the values for the pairs that print in printMetaData. No put
90      * method should be called as the correct conditions have not been met.
91      * @throws Exception
92      */
93     @Test
testShowExifData_PartialGpsTags()94     public void testShowExifData_PartialGpsTags() throws Exception {
95         Bundle data = new Bundle();
96         data.putDouble(ExifInterface.TAG_GPS_LATITUDE, 37.7749);
97 
98         mMetadata.putBundle(DocumentsContract.METADATA_EXIF, data);
99         MediaView.showExifData(mTable, mResources, TestEnv.FILE_JPG, mMetadata, null, mGeo);
100         mTable.assertEmpty();
101     }
102 
103     /**
104      * Bundle only supplies half of the values for the pairs that print in printMetaData. No put
105      * method should be called as the correct conditions have not been met.
106      * @throws Exception
107      */
108     @Test
testShowExifData_PartialDimensionTags()109     public void testShowExifData_PartialDimensionTags() throws Exception {
110         Bundle data = new Bundle();
111         data.putInt(ExifInterface.TAG_IMAGE_WIDTH, 3840);
112 
113         mMetadata.putBundle(DocumentsContract.METADATA_EXIF, data);
114         MediaView.showExifData(mTable, mResources, TestEnv.FILE_JPG, mMetadata, null, mGeo);
115         mTable.assertEmpty();
116     }
117 
118     /**
119      * Test that the updateMetadata method is printing metadata for selected items found in the
120      * bundle.
121      */
122     @Test
testShowVideoData()123     public void testShowVideoData() throws Exception {
124         Bundle data = mMetadata.getBundle(Shared.METADATA_KEY_VIDEO);
125         MediaView.showVideoData(mTable, mResources, TestEnv.FILE_MP4, data, null);
126 
127         mTable.assertHasRow(R.string.metadata_duration, "01:12");
128         mTable.assertHasRow(R.string.metadata_dimensions, "1920 x 1080, 2.1MP");
129     }
130 
131     @Test
testShowAudioData()132     public void testShowAudioData() throws Exception {
133         Bundle data = mMetadata.getBundle(Shared.METADATA_KEY_AUDIO);
134         MediaView.showAudioData(mTable, data);
135 
136         mTable.assertHasRow(R.string.metadata_duration, "01:12");
137         mTable.assertHasRow(R.string.metadata_artist, "artist");
138         mTable.assertHasRow(R.string.metadata_composer, "composer");
139         mTable.assertHasRow(R.string.metadata_album, "album");
140     }
141 
142     /**
143      * Test that the updateMetadata method is printing metadata for selected items found in the
144      * bundle.
145      */
146     @Test
testShowVideoData_HourPlusDuration()147     public void testShowVideoData_HourPlusDuration() throws Exception {
148         Bundle data = mMetadata.getBundle(Shared.METADATA_KEY_VIDEO);
149         data.putInt(MediaMetadata.METADATA_KEY_DURATION, 21660000);
150         MediaView.showVideoData(mTable, mResources, TestEnv.FILE_MP4, data, null);
151 
152         mTable.assertHasRow(R.string.metadata_duration, "6:01:00");
153     }
154 
155     @Test
testGetAddress()156     public void testGetAddress() throws Exception {
157         Consumer<float[]> badAddress = (float[] coords) -> {
158         };
159         Bundle data = new Bundle();
160         data.putInt(ExifInterface.TAG_IMAGE_WIDTH, 3840);
161         data.putInt(ExifInterface.TAG_IMAGE_LENGTH, 2160);
162 
163         mMetadata.getBundle(DocumentsContract.METADATA_EXIF);
164         MediaView.showExifData(mTable, mResources, TestEnv.FILE_JPG, mMetadata, null, badAddress);
165         mTable.assertNotInTable(R.string.metadata_address);
166     }
167 }
168