View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2017 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.qa.glnm.selectors;
21  
22  import com.galenframework.specs.page.CorrectionsRect;
23  import com.galenframework.specs.page.CorrectionsRect.Correction;
24  import com.galenframework.specs.page.Locator;
25  
26  /**
27   * Convenience wrapper to add corrections without modifying original locator.
28   *
29   * @since 1.0.0
30   */
31  public class LocatorCorrectionsWrapper extends Locator {
32  
33    private CorrectionsRect additionalCorrections;
34  
35    /**
36     * <p>Constructor for LocatorCorrectionsWrapper.</p>
37     *
38     * @param locator locator to delegate everything except additionalCorrections to
39     * @param corrections additional corrections to use on this locator
40     */
41    public LocatorCorrectionsWrapper(Locator locator, CorrectionsRect corrections) {
42      super(locator.getLocatorType(), locator.getLocatorValue(), locator.getIndex());
43      setParent(locator.getParent());
44      setAdditionalCorrections(corrections);
45    }
46  
47    /**
48     * <p>Getter for the field <code>additionalCorrections</code>.</p>
49     *
50     * @return a {@link com.galenframework.specs.page.CorrectionsRect} object.
51     */
52    public CorrectionsRect getAdditionalCorrections() {
53      return additionalCorrections;
54    }
55  
56    /** {@inheritDoc} */
57    @Override
58    public CorrectionsRect getCorrections() {
59      return combinedCorrections(super.getCorrections(), getAdditionalCorrections());
60    }
61  
62    /**
63     * <p>Setter for the field <code>additionalCorrections</code>.</p>
64     *
65     * @param additionalCorrections a {@link com.galenframework.specs.page.CorrectionsRect} object.
66     */
67    public void setAdditionalCorrections(CorrectionsRect additionalCorrections) {
68      this.additionalCorrections = additionalCorrections;
69    }
70  
71    private Correction combinedCorrection(Correction c1, Correction c2) {
72      return new CombinedCorrection(c1, c2);
73    }
74  
75    private CorrectionsRect combinedCorrections(CorrectionsRect cr1, CorrectionsRect cr2) {
76      if (cr1 == null) {
77        return cr2;
78      }
79      if (cr2 == null) {
80        return cr1;
81      }
82      Correction left = combinedCorrection(cr1.getLeft(), cr2.getLeft());
83      Correction top = combinedCorrection(cr1.getTop(), cr2.getTop());
84      Correction width = combinedCorrection(cr1.getWidth(), cr2.getWidth());
85      Correction height = combinedCorrection(cr1.getHeight(), cr2.getHeight());
86      return new CorrectionsRect(left, top, width, height);
87    }
88  
89    /**
90     * Combines two corrections into one.
91     */
92    private class CombinedCorrection extends Correction {
93  
94      private Correction additionalCorrection;
95  
96      /**
97       * @param original applied first
98       * @param additional applied to result of first
99       */
100     CombinedCorrection(Correction original, Correction additional) {
101       super(original.getValue(), original.getType());
102       additionalCorrection = additional;
103     }
104 
105     @Override
106     public int correct(int oldValue) {
107       int correctedValue = super.correct(oldValue);
108       return additionalCorrection.correct(correctedValue);
109     }
110   }
111 }