|
1 | 1 | package com.d4rk.androidtutorials.java.data.repository; |
2 | 2 |
|
3 | 3 | import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
4 | 5 | import static org.junit.Assert.assertTrue; |
5 | 6 |
|
6 | 7 | import com.d4rk.androidtutorials.java.data.model.PromotedApp; |
|
9 | 10 |
|
10 | 11 | import org.junit.Test; |
11 | 12 |
|
| 13 | +import java.util.Collections; |
12 | 14 | import java.util.List; |
| 15 | +import java.util.concurrent.atomic.AtomicInteger; |
13 | 16 | import java.util.concurrent.atomic.AtomicReference; |
14 | 17 |
|
15 | 18 | public class DefaultHomeRepositoryTest { |
16 | 19 |
|
| 20 | + private static final List<PromotedApp> PROMOTED_APPS = |
| 21 | + List.of(new PromotedApp("Name", "pkg", "icon")); |
| 22 | + |
17 | 23 | @Test |
18 | | - public void repositoryDelegatesToDataSources() { |
19 | | - List<PromotedApp> promoted = List.of(new PromotedApp("Name", "pkg", "icon")); |
20 | | - FakeHomeRemoteDataSource remote = new FakeHomeRemoteDataSource(promoted); |
21 | | - FakeHomeLocalDataSource local = new FakeHomeLocalDataSource(); |
| 24 | + public void getPlayStoreUrlDelegatesToLocalSource() { |
| 25 | + SpyHomeLocalDataSource local = new SpyHomeLocalDataSource(); |
| 26 | + RecordingHomeRemoteDataSource remote = |
| 27 | + new RecordingHomeRemoteDataSource(Collections.emptyList()); |
| 28 | + DefaultHomeRepository repository = new DefaultHomeRepository(remote, local); |
22 | 29 |
|
| 30 | + String result = repository.getPlayStoreUrl(); |
| 31 | + |
| 32 | + assertEquals("play", result); |
| 33 | + assertTrue(local.playStoreUrlCalled); |
| 34 | + assertFalse(local.appPlayStoreUrlCalled); |
| 35 | + assertFalse(local.dailyTipCalled); |
| 36 | + assertFalse(remote.fetchCalled); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void getAppPlayStoreUrlDelegatesToLocalSource() { |
| 41 | + SpyHomeLocalDataSource local = new SpyHomeLocalDataSource(); |
| 42 | + RecordingHomeRemoteDataSource remote = |
| 43 | + new RecordingHomeRemoteDataSource(Collections.emptyList()); |
23 | 44 | DefaultHomeRepository repository = new DefaultHomeRepository(remote, local); |
24 | 45 |
|
25 | | - assertEquals("play", repository.getPlayStoreUrl()); |
26 | | - assertEquals("play/pkg", repository.getAppPlayStoreUrl("pkg")); |
27 | | - assertEquals("tip", repository.dailyTip()); |
| 46 | + String packageName = "pkg"; |
| 47 | + String result = repository.getAppPlayStoreUrl(packageName); |
| 48 | + |
| 49 | + assertEquals("play/pkg", result); |
| 50 | + assertTrue(local.appPlayStoreUrlCalled); |
| 51 | + assertEquals(packageName, local.lastRequestedPackage); |
| 52 | + assertFalse(local.playStoreUrlCalled); |
| 53 | + assertFalse(local.dailyTipCalled); |
| 54 | + assertFalse(remote.fetchCalled); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void dailyTipDelegatesToLocalSource() { |
| 59 | + SpyHomeLocalDataSource local = new SpyHomeLocalDataSource(); |
| 60 | + RecordingHomeRemoteDataSource remote = |
| 61 | + new RecordingHomeRemoteDataSource(Collections.emptyList()); |
| 62 | + DefaultHomeRepository repository = new DefaultHomeRepository(remote, local); |
| 63 | + |
| 64 | + String result = repository.dailyTip(); |
| 65 | + |
| 66 | + assertEquals("tip", result); |
| 67 | + assertTrue(local.dailyTipCalled); |
| 68 | + assertFalse(local.playStoreUrlCalled); |
| 69 | + assertFalse(local.appPlayStoreUrlCalled); |
| 70 | + assertFalse(remote.fetchCalled); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void fetchPromotedAppsDelegatesToRemoteSource() { |
| 75 | + SpyHomeLocalDataSource local = new SpyHomeLocalDataSource(); |
| 76 | + RecordingHomeRemoteDataSource remote = |
| 77 | + new RecordingHomeRemoteDataSource(List.of(PROMOTED_APPS)); |
| 78 | + DefaultHomeRepository repository = new DefaultHomeRepository(remote, local); |
28 | 79 |
|
29 | 80 | AtomicReference<List<PromotedApp>> result = new AtomicReference<>(); |
30 | 81 | repository.fetchPromotedApps(result::set); |
31 | | - assertTrue(remote.called); |
32 | | - assertEquals(promoted, result.get()); |
| 82 | + |
| 83 | + assertTrue(remote.fetchCalled); |
| 84 | + assertEquals(PROMOTED_APPS, result.get()); |
| 85 | + assertFalse(local.playStoreUrlCalled); |
| 86 | + assertFalse(local.appPlayStoreUrlCalled); |
| 87 | + assertFalse(local.dailyTipCalled); |
33 | 88 | } |
34 | 89 |
|
35 | | - private static class FakeHomeLocalDataSource implements HomeLocalDataSource { |
| 90 | + @Test |
| 91 | + public void fetchPromotedAppsEmitsOnlyFirstRemoteResponse() { |
| 92 | + SpyHomeLocalDataSource local = new SpyHomeLocalDataSource(); |
| 93 | + List<PromotedApp> secondResponse = |
| 94 | + List.of(new PromotedApp("Second", "second.pkg", "secondIcon")); |
| 95 | + RecordingHomeRemoteDataSource remote = |
| 96 | + new RecordingHomeRemoteDataSource(List.of(PROMOTED_APPS, secondResponse)); |
| 97 | + DefaultHomeRepository repository = new DefaultHomeRepository(remote, local); |
| 98 | + |
| 99 | + AtomicInteger callbackCount = new AtomicInteger(); |
| 100 | + AtomicReference<List<PromotedApp>> result = new AtomicReference<>(); |
| 101 | + repository.fetchPromotedApps(apps -> { |
| 102 | + callbackCount.incrementAndGet(); |
| 103 | + result.set(apps); |
| 104 | + }); |
| 105 | + |
| 106 | + assertTrue(remote.fetchCalled); |
| 107 | + assertEquals(1, callbackCount.get()); |
| 108 | + assertEquals(PROMOTED_APPS, result.get()); |
| 109 | + } |
| 110 | + |
| 111 | + private static class SpyHomeLocalDataSource implements HomeLocalDataSource { |
| 112 | + boolean playStoreUrlCalled; |
| 113 | + boolean appPlayStoreUrlCalled; |
| 114 | + boolean dailyTipCalled; |
| 115 | + String lastRequestedPackage; |
| 116 | + |
36 | 117 | @Override |
37 | 118 | public String getPlayStoreUrl() { |
| 119 | + playStoreUrlCalled = true; |
38 | 120 | return "play"; |
39 | 121 | } |
40 | 122 |
|
41 | 123 | @Override |
42 | 124 | public String getAppPlayStoreUrl(String packageName) { |
| 125 | + appPlayStoreUrlCalled = true; |
| 126 | + lastRequestedPackage = packageName; |
43 | 127 | return "play/" + packageName; |
44 | 128 | } |
45 | 129 |
|
46 | 130 | @Override |
47 | 131 | public String getDailyTip() { |
| 132 | + dailyTipCalled = true; |
48 | 133 | return "tip"; |
49 | 134 | } |
50 | 135 | } |
51 | 136 |
|
52 | | - private static class FakeHomeRemoteDataSource implements HomeRemoteDataSource { |
53 | | - private final List<PromotedApp> apps; |
54 | | - boolean called = false; |
| 137 | + private static class RecordingHomeRemoteDataSource implements HomeRemoteDataSource { |
| 138 | + private final List<List<PromotedApp>> responses; |
| 139 | + boolean fetchCalled; |
55 | 140 |
|
56 | | - FakeHomeRemoteDataSource(List<PromotedApp> apps) { |
57 | | - this.apps = apps; |
| 141 | + RecordingHomeRemoteDataSource(List<List<PromotedApp>> responses) { |
| 142 | + this.responses = responses; |
58 | 143 | } |
59 | 144 |
|
60 | 145 | @Override |
61 | 146 | public void fetchPromotedApps(PromotedAppsCallback callback) { |
62 | | - called = true; |
63 | | - callback.onResult(apps); |
| 147 | + fetchCalled = true; |
| 148 | + for (List<PromotedApp> response : responses) { |
| 149 | + callback.onResult(response); |
| 150 | + } |
64 | 151 | } |
65 | 152 | } |
66 | 153 | } |
0 commit comments