PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/atlassian-plugins-webresource/src/test/java/com/atlassian/plugin/webresource/assembler/TestWebResourceWithInlinePhase.java

https://bitbucket.org/atlassian/atlassian-plugins-webresource
Java | 224 lines | 186 code | 28 blank | 10 comment | 0 complexity | 2552ad0eb145f211163b66063f8fc5bd MD5 | raw file
  1. package com.atlassian.plugin.webresource.assembler;
  2. import com.atlassian.webresource.api.UrlMode;
  3. import com.atlassian.webresource.api.assembler.WebResourceAssembler;
  4. import org.junit.Before;
  5. import org.junit.Ignore;
  6. import org.junit.Test;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.StringWriter;
  10. import static com.atlassian.plugin.webresource.TestUtils.removeWebResourceLogs;
  11. import static com.atlassian.webresource.api.assembler.resource.ResourcePhase.DEFER;
  12. import static com.atlassian.webresource.api.assembler.resource.ResourcePhase.INLINE;
  13. import static com.atlassian.webresource.api.assembler.resource.ResourcePhase.INTERACTION;
  14. import static com.atlassian.webresource.api.assembler.resource.ResourcePhase.REQUIRE;
  15. import static org.apache.commons.io.IOUtils.copy;
  16. import static org.hamcrest.MatcherAssert.assertThat;
  17. import static org.hamcrest.Matchers.containsString;
  18. import static org.hamcrest.Matchers.equalTo;
  19. import static org.hamcrest.Matchers.startsWith;
  20. import static org.junit.Assert.fail;
  21. public class TestWebResourceWithInlinePhase {
  22. private AssemblerTestFixture f;
  23. private StringWriter writer;
  24. @Before
  25. public void setUp() {
  26. f = new AssemblerTestFixture();
  27. f.mockPlugin("test.atlassian:first-feature")
  28. .res("first-1.js")
  29. .res("first-2.js")
  30. .res("first-1.css")
  31. .res("first-2.css")
  32. .ctx("first", "all");
  33. f.mockPlugin("test.atlassian:second-feature")
  34. .res("second-1.css")
  35. .res("second-1.js")
  36. .res("second-2.css")
  37. .res("second-2.js")
  38. .ctx("second", "all");
  39. f.mockPlugin("test.atlassian:third-feature")
  40. .res("third.css")
  41. .res("third.js")
  42. .deps("test.atlassian:first-feature", "test.atlassian:second-feature")
  43. .ctx("third", "all");
  44. f.mockContent("first-1.css", ".first-1{color:red}");
  45. f.mockContent("first-2.css", ".first-2{color:#f00}");
  46. f.mockContent("second-1.css", ".second-1{color:green}");
  47. f.mockContent("second-2.css", ".second-2{color:#0f0}");
  48. f.mockContent("third.css", ".third{color:blue}");
  49. f.mockContent("first-1.js", "console.log('first-1');");
  50. f.mockContent("first-2.js", "console.log('first-2');");
  51. f.mockContent("second-1.js", "console.log('second-1');");
  52. f.mockContent("second-2.js", "console.log('second-2');");
  53. f.mockContent("third.js", "console.log('third');");
  54. writer = new StringWriter();
  55. }
  56. @Test
  57. public void testLoadAllFeaturesInlineIndividually() {
  58. WebResourceAssembler assembler = f.create();
  59. assembler.resources().requireWebResource(INLINE, "test.atlassian:first-feature");
  60. assembler.resources().requireWebResource(INLINE, "test.atlassian:second-feature");
  61. assembler.assembled().drainIncludedResources().writeHtmlTags(writer, UrlMode.AUTO);
  62. final String actual = removeWebResourceLogs(writer.toString());
  63. final String expectedStyles = "<style>" +
  64. ".first-1{color:red}\n" +
  65. ".first-2{color:#f00}\n" +
  66. ".second-1{color:green}\n" +
  67. ".second-2{color:#0f0}\n" +
  68. "</style>";
  69. final String expectedScripts = "<script>" +
  70. "console.log('first-1');\n" +
  71. "console.log('first-2');\n" +
  72. "console.log('second-1');\n" +
  73. "console.log('second-2');\n" +
  74. "</script>";
  75. // todo PLUGWEB-545 add CSS support for inline phase
  76. final String expected = expectedScripts;
  77. assertThat(actual, equalTo(expected));
  78. }
  79. @Test
  80. public void testLoadAllFeaturesInlineViaContext() {
  81. WebResourceAssembler assembler = f.create();
  82. assembler.resources().requireContext(INLINE, "all");
  83. assembler.assembled().drainIncludedResources().writeHtmlTags(writer, UrlMode.AUTO);
  84. final String actual = removeWebResourceLogs(writer.toString());
  85. final String expectedStyles = "<style>" +
  86. ".first-1{color:red}\n" +
  87. ".first-2{color:#f00}\n" +
  88. ".second-1{color:green}\n" +
  89. ".second-2{color:#0f0}\n" +
  90. ".third{color:blue}\n" +
  91. "</style>";
  92. final String expectedScripts = "<script>" +
  93. "console.log('first-1');\n" +
  94. "console.log('first-2');\n" +
  95. "console.log('second-1');\n" +
  96. "console.log('second-2');\n" +
  97. "console.log('third');\n" +
  98. "</script>";
  99. // todo PLUGWEB-545 add CSS support for inline phase
  100. final String expected = expectedScripts;
  101. assertThat(actual, equalTo(expected));
  102. }
  103. @Test
  104. public void testRequireNotOutputIfRequestedAsInline() {
  105. WebResourceAssembler assembler = f.create();
  106. assembler.resources().requireContext(REQUIRE, "first");
  107. assembler.resources().requireWebResource(INLINE, "test.atlassian:first-feature");
  108. assembler.assembled().drainIncludedResources().writeHtmlTags(writer, UrlMode.AUTO);
  109. final String actual = removeWebResourceLogs(writer.toString());
  110. String expectedStyles = "<style>" +
  111. ".first-1{color:red}\n" +
  112. ".first-2{color:#f00}\n" +
  113. "</style>";
  114. final String expectedScripts = "<script>" +
  115. "console.log('first-1');\n" +
  116. "console.log('first-2');\n" +
  117. "</script>";
  118. // todo PLUGWEB-545 add CSS support for inline phase
  119. final String expected = expectedScripts;
  120. assertThat(actual, equalTo(expected));
  121. }
  122. @Test
  123. public void testInteractiveNotOutputIfRequestedAsInline() {
  124. WebResourceAssembler assembler = f.create();
  125. assembler.resources().requireContext(INTERACTION, "first");
  126. assembler.resources().requireWebResource(INLINE, "test.atlassian:first-feature");
  127. assembler.assembled().drainIncludedResources().writeHtmlTags(writer, UrlMode.AUTO);
  128. final String actual = removeWebResourceLogs(writer.toString());
  129. final String expectedStyles = "<style>" +
  130. ".first-1{color:red}\n" +
  131. ".first-2{color:#f00}\n" +
  132. "</style>";
  133. final String expectedScripts = "<script>" +
  134. "console.log('first-1');\n" +
  135. "console.log('first-2');\n" +
  136. "</script>";
  137. // todo PLUGWEB-545 add CSS support for inline phase
  138. final String expected = expectedScripts;
  139. assertThat(actual, equalTo(expected));
  140. }
  141. @Test
  142. @Ignore("PLUGWEB-642 - Need to fix interaction phase renderer first")
  143. public void testRequirePartiallyOutputIfSubsetRequestedAsInline() {
  144. WebResourceAssembler assembler = f.create();
  145. assembler.resources().requireContext(REQUIRE, "all");
  146. assembler.resources().requireWebResource(INLINE, "test.atlassian:first-feature");
  147. assembler.assembled().drainIncludedResources().writeHtmlTags(writer, UrlMode.AUTO);
  148. final String actual = removeWebResourceLogs(writer.toString());
  149. final String expectedStyles = "<style>" +
  150. ".first-1{color:red}\n" +
  151. ".first-2{color:#f00}\n" +
  152. "</style>";
  153. final String expectedScripts = "<script>" +
  154. "console.log('first-1');\n" +
  155. "console.log('first-2');\n" +
  156. "</script>";
  157. // todo PLUGWEB-545 add CSS support for inline phase
  158. final String expectedInline = expectedScripts;
  159. assertThat(actual, startsWith(expectedInline));
  160. final String expectedBatchTags = "<link rel=\"stylesheet\"" +
  161. " href=\"/download/contextbatch/css/all,-test.atlassian:first-feature/batch.css\"" +
  162. " data-wrm-key=\"all,-test.atlassian:first-feature\"" +
  163. " data-wrm-batch-type=\"context\"" +
  164. " media=\"all\">\n" +
  165. "<script src=\"/download/contextbatch/js/all,-test.atlassian:first-feature/batch.js\"" +
  166. " data-wrm-key=\"all,-test.atlassian:first-feature\"" +
  167. " data-wrm-batch-type=\"context\"" +
  168. " data-initially-rendered></script>";
  169. assertThat(actual, containsString(expectedBatchTags));
  170. }
  171. @Test
  172. @Ignore("PLUGWEB-642 - Need to fix interaction phase renderer first")
  173. public void testInteractivePartiallyOutputIfSubsetRequestedAsInline() {
  174. WebResourceAssembler assembler = f.create();
  175. assembler.resources().requireContext(INTERACTION, "all");
  176. assembler.resources().requireWebResource(INLINE, "test.atlassian:first-feature");
  177. assembler.assembled().drainIncludedResources().writeHtmlTags(writer, UrlMode.AUTO);
  178. final String actual = removeWebResourceLogs(writer.toString());
  179. final String expectedStyles = "<style>" +
  180. ".first-1{color:red}\n" +
  181. ".first-2{color:#f00}\n" +
  182. "</style>";
  183. final String expectedScripts = "<script>" +
  184. "console.log('first-1');\n" +
  185. "console.log('first-2');\n" +
  186. "</script>";
  187. // todo PLUGWEB-545 add CSS support for inline phase
  188. final String expectedInline = expectedScripts;
  189. // @note: this might change if batching is ever improved...
  190. // it could also be a single request for `all,-test.atlassian:first-feature`
  191. final String expectedInteraction = "<script defer>WRM.requireLazily([\"wr!test.atlassian:second-feature\",\"wr!test.atlassian:third-feature\"])</script>";
  192. assertThat(actual, equalTo(expectedInline + "\n" + expectedInteraction));
  193. }
  194. // @todo: test permutations of resources within features - e.g., no CSS, no JS - to ensure lazy require happens even if one media type isn't there
  195. // todo: interaction with sync batch, full and partial set overlaps
  196. }