1 /* 2 * Copyright (C) 2018 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 android.widget; 18 19 import android.view.View; 20 import android.view.ViewGroup; 21 22 import androidx.test.InstrumentationRegistry; 23 import androidx.test.annotation.UiThreadTest; 24 import androidx.test.filters.SmallTest; 25 import androidx.test.runner.AndroidJUnit4; 26 27 import org.junit.Assert; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 import java.time.Duration; 32 33 @RunWith(AndroidJUnit4.class) 34 @SmallTest 35 public class DateTimeViewTest { 36 37 @UiThreadTest 38 @Test additionalOnDetachedFromWindow_noException()39 public void additionalOnDetachedFromWindow_noException() { 40 final TestDateTimeView dateTimeView = new TestDateTimeView(); 41 dateTimeView.attachedToWindow(); 42 dateTimeView.detachedFromWindow(); 43 // Even there is an additional detach (abnormal), DateTimeView should not unregister 44 // receiver again that raises "java.lang.IllegalArgumentException: Receiver not registered". 45 dateTimeView.detachedFromWindow(); 46 } 47 48 @UiThreadTest 49 @Test noChangeInRelativeText_doesNotTriggerRelayout()50 public void noChangeInRelativeText_doesNotTriggerRelayout() { 51 // Week in the future is chosen because it'll result in a stable string during this test 52 // run. This should be improved once the class is refactored to be more testable in 53 // respect of clock retrieval. 54 final long weekInTheFuture = System.currentTimeMillis() + Duration.ofDays(7).toMillis(); 55 final TestDateTimeView dateTimeView = new TestDateTimeView(); 56 dateTimeView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 57 ViewGroup.LayoutParams.WRAP_CONTENT)); 58 dateTimeView.setShowRelativeTime(true); 59 dateTimeView.setTime(weekInTheFuture); 60 // View needs to be measured to request layout, skipping this would make this test pass 61 // always. 62 dateTimeView.measure(View.MeasureSpec.makeMeasureSpec(200, View.MeasureSpec.UNSPECIFIED), 63 View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.UNSPECIFIED)); 64 dateTimeView.reset(); 65 66 // This should not change the text content and thus no relayout is expected. 67 dateTimeView.setTime(weekInTheFuture + 1000); 68 69 Assert.assertFalse(dateTimeView.wasLayoutRequested()); 70 } 71 72 private static class TestDateTimeView extends DateTimeView { 73 private boolean mRequestedLayout = false; 74 TestDateTimeView()75 TestDateTimeView() { 76 super(InstrumentationRegistry.getContext()); 77 } 78 attachedToWindow()79 void attachedToWindow() { 80 super.onAttachedToWindow(); 81 } 82 detachedFromWindow()83 void detachedFromWindow() { 84 super.onDetachedFromWindow(); 85 } 86 requestLayout()87 public void requestLayout() { 88 super.requestLayout(); 89 mRequestedLayout = true; 90 } 91 wasLayoutRequested()92 public boolean wasLayoutRequested() { 93 return mRequestedLayout; 94 } 95 reset()96 public void reset() { 97 mRequestedLayout = false; 98 } 99 } 100 } 101