Fix widget view switching with > 1 account

* Add test case for view switching

Bug: 3329906
Change-Id: I738bad118d7d7a8d2839fbd2a9fdc00af2af3153
This commit is contained in:
Marc Blank 2011-01-06 17:38:51 -08:00
parent 2f1a7e8d0a
commit dc3f9f4d69
2 changed files with 122 additions and 10 deletions

View File

@ -163,7 +163,7 @@ public class WidgetProvider extends AppWidgetProvider {
private WidgetLoader mLoader;
// The current view type (all mail, unread, or starred for now)
private ViewType mViewType = ViewType.STARRED;
/*package*/ ViewType mViewType = ViewType.STARRED;
// The projection to be used by the WidgetLoader
public static final String[] WIDGET_PROJECTION = new String[] {
@ -235,8 +235,6 @@ public class WidgetProvider extends AppWidgetProvider {
sWidgetManager.notifyAppWidgetViewDataChanged(mWidgetId, R.id.message_list);
}
});
new WidgetViewSwitcher(EmailWidget.this).execute();
}
/**
@ -252,6 +250,13 @@ public class WidgetProvider extends AppWidgetProvider {
}
}
/**
* Initialize to first appropriate view (depending on the number of accounts)
*/
private void init() {
new WidgetViewSwitcher(this).execute();
}
/**
* Reset cursor and cursor count, notify widget that list data is invalid, and start loading
* with our current ViewType
@ -565,7 +570,17 @@ public class WidgetProvider extends AppWidgetProvider {
}
}
private static EmailWidget getOrCreateWidget(Context context, int widgetId) {
/**
* Force a context for widgets (used by unit tests)
* @param context the Context to set
*/
/*package*/ static void setContextForTest(Context context) {
sContext = context;
sResolver = context.getContentResolver();
sWidgetManager = AppWidgetManager.getInstance(context);
}
/*package*/ static EmailWidget getOrCreateWidget(Context context, int widgetId) {
// Lazily initialize these
if (sContext == null) {
if (context == null) { // STOPSHIP remove this check
@ -584,6 +599,7 @@ public class WidgetProvider extends AppWidgetProvider {
Log.d(TAG, "Creating EmailWidget for id #" + widgetId);
}
widget = new EmailWidget(widgetId);
widget.init();
sWidgetMap.put(widgetId, widget);
}
return widget;
@ -638,19 +654,21 @@ public class WidgetProvider extends AppWidgetProvider {
* to determine account status, etc. In the foreground, we start up the Loader with new
* parameters
*/
public static class WidgetViewSwitcher extends AsyncTask<Void, Void, Void> {
/*package*/ static class WidgetViewSwitcher extends AsyncTask<Void, Void, Void> {
private final EmailWidget mWidget;
private boolean mLoadAfterSwitch;
public WidgetViewSwitcher(EmailWidget widget) {
mWidget = widget;
}
/*package*/ void disableLoadAfterSwitchForTest() {
mLoadAfterSwitch = false;
}
@Override
protected Void doInBackground(Void... params) {
// Don't use the "all mail" view if we've got 0 or 1 account
if (EmailContent.count(sContext, Account.CONTENT_URI) < 2) {
mWidget.switchToNextView();
}
mWidget.switchToNextView();
return null;
}
@ -659,7 +677,9 @@ public class WidgetProvider extends AppWidgetProvider {
if (isCancelled()) {
return;
}
mWidget.loadView();
if (mLoadAfterSwitch) {
mWidget.loadView();
}
}
}

View File

@ -0,0 +1,92 @@
/* Copyright (C) 2011 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.email.provider;
import com.android.email.provider.WidgetProvider.EmailWidget;
import com.android.email.provider.WidgetProvider.WidgetViewSwitcher;
import android.content.Context;
import android.test.ProviderTestCase2;
import java.util.concurrent.ExecutionException;
/**
* Tests of WidgetProvider
*
* You can run this entire test case with:
* runtest -c com.android.email.provider.WidgetProviderTests email
*/
public class WidgetProviderTests extends ProviderTestCase2<EmailProvider> {
private Context mMockContext;
public WidgetProviderTests() {
super(EmailProvider.class, EmailProvider.EMAIL_AUTHORITY);
}
@Override
public void setUp() throws Exception {
super.setUp();
mMockContext = getMockContext();
}
@Override
public void tearDown() throws Exception {
super.tearDown();
}
/**
* Switch views synchronously without loading
*/
private void switchSync(EmailWidget widget) {
WidgetViewSwitcher switcher = new WidgetProvider.WidgetViewSwitcher(widget);
try {
switcher.disableLoadAfterSwitchForTest();
switcher.execute().get();
} catch (InterruptedException e) {
} catch (ExecutionException e) {
}
}
public void testWidgetSwitcher() {
// Create account
ProviderTestUtils.setupAccount("account1", true, mMockContext);
// Manually set up context
WidgetProvider.setContextForTest(mMockContext);
// Create a widget
EmailWidget widget = new EmailWidget(1);
// Since there is one account, this should switch to the ACCOUNT view
switchSync(widget);
assertEquals(WidgetProvider.ViewType.ACCOUNT, widget.mViewType);
// Create account
ProviderTestUtils.setupAccount("account2", true, mMockContext);
// Create a widget
widget = new EmailWidget(2);
// Since there are two accounts, this should switch to the ALL_MAIL view
switchSync(widget);
assertEquals(WidgetProvider.ViewType.ALL_MAIL, widget.mViewType);
// The next two switches should be to the two accounts
switchSync(widget);
assertEquals(WidgetProvider.ViewType.ACCOUNT, widget.mViewType);
switchSync(widget);
assertEquals(WidgetProvider.ViewType.ACCOUNT, widget.mViewType);
switchSync(widget);
assertEquals(WidgetProvider.ViewType.UNREAD, widget.mViewType);
switchSync(widget);
assertEquals(WidgetProvider.ViewType.STARRED, widget.mViewType);
}
}