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