find_in_page_request_unittest.mm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #import "ios/web/find_in_page/find_in_page_request.h"
  5. #include "ios/web/public/test/fakes/fake_web_frame.h"
  6. #include "ios/web/public/test/web_test.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. #if !defined(__has_feature) || !__has_feature(objc_arc)
  9. #error "This file requires ARC support."
  10. #endif
  11. namespace web {
  12. class FindInPageRequestTest : public WebTest {
  13. protected:
  14. FindInPageRequestTest() {
  15. auto main_frame = FakeWebFrame::CreateMainWebFrame(GURL::EmptyGURL());
  16. request_.AddFrame(main_frame.get());
  17. auto frame_with_two_matches =
  18. FakeWebFrame::CreateChildWebFrame(GURL::EmptyGURL());
  19. request_.AddFrame(frame_with_two_matches.get());
  20. request_.Reset(@"foo", 2);
  21. request_.SetMatchCountForFrame(1, kMainFakeFrameId);
  22. request_.SetMatchCountForFrame(2, kChildFakeFrameId);
  23. }
  24. FindInPageRequest request_;
  25. };
  26. // Tests that FindInPageRequest properly clears its properties in respond to a
  27. // Reset() call.
  28. TEST_F(FindInPageRequestTest, Reset) {
  29. EXPECT_EQ(3, request_.GetTotalMatchCount());
  30. EXPECT_TRUE(request_.GoToFirstMatch());
  31. EXPECT_EQ(0, request_.GetCurrentSelectedMatchFrameIndex());
  32. EXPECT_EQ(1, request_.GetMatchCountForSelectedFrame());
  33. int request_id = request_.GetRequestId();
  34. request_.Reset(@"foobar", 2);
  35. EXPECT_GE(request_.GetRequestId(), request_id);
  36. EXPECT_EQ(-1, request_.GetCurrentSelectedMatchFrameIndex());
  37. EXPECT_EQ(@"foobar", request_.GetRequestQuery());
  38. EXPECT_EQ(0, request_.GetTotalMatchCount());
  39. EXPECT_EQ(-1, request_.GetMatchCountForSelectedFrame());
  40. }
  41. // Tests that FindinPageRequest properly decrements |pending_frame_call_count_|
  42. // properly.
  43. TEST_F(FindInPageRequestTest, AllFindResponsesReturned) {
  44. request_.DidReceiveFindResponseFromOneFrame();
  45. EXPECT_FALSE(request_.AreAllFindResponsesReturned());
  46. request_.DidReceiveFindResponseFromOneFrame();
  47. EXPECT_TRUE(request_.AreAllFindResponsesReturned());
  48. }
  49. // Tests that FindInPageRequest GoToNextMatch() is able to traverse all matches
  50. // in multiple frames.
  51. TEST_F(FindInPageRequestTest, GoToNext) {
  52. request_.GoToFirstMatch();
  53. EXPECT_EQ(0, request_.GetCurrentSelectedMatchFrameIndex());
  54. EXPECT_EQ(0, request_.GetCurrentSelectedMatchPageIndex());
  55. request_.GoToNextMatch();
  56. EXPECT_EQ(0, request_.GetCurrentSelectedMatchFrameIndex());
  57. EXPECT_EQ(1, request_.GetCurrentSelectedMatchPageIndex());
  58. request_.GoToNextMatch();
  59. EXPECT_EQ(1, request_.GetCurrentSelectedMatchFrameIndex());
  60. EXPECT_EQ(2, request_.GetCurrentSelectedMatchPageIndex());
  61. request_.GoToNextMatch();
  62. EXPECT_EQ(0, request_.GetCurrentSelectedMatchFrameIndex());
  63. EXPECT_EQ(0, request_.GetCurrentSelectedMatchPageIndex());
  64. }
  65. // Tests that FindInPageRequest GoToPreviousMatch() is able to traverse all
  66. // matches in multiple frames.
  67. TEST_F(FindInPageRequestTest, GoToPrevious) {
  68. request_.GoToFirstMatch();
  69. EXPECT_EQ(0, request_.GetCurrentSelectedMatchFrameIndex());
  70. EXPECT_EQ(0, request_.GetCurrentSelectedMatchPageIndex());
  71. request_.GoToPreviousMatch();
  72. EXPECT_EQ(1, request_.GetCurrentSelectedMatchFrameIndex());
  73. EXPECT_EQ(2, request_.GetCurrentSelectedMatchPageIndex());
  74. request_.GoToPreviousMatch();
  75. EXPECT_EQ(0, request_.GetCurrentSelectedMatchFrameIndex());
  76. EXPECT_EQ(1, request_.GetCurrentSelectedMatchPageIndex());
  77. request_.GoToPreviousMatch();
  78. EXPECT_EQ(0, request_.GetCurrentSelectedMatchFrameIndex());
  79. EXPECT_EQ(0, request_.GetCurrentSelectedMatchPageIndex());
  80. }
  81. // Tests that FindInPageRequest returns the correct relative match count within
  82. // a frame and total match count when traversing matches in multiple frames.
  83. TEST_F(FindInPageRequestTest, RelativeMatchCount) {
  84. request_.GoToFirstMatch();
  85. EXPECT_EQ(3, request_.GetTotalMatchCount());
  86. EXPECT_EQ(1, request_.GetMatchCountForSelectedFrame());
  87. request_.GoToNextMatch();
  88. EXPECT_EQ(3, request_.GetTotalMatchCount());
  89. EXPECT_EQ(2, request_.GetMatchCountForSelectedFrame());
  90. }
  91. // Tests that FindInPageRequest returns the correct relative match count within
  92. // a frame and total match count when a frame is removed. Also tests that going
  93. // to the next match after removing the currently selected frame produces the
  94. // expected relative and total selected match index.
  95. TEST_F(FindInPageRequestTest, RemoveFrame) {
  96. request_.GoToFirstMatch();
  97. EXPECT_EQ(3, request_.GetTotalMatchCount());
  98. EXPECT_EQ(1, request_.GetMatchCountForSelectedFrame());
  99. request_.RemoveFrame(kMainFakeFrameId);
  100. EXPECT_EQ(2, request_.GetTotalMatchCount());
  101. request_.GoToNextMatch();
  102. EXPECT_EQ(0, request_.GetCurrentSelectedMatchFrameIndex());
  103. EXPECT_EQ(0, request_.GetCurrentSelectedMatchPageIndex());
  104. }
  105. // Tests that FindInPageRequest returns the correct relative match count within
  106. // a frame and total match count when the match count for the currently selected
  107. // frame changes.
  108. TEST_F(FindInPageRequestTest, SetMatchCountForSelectedFrame) {
  109. request_.GoToFirstMatch();
  110. request_.SetMatchCountForSelectedFrame(5);
  111. EXPECT_EQ(7, request_.GetTotalMatchCount());
  112. EXPECT_EQ(5, request_.GetMatchCountForSelectedFrame());
  113. }
  114. // Tests that FindInPageRequest returns the currently selected match index
  115. // relative to the frame and the total are correct when the total matches and
  116. // the relative match index change.
  117. TEST_F(FindInPageRequestTest, SetCurrentSelectedMatchIndex) {
  118. request_.GoToFirstMatch();
  119. request_.SetMatchCountForSelectedFrame(5);
  120. request_.SetCurrentSelectedMatchFrameIndex(1);
  121. EXPECT_EQ(1, request_.GetCurrentSelectedMatchFrameIndex());
  122. EXPECT_EQ(1, request_.GetCurrentSelectedMatchPageIndex());
  123. }
  124. // Tests that FindInPageRequest returns the correct match count within
  125. // a frame and total match count when the match count for a not currently
  126. // selected frame changes.
  127. TEST_F(FindInPageRequestTest, SetMatchCountForFrame) {
  128. request_.GoToFirstMatch();
  129. EXPECT_EQ(3, request_.GetTotalMatchCount());
  130. EXPECT_EQ(1, request_.GetMatchCountForSelectedFrame());
  131. request_.SetMatchCountForFrame(5, kChildFakeFrameId);
  132. EXPECT_EQ(6, request_.GetTotalMatchCount());
  133. EXPECT_EQ(1, request_.GetMatchCountForSelectedFrame());
  134. }
  135. } // namespace web