Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Offset_to_text_point function and add unit tests #19946

Merged
merged 1 commit into from Feb 6, 2018

Conversation

paavininanda
Copy link
Contributor

@paavininanda paavininanda commented Feb 4, 2018


  • Added unit tests and web platform tests

This change is Reviewable

@highfive
Copy link

highfive commented Feb 4, 2018

Thanks for the pull request, and welcome! The Servo team is excited to review your changes, and you should hear from @emilio (or someone else) soon.

@highfive
Copy link

highfive commented Feb 4, 2018

Heads up! This PR modifies the following files:

@highfive
Copy link

highfive commented Feb 4, 2018

warning Warning warning

  • These commits modify script code, but no tests are modified. Please consider adding a test!

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Feb 4, 2018
@jdm
Copy link
Member

jdm commented Feb 4, 2018

If you change the testcase in #18613 to use any other value than 1, does the assertion still fail?

Copy link
Member

@emilio emilio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try to take a closer look, but also maybe @mbrubeck and @jonleighton can take a look.

@@ -276,7 +276,7 @@ impl<T: ClipboardProvider> TextInput<T> {
}

debug_assert!(self.edit_point.line < self.lines.len());
debug_assert!(self.edit_point.index <= self.lines[self.edit_point.line].len());
debug_assert!(self.edit_point.index <= max(1, self.lines[self.edit_point.line].len()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to take a look at what's going on on the test-case exactly, but I suspect this is wrong.

In particular, this should be self.lines[self.edit_point.line].len() + 1 instead, but in any case, I still suspect it's the wrong solution, and the edit_point should end up being 1, 0 instead of 0, 1 in this case, since we have two lines in the test-case.

@@ -276,7 +276,7 @@ impl<T: ClipboardProvider> TextInput<T> {
}

debug_assert!(self.edit_point.line < self.lines.len());
debug_assert!(self.edit_point.index <= self.lines[self.edit_point.line].len());
debug_assert!(self.edit_point.index <= max(1, self.lines[self.edit_point.line].len()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to take a look at what's going on on the test-case exactly, but I suspect this is wrong.

In particular, this should be self.lines[self.edit_point.line].len() + 1 instead, but in any case, I still suspect it's the wrong solution, and the edit_point should end up being 1, 0 instead of 0, 1 in this case, since we have two lines in the test-case.

@emilio
Copy link
Member

emilio commented Feb 4, 2018

So the issue is that with two empty lines (["", ""]), offset_to_text_point(1) is returning (0, 1) instead of (1, 0).

@paavininanda
Copy link
Contributor Author

@jdm Yes it fails then also.

@paavininanda
Copy link
Contributor Author

@emilio So should we alter the offset_to_text_point function instead?

@emilio
Copy link
Member

emilio commented Feb 4, 2018

Yes, I think so...

@emilio
Copy link
Member

emilio commented Feb 4, 2018

In particular I think that what offset_to_text_point wants to do is something like this:

        for line_content in self.lines.iter() {
            // If the index fits on the line, we're done.
            if index <= line_content.len() {
                break;
            }

            // else advance a line.
            index -= line_content.len();
            line += 1;
            if index > 0 {
                index -= 1; // Account for the \n
            }
        }

But I'm not sure that's 100% right, it may need to go through tests and all that :)

@jonleighton
Copy link
Contributor

It would be great to add additional unit test(s) to cover this case I think. (The relevant file is tests/unit/script/textinput.rs.)

@emilio: I'm happy to try to review this if you like.

@jdm
Copy link
Member

jdm commented Feb 4, 2018

Note: unit tests can be run with ./mach test-unit -p script

@paavininanda paavininanda changed the title Changed an assertion for the case when line has value "\n" Change Offset_to_text_point function and add unit tests Feb 4, 2018
@emilio
Copy link
Member

emilio commented Feb 4, 2018

@bors-servo try

@bors-servo
Copy link
Contributor

⌛ Trying commit 5a6e63e with merge 13e2000...

bors-servo pushed a commit that referenced this pull request Feb 4, 2018
Change Offset_to_text_point function and add unit tests

<!-- Please describe your changes on the following line: -->

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #(#18613).

<!-- Either: -->
- [x] These changes do not require tests because they solve an issue related to a already existing code

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19946)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

@paavininanda
Copy link
Contributor Author

r? @emilio

Copy link
Member

@emilio emilio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

if abs_point > new_acc && index > line_end {
let line_end = val.len();
let new_acc = acc + line_end + 1;
if abs_point >= new_acc && index > line_end {
index -= line_end + 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think this function is way harder to understand than what it should, but I'll file a followup after this lands to do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah! Actually changing the function altogether was creating some new issues. So i preferred changing it this way only.

@@ -600,6 +600,13 @@ fn test_textinput_set_selection_with_direction() {
assert!(textinput.selection_origin.is_some());
assert_eq!(textinput.selection_origin.unwrap().line, 0);
assert_eq!(textinput.selection_origin.unwrap().index, 6);

textinput = text_input(Lines::Multiple, "\n\n");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's test also with only \n, which is the case we're concerned about. Also, let's add a crashtest for this based on @mateon1's test-case, into tests/wpt/web-platform-tests/selection/. See https://github.com/servo/servo/blob/master/tests/wpt/README.md#writing-new-tests.

The test should call setSelectionRange, but also that the selectionStart and selectionEnd values agree with other browsers, that is, something like:

<!doctype html>
<textarea>

</textarea>
<script>
test(function() {
    let textarea = document.querySelector('textarea');
    assert_equals(textarea.selectionStart, 0);
    assert_equals(textarea.selectionEnd, 0);
    textarea.setSelectionRange(0, 1);
    assert_equals(textarea.selectionStart, 0);
    assert_equals(textarea.selectionEnd, 1);
}, "setSelectionRange on line boundaries");
</script>

@servo-wpt-sync
Copy link
Collaborator

Opened new PR for upstreamable changes.

Completed upstream sync of web-platform-test changes at jdm/web-platform-tests#23.

Copy link
Member

@emilio emilio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the unit test you wrote seems to be failing on travis:

	thread 'textinput::test_textinput_set_selection_with_direction' panicked at 'assertion failed: `(left == right)`
  left: `1`,
 right: `0`', tests/unit/script/textinput.rs:616:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::print
             at libstd/sys_common/backtrace.rs:68
             at libstd/sys_common/backtrace.rs:57
   2: std::panicking::default_hook::{{closure}}
             at libstd/panicking.rs:380
   3: std::panicking::default_hook
             at libstd/panicking.rs:390
   4: std::panicking::rust_panic_with_hook
             at libstd/panicking.rs:576
   5: std::panicking::begin_panic
             at libstd/panicking.rs:537
   6: std::panicking::begin_panic_fmt
             at libstd/panicking.rs:521
   7: script_tests::textinput::test_textinput_set_selection_with_direction
             at tests/unit/script/textinput.rs:616
   8: <F as alloc::boxed::FnBox<A>>::call_box
             at libtest/lib.rs:1441
             at /checkout/src/libcore/ops/function.rs:223
             at /checkout/src/liballoc/boxed.rs:788
   9: __rust_maybe_catch_panic
             at libpanic_unwind/lib.rs:102

Additionally, you need to make sure the manifest is up-to-date running ./mach update-manifest.


textinput = text_input(Lines::Multiple, "\n");
textinput.set_selection_range(0, 1, SelectionDirection::Forward);
assert_eq!(textinput.edit_point.line, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I suspect this should be 1...

@servo-wpt-sync
Copy link
Collaborator

Transplanted upstreamable changes to existing PR.

Completed upstream sync of web-platform-test changes at jdm/web-platform-tests#23.

1 similar comment
@servo-wpt-sync
Copy link
Collaborator

Transplanted upstreamable changes to existing PR.

Completed upstream sync of web-platform-test changes at jdm/web-platform-tests#23.

@servo-wpt-sync
Copy link
Collaborator

Transplanted upstreamable changes to existing PR.

Completed upstream sync of web-platform-test changes at jdm/web-platform-tests#23.

@jdm
Copy link
Member

jdm commented Feb 6, 2018

@bors-servo r+

@bors-servo
Copy link
Contributor

📌 Commit 7b58fb5 has been approved by jdm

@highfive highfive assigned jdm and unassigned emilio Feb 6, 2018
@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-awaiting-review There is new code that needs to be reviewed. labels Feb 6, 2018
@bors-servo
Copy link
Contributor

⌛ Testing commit 7b58fb5 with merge 36eb711...

bors-servo pushed a commit that referenced this pull request Feb 6, 2018
Change Offset_to_text_point function and add unit tests

<!-- Please describe your changes on the following line: -->

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #18613.

<!-- Either: -->
- [x] Added unit tests and web platform tests

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19946)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css1, mac-rel-css2, mac-rel-wpt1, mac-rel-wpt2, mac-rel-wpt3, mac-rel-wpt4, windows-msvc-dev
Approved by: jdm
Pushing 36eb711 to master...

@bors-servo bors-servo merged commit 7b58fb5 into servo:master Feb 6, 2018
@highfive highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Feb 6, 2018
jdm pushed a commit to web-platform-tests/wpt that referenced this pull request Feb 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

assertion failed: self.edit_point.index <= self.lines[self.edit_point.line].len()
7 participants