-
-
Notifications
You must be signed in to change notification settings - Fork 225
Improve ShortcuHelper.updateShortcuts to take all actions into account #2919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TheLastProject
merged 4 commits into
CatimaLoyalty:main
from
mpaulmier:improve-update-shortcuts
Jan 9, 2026
+163
−83
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7629fea
Improve ShortcuHelper.updateShortcuts to take all actions into account
mpaulmier ec657cc
Remove now useless calls to removeShortcut
mpaulmier 9f1608d
Add doc to explain the usage of maxShortcut
mpaulmier 427b538
Fix typo in doc of maxShortcuts
mpaulmier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
app/src/test/java/protect/card_locker/ShortcutHelperTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| package protect.card_locker; | ||
|
|
||
| import static org.junit.Assert.assertArrayEquals; | ||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| import android.app.Activity; | ||
| import android.content.Context; | ||
| import android.database.sqlite.SQLiteDatabase; | ||
| import android.graphics.Color; | ||
|
|
||
| import androidx.core.content.pm.ShortcutInfoCompat; | ||
| import androidx.core.content.pm.ShortcutManagerCompat; | ||
|
|
||
| import com.google.zxing.BarcodeFormat; | ||
|
|
||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.robolectric.Robolectric; | ||
| import org.robolectric.RobolectricTestRunner; | ||
| import org.robolectric.android.controller.ActivityController; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.Comparator; | ||
|
|
||
| @RunWith(RobolectricTestRunner.class) | ||
| public class ShortcutHelperTest { | ||
| private Activity mActivity; | ||
| private SQLiteDatabase mDatabase; | ||
| private int id1; | ||
| private int id2; | ||
| private int id3; | ||
| private int id4; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| mActivity = Robolectric.setupActivity(MainActivity.class); | ||
| mDatabase = TestHelpers.getEmptyDb(mActivity).getWritableDatabase(); | ||
|
|
||
| long now = System.currentTimeMillis(); | ||
| id1 = (int) DBHelper.insertLoyaltyCard(mDatabase, "store1", "note1", null, null, new BigDecimal("0"), null, "cardId1", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), null, Color.BLACK, 0, now,0); | ||
| id2 = (int) DBHelper.insertLoyaltyCard(mDatabase, "store2", "note2", null, null, new BigDecimal("0"), null, "cardId2", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), null, Color.BLACK, 0, now + 10,0); | ||
| id3 = (int) DBHelper.insertLoyaltyCard(mDatabase, "store3", "note3", null, null, new BigDecimal("0"), null, "cardId3", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), null, Color.BLACK, 0, now + 20,0); | ||
| id4 = (int) DBHelper.insertLoyaltyCard(mDatabase, "store4", "note4", null, null, new BigDecimal("0"), null, "cardId4", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), null, Color.BLACK, 0, now + 30,0); | ||
|
|
||
| ShortcutHelper.maxShortcuts = 3; | ||
| } | ||
|
|
||
| private Integer[] getShortcutIds(Context context) { | ||
| return ShortcutManagerCompat.getDynamicShortcuts(context) | ||
| .stream() | ||
| .sorted(Comparator.comparingInt(ShortcutInfoCompat::getRank)) | ||
| .map(shortcut -> Integer.parseInt(shortcut.getId())) | ||
| .toArray(Integer[]::new); | ||
| } | ||
|
|
||
| @Test | ||
| public void onArchiveUnarchive() { | ||
| ActivityController activityController = Robolectric.buildActivity(MainActivity.class).create(); | ||
|
|
||
| Activity mainActivity = (Activity) activityController.get(); | ||
|
|
||
| activityController.pause(); | ||
| activityController.resume(); | ||
|
|
||
| assertEquals(3, ShortcutManagerCompat.getDynamicShortcuts(mainActivity).stream().count()); | ||
|
|
||
| Integer[] ids = getShortcutIds(mainActivity); | ||
|
|
||
| assertArrayEquals(new Integer[] {id4, id3, id2}, ids); | ||
|
|
||
| DBHelper.updateLoyaltyCardArchiveStatus(mDatabase, id4, 1); | ||
|
|
||
| activityController.pause(); | ||
| activityController.resume(); | ||
|
|
||
| Integer[] idsAfterArchive = getShortcutIds(mainActivity); | ||
|
|
||
| assertArrayEquals(new Integer[] {id3, id2, id1}, idsAfterArchive); | ||
|
|
||
| DBHelper.updateLoyaltyCardArchiveStatus(mDatabase, id4, 0); | ||
|
|
||
| activityController.pause(); | ||
| activityController.resume(); | ||
|
|
||
| Integer[] idsAfterUnarchive = getShortcutIds(mainActivity); | ||
|
|
||
| assertArrayEquals(new Integer[] {id4, id3, id2}, idsAfterUnarchive); | ||
| } | ||
|
|
||
| @Test | ||
| public void onAddRemoveFavorite() { | ||
| ActivityController activityController = Robolectric.buildActivity(MainActivity.class).create(); | ||
|
|
||
| Activity mainActivity = (Activity) activityController.get(); | ||
|
|
||
| activityController.pause(); | ||
| activityController.resume(); | ||
|
|
||
| assertEquals(3, ShortcutManagerCompat.getDynamicShortcuts(mainActivity).stream().count()); | ||
|
|
||
| Integer[] ids = getShortcutIds(mainActivity); | ||
|
|
||
| assertArrayEquals(new Integer[] {id4, id3, id2}, ids); | ||
|
|
||
| DBHelper.updateLoyaltyCardStarStatus(mDatabase, id1, 1); | ||
|
|
||
| activityController.pause(); | ||
| activityController.resume(); | ||
|
|
||
| Integer[] idsAfterFav = getShortcutIds(mainActivity); | ||
|
|
||
| assertArrayEquals(new Integer[] {id1, id4, id3}, idsAfterFav); | ||
|
|
||
| DBHelper.updateLoyaltyCardStarStatus(mDatabase, id1, 0); | ||
|
|
||
| activityController.pause(); | ||
| activityController.resume(); | ||
|
|
||
| Integer[] idsAfterUnfav = getShortcutIds(mainActivity); | ||
|
|
||
| assertArrayEquals(new Integer[] {id4, id3, id2}, idsAfterUnfav); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.