Skip to content

Commit

Permalink
plusonelabs#356 Fixed tests when there is no (permitted) active sourc…
Browse files Browse the repository at this point in the history
…e on a device
  • Loading branch information
yvolk committed Dec 30, 2019
1 parent b6a4ab4 commit 87c6917
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ public class BaseWidgetTest {
protected MockCalendarContentProvider provider = null;
protected RemoteViewsFactory factory = null;

protected int getNumberOfOpenTasksSources() {
return 0;
}

@Before
public void setUp() throws Exception {
provider = MockCalendarContentProvider.getContentProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
*/
public class PastDueHeaderWithTasksTest extends BaseWidgetTest {

@Override
protected int getNumberOfOpenTasksSources() {
return 1;
}

/**
* https://github.com/plusonelabs/calendar-widget/issues/205
*/
Expand Down
11 changes: 11 additions & 0 deletions app/src/androidTest/res/raw/birthday.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@
"versionName": "1.10.0"
},
"settings": {
"widgetInstanceName": "Birthday test",
"activeSources": [
{
"providerType": 1,
"id": 188,
"title": "[email protected]",
"summary": "Test active source",
"color": -12162669,
"isAvailable": true
}
],
"showDaysWithoutEvents": false,
"hideBasedOnKeywords": "",
"eventRange": 30,
Expand Down
11 changes: 11 additions & 0 deletions app/src/androidTest/res/raw/multi_day.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@
"versionName": "1.10.0"
},
"settings": {
"widgetInstanceName": "Multiple days events",
"activeSources": [
{
"providerType": 1,
"id": 194,
"title": "[email protected]",
"summary": "Test active source",
"color": -12162669,
"isAvailable": true
}
],
"showDaysWithoutEvents": false,
"hideBasedOnKeywords": "",
"eventRange": 30,
Expand Down
11 changes: 11 additions & 0 deletions app/src/androidTest/res/raw/wrong_dates_lost_events.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,17 @@
"versionName": "1.10.0"
},
"settings": {
"widgetInstanceName": "Wrong Dates Lost Events",
"activeSources": [
{
"providerType": 1,
"id": 354,
"title": "[email protected]",
"summary": "Test active source",
"color": -12162669,
"isAvailable": true
}
],
"showDaysWithoutEvents": false,
"hideBasedOnKeywords": "",
"eventRange": 7,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,8 @@ private List<WidgetEntryVisualizer<? extends WidgetEntry>> getVisualizers() {
getSettings().getContext(),
widgetId);
visualizers.add(dayHeaderVisualizer);
for (EventProviderType type : EventProviderType.values()) {
if (type.hasEventSources()) {
visualizers.add(type.getVisualizer(getSettings().getContext(), widgetId));
}
for (EventProviderType type : getSettings().getTypesOfActiveEventProviders()) {
visualizers.add(type.getVisualizer(getSettings().getContext(), widgetId));
}
visualizers.add(new LastEntryVisualizer(context, widgetId));
return visualizers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import static org.andstatus.todoagenda.prefs.SettingsStorage.saveJson;

Expand Down Expand Up @@ -288,7 +290,7 @@ private InstanceSettings setFromJson(JSONObject json) {
dayHeaderAlignment = json.getString(PREF_DAY_HEADER_ALIGNMENT);
}
if (json.has(PREF_QUERY_RESULTS)) {
queryResults = QueryResultsStorage.fromJson(widgetId, json.getJSONArray(PREF_QUERY_RESULTS));
setQueryResults(QueryResultsStorage.fromJson(widgetId, json.getJSONArray(PREF_QUERY_RESULTS)));
}
} catch (JSONException e) {
Log.w(TAG, "setFromJson failed, widgetId:" + widgetId + "\n" + json);
Expand Down Expand Up @@ -678,6 +680,10 @@ public boolean noTaskSources() {
return true;
}

public List<EventProviderType> getTypesOfActiveEventProviders() {
return getActiveEventSources().stream().map(s -> s.source.providerType).distinct().collect(Collectors.toList());
}

public OrderedEventSource getActiveEventSource(EventProviderType type, int id) {
for(OrderedEventSource orderedSource: getActiveEventSources()) {
if (orderedSource.source.providerType == type && orderedSource.source.getId() == id) {
Expand All @@ -698,5 +704,32 @@ public QueryResultsStorage getQueryResults() {

public void setQueryResults(QueryResultsStorage queryResults) {
this.queryResults = queryResults;
if (queryResults != null) {
for (EventProviderType providerType : queryResults.getProviderTypes(widgetId)) {
if (activeEventSources.stream().noneMatch(s -> s.source.providerType == providerType)) {
addActiveEventSource(providerType);
}
}
}
}

private void addActiveEventSource(EventProviderType providerType) {
int id = maxSourceId() + 1;
List<OrderedEventSource> list = new ArrayList<>(activeEventSources);
list.add(
new OrderedEventSource(
new EventSource(providerType, id, "Test source " + id, "", Color.CYAN, true),
getActiveEventSources().size() + 1
)
);
activeEventSources = list;
}

private int maxSourceId() {
int id1 = activeEventSources.stream().map(s -> s.source.getId())
.max(Comparator.comparingInt(id -> id)).orElse(1);
int id2 = EventProviderType.getAvailableSources().stream().map(s -> s.source.getId())
.max(Comparator.comparingInt(id -> id)).orElse(1);
return Math.max(id1, id2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,15 @@ public <R> R foldAvailableSources(@NonNull Uri uri, @Nullable String[] projectio
}

private Cursor queryAvailableSources(@NonNull Uri uri, @Nullable String[] projection) {
return widgetId == 0 || getSettings().getQueryResults() == null
? context.getContentResolver().query(uri, projection, null, null, null)
: getSettings().getQueryResults().getResult(type, requestsCounter.incrementAndGet() - 1)
try {
return widgetId == 0 || getSettings().getQueryResults() == null
? context.getContentResolver().query(uri, projection, null, null, null)
: getSettings().getQueryResults().getResult(type, requestsCounter.incrementAndGet() - 1)
.map(r -> r.querySource(projection)).orElse(null);
} catch (Exception e) {
Log.d(TAG, "Failed to get available sources for " + uri + "; " + e.getMessage());
return null;
}
}

public void onQueryEvents() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ public List<QueryResult> getResults(EventProviderType type, int widgetId) {
.collect(Collectors.toList());
}

public List<EventProviderType> getProviderTypes(int widgetId) {
return results.stream()
.filter(result -> widgetId == 0 || result.getWidgetId() == widgetId)
.map(result -> result.providerType)
.distinct()
.collect(Collectors.toList());
}

public Optional<QueryResult> findLast(EventProviderType type) {
for (int index = results.size() - 1; index >=0; index--) {
QueryResult result = results.get(index);
Expand Down

0 comments on commit 87c6917

Please sign in to comment.