Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / find_bar / find_backend_unittest.cc
blob699c1000867fe51373fc4a8e3a986de7bd607146
1 // Copyright (c) 2012 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.
5 #include "base/strings/string16.h"
6 #include "base/strings/string_util.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/ui/find_bar/find_bar_state.h"
9 #include "chrome/browser/ui/find_bar/find_bar_state_factory.h"
10 #include "chrome/browser/ui/find_bar/find_tab_helper.h"
11 #include "chrome/common/url_constants.h"
12 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/test/web_contents_tester.h"
17 using content::WebContents;
18 using content::WebContentsTester;
20 class FindBackendTest : public ChromeRenderViewHostTestHarness {
21 protected:
22 void SetUp() override {
23 ChromeRenderViewHostTestHarness::SetUp();
24 FindTabHelper::CreateForWebContents(web_contents());
28 namespace {
30 base::string16 FindPrepopulateText(WebContents* contents) {
31 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
32 return FindBarStateFactory::GetLastPrepopulateText(profile);
35 } // end namespace
37 // This test takes two WebContents objects, searches in both of them and
38 // tests the internal state for find_text and find_prepopulate_text.
39 TEST_F(FindBackendTest, InternalState) {
40 FindTabHelper* find_tab_helper =
41 FindTabHelper::FromWebContents(web_contents());
42 // Initial state for the WebContents is blank strings.
43 EXPECT_EQ(base::string16(), FindPrepopulateText(web_contents()));
44 EXPECT_EQ(base::string16(), find_tab_helper->find_text());
46 // Get another WebContents object ready.
47 scoped_ptr<WebContents> contents2(
48 WebContentsTester::CreateTestWebContents(profile(), NULL));
49 FindTabHelper::CreateForWebContents(contents2.get());
50 FindTabHelper* find_tab_helper2 =
51 FindTabHelper::FromWebContents(contents2.get());
53 // No search has still been issued, strings should be blank.
54 EXPECT_EQ(base::string16(), FindPrepopulateText(web_contents()));
55 EXPECT_EQ(base::string16(), find_tab_helper->find_text());
56 EXPECT_EQ(base::string16(), FindPrepopulateText(contents2.get()));
57 EXPECT_EQ(base::string16(), find_tab_helper2->find_text());
59 base::string16 search_term1 = base::ASCIIToUTF16(" I had a 401K ");
60 base::string16 search_term2 = base::ASCIIToUTF16(" but the economy ");
61 base::string16 search_term3 = base::ASCIIToUTF16(" eated it. ");
63 // Start searching in the first WebContents, searching forwards but not case
64 // sensitive (as indicated by the last two params).
65 find_tab_helper->StartFinding(search_term1, true, false);
67 // Pre-populate string should always match between the two, but find_text
68 // should not.
69 EXPECT_EQ(search_term1, FindPrepopulateText(web_contents()));
70 EXPECT_EQ(search_term1, find_tab_helper->find_text());
71 EXPECT_EQ(search_term1, FindPrepopulateText(contents2.get()));
72 EXPECT_EQ(base::string16(), find_tab_helper2->find_text());
74 // Now search in the other WebContents, searching forwards but not case
75 // sensitive (as indicated by the last two params).
76 find_tab_helper2->StartFinding(search_term2, true, false);
78 // Again, pre-populate string should always match between the two, but
79 // find_text should not.
80 EXPECT_EQ(search_term2, FindPrepopulateText(web_contents()));
81 EXPECT_EQ(search_term1, find_tab_helper->find_text());
82 EXPECT_EQ(search_term2, FindPrepopulateText(contents2.get()));
83 EXPECT_EQ(search_term2, find_tab_helper2->find_text());
85 // Search again in the first WebContents, searching forwards but not case
86 // find_tab_helper (as indicated by the last two params).
87 find_tab_helper->StartFinding(search_term3, true, false);
89 // Once more, pre-populate string should always match between the two, but
90 // find_text should not.
91 EXPECT_EQ(search_term3, FindPrepopulateText(web_contents()));
92 EXPECT_EQ(search_term3, find_tab_helper->find_text());
93 EXPECT_EQ(search_term3, FindPrepopulateText(contents2.get()));
94 EXPECT_EQ(search_term2, find_tab_helper2->find_text());