find_backend_unittest.cc revision dc0f95d653279beabeb9817299e2902918ba123e
1// Copyright (c) 2009 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/string16.h"
6#include "base/string_util.h"
7#include "base/utf_string_conversions.h"
8#include "chrome/browser/ui/find_bar/find_bar_state.h"
9#include "chrome/browser/ui/find_bar/find_tab_helper.h"
10#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
11#include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h"
12#include "chrome/common/url_constants.h"
13#include "chrome/test/testing_profile.h"
14#include "content/browser/tab_contents/test_tab_contents.h"
15
16typedef TabContentsWrapperTestHarness FindBackendTest;
17
18namespace {
19
20string16 FindPrepopulateText(TabContents* contents) {
21  return FindBarState::GetLastPrepopulateText(contents->profile());
22}
23
24}  // end namespace
25
26// This test takes two TabContents objects, searches in both of them and
27// tests the internal state for find_text and find_prepopulate_text.
28TEST_F(FindBackendTest, InternalState) {
29  FindTabHelper* find_tab_helper = contents_wrapper()->find_tab_helper();
30  // Initial state for the TabContents is blank strings.
31  EXPECT_EQ(string16(), FindPrepopulateText(contents()));
32  EXPECT_EQ(string16(), find_tab_helper->find_text());
33
34  // Get another TabContents object ready.
35  TestTabContents* contents2 = new TestTabContents(profile_.get(), NULL);
36  TabContentsWrapper wrapper2(contents2);
37  FindTabHelper* find_tab_helper2 = wrapper2.find_tab_helper();
38
39  // No search has still been issued, strings should be blank.
40  EXPECT_EQ(string16(), FindPrepopulateText(contents()));
41  EXPECT_EQ(string16(), find_tab_helper->find_text());
42  EXPECT_EQ(string16(), FindPrepopulateText(contents2));
43  EXPECT_EQ(string16(), find_tab_helper2->find_text());
44
45  string16 search_term1 = ASCIIToUTF16(" I had a 401K    ");
46  string16 search_term2 = ASCIIToUTF16(" but the economy ");
47  string16 search_term3 = ASCIIToUTF16(" eated it.       ");
48
49  // Start searching in the first TabContents, searching forwards but not case
50  // sensitive (as indicated by the last two params).
51  find_tab_helper->StartFinding(search_term1, true, false);
52
53  // Pre-populate string should always match between the two, but find_text
54  // should not.
55  EXPECT_EQ(search_term1, FindPrepopulateText(contents()));
56  EXPECT_EQ(search_term1, find_tab_helper->find_text());
57  EXPECT_EQ(search_term1, FindPrepopulateText(contents2));
58  EXPECT_EQ(string16(), find_tab_helper2->find_text());
59
60  // Now search in the other TabContents, searching forwards but not case
61  // sensitive (as indicated by the last two params).
62  find_tab_helper2->StartFinding(search_term2, true, false);
63
64  // Again, pre-populate string should always match between the two, but
65  // find_text should not.
66  EXPECT_EQ(search_term2, FindPrepopulateText(contents()));
67  EXPECT_EQ(search_term1, find_tab_helper->find_text());
68  EXPECT_EQ(search_term2, FindPrepopulateText(contents2));
69  EXPECT_EQ(search_term2, find_tab_helper2->find_text());
70
71  // Search again in the first TabContents, searching forwards but not case
72  // find_tab_helper (as indicated by the last two params).
73  find_tab_helper->StartFinding(search_term3, true, false);
74
75  // Once more, pre-populate string should always match between the two, but
76  // find_text should not.
77  EXPECT_EQ(search_term3, FindPrepopulateText(contents()));
78  EXPECT_EQ(search_term3, find_tab_helper->find_text());
79  EXPECT_EQ(search_term3, FindPrepopulateText(contents2));
80  EXPECT_EQ(search_term2, find_tab_helper2->find_text());
81}
82