|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * <p>This source code is licensed under the MIT license found in the LICENSE file in the root |
| 5 | + * directory of this source tree. |
| 6 | + */ |
| 7 | +package com.facebook.react.devsupport; |
| 8 | + |
| 9 | +import static org.fest.assertions.api.Assertions.assertThat; |
| 10 | + |
| 11 | +import com.facebook.react.common.StandardCharsets; |
| 12 | +import com.facebook.react.devsupport.BundleDeltaClient; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.runner.RunWith; |
| 16 | +import org.robolectric.RobolectricTestRunner; |
| 17 | +import okio.BufferedSource; |
| 18 | +import org.junit.Rule; |
| 19 | +import org.junit.rules.TemporaryFolder; |
| 20 | +import okio.Okio; |
| 21 | +import java.io.ByteArrayInputStream; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.io.File; |
| 24 | +import java.io.IOException; |
| 25 | + |
| 26 | +@RunWith(RobolectricTestRunner.class) |
| 27 | +public class BundleDeltaClientTest { |
| 28 | + private BundleDeltaClient mClient; |
| 29 | + |
| 30 | + @Rule public TemporaryFolder mFolder = new TemporaryFolder(); |
| 31 | + |
| 32 | + @Before |
| 33 | + public void setUp() { |
| 34 | + mClient = BundleDeltaClient.create(BundleDeltaClient.ClientType.DEV_SUPPORT); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testAcceptsSimpleInitialBundle() throws IOException { |
| 39 | + File file = mFolder.newFile(); |
| 40 | + mClient.processDelta( |
| 41 | + bufferedSource( |
| 42 | + "{" |
| 43 | + + "\"pre\": \"console.log('Hello World!');\"," |
| 44 | + + "\"post\": \"console.log('That is all folks!');\"," |
| 45 | + + "\"modules\": [[0, \"console.log('Best module.');\"]]" |
| 46 | + + "}"), |
| 47 | + file); |
| 48 | + assertThat(contentOf(file)) |
| 49 | + .isEqualTo( |
| 50 | + "console.log('Hello World!');\n" |
| 51 | + + "console.log('Best module.');\n" |
| 52 | + + "console.log('That is all folks!');\n"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testPatchesInitialBundleWithDeltaBundle() throws IOException { |
| 57 | + File file = mFolder.newFile(); |
| 58 | + mClient.processDelta( |
| 59 | + bufferedSource( |
| 60 | + "{" |
| 61 | + + "\"pre\": \"pre\"," |
| 62 | + + "\"post\": \"post\"," |
| 63 | + + "\"modules\": [[0, \"0\"], [1, \"1\"]]" |
| 64 | + + "}"), |
| 65 | + file); |
| 66 | + file = mFolder.newFile(); |
| 67 | + mClient.processDelta( |
| 68 | + bufferedSource( |
| 69 | + "{" |
| 70 | + + "\"added\": [[2, \"2\"]]," |
| 71 | + + "\"modified\": [[0, \"0.1\"]]," |
| 72 | + + "\"deleted\": [1]" |
| 73 | + + "}"), |
| 74 | + file); |
| 75 | + assertThat(contentOf(file)) |
| 76 | + .isEqualTo( |
| 77 | + "pre\n" |
| 78 | + + "0.1\n" |
| 79 | + + "2\n" |
| 80 | + + "post\n"); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testSortsModulesByIdInInitialBundle() throws IOException { |
| 85 | + File file = mFolder.newFile(); |
| 86 | + mClient.processDelta( |
| 87 | + bufferedSource( |
| 88 | + "{" |
| 89 | + + "\"pre\": \"console.log('Hello World!');\"," |
| 90 | + + "\"post\": \"console.log('That is all folks!');\"," |
| 91 | + + "\"modules\": [[3, \"3\"], [0, \"0\"], [2, \"2\"], [1, \"1\"]]" |
| 92 | + + "}"), |
| 93 | + file); |
| 94 | + assertThat(contentOf(file)) |
| 95 | + .isEqualTo( |
| 96 | + "console.log('Hello World!');\n" |
| 97 | + + "0\n" |
| 98 | + + "1\n" |
| 99 | + + "2\n" |
| 100 | + + "3\n" |
| 101 | + + "console.log('That is all folks!');\n"); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testSortsModulesByIdInPatchedBundle() throws IOException { |
| 106 | + File file = mFolder.newFile(); |
| 107 | + mClient.processDelta( |
| 108 | + bufferedSource( |
| 109 | + "{" |
| 110 | + + "\"pre\": \"console.log('Hello World!');\"," |
| 111 | + + "\"post\": \"console.log('That is all folks!');\"," |
| 112 | + + "\"modules\": [[3, \"3\"], [0, \"0\"], [1, \"1\"]]" |
| 113 | + + "}"), |
| 114 | + file); |
| 115 | + file = mFolder.newFile(); |
| 116 | + mClient.processDelta( |
| 117 | + bufferedSource( |
| 118 | + "{" |
| 119 | + + "\"added\": [[2, \"2\"]]," |
| 120 | + + "\"modified\": [[0, \"0.1\"]]," |
| 121 | + + "\"deleted\": [1]" |
| 122 | + + "}"), |
| 123 | + file); |
| 124 | + assertThat(contentOf(file)) |
| 125 | + .isEqualTo( |
| 126 | + "console.log('Hello World!');\n" |
| 127 | + + "0.1\n" |
| 128 | + + "2\n" |
| 129 | + + "3\n" |
| 130 | + + "console.log('That is all folks!');\n"); |
| 131 | + } |
| 132 | + |
| 133 | + private static BufferedSource bufferedSource(String string) { |
| 134 | + return Okio.buffer( |
| 135 | + Okio.source(new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8)))); |
| 136 | + } |
| 137 | + |
| 138 | + private static String contentOf(File file) throws IOException { |
| 139 | + return new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8); |
| 140 | + } |
| 141 | +} |
0 commit comments