1
0

ThemingAppContext.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. *
  4. * @copyright Copyright (c) 2017, Daniel Calviño Sánchez (danxuliu@gmail.com)
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. use Behat\Behat\Context\Context;
  23. class ThemingAppContext implements Context, ActorAwareInterface {
  24. use ActorAware;
  25. /**
  26. * @return Locator
  27. */
  28. public static function inputFieldFor($parameterName) {
  29. return Locator::forThe()->css("input")->
  30. descendantOf(self::parameterDivFor($parameterName))->
  31. describedAs("Input field for $parameterName parameter in Theming app");
  32. }
  33. /**
  34. * @return Locator
  35. */
  36. public static function resetButtonFor($parameterName) {
  37. return Locator::forThe()->css(".theme-undo")->
  38. descendantOf(self::parameterDivFor($parameterName))->
  39. describedAs("Reset button for $parameterName parameter in Theming app");
  40. }
  41. /**
  42. * @return Locator
  43. */
  44. private static function parameterDivFor($parameterName) {
  45. return Locator::forThe()->xpath("//*[@id='theming']//label//*[normalize-space() = '$parameterName']/ancestor::div[1]")->
  46. describedAs("Div for $parameterName parameter in Theming app");
  47. }
  48. /**
  49. * @return Locator
  50. */
  51. public static function statusMessage() {
  52. return Locator::forThe()->id("theming_settings_msg")->
  53. describedAs("Status message in Theming app");
  54. }
  55. /**
  56. * @When I set the :parameterName parameter in the Theming app to :parameterValue
  57. */
  58. public function iSetTheParameterInTheThemingAppTo($parameterName, $parameterValue) {
  59. $this->actor->find(self::inputFieldFor($parameterName), 10)->setValue($parameterValue . "\r");
  60. }
  61. /**
  62. * @When I reset the :parameterName parameter in the Theming app to its default value
  63. */
  64. public function iSetTheParameterInTheThemingAppToItsDefaultValue($parameterName) {
  65. // The reset button is not shown when the cursor is outside the input
  66. // field, so ensure that the cursor is on the input field by clicking on
  67. // it.
  68. $this->actor->find(self::inputFieldFor($parameterName), 10)->click();
  69. $this->actor->find(self::resetButtonFor($parameterName), 10)->click();
  70. }
  71. /**
  72. * @Then I see that the color selector in the Theming app has loaded
  73. */
  74. public function iSeeThatTheColorSelectorInTheThemingAppHasLoaded() {
  75. // Checking if the color selector has loaded by getting the background color
  76. // of the input element. If the value present in the element matches the
  77. // background of the input element, it means the color element has been
  78. // initialized.
  79. PHPUnit_Framework_Assert::assertTrue($this->actor->find(self::inputFieldFor("Color"), 10)->isVisible());
  80. $actor = $this->actor;
  81. $colorSelectorLoadedCallback = function() use($actor) {
  82. $colorSelectorValue = $this->getRGBArray($actor->getSession()->evaluateScript("return $('#theming-color')[0].value;"));
  83. $inputBgColor = $this->getRGBArray($actor->getSession()->evaluateScript("return $('#theming-color').css('background-color');"));
  84. if ($colorSelectorValue == $inputBgColor) {
  85. return true;
  86. }
  87. return false;
  88. };
  89. if (!Utils::waitFor($colorSelectorLoadedCallback, $timeout = 10 * $this->actor->getFindTimeoutMultiplier(), $timeoutStep = 1)) {
  90. PHPUnit_Framework_Assert::fail("The color selector in Theming app has not been loaded after $timeout seconds");
  91. }
  92. }
  93. private function getRGBArray ($color) {
  94. if (preg_match("/rgb\(\s*(\d+),\s*(\d+),\s*(\d+)\)/", $color, $matches)) {
  95. // Already an RGB (R, G, B) color
  96. // Convert from "rgb(R, G, B)" string to RGB array
  97. $tmpColor = array_splice($matches, 1);
  98. } else if ($color[0] === '#') {
  99. $color = substr($color, 1);
  100. // HEX Color, convert to RGB array.
  101. $tmpColor = sscanf($color, "%02X%02X%02X");
  102. } else {
  103. PHPUnit_Framework_Assert::fail("The acceptance test does not know how to handle the color string : '$color'. "
  104. . "Please provide # before HEX colors in your features.");
  105. }
  106. return $tmpColor;
  107. }
  108. /**
  109. * @Then I see that the header color is :color
  110. */
  111. public function iSeeThatTheHeaderColorIs($color) {
  112. $headerColor = $this->actor->getSession()->evaluateScript("return $('#header').css('background-color');");
  113. $headerColor = $this->getRGBArray($headerColor);
  114. $color = $this->getRGBArray($color);
  115. PHPUnit_Framework_Assert::assertEquals($color, $headerColor);
  116. }
  117. /**
  118. * @Then I see that the parameters in the Theming app are eventually saved
  119. */
  120. public function iSeeThatTheParametersInTheThemingAppAreEventuallySaved() {
  121. PHPUnit_Framework_Assert::assertTrue($this->actor->find(self::statusMessage(), 10)->isVisible());
  122. $actor = $this->actor;
  123. $savedStatusMessageShownCallback = function() use($actor) {
  124. if ($actor->find(self::statusMessage())->getText() !== "Saved") {
  125. return false;
  126. }
  127. return true;
  128. };
  129. if (!Utils::waitFor($savedStatusMessageShownCallback, $timeout = 10 * $this->actor->getFindTimeoutMultiplier(), $timeoutStep = 1)) {
  130. PHPUnit_Framework_Assert::fail("The 'Saved' status messages in Theming app has not been shown after $timeout seconds");
  131. }
  132. }
  133. }