- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / find_bar / find_backend_unittest.cc
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.
4
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"
16
17 using content::WebContents;
18 using content::WebContentsTester;
19
20 class FindBackendTest : public ChromeRenderViewHostTestHarness {
21  protected:
22   virtual void SetUp() OVERRIDE {
23     ChromeRenderViewHostTestHarness::SetUp();
24     FindTabHelper::CreateForWebContents(web_contents());
25   }
26 };
27
28 namespace {
29
30 string16 FindPrepopulateText(WebContents* contents) {
31   Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
32   return FindBarStateFactory::GetLastPrepopulateText(profile);
33 }
34
35 }  // end namespace
36
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(string16(), FindPrepopulateText(web_contents()));
44   EXPECT_EQ(string16(), find_tab_helper->find_text());
45
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());
52
53   // No search has still been issued, strings should be blank.
54   EXPECT_EQ(string16(), FindPrepopulateText(web_contents()));
55   EXPECT_EQ(string16(), find_tab_helper->find_text());
56   EXPECT_EQ(string16(), FindPrepopulateText(contents2.get()));
57   EXPECT_EQ(string16(), find_tab_helper2->find_text());
58
59   string16 search_term1 = ASCIIToUTF16(" I had a 401K    ");
60   string16 search_term2 = ASCIIToUTF16(" but the economy ");
61   string16 search_term3 = ASCIIToUTF16(" eated it.       ");
62
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);
66
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(string16(), find_tab_helper2->find_text());
73
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);
77
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());
84
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);
88
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());
95 }