• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.launcher3.model;
2 
3 
4 import static android.database.DatabaseUtils.queryNumEntries;
5 
6 import static com.android.launcher3.LauncherSettings.Favorites.BACKUP_TABLE_NAME;
7 import static com.android.launcher3.LauncherSettings.Favorites.TABLE_NAME;
8 import static com.android.launcher3.provider.LauncherDbUtils.tableExists;
9 import static com.android.launcher3.util.LauncherModelHelper.APP_ICON;
10 import static com.android.launcher3.util.LauncherModelHelper.DESKTOP;
11 import static com.android.launcher3.util.LauncherModelHelper.NO__ICON;
12 import static com.android.launcher3.util.LauncherModelHelper.SHORTCUT;
13 
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertFalse;
16 import static org.junit.Assert.assertTrue;
17 
18 import android.content.ContentValues;
19 import android.content.Context;
20 import android.database.sqlite.SQLiteDatabase;
21 import android.graphics.Point;
22 
23 import com.android.launcher3.LauncherSettings.Favorites;
24 import com.android.launcher3.LauncherSettings.Settings;
25 import com.android.launcher3.util.LauncherModelHelper;
26 
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.robolectric.RobolectricTestRunner;
31 import org.robolectric.RuntimeEnvironment;
32 
33 /**
34  * Unit tests for {@link GridBackupTable}
35  */
36 @RunWith(RobolectricTestRunner.class)
37 public class GridBackupTableTest {
38 
39     private static final int BACKUP_ITEM_COUNT = 12;
40 
41     private LauncherModelHelper mModelHelper;
42     private Context mContext;
43     private SQLiteDatabase mDb;
44 
45     @Before
setUp()46     public void setUp() {
47         mModelHelper = new LauncherModelHelper();
48         mContext = RuntimeEnvironment.application;
49         mDb = mModelHelper.provider.getDb();
50 
51         setupGridData();
52     }
53 
setupGridData()54     private void setupGridData() {
55         mModelHelper.createGrid(new int[][][]{{
56                 { APP_ICON, APP_ICON, SHORTCUT, SHORTCUT},
57                 { SHORTCUT, SHORTCUT, NO__ICON, NO__ICON},
58                 { NO__ICON, NO__ICON, SHORTCUT, SHORTCUT},
59                 { APP_ICON, SHORTCUT, SHORTCUT, APP_ICON},
60         }});
61         assertEquals(BACKUP_ITEM_COUNT, queryNumEntries(mDb, TABLE_NAME));
62     }
63 
64     @Test
backupTableCreated()65     public void backupTableCreated() {
66         GridBackupTable backupTable = new GridBackupTable(mContext, mDb, 4, 4, 4);
67         assertFalse(backupTable.backupOrRestoreAsNeeded());
68         Settings.call(mContext.getContentResolver(), Settings.METHOD_REFRESH_BACKUP_TABLE);
69 
70         assertTrue(tableExists(mDb, BACKUP_TABLE_NAME));
71 
72         // One extra entry for properties
73         assertEquals(BACKUP_ITEM_COUNT + 1, queryNumEntries(mDb, BACKUP_TABLE_NAME));
74     }
75 
76     @Test
backupTableRestored()77     public void backupTableRestored() {
78         assertFalse(new GridBackupTable(mContext, mDb, 4, 4, 4).backupOrRestoreAsNeeded());
79         Settings.call(mContext.getContentResolver(), Settings.METHOD_REFRESH_BACKUP_TABLE);
80 
81         // Delete entries
82         mDb.delete(TABLE_NAME, null, null);
83         assertEquals(0, queryNumEntries(mDb, TABLE_NAME));
84 
85         GridBackupTable backupTable = new GridBackupTable(mContext, mDb, 3, 3, 3);
86         assertTrue(backupTable.backupOrRestoreAsNeeded());
87 
88         // Items have been restored
89         assertEquals(BACKUP_ITEM_COUNT, queryNumEntries(mDb, TABLE_NAME));
90 
91         Point outSize = new Point();
92         assertEquals(4, backupTable.getRestoreHotseatAndGridSize(outSize));
93         assertEquals(4, outSize.x);
94         assertEquals(4, outSize.y);
95     }
96 
97     @Test
backupTableRemovedOnAdd()98     public void backupTableRemovedOnAdd() {
99         assertFalse(new GridBackupTable(mContext, mDb, 4, 4, 4).backupOrRestoreAsNeeded());
100         Settings.call(mContext.getContentResolver(), Settings.METHOD_REFRESH_BACKUP_TABLE);
101 
102         assertTrue(tableExists(mDb, BACKUP_TABLE_NAME));
103 
104         mModelHelper.addItem(1, 2, DESKTOP, 1, 1);
105         assertFalse(tableExists(mDb, BACKUP_TABLE_NAME));
106     }
107 
108     @Test
backupTableRemovedOnDelete()109     public void backupTableRemovedOnDelete() {
110         assertFalse(new GridBackupTable(mContext, mDb, 4, 4, 4).backupOrRestoreAsNeeded());
111         Settings.call(mContext.getContentResolver(), Settings.METHOD_REFRESH_BACKUP_TABLE);
112 
113         assertTrue(tableExists(mDb, BACKUP_TABLE_NAME));
114 
115         mContext.getContentResolver().delete(Favorites.CONTENT_URI, null, null);
116         assertFalse(tableExists(mDb, BACKUP_TABLE_NAME));
117     }
118 
119     @Test
backupTableRetainedOnUpdate()120     public void backupTableRetainedOnUpdate() {
121         assertFalse(new GridBackupTable(mContext, mDb, 4, 4, 4).backupOrRestoreAsNeeded());
122         Settings.call(mContext.getContentResolver(), Settings.METHOD_REFRESH_BACKUP_TABLE);
123 
124         assertTrue(tableExists(mDb, BACKUP_TABLE_NAME));
125 
126         ContentValues values = new ContentValues();
127         values.put(Favorites.RANK, 4);
128         // Something was updated
129         assertTrue(mContext.getContentResolver()
130                 .update(Favorites.CONTENT_URI, values, null, null) > 0);
131 
132         // Backup table remains
133         assertTrue(tableExists(mDb, BACKUP_TABLE_NAME));
134     }
135 }
136