New time zone picker page

- Show current selected region and time zone in a 2 rows.
  Defailed info of current selected time zone in footer
- Show option menu to switch to select UTC offset
- This picker will be changed to the default picker in a later CL

Bug: 73952488
Test: m RunSettingsRoboTests
Change-Id: Ia81bb022e1021369612f5bd60c2c1f4d08db2af8
(cherry picked from commit b7d588f341)
This commit is contained in:
Victor Chang
2018-02-28 19:37:36 +00:00
parent 368b5a6b2c
commit d5adf42c5e
15 changed files with 1182 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.datetime.timezone;
import android.app.Activity;
import android.content.Context;
import android.support.v7.preference.Preference;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import static com.google.common.truth.Truth.assertThat;
@RunWith(SettingsRobolectricTestRunner.class)
public class BaseTimeZonePreferenceControllerTest {
private Activity mActivity;
@Before
public void setUp() {
mActivity = Robolectric.setupActivity(Activity.class);
}
@Test
public void handlePreferenceTreeClick_correctKey_triggerOnClickListener() {
String prefKey = "key1";
TestClickListener clickListener = new TestClickListener();
TestPreference preference = new TestPreference(mActivity, prefKey);
TestPreferenceController controller = new TestPreferenceController(mActivity, prefKey);
controller.setOnClickListener(clickListener);
controller.handlePreferenceTreeClick(preference);
assertThat(clickListener.isClicked()).isTrue();
}
@Test
public void handlePreferenceTreeClick_wrongKey_triggerOnClickListener() {
String prefKey = "key1";
TestClickListener clickListener = new TestClickListener();
TestPreference preference = new TestPreference(mActivity, "wrong_key");
TestPreferenceController controller = new TestPreferenceController(mActivity, prefKey);
controller.setOnClickListener(clickListener);
controller.handlePreferenceTreeClick(preference);
assertThat(clickListener.isClicked()).isFalse();
}
private static class TestPreferenceController extends BaseTimeZonePreferenceController {
private final Preference mTestPreference;
public TestPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mTestPreference = new Preference(context);
mTestPreference.setKey(preferenceKey);
}
}
private static class TestPreference extends Preference {
public TestPreference(Context context, String preferenceKey) {
super(context);
setKey(preferenceKey);
}
}
private static class TestClickListener implements OnPreferenceClickListener {
private boolean isClicked = false;
@Override
public void onClick() {
isClicked = true;
}
public boolean isClicked() {
return isClicked;
}
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.datetime.timezone;
import android.app.Activity;
import android.icu.util.TimeZone;
import android.support.v7.preference.Preference;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import static com.google.common.truth.Truth.assertThat;
@RunWith(SettingsRobolectricTestRunner.class)
public class FixedOffsetPreferenceControllerTest {
private Activity mActivity;
@Before
public void setUp() {
mActivity = Robolectric.setupActivity(Activity.class);
}
@Test
public void updateState_matchTimeZoneSummary() {
TimeZoneInfo fixedOffsetZone = new TimeZoneInfo.Builder(
TimeZone.getFrozenTimeZone("Etc/GMT-8"))
.setExemplarLocation("Los Angeles")
.setGmtOffset("GMT-08:00")
.setItemId(0)
.build();
Preference preference = new Preference(mActivity);
FixedOffsetPreferenceController controller = new FixedOffsetPreferenceController(mActivity);
controller.setTimeZoneInfo(fixedOffsetZone);
controller.updateState(preference);
assertThat(preference.getSummary()).isEqualTo("GMT-08:00");
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.datetime.timezone;
import android.app.Activity;
import android.support.v7.preference.Preference;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import static com.google.common.truth.Truth.assertThat;
@RunWith(SettingsRobolectricTestRunner.class)
public class RegionPreferenceControllerTest {
private Activity mActivity;
@Before
public void setUp() {
mActivity = Robolectric.setupActivity(Activity.class);
}
@Test
public void updateState_matchCountryName() {
Preference preference = new Preference(mActivity);
RegionPreferenceController controller = new RegionPreferenceController(mActivity);
controller.setRegionId("US");
controller.updateState(preference);
assertThat(controller.getSummary()).isEqualTo("United States");
assertThat(preference.getSummary()).isEqualTo("United States");
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.datetime.timezone;
import android.app.Activity;
import android.icu.util.TimeZone;
import android.support.v7.preference.Preference;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import static com.google.common.truth.Truth.assertThat;
@RunWith(SettingsRobolectricTestRunner.class)
public class RegionZonePreferenceControllerTest {
private Activity mActivity;
@Before
public void setUp() {
mActivity = Robolectric.setupActivity(Activity.class);
}
@Test
public void updateState_matchTimeZoneName() {
TimeZoneInfo tzInfo = new TimeZoneInfo.Builder(
TimeZone.getFrozenTimeZone("America/Los_Angeles"))
.setGenericName("Pacific Time")
.setStandardName("Pacific Standard Time")
.setDaylightName("Pacific Daylight Time")
.setExemplarLocation("Los Angeles")
.setGmtOffset("GMT-08:00")
.setItemId(0)
.build();
Preference preference = new Preference(mActivity);
RegionZonePreferenceController controller = new RegionZonePreferenceController(mActivity);
controller.setTimeZoneInfo(tzInfo);
controller.setClickable(false);
controller.updateState(preference);
String expectedSummary = "Los Angeles (GMT-08:00)";
assertThat(controller.getSummary().toString()).isEqualTo(expectedSummary);
assertThat(preference.getSummary().toString()).isEqualTo(expectedSummary);
assertThat(preference.isEnabled()).isFalse();
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.datetime.timezone;
import android.support.v7.preference.Preference;
import com.android.settings.datetime.timezone.TimeZoneInfo.Formatter;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import java.util.Date;
import java.util.Locale;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.spy;
@RunWith(SettingsRobolectricTestRunner.class)
public class TimeZoneInfoPreferenceControllerTest {
@Test
public void updateState_matchExpectedFormattedText() {
Date now = new Date(0L); // 00:00 1/1/1970
Formatter formatter = new Formatter(Locale.US, now);
TimeZoneInfo timeZoneInfo = formatter.format("America/Los_Angeles");
TimeZoneInfoPreferenceController controller =
new TimeZoneInfoPreferenceController(RuntimeEnvironment.application, now);
controller.setTimeZoneInfo(timeZoneInfo);
Preference preference = spy(new Preference(RuntimeEnvironment.application));
controller.updateState(preference);
assertEquals("Uses Pacific Time (GMT-08:00). "
+ "Pacific Daylight Time starts on April 26, 1970.",
preference.getTitle().toString());
}
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.datetime.timezone;
import com.android.settings.datetime.timezone.model.TimeZoneData;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowResources;
import com.android.settingslib.core.AbstractPreferenceController;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(shadows = {
SettingsShadowResources.class,
SettingsShadowResources.SettingsShadowTheme.class,
})
public class TimeZoneSettingsTest {
@Test
public void findRegionIdForTzId_matchExpectedCountry() {
String tzId = "Unknown/Secret_City";
TimeZoneData timeZoneData = mock(TimeZoneData.class);
when(timeZoneData.lookupCountryCodesForZoneId(tzId))
.thenReturn(new HashSet<>(Arrays.asList("US", "GB")));
TimeZoneSettings settings = new TimeZoneSettings();
settings.setTimeZoneData(timeZoneData);
assertThat(settings.findRegionIdForTzId(tzId, null, "")).matches("US|GB");
assertThat(settings.findRegionIdForTzId(tzId, "GB", "")).isEqualTo("GB");
assertThat(settings.findRegionIdForTzId(tzId, null, "GB")).isEqualTo("GB");
}
@Test
public void createPreferenceControllers_matchExpectedControllers() {
TimeZoneSettings settings = new TimeZoneSettings();
List<AbstractPreferenceController> controllers =
settings.createPreferenceControllers(RuntimeEnvironment.application);
assertThat(controllers).hasSize(4);
assertThat(controllers.get(0)).isInstanceOf(RegionPreferenceController.class);
assertThat(controllers.get(1)).isInstanceOf(RegionZonePreferenceController.class);
assertThat(controllers.get(2)).isInstanceOf(TimeZoneInfoPreferenceController.class);
assertThat(controllers.get(3)).isInstanceOf(FixedOffsetPreferenceController.class);
}
}