From dc3f9f4d698267f8b2d44127d6c7bc8c1f450aba Mon Sep 17 00:00:00 2001 From: Marc Blank Date: Thu, 6 Jan 2011 17:38:51 -0800 Subject: [PATCH] Fix widget view switching with > 1 account * Add test case for view switching Bug: 3329906 Change-Id: I738bad118d7d7a8d2839fbd2a9fdc00af2af3153 --- .../email/provider/WidgetProvider.java | 40 ++++++-- .../email/provider/WidgetProviderTests.java | 92 +++++++++++++++++++ 2 files changed, 122 insertions(+), 10 deletions(-) create mode 100644 tests/src/com/android/email/provider/WidgetProviderTests.java diff --git a/src/com/android/email/provider/WidgetProvider.java b/src/com/android/email/provider/WidgetProvider.java index 584f164e1..20615c367 100644 --- a/src/com/android/email/provider/WidgetProvider.java +++ b/src/com/android/email/provider/WidgetProvider.java @@ -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 { + /*package*/ static class WidgetViewSwitcher extends AsyncTask { 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(); + } } } diff --git a/tests/src/com/android/email/provider/WidgetProviderTests.java b/tests/src/com/android/email/provider/WidgetProviderTests.java new file mode 100644 index 000000000..109246949 --- /dev/null +++ b/tests/src/com/android/email/provider/WidgetProviderTests.java @@ -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 { + 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); + } +}