1 /*
2  * Copyright (C) 2012 Google Inc.
3  * Licensed to The Android Open Source Project.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.android.mail.ui;
19 
20 import android.os.Bundle;
21 import android.test.AndroidTestCase;
22 import android.test.suitebuilder.annotation.SmallTest;
23 import android.test.suitebuilder.annotation.Smoke;
24 
25 import com.android.mail.ui.ViewMode.ModeChangeListener;
26 
27 @Smoke
28 public class ViewModeTests extends AndroidTestCase {
29     /**
30      * Saving and restoring a view mode work correctly.
31      */
32     @SmallTest
testBundleSaveRestorePreserveState()33     public void testBundleSaveRestorePreserveState() {
34         Bundle state = new Bundle();
35         ViewMode first = new ViewMode();
36         // Set the state to something known.
37         first.enterConversationListMode();
38         first.handleSaveInstanceState(state);
39         ViewMode second = new ViewMode();
40         second.handleRestore(state);
41         assertEquals(ViewMode.CONVERSATION_LIST, second.getMode());
42     }
43 
44     /**
45      * Register a listener for mode changes. Change a mode, and verify that the listener was
46      * called. Then unregister the listener and change the mode again. Verify that the listener
47      * was NOT called.
48      */
49     @SmallTest
testListenerCalledAfterRegistering()50     public void testListenerCalledAfterRegistering() {
51         class Ears implements ModeChangeListener {
52             public int numCalls = 0;
53             @Override
54             public void onViewModeChanged(int mode) {
55                 numCalls++;
56             }
57         }
58 
59         ViewMode mode = new ViewMode();
60         Ears ears = new Ears();
61         mode.addListener(ears);
62         mode.enterConversationListMode();
63         assertEquals(ears.numCalls, 1);
64         mode.removeListener(ears);
65         mode.enterConversationMode();
66         assertEquals(ears.numCalls, 1);
67     }
68 
69     /**
70      * The view mode can transition to a state only if it isn't already in that state.
71      */
72     @SmallTest
testMultipleTransitionsFail()73     public void testMultipleTransitionsFail() {
74         ViewMode first = new ViewMode();
75         // Set the state to something known.
76         first.enterConversationListMode();
77         assertEquals(ViewMode.CONVERSATION_LIST, first.getMode());
78         // Cannot transition to Conversation List mode. I'm in it already.
79         first.enterConversationListMode();
80         assertEquals(ViewMode.CONVERSATION_LIST, first.getMode());
81     }
82 }