From b6b1b3d730d8e811fb2bb6a535f6e53a736c7386 Mon Sep 17 00:00:00 2001 From: simonpoole Date: Sun, 6 Jul 2025 09:26:48 +0200 Subject: [PATCH] Target API 35 This bumps the min API to 21 and target API 35. Includes some clean up and a fix for default template loading. --- CHANGELOG.txt | 5 + README.md | 2 +- build.gradle | 6 +- gradle/wrapper/gradle-wrapper.properties | 2 +- lib/build.gradle | 56 +- lib/src/androidTest/AndroidManifest.xml | 54 +- .../openinghoursfragment/FragmentTest.java | 666 +++++++++--------- .../openinghoursfragment/MixedModeTest.java | 2 +- .../openinghoursfragment/TextInputTest.java | 170 ++--- .../templates/TemplatesTest.java | 242 +++---- .../OpeningHoursFragment.java | 65 +- lib/src/main/res/values/styles.xml | 40 +- testapp/build.gradle | 33 +- .../java/ch/poole/ohfragmenttest/Test.java | 3 +- .../ch/poole/ohfragmenttest/TestFragment.java | 116 ++- 15 files changed, 741 insertions(+), 721 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 5c78ce3..5f80388 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,5 +1,10 @@ Please see the github repository for a complete list of changes: https://github.com/simonpoole/OpeningHoursFragment/commits/master +0.15.0: + +- Bump the minimum API to 21 and target API 35. +- Fix for default template loading. + 0.14.3: - Properly constrain selection position when clicking on error message diff --git a/README.md b/README.md index d61ece2..12e73e6 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,6 @@ repositories { ``` groovy dependencies { - compile "ch.poole:OpeningHoursFragment:0.14.3" + compile "ch.poole:OpeningHoursFragment:0.15.0" } ``` diff --git a/build.gradle b/build.gradle index 858bd63..7863d0a 100644 --- a/build.gradle +++ b/build.gradle @@ -6,9 +6,9 @@ buildscript { mavenLocal() } dependencies { - classpath 'com.android.tools.build:gradle:7.0.2' - classpath 'org.jacoco:org.jacoco.core:0.8.7' - classpath 'com.github.ksoichiro:gradle-eclipse-aar-plugin:0.3.1' + classpath 'com.android.tools.build:gradle:8.6.1' + classpath 'org.jacoco:org.jacoco.core:0.8.9' + classpath 'ch.poole.misc:gradle-eclipse-aar-plugin:0.1.0' } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index af01817..f3fdbe1 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/lib/build.gradle b/lib/build.gradle index b6e27b3..9294857 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -22,7 +22,7 @@ apply plugin: 'signing' apply plugin: "jacoco" apply plugin: "com.github.breadmoirai.github-release" -version = '0.14.3' +version = '0.15.0' def libName = "OpeningHoursFragment" task updateTranslations(type: Exec) { @@ -39,11 +39,13 @@ apply from: 'https://raw.githubusercontent.com/simonpoole/gradle-tasks/master/ec // apply from: '../../gradle-tasks/eclipse-android-3-lib' android { - compileSdkVersion 28 + namespace "ch.poole.openinghoursfragment" + + compileSdkVersion 35 defaultConfig { - minSdkVersion 16 - targetSdkVersion 31 + minSdkVersion 21 + targetSdkVersion 35 versionCode 100 versionName "${project.version}" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" @@ -99,34 +101,34 @@ android { ext { - androidxVersion = "1.0.0" adb = android.getAdbExe().toString() githubToken = System.getenv('GITHUB_TOKEN') } dependencies { - implementation "androidx.appcompat:appcompat:$androidxVersion" - implementation "androidx.recyclerview:recyclerview:$androidxVersion" - implementation "androidx.preference:preference:$androidxVersion" - implementation "com.google.android.material:material:$androidxVersion" - implementation "androidx.annotation:annotation:$androidxVersion" - implementation "androidx.core:core:$androidxVersion" + implementation "androidx.appcompat:appcompat:1.7.0" + implementation "androidx.appcompat:appcompat-resources:1.7.0" + implementation "androidx.recyclerview:recyclerview:1.1.0" + implementation "androidx.preference:preference:1.2.1" + implementation "com.google.android.material:material:1.8.0" + implementation "androidx.annotation:annotation:1.1.0" + implementation "androidx.core:core:1.16.0" implementation "ch.poole:OpeningHoursParser:0.26.0" implementation "ch.poole.android:rangebar:0.1.6" implementation 'ch.poole.android:numberpickerview:1.3.0' - implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + implementation 'androidx.constraintlayout:constraintlayout:2.2.1' // Instrumentation tests - androidTestImplementation 'androidx.test.ext:junit:1.1.3' - androidTestImplementation 'androidx.test:rules:1.4.0' - androidTestImplementation "org.hamcrest:hamcrest-library:1.3" - androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0' + androidTestImplementation 'androidx.test.ext:junit:1.2.1' + androidTestImplementation 'androidx.test:rules:1.6.1' + androidTestImplementation "org.hamcrest:hamcrest-library:2.2" + androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.3.0' // Unit tests - testImplementation "junit:junit:4.13" - testImplementation 'org.robolectric:robolectric:4.8.1' - testImplementation 'androidx.test.ext:junit:1.1.3' - testImplementation 'androidx.test:rules:1.4.0' + testImplementation "junit:junit:4.13.2" + testImplementation "org.robolectric:robolectric:4.15.1" + testImplementation 'androidx.test.ext:junit:1.2.1' + testImplementation 'androidx.test:rules:1.6.1' } android.libraryVariants.all { variant -> @@ -181,7 +183,7 @@ sonarqube { properties { property "sonar.java.source","1.8" property "sonar.sourceEncoding","UTF-8" - property "sonar.java.binaries", "build/intermediates/compile_library_classes_jar/debug/classes.jar" + property "sonar.java.binaries", "build/intermediates/compile_library_classes_jar/debug/bundleLibCompileToJarDebug/classes.jar" property "sonar.coverage.jacoco.xmlReportPaths","build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml" property "sonar.projectName","OpeningHoursFragment" } @@ -195,11 +197,11 @@ ext { task sourcesJar(type: Jar) { from releaseSource - classifier = 'sources' + archiveClassifier = 'sources' } task javadocJar(type: Jar, dependsOn: 'generateReleaseJavadoc') { - classifier = 'javadoc' + archiveClassifier = 'javadoc' from releaseJavadoc // options.encoding = 'UTF-8' } @@ -241,8 +243,8 @@ task jacocoTestReport(type:JacocoReport, dependsOn: "testDebugUnitTest") { include : ['jacoco.exec', 'jacoco/testDebugUnitTest.exec', 'outputs/code_coverage/debugAndroidTest/connected/*coverage.ec', 'spoon-output/currentDebug/coverage/merged-coverage.ec']) ]) reports { - xml.enabled = true - html.enabled = true + xml.required = true + html.required = true } sourceDirectories.from = files(coverageSourceDirs) @@ -302,8 +304,8 @@ publishing { username System.getenv('SONATYPE_USER') password System.getenv('SONATYPE_PASSWORD') } - def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/" - def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/" + def releasesRepoUrl = "https://ossrh-staging-api.central.sonatype.com/service/local/staging/deploy/maven2/" + def snapshotsRepoUrl = "https://ossrh-staging-api.central.sonatype.com/content/repositories/snapshots/" url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl } } diff --git a/lib/src/androidTest/AndroidManifest.xml b/lib/src/androidTest/AndroidManifest.xml index 8348f44..f048694 100644 --- a/lib/src/androidTest/AndroidManifest.xml +++ b/lib/src/androidTest/AndroidManifest.xml @@ -1,28 +1,28 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/src/androidTest/java/ch/poole/openinghoursfragment/FragmentTest.java b/lib/src/androidTest/java/ch/poole/openinghoursfragment/FragmentTest.java index ba4d40f..2f207bc 100644 --- a/lib/src/androidTest/java/ch/poole/openinghoursfragment/FragmentTest.java +++ b/lib/src/androidTest/java/ch/poole/openinghoursfragment/FragmentTest.java @@ -1,334 +1,334 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not - * distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -package ch.poole.openinghoursfragment; - -import java.util.Date; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentManager; -import androidx.fragment.app.FragmentTransaction; -import androidx.test.ext.junit.runners.AndroidJUnit4; -import androidx.test.filters.LargeTest; -import androidx.test.platform.app.InstrumentationRegistry; -import androidx.test.rule.ActivityTestRule; -import androidx.test.uiautomator.UiDevice; -import androidx.test.uiautomator.UiObject; -import androidx.test.uiautomator.UiObjectNotFoundException; -import androidx.test.uiautomator.UiSelector; - -@RunWith(AndroidJUnit4.class) -@LargeTest -public class FragmentTest { - - private OpeningHoursFragment fragment; - private UiDevice device; - - @Rule - public ActivityTestRule mActivityRule = new ActivityTestRule<>(TestActivity.class); - - @Before - public void setup() { - device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); - - FragmentManager fm = mActivityRule.getActivity().getSupportFragmentManager(); - FragmentTransaction ft = fm.beginTransaction(); - Fragment prev = fm.findFragmentByTag("fragment_openinghours"); - if (prev != null) { - ft.remove(prev); - } - ft.commit(); - - ValueWithDescription key = new ValueWithDescription("collection_times", "Collection times"); - - fragment = OpeningHoursFragment.newInstance(key, null, null, null, R.style.Theme_AppCompat_Dialog_Alert, 5, true, null, null); - fragment.show(fm, "fragment_openinghours"); - } - - @Test - public void weekdays() { - Assert.assertTrue(TestUtils.clickText(device, false, "Weekdays", true)); - - clickCheckBox(3); - clickCheckBox(6); - - Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); - Assert.assertEquals("Mo-We,Fr,Su 09:00-12:00,13:30-18:30; Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); - } - - @Test - public void addDaterange1() { - clickAddDateRange(); - Assert.assertTrue(TestUtils.clickText(device, false, "Date - date", true)); - UiSelector uiSelector = new UiSelector().resourceIdMatches(".*startMonth.*").instance(0); - UiObject date = device.findObject(uiSelector); - try { - date.clickAndWaitForNewWindow(); - } catch (UiObjectNotFoundException ex) { - Assert.fail(ex.getMessage()); - } - uiSelector = new UiSelector().resourceIdMatches(".*startMonth.*"); - UiObject months = device.findObject(uiSelector); - try { - months.clickBottomRight(); // should select Mar - } catch (UiObjectNotFoundException ex) { - Assert.fail(ex.getMessage()); - } - Assert.assertTrue(TestUtils.clickText(device, false, "OK", true)); - Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); - Assert.assertEquals("Mar Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); - } - - @Test - public void addDaterange2() { - clickAddDateRange(); - Assert.assertTrue(TestUtils.clickText(device, false, "Variable date - date", true)); - Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); - Assert.assertEquals("easter Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); - } - - @Test - public void addDaterange3() { - clickAddDateRange(); - Assert.assertTrue(TestUtils.clickText(device, false, "Occurrence in month - occurrence", true)); - Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); - Assert.assertEquals("Jan Mo[1]-Feb Mo[1] Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); - } - - @Test - public void addDaterange4() { - clickAddDateRange(); - TestUtils.scrollTo("With offsets"); - Assert.assertTrue(TestUtils.clickText(device, false, "With offsets", true)); - Assert.assertTrue(TestUtils.clickText(device, false, "Variable date - occurrence", true)); - Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); - Assert.assertEquals("easter+Mo-Jan Mo[1] Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); - } - - @Test - public void addDaterange5() { - clickAddDateRange(); - TestUtils.scrollTo("With offsets"); - Assert.assertTrue(TestUtils.clickText(device, false, "With offsets", true)); - Assert.assertTrue(TestUtils.clickText(device, false, "Date - variable date", true)); - Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); - Assert.assertEquals("Jan 1+Mo-easter Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); - } - - @Test - public void addDaterange6() { - clickAddDateRange(); - TestUtils.scrollTo("With offsets"); - Assert.assertTrue(TestUtils.clickText(device, false, "With offsets", true)); - Assert.assertTrue(TestUtils.clickText(device, false, "Variable date - variable date", true)); - Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); - Assert.assertEquals("easter+Mo-easter Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); - } - - /** - * - */ - private void clickAddDateRange() { - Assert.assertTrue(TestUtils.clickText(device, false, "Weekdays", true)); - TestUtils.clickOverflowButton(); - Assert.assertTrue(TestUtils.clickText(device, false, "Delete", true)); - TestUtils.clickOverflowButton(); - Assert.assertTrue(TestUtils.clickText(device, false, "Add date range", true)); - } - - public void occurranceInMonth() { - Assert.assertTrue(TestUtils.clickText(device, false, "Weekdays", true)); - TestUtils.clickOverflowButton(); - Assert.assertTrue(TestUtils.clickText(device, false, "Delete", true)); - TestUtils.clickOverflowButton(); - UiSelector uiSelector = new UiSelector().description("More options").instance(1); - UiObject overflowButton = device.findObject(uiSelector); - try { - overflowButton.clickAndWaitForNewWindow(); - } catch (UiObjectNotFoundException ex) { - Assert.fail(ex.getMessage()); - } - Assert.assertTrue(TestUtils.clickText(device, false, "Add occurrence", true)); - clickCheckBox(10); - Assert.assertEquals("Sa[3] 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); - } - - @Test - public void addWeekRange() { - Assert.assertTrue(TestUtils.clickText(device, false, "Weekdays", true)); - TestUtils.clickOverflowButton(); - Assert.assertTrue(TestUtils.clickText(device, false, "Delete", true)); - TestUtils.clickOverflowButton(); - Assert.assertTrue(TestUtils.clickText(device, false, "Add week range", true)); - UiSelector uiSelector = new UiSelector().description("More options").instance(1); - UiObject overflowButton = device.findObject(uiSelector); - try { - overflowButton.clickAndWaitForNewWindow(); - } catch (UiObjectNotFoundException ex) { - Assert.fail(ex.getMessage()); - } - Assert.assertTrue(TestUtils.clickText(device, false, "Show interval", true)); - Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); - Assert.assertEquals("week 01 Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); - } - - @Test - public void addYearRange() { - Assert.assertTrue(TestUtils.clickText(device, false, "Weekdays", true)); - TestUtils.clickOverflowButton(); - Assert.assertTrue(TestUtils.clickText(device, false, "Delete", true)); - TestUtils.clickOverflowButton(); - Assert.assertTrue(TestUtils.clickText(device, false, fragment.getString(R.string.spd_ohf_year_range_menu), true)); - Assert.assertTrue(TestUtils.clickTextExact(device, false, fragment.getString(R.string.add_year_range), true)); - UiSelector uiSelector = new UiSelector().description("More options").instance(1); - UiObject overflowButton = device.findObject(uiSelector); - try { - overflowButton.clickAndWaitForNewWindow(); - } catch (UiObjectNotFoundException ex) { - Assert.fail(ex.getMessage()); - } - Assert.assertTrue(TestUtils.clickText(device, false, "Show interval", true)); - Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); - Assert.assertEquals(Integer.toString(1900 + new Date().getYear()) + " Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); - } - - @Test - public void timerange() { - Assert.assertTrue(TestUtils.clickText(device, false, "Weekdays", true)); - UiSelector uiSelector = new UiSelector().resourceIdMatches(".*timebar.*").instance(0); - UiObject bar = device.findObject(uiSelector); - try { - bar.clickAndWaitForNewWindow(); - } catch (UiObjectNotFoundException ex) { - Assert.fail(ex.getMessage()); - } - uiSelector = new UiSelector().resourceIdMatches(".*startHour.*"); - UiObject hours = device.findObject(uiSelector); - try { - hours.clickTopLeft(); // should select 7 - } catch (UiObjectNotFoundException ex) { - Assert.fail(ex.getMessage()); - } - Assert.assertTrue(TestUtils.clickText(device, false, "OK", true)); - Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); - Assert.assertEquals("Mo-Fr 07:00-12:00,13:30-18:30; Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); - } - - /** - * Click via instance count as using the res id doesn't seem to work - * - * @param instance the 0 based occurrence of the checkbox - */ - private void clickCheckBox(int instance) { - UiSelector uiSelector = new UiSelector().className("android.widget.CheckBox").instance(instance); - UiObject button = device.findObject(uiSelector); - try { - button.click(); - } catch (UiObjectNotFoundException ex) { - Assert.fail(ex.getMessage()); - } - } - - @Test - public void addTimeRange1() { - deleteTimeBars(); - clickAddTimeSpan(); - Assert.assertTrue(TestUtils.clickText(device, false, "Time - extended time", true)); - Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); - Assert.assertEquals("Mo-Fr 00:00-48:00; Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); - } - - @Test - public void addTimeRange2() { - deleteTimeBars(); - clickAddTimeSpan(); - Assert.assertTrue(TestUtils.clickText(device, false, "Var. time - time", true)); - Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); - Assert.assertEquals("Mo-Fr dawn-24:00; Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); - } - - /** - * - */ - private void clickAddTimeSpan() { - TestUtils.clickOverflowButton(); - Assert.assertTrue(TestUtils.clickText(device, false, "Add time span", true)); - } - - @Test - public void addTimeRange3() { - deleteTimeBars(); - clickAddTimeSpan(); - Assert.assertTrue(TestUtils.clickText(device, false, "Var. time - var. time", true)); - Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); - Assert.assertEquals("Mo-Fr dawn-dusk; Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); - } - - @Test - public void addTimeRange4() { - deleteTimeBars(); - clickAddTimeSpan(); - Assert.assertTrue(TestUtils.clickText(device, false, "Time-open end", true)); - Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); - Assert.assertEquals("Mo-Fr 06:00+; Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); - } - - @Test - public void addTimeRange5() { - deleteTimeBars(); - clickAddTimeSpan(); - String text = fragment.getString(R.string.variable_time_open_end); - TestUtils.scrollTo(text); - Assert.assertTrue(TestUtils.clickText(device, false, text, true)); - Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); - Assert.assertEquals("Mo-Fr dawn+; Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); - } - - @Test - public void addTimeRange6() { - deleteTimeBars(); - clickAddTimeSpan(); - Assert.assertTrue(TestUtils.clickText(device, false, "Time - var. time", true)); - Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); - Assert.assertEquals("Mo-Fr 06:00-dusk; Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); - } - - @Test - public void addTimeRange7() { - deleteTimeBars(); - clickAddTimeSpan(); - Assert.assertTrue(TestUtils.clickTextExact(device, false, "Time", true)); - Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); - Assert.assertEquals("Mo-Fr 06:00; Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); - } - - /** - * - */ - private void deleteTimeBars() { - Assert.assertTrue(TestUtils.clickText(device, false, "Weekdays", true)); - UiSelector uiSelector = new UiSelector().resourceIdMatches(".*timebar.*").instance(0); - UiObject overflowButton = device.findObject(uiSelector.fromParent(new UiSelector().description("More options"))); - try { - overflowButton.clickAndWaitForNewWindow(); - } catch (UiObjectNotFoundException ex) { - Assert.fail(ex.getMessage()); - } - TestUtils.clickText(device, false, "Delete", true); - overflowButton = device.findObject(uiSelector.fromParent(new UiSelector().description("More options"))); - try { - overflowButton.clickAndWaitForNewWindow(); - } catch (UiObjectNotFoundException ex) { - Assert.fail(ex.getMessage()); - } - TestUtils.clickText(device, false, "Delete", true); - } +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not + * distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package ch.poole.openinghoursfragment; + +import java.util.Date; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentTransaction; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.LargeTest; +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.rule.ActivityTestRule; +import androidx.test.uiautomator.UiDevice; +import androidx.test.uiautomator.UiObject; +import androidx.test.uiautomator.UiObjectNotFoundException; +import androidx.test.uiautomator.UiSelector; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class FragmentTest { + + private OpeningHoursFragment fragment; + private UiDevice device; + + @Rule + public ActivityTestRule mActivityRule = new ActivityTestRule<>(TestActivity.class); + + @Before + public void setup() { + device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); + + FragmentManager fm = mActivityRule.getActivity().getSupportFragmentManager(); + FragmentTransaction ft = fm.beginTransaction(); + Fragment prev = fm.findFragmentByTag("fragment_openinghours"); + if (prev != null) { + ft.remove(prev); + } + ft.commit(); + + ValueWithDescription key = new ValueWithDescription("collection_times", "Collection times"); + + fragment = OpeningHoursFragment.newInstance(key, null, null, null, R.style.Theme_DialogLight, 5, true, null, null); + fragment.show(fm, "fragment_openinghours"); + } + + @Test + public void weekdays() { + Assert.assertTrue(TestUtils.clickText(device, false, "Weekdays", true)); + + clickCheckBox(3); + clickCheckBox(6); + + Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); + Assert.assertEquals("Mo-We,Fr,Su 09:00-12:00,13:30-18:30; Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); + } + + @Test + public void addDaterange1() { + clickAddDateRange(); + Assert.assertTrue(TestUtils.clickText(device, false, "Date - date", true)); + UiSelector uiSelector = new UiSelector().resourceIdMatches(".*startMonth.*").instance(0); + UiObject date = device.findObject(uiSelector); + try { + date.clickAndWaitForNewWindow(); + } catch (UiObjectNotFoundException ex) { + Assert.fail(ex.getMessage()); + } + uiSelector = new UiSelector().resourceIdMatches(".*startMonth.*"); + UiObject months = device.findObject(uiSelector); + try { + months.clickBottomRight(); // should select Mar + } catch (UiObjectNotFoundException ex) { + Assert.fail(ex.getMessage()); + } + Assert.assertTrue(TestUtils.clickText(device, false, "OK", true)); + Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); + Assert.assertEquals("Mar Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); + } + + @Test + public void addDaterange2() { + clickAddDateRange(); + Assert.assertTrue(TestUtils.clickText(device, false, "Variable date - date", true)); + Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); + Assert.assertEquals("easter Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); + } + + @Test + public void addDaterange3() { + clickAddDateRange(); + Assert.assertTrue(TestUtils.clickText(device, false, "Occurrence in month - occurrence", true)); + Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); + Assert.assertEquals("Jan Mo[1]-Feb Mo[1] Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); + } + + @Test + public void addDaterange4() { + clickAddDateRange(); + TestUtils.scrollTo("With offsets"); + Assert.assertTrue(TestUtils.clickText(device, false, "With offsets", true)); + Assert.assertTrue(TestUtils.clickText(device, false, "Variable date - occurrence", true)); + Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); + Assert.assertEquals("easter+Mo-Jan Mo[1] Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); + } + + @Test + public void addDaterange5() { + clickAddDateRange(); + TestUtils.scrollTo("With offsets"); + Assert.assertTrue(TestUtils.clickText(device, false, "With offsets", true)); + Assert.assertTrue(TestUtils.clickText(device, false, "Date - variable date", true)); + Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); + Assert.assertEquals("Jan 1+Mo-easter Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); + } + + @Test + public void addDaterange6() { + clickAddDateRange(); + TestUtils.scrollTo("With offsets"); + Assert.assertTrue(TestUtils.clickText(device, false, "With offsets", true)); + Assert.assertTrue(TestUtils.clickText(device, false, "Variable date - variable date", true)); + Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); + Assert.assertEquals("easter+Mo-easter Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); + } + + /** + * + */ + private void clickAddDateRange() { + Assert.assertTrue(TestUtils.clickText(device, false, "Weekdays", true)); + TestUtils.clickOverflowButton(); + Assert.assertTrue(TestUtils.clickText(device, false, "Delete", true)); + TestUtils.clickOverflowButton(); + Assert.assertTrue(TestUtils.clickText(device, false, "Add date range", true)); + } + + public void occurranceInMonth() { + Assert.assertTrue(TestUtils.clickText(device, false, "Weekdays", true)); + TestUtils.clickOverflowButton(); + Assert.assertTrue(TestUtils.clickText(device, false, "Delete", true)); + TestUtils.clickOverflowButton(); + UiSelector uiSelector = new UiSelector().description("More options").instance(1); + UiObject overflowButton = device.findObject(uiSelector); + try { + overflowButton.clickAndWaitForNewWindow(); + } catch (UiObjectNotFoundException ex) { + Assert.fail(ex.getMessage()); + } + Assert.assertTrue(TestUtils.clickText(device, false, "Add occurrence", true)); + clickCheckBox(10); + Assert.assertEquals("Sa[3] 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); + } + + @Test + public void addWeekRange() { + Assert.assertTrue(TestUtils.clickText(device, false, "Weekdays", true)); + TestUtils.clickOverflowButton(); + Assert.assertTrue(TestUtils.clickText(device, false, "Delete", true)); + TestUtils.clickOverflowButton(); + Assert.assertTrue(TestUtils.clickText(device, false, "Add week range", true)); + UiSelector uiSelector = new UiSelector().description("More options").instance(1); + UiObject overflowButton = device.findObject(uiSelector); + try { + overflowButton.clickAndWaitForNewWindow(); + } catch (UiObjectNotFoundException ex) { + Assert.fail(ex.getMessage()); + } + Assert.assertTrue(TestUtils.clickText(device, false, "Show interval", true)); + Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); + Assert.assertEquals("week 01 Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); + } + + @Test + public void addYearRange() { + Assert.assertTrue(TestUtils.clickText(device, false, "Weekdays", true)); + TestUtils.clickOverflowButton(); + Assert.assertTrue(TestUtils.clickText(device, false, "Delete", true)); + TestUtils.clickOverflowButton(); + Assert.assertTrue(TestUtils.clickText(device, false, fragment.getString(R.string.spd_ohf_year_range_menu), true)); + Assert.assertTrue(TestUtils.clickTextExact(device, false, fragment.getString(R.string.add_year_range), true)); + UiSelector uiSelector = new UiSelector().description("More options").instance(1); + UiObject overflowButton = device.findObject(uiSelector); + try { + overflowButton.clickAndWaitForNewWindow(); + } catch (UiObjectNotFoundException ex) { + Assert.fail(ex.getMessage()); + } + Assert.assertTrue(TestUtils.clickText(device, false, "Show interval", true)); + Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); + Assert.assertEquals(Integer.toString(1900 + new Date().getYear()) + " Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); + } + + @Test + public void timerange() { + Assert.assertTrue(TestUtils.clickText(device, false, "Weekdays", true)); + UiSelector uiSelector = new UiSelector().resourceIdMatches(".*timebar.*").instance(0); + UiObject bar = device.findObject(uiSelector); + try { + bar.clickAndWaitForNewWindow(); + } catch (UiObjectNotFoundException ex) { + Assert.fail(ex.getMessage()); + } + uiSelector = new UiSelector().resourceIdMatches(".*startHour.*"); + UiObject hours = device.findObject(uiSelector); + try { + hours.clickTopLeft(); // should select 7 + } catch (UiObjectNotFoundException ex) { + Assert.fail(ex.getMessage()); + } + Assert.assertTrue(TestUtils.clickText(device, false, "OK", true)); + Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); + Assert.assertEquals("Mo-Fr 07:00-12:00,13:30-18:30; Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); + } + + /** + * Click via instance count as using the res id doesn't seem to work + * + * @param instance the 0 based occurrence of the checkbox + */ + private void clickCheckBox(int instance) { + UiSelector uiSelector = new UiSelector().className("android.widget.CheckBox").instance(instance); + UiObject button = device.findObject(uiSelector); + try { + button.click(); + } catch (UiObjectNotFoundException ex) { + Assert.fail(ex.getMessage()); + } + } + + @Test + public void addTimeRange1() { + deleteTimeBars(); + clickAddTimeSpan(); + Assert.assertTrue(TestUtils.clickText(device, false, "Time - extended time", true)); + Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); + Assert.assertEquals("Mo-Fr 00:00-48:00; Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); + } + + @Test + public void addTimeRange2() { + deleteTimeBars(); + clickAddTimeSpan(); + Assert.assertTrue(TestUtils.clickText(device, false, "Var. time - time", true)); + Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); + Assert.assertEquals("Mo-Fr dawn-24:00; Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); + } + + /** + * + */ + private void clickAddTimeSpan() { + TestUtils.clickOverflowButton(); + Assert.assertTrue(TestUtils.clickText(device, false, "Add time span", true)); + } + + @Test + public void addTimeRange3() { + deleteTimeBars(); + clickAddTimeSpan(); + Assert.assertTrue(TestUtils.clickText(device, false, "Var. time - var. time", true)); + Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); + Assert.assertEquals("Mo-Fr dawn-dusk; Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); + } + + @Test + public void addTimeRange4() { + deleteTimeBars(); + clickAddTimeSpan(); + Assert.assertTrue(TestUtils.clickText(device, false, "Time-open end", true)); + Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); + Assert.assertEquals("Mo-Fr 06:00+; Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); + } + + @Test + public void addTimeRange5() { + deleteTimeBars(); + clickAddTimeSpan(); + String text = fragment.getString(R.string.variable_time_open_end); + TestUtils.scrollTo(text); + Assert.assertTrue(TestUtils.clickText(device, false, text, true)); + Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); + Assert.assertEquals("Mo-Fr dawn+; Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); + } + + @Test + public void addTimeRange6() { + deleteTimeBars(); + clickAddTimeSpan(); + Assert.assertTrue(TestUtils.clickText(device, false, "Time - var. time", true)); + Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); + Assert.assertEquals("Mo-Fr 06:00-dusk; Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); + } + + @Test + public void addTimeRange7() { + deleteTimeBars(); + clickAddTimeSpan(); + Assert.assertTrue(TestUtils.clickTextExact(device, false, "Time", true)); + Assert.assertTrue(TestUtils.clickText(device, false, "Save", true)); + Assert.assertEquals("Mo-Fr 06:00; Sa 09:00-17:00; PH closed", mActivityRule.getActivity().getResult()); + } + + /** + * + */ + private void deleteTimeBars() { + Assert.assertTrue(TestUtils.clickText(device, false, "Weekdays", true)); + UiSelector uiSelector = new UiSelector().resourceIdMatches(".*timebar.*").instance(0); + UiObject overflowButton = device.findObject(uiSelector.fromParent(new UiSelector().description("More options"))); + try { + overflowButton.clickAndWaitForNewWindow(); + } catch (UiObjectNotFoundException ex) { + Assert.fail(ex.getMessage()); + } + TestUtils.clickText(device, false, "Delete", true); + overflowButton = device.findObject(uiSelector.fromParent(new UiSelector().description("More options"))); + try { + overflowButton.clickAndWaitForNewWindow(); + } catch (UiObjectNotFoundException ex) { + Assert.fail(ex.getMessage()); + } + TestUtils.clickText(device, false, "Delete", true); + } } \ No newline at end of file diff --git a/lib/src/androidTest/java/ch/poole/openinghoursfragment/MixedModeTest.java b/lib/src/androidTest/java/ch/poole/openinghoursfragment/MixedModeTest.java index 42ede17..b085413 100644 --- a/lib/src/androidTest/java/ch/poole/openinghoursfragment/MixedModeTest.java +++ b/lib/src/androidTest/java/ch/poole/openinghoursfragment/MixedModeTest.java @@ -52,7 +52,7 @@ public void setup() { values.add(new ValueWithDescription("yes", "Yes")); values.add(new ValueWithDescription("no", "No")); - OpeningHoursFragment fragment = OpeningHoursFragment.newInstance(key, null, null, "no", R.style.Theme_AppCompat_Dialog_Alert, 5, false, values, null); + OpeningHoursFragment fragment = OpeningHoursFragment.newInstance(key, null, null, "no", R.style.Theme_DialogLight, 5, false, values, null); fragment.show(fm, "fragment_openinghours"); } diff --git a/lib/src/androidTest/java/ch/poole/openinghoursfragment/TextInputTest.java b/lib/src/androidTest/java/ch/poole/openinghoursfragment/TextInputTest.java index 575760d..620aae9 100644 --- a/lib/src/androidTest/java/ch/poole/openinghoursfragment/TextInputTest.java +++ b/lib/src/androidTest/java/ch/poole/openinghoursfragment/TextInputTest.java @@ -1,86 +1,86 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not - * distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -package ch.poole.openinghoursfragment; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -import android.app.Instrumentation; -import android.view.KeyEvent; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentManager; -import androidx.fragment.app.FragmentTransaction; -import androidx.test.ext.junit.runners.AndroidJUnit4; -import androidx.test.filters.LargeTest; -import androidx.test.platform.app.InstrumentationRegistry; -import androidx.test.rule.ActivityTestRule; -import androidx.test.uiautomator.UiDevice; -import androidx.test.uiautomator.UiObject; -import androidx.test.uiautomator.UiObjectNotFoundException; -import androidx.test.uiautomator.UiSelector; -import androidx.test.uiautomator.Until; - -@RunWith(AndroidJUnit4.class) -@LargeTest -public class TextInputTest { - - private OpeningHoursFragment fragment; - private UiDevice device; - private Instrumentation instrumentation; - - @Rule - public ActivityTestRule mActivityRule = new ActivityTestRule<>(TestActivity.class); - - @Before - public void setup() { - instrumentation = InstrumentationRegistry.getInstrumentation(); - device = UiDevice.getInstance(instrumentation); - - FragmentManager fm = mActivityRule.getActivity().getSupportFragmentManager(); - FragmentTransaction ft = fm.beginTransaction(); - Fragment prev = fm.findFragmentByTag("fragment_openinghours"); - if (prev != null) { - ft.remove(prev); - } - ft.commit(); - - ValueWithDescription key = new ValueWithDescription("opening_hours", "Opening hours"); - - fragment = OpeningHoursFragment.newInstance(key, null, null, null, R.style.Theme_AppCompat_Dialog_Alert, 5, false, null, null); - fragment.show(fm, "fragment_openinghours"); - UiSelector uiSelector = new UiSelector().resourceIdMatches(".*more.*"); - UiObject fab = device.findObject(uiSelector); - try { - fab.click(); - TestUtils.clickText(device, false, fragment.getString(R.string.clear), true); - } catch (UiObjectNotFoundException e) { - fail(e.getMessage()); - } - } - - @Test - public void twentyfourseven() { - UiSelector uiSelector = new UiSelector().resourceIdMatches(".*openinghours_string_edit.*"); - UiObject text = device.findObject(uiSelector); - try { - text.click(); - device.pressKeyCode(KeyEvent.KEYCODE_2); - device.pressKeyCode(KeyEvent.KEYCODE_4); - device.pressKeyCode(KeyEvent.KEYCODE_SLASH); - device.pressKeyCode(KeyEvent.KEYCODE_7); - device.pressEnter(); - device.performActionAndWait(()->{}, Until.newWindow(), 1000); - } catch (UiObjectNotFoundException e) { - fail(e.getMessage()); - } - assertNotNull(TestUtils.findText(device, false, fragment.getString(R.string.twentyfourseven))); - } +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not + * distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package ch.poole.openinghoursfragment; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import android.app.Instrumentation; +import android.view.KeyEvent; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentTransaction; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.LargeTest; +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.rule.ActivityTestRule; +import androidx.test.uiautomator.UiDevice; +import androidx.test.uiautomator.UiObject; +import androidx.test.uiautomator.UiObjectNotFoundException; +import androidx.test.uiautomator.UiSelector; +import androidx.test.uiautomator.Until; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class TextInputTest { + + private OpeningHoursFragment fragment; + private UiDevice device; + private Instrumentation instrumentation; + + @Rule + public ActivityTestRule mActivityRule = new ActivityTestRule<>(TestActivity.class); + + @Before + public void setup() { + instrumentation = InstrumentationRegistry.getInstrumentation(); + device = UiDevice.getInstance(instrumentation); + + FragmentManager fm = mActivityRule.getActivity().getSupportFragmentManager(); + FragmentTransaction ft = fm.beginTransaction(); + Fragment prev = fm.findFragmentByTag("fragment_openinghours"); + if (prev != null) { + ft.remove(prev); + } + ft.commit(); + + ValueWithDescription key = new ValueWithDescription("opening_hours", "Opening hours"); + + fragment = OpeningHoursFragment.newInstance(key, null, null, null, R.style.Theme_DialogLight, 5, false, null, null); + fragment.show(fm, "fragment_openinghours"); + UiSelector uiSelector = new UiSelector().resourceIdMatches(".*more.*"); + UiObject fab = device.findObject(uiSelector); + try { + fab.click(); + TestUtils.clickText(device, false, fragment.getString(R.string.clear), true); + } catch (UiObjectNotFoundException e) { + fail(e.getMessage()); + } + } + + @Test + public void twentyfourseven() { + UiSelector uiSelector = new UiSelector().resourceIdMatches(".*openinghours_string_edit.*"); + UiObject text = device.findObject(uiSelector); + try { + text.click(); + device.pressKeyCode(KeyEvent.KEYCODE_2); + device.pressKeyCode(KeyEvent.KEYCODE_4); + device.pressKeyCode(KeyEvent.KEYCODE_SLASH); + device.pressKeyCode(KeyEvent.KEYCODE_7); + device.pressEnter(); + device.performActionAndWait(()->{}, Until.newWindow(), 1000); + } catch (UiObjectNotFoundException e) { + fail(e.getMessage()); + } + assertNotNull(TestUtils.findText(device, false, fragment.getString(R.string.twentyfourseven))); + } } \ No newline at end of file diff --git a/lib/src/androidTest/java/ch/poole/openinghoursfragment/templates/TemplatesTest.java b/lib/src/androidTest/java/ch/poole/openinghoursfragment/templates/TemplatesTest.java index bfcbd4f..335f9e8 100644 --- a/lib/src/androidTest/java/ch/poole/openinghoursfragment/templates/TemplatesTest.java +++ b/lib/src/androidTest/java/ch/poole/openinghoursfragment/templates/TemplatesTest.java @@ -1,122 +1,122 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not - * distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -package ch.poole.openinghoursfragment.templates; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.InputStream; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentActivity; -import androidx.fragment.app.FragmentManager; -import androidx.fragment.app.FragmentTransaction; -import androidx.test.ext.junit.runners.AndroidJUnit4; -import androidx.test.filters.LargeTest; -import androidx.test.platform.app.InstrumentationRegistry; -import androidx.test.rule.ActivityTestRule; -import androidx.test.uiautomator.UiDevice; -import androidx.test.uiautomator.UiObject; -import androidx.test.uiautomator.UiObjectNotFoundException; -import androidx.test.uiautomator.UiSelector; -import ch.poole.openinghoursfragment.OpeningHoursFragment; -import ch.poole.openinghoursfragment.R; -import ch.poole.openinghoursfragment.TestActivity; -import ch.poole.openinghoursfragment.TestUtils; -import ch.poole.openinghoursfragment.ValueWithDescription; - -@RunWith(AndroidJUnit4.class) -@LargeTest -public class TemplatesTest { - - private OpeningHoursFragment fragment; - private UiDevice device; - FragmentManager fm; - TemplateDatabaseHelper db; - FragmentActivity activity; - - @Rule - public ActivityTestRule mActivityRule = new ActivityTestRule<>(TestActivity.class); - - @Before - public void setup() { - device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); - activity = mActivityRule.getActivity(); - fm = activity.getSupportFragmentManager(); - FragmentTransaction ft = fm.beginTransaction(); - Fragment prev = fm.findFragmentByTag("fragment_openinghours"); - if (prev != null) { - ft.remove(prev); - } - ft.commit(); - - // create the template database - db = new TemplateDatabaseHelper(activity); - ClassLoader loader = Thread.currentThread().getContextClassLoader(); - InputStream is = loader.getResourceAsStream("templates.json"); - TemplateDatabase.loadJson(db.getWritableDatabase(), is, true); - } - - @Test - public void manageTemplates() { - ValueWithDescription key = new ValueWithDescription("opening_hours", "Opening hours"); - fragment = OpeningHoursFragment.newInstance(key, "AT", null, null, R.style.Theme_AppCompat_Dialog_Alert, 5, true, null, null); - fragment.show(fm, "fragment_openinghours"); - assertNotNull(TestUtils.findTextContains(device, false, "Austria")); - assertNull(TestUtils.findTextContains(device, false, "Switzerland")); - assertTrue(TestUtils.clickText(device, false, "Weekdays", true)); - assertTrue(TestUtils.clickMenuButton("more button", false, true)); - assertTrue(TestUtils.clickText(device, false, "Manage templates", true)); - assertNotNull(TestUtils.findTextContains(device, false, "Austria")); - assertNull(TestUtils.findTextContains(device, false, "Switzerland")); - assertTrue(TestUtils.clickMenuButton("more button", false, true)); - assertTrue(TestUtils.clickText(device, false, "Show all", true)); - assertNotNull(TestUtils.findTextContains(device, false, "Austria")); - assertNotNull(TestUtils.findTextContains(device, false, "Switzerland")); - UiObject template = device.findObject(new UiSelector().textContains("Austria")); - try { - assertTrue(template.clickAndWaitForNewWindow()); - } catch (UiObjectNotFoundException e) { - fail(e.getMessage()); - } - assertTrue(TestUtils.clickText(device, false, "Delete", true)); - assertNull(TestUtils.findTextContains(device, false, "Austria")); - } - - @Test - public void useDefault() { - ValueWithDescription key = new ValueWithDescription("opening_hours", "Opening hours"); - fragment = OpeningHoursFragment.newInstance(key, "CH", null, null, R.style.Theme_AppCompat_Dialog_Alert, 5, false, null, null); - fragment.show(fm, "fragment_openinghours"); - assertNotNull(TestUtils.findTextContains(device, false, "Mo-Fr 09:00-12:00,13:30-18:30; Sa 09:00-17:00; PH closed")); - } - - @Test - public void exportImportTemplates() { - File dir = activity.getExternalCacheDir(); - final File file = new File(dir, "template_out.json"); - try { - assertTrue(TemplateDatabase.writeJSON(db.getWritableDatabase(), new FileOutputStream(file))); - TemplateDatabase.loadJson(db.getWritableDatabase(), new FileInputStream(file), true); - } catch (FileNotFoundException e) { - fail(e.getMessage()); - } finally { - file.delete(); - } - manageTemplates(); - } +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not + * distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package ch.poole.openinghoursfragment.templates; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.InputStream; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentTransaction; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.LargeTest; +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.rule.ActivityTestRule; +import androidx.test.uiautomator.UiDevice; +import androidx.test.uiautomator.UiObject; +import androidx.test.uiautomator.UiObjectNotFoundException; +import androidx.test.uiautomator.UiSelector; +import ch.poole.openinghoursfragment.OpeningHoursFragment; +import ch.poole.openinghoursfragment.R; +import ch.poole.openinghoursfragment.TestActivity; +import ch.poole.openinghoursfragment.TestUtils; +import ch.poole.openinghoursfragment.ValueWithDescription; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class TemplatesTest { + + private OpeningHoursFragment fragment; + private UiDevice device; + FragmentManager fm; + TemplateDatabaseHelper db; + FragmentActivity activity; + + @Rule + public ActivityTestRule mActivityRule = new ActivityTestRule<>(TestActivity.class); + + @Before + public void setup() { + device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); + activity = mActivityRule.getActivity(); + fm = activity.getSupportFragmentManager(); + FragmentTransaction ft = fm.beginTransaction(); + Fragment prev = fm.findFragmentByTag("fragment_openinghours"); + if (prev != null) { + ft.remove(prev); + } + ft.commit(); + + // create the template database + db = new TemplateDatabaseHelper(activity); + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + InputStream is = loader.getResourceAsStream("templates.json"); + TemplateDatabase.loadJson(db.getWritableDatabase(), is, true); + } + + @Test + public void manageTemplates() { + ValueWithDescription key = new ValueWithDescription("opening_hours", "Opening hours"); + fragment = OpeningHoursFragment.newInstance(key, "AT", null, null, R.style.Theme_DialogLight, 5, true, null, null); + fragment.show(fm, "fragment_openinghours"); + assertNotNull(TestUtils.findTextContains(device, false, "Austria")); + assertNull(TestUtils.findTextContains(device, false, "Switzerland")); + assertTrue(TestUtils.clickText(device, false, "Weekdays", true)); + assertTrue(TestUtils.clickMenuButton("more button", false, true)); + assertTrue(TestUtils.clickText(device, false, "Manage templates", true)); + assertNotNull(TestUtils.findTextContains(device, false, "Austria")); + assertNull(TestUtils.findTextContains(device, false, "Switzerland")); + assertTrue(TestUtils.clickMenuButton("more button", false, true)); + assertTrue(TestUtils.clickText(device, false, "Show all", true)); + assertNotNull(TestUtils.findTextContains(device, false, "Austria")); + assertNotNull(TestUtils.findTextContains(device, false, "Switzerland")); + UiObject template = device.findObject(new UiSelector().textContains("Austria")); + try { + assertTrue(template.clickAndWaitForNewWindow()); + } catch (UiObjectNotFoundException e) { + fail(e.getMessage()); + } + assertTrue(TestUtils.clickText(device, false, "Delete", true)); + assertNull(TestUtils.findTextContains(device, false, "Austria")); + } + + @Test + public void useDefault() { + ValueWithDescription key = new ValueWithDescription("opening_hours", "Opening hours"); + fragment = OpeningHoursFragment.newInstance(key, "CH", null, null, R.style.Theme_DialogLight, 5, false, null, null); + fragment.show(fm, "fragment_openinghours"); + assertNotNull(TestUtils.findTextContains(device, false, "Mo-Fr 09:00-12:00,13:30-18:30; Sa 09:00-17:00; PH closed")); + } + + @Test + public void exportImportTemplates() { + File dir = activity.getExternalCacheDir(); + final File file = new File(dir, "template_out.json"); + try { + assertTrue(TemplateDatabase.writeJSON(db.getWritableDatabase(), new FileOutputStream(file))); + TemplateDatabase.loadJson(db.getWritableDatabase(), new FileInputStream(file), true); + } catch (FileNotFoundException e) { + fail(e.getMessage()); + } finally { + file.delete(); + } + manageTemplates(); + } } \ No newline at end of file diff --git a/lib/src/main/java/ch/poole/openinghoursfragment/OpeningHoursFragment.java b/lib/src/main/java/ch/poole/openinghoursfragment/OpeningHoursFragment.java index 492e758..e2223c4 100644 --- a/lib/src/main/java/ch/poole/openinghoursfragment/OpeningHoursFragment.java +++ b/lib/src/main/java/ch/poole/openinghoursfragment/OpeningHoursFragment.java @@ -105,6 +105,7 @@ * */ public class OpeningHoursFragment extends DialogFragment implements SetDateRangeListener, SetRangeListener, SetTimeRangeListener, UpdateTextListener { + private static final String DEBUG_TAG = OpeningHoursFragment.class.getSimpleName(); private static final String VALUE_KEY = "value"; @@ -119,6 +120,9 @@ public class OpeningHoursFragment extends DialogFragment implements SetDateRange private static final String FRAGMENT_KEY = "fragment"; private static final String LOCALE_KEY = "locale"; + private static final String UNSUPPORTED_DATE = "Unsupported date "; + private static final String RULE_MISSING_FROM_LIST = "Rule missing from list!"; + protected static final int OSM_MAX_TAG_LENGTH = 255; private Context context = null; @@ -152,8 +156,6 @@ public class OpeningHoursFragment extends DialogFragment implements SetDateRange List months = Month.nameValues(); - private SQLiteDatabase mDatabase; - private boolean loadedDefault = false; private boolean showTemplates = false; @@ -379,13 +381,6 @@ public static OpeningHoursFragment newInstanceForFragment(@NonNull ValueWithDesc return f; } - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - Log.d(DEBUG_TAG, "onCreate"); - mDatabase = new TemplateDatabaseHelper(getContext()).getReadableDatabase(); - } - @Override @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { @@ -410,7 +405,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa showTemplates = getArguments().getBoolean(SHOWTEMPLATES_KEY); } if (styleRes == 0) { - styleRes = R.style.AlertDialog_AppCompat_Light; // fallback + styleRes = R.style.Theme_DialogLight; // fallback } if (openingHoursValue == null || "".equals(openingHoursValue)) { if (!showTemplates) { @@ -505,7 +500,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa saveListener.save(key.getValue(), text.getText().toString()); dismiss(); }); - return openingHoursLayout; } @@ -562,14 +556,19 @@ private void setUpOHMode() { * Try to locate a reasonable default value */ private void loadDefault() { - String[][] values = new String[][] { { region, object }, { null, object }, { region, null } }; - for (String[] v : values) { - openingHoursValue = TemplateDatabase.getDefault(mDatabase, key.getValue(), region, object); - if (openingHoursValue != null) { - return; + TemplateDatabaseHelper helper = new TemplateDatabaseHelper(getContext()); + try (SQLiteDatabase mDatabase = helper.getReadableDatabase()) { + String[][] values = new String[][] { { region, object }, { null, object }, { region, null } }; + for (String[] v : values) { + openingHoursValue = TemplateDatabase.getDefault(mDatabase, key.getValue(), v[0], v[1]); + if (openingHoursValue != null) { + return; + } } + openingHoursValue = TemplateDatabase.getDefault(mDatabase, null, null, null); + } finally { + helper.close(); } - openingHoursValue = TemplateDatabase.getDefault(mDatabase, null, null, null); } @Override @@ -577,7 +576,8 @@ public void onStart() { super.onStart(); Dialog dialog = getDialog(); if (dialog != null) { - dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); + final Window window = dialog.getWindow(); + window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, window.getAttributes().height); } } @@ -587,7 +587,7 @@ public void onStart() { * @author simon * */ - private class TextTextWatcher extends DefaultTextWatcher { + private final class TextTextWatcher extends DefaultTextWatcher { @Override public void afterTextChanged(Editable s) { @@ -1469,7 +1469,7 @@ public boolean onMenuItemClick(MenuItem item) { Rule duplicate = r.copy(); int current = rules.indexOf(r); if (current < 0) { // not found shouldn't happen - Log.e(DEBUG_TAG, "Rule missing from list!"); + Log.e(DEBUG_TAG, RULE_MISSING_FROM_LIST); return true; } rules.add(Math.max(0, current + 1), duplicate); @@ -1490,7 +1490,7 @@ public boolean onMenuItemClick(MenuItem item) { moveUp.setOnMenuItemClickListener(item -> { int current = rules.indexOf(r); if (current < 0) { // not found shouldn't happen - Log.e(DEBUG_TAG, "Rule missing from list!"); + Log.e(DEBUG_TAG, RULE_MISSING_FROM_LIST); return true; } rules.remove(current); @@ -1505,7 +1505,7 @@ public boolean onMenuItemClick(MenuItem item) { moveDown.setOnMenuItemClickListener(item -> { int current = rules.indexOf(r); if (current < 0) { // not found shouldn't happen - Log.e(DEBUG_TAG, "Rule missing from list!"); + Log.e(DEBUG_TAG, RULE_MISSING_FROM_LIST); return true; } int size = rules.size(); @@ -1867,7 +1867,7 @@ private void addDateRangeUI(@NonNull LinearLayout ll, @NonNull final Rule r, @No OccurrenceInMonthPicker.showDialog(OpeningHoursFragment.this, R.string.date_weekday_occurrence, start.getYear(), start.getMonth(), start.getNthWeekDay(), start.getNth()); } else { - Log.e(DEBUG_TAG, "Unsupported date " + start); + Log.e(DEBUG_TAG, UNSUPPORTED_DATE + start); } }); endDateLayout.setOnClickListener(v -> { @@ -1931,7 +1931,7 @@ private void addDateRangeUI(@NonNull LinearLayout ll, @NonNull final Rule r, @No OccurrenceInMonthPicker.showDialog(OpeningHoursFragment.this, R.string.date_weekday_occurrence, endDate.getYear(), endDate.getMonth(), endDate.getNthWeekDay(), endDate.getNth()); } else { - Log.e(DEBUG_TAG, "Unsupported date " + endDate); + Log.e(DEBUG_TAG, UNSUPPORTED_DATE + endDate); } }); } else { @@ -2121,7 +2121,7 @@ private void setDateRangeValues(@NonNull final DateWithOffset start, @Nullable f startDayView.setText(""); } } else { - Log.e(DEBUG_TAG, "Unsupported date " + start); + Log.e(DEBUG_TAG, UNSUPPORTED_DATE + start); } // offset stuff RelativeLayout startWeekDayContainer = (RelativeLayout) dateRangeRow.findViewById(R.id.startWeekDayContainer); @@ -2209,7 +2209,7 @@ private void setDateRangeValues(@NonNull final DateWithOffset start, @Nullable f endDayView.setText(""); } } else { - Log.e(DEBUG_TAG, "Unsupported date " + end); + Log.e(DEBUG_TAG, UNSUPPORTED_DATE + end); } Log.d(DEBUG_TAG, "month " + end.getMonth() + " day " + end.getDay() + " var date " + end.getVarDate()); if ((end.getMonth() != null && end.getDay() != DateWithOffset.UNDEFINED_MONTH_DAY) || end.getVarDate() != null || end.getNthWeekDay() != null) { @@ -3162,6 +3162,15 @@ private void checkNth(@NonNull RelativeLayout container, int nth) { } } + /** + * Setup the listeners for the weekday UI + * + * @param container container layout + * @param days + * @param inContainer + * @param justOne true if just one weekday + * @param nthMenuItem menuitem for Nth + */ private void setWeekDayListeners(@NonNull final RelativeLayout container, @NonNull final List days, @NonNull final List inContainer, final boolean justOne, @NonNull final MenuItem nthMenuItem) { OnCheckedChangeListener listener = (buttonView, isChecked) -> { @@ -3184,7 +3193,7 @@ private void setWeekDayListeners(@NonNull final RelativeLayout container, @NonNu } } } - ((CheckBox) buttonView).setChecked(true); + buttonView.setChecked(true); } } else { List temp = new ArrayList<>(days); @@ -3242,7 +3251,7 @@ private void setWeekDayListeners(@NonNull final RelativeLayout container, @NonNu } } } - ((CheckBox) buttonView).setChecked(false); + buttonView.setChecked(false); dwo.setWeekDayOffset((WeekDay) null); } updateString(); diff --git a/lib/src/main/res/values/styles.xml b/lib/src/main/res/values/styles.xml index fe2417b..0731158 100644 --- a/lib/src/main/res/values/styles.xml +++ b/lib/src/main/res/values/styles.xml @@ -1,19 +1,21 @@ - - - - - - - - + + + + + +