autocomplete_edit_unittest.cc revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2010 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 "chrome/browser/autocomplete/autocomplete_edit.h"
6#include "chrome/browser/autocomplete/autocomplete_edit_view.h"
7#include "chrome/test/testing_profile.h"
8#include "third_party/skia/include/core/SkBitmap.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
11namespace {
12
13class TestingAutocompleteEditView : public AutocompleteEditView {
14 public:
15  TestingAutocompleteEditView() {}
16
17  virtual AutocompleteEditModel* model() { return NULL; }
18  virtual const AutocompleteEditModel* model() const { return NULL; }
19  virtual void SaveStateToTab(TabContents* tab) {}
20  virtual void Update(const TabContents* tab_for_state_restoring) {}
21  virtual void OpenURL(const GURL& url,
22                       WindowOpenDisposition disposition,
23                       PageTransition::Type transition,
24                       const GURL& alternate_nav_url,
25                       size_t selected_line,
26                       const std::wstring& keyword) {}
27  virtual std::wstring GetText() const { return std::wstring(); }
28  virtual bool IsEditingOrEmpty() const { return true; }
29  virtual int GetIcon() const { return 0; }
30  virtual void SetUserText(const std::wstring& text) {}
31  virtual void SetUserText(const std::wstring& text,
32                           const std::wstring& display_text,
33                           bool update_popup) {}
34  virtual void SetWindowTextAndCaretPos(const std::wstring& text,
35                                        size_t caret_pos) {}
36  virtual void SetForcedQuery() {}
37  virtual bool IsSelectAll() { return false; }
38  virtual void SelectAll(bool reversed) {}
39  virtual void RevertAll() {}
40  virtual void UpdatePopup() {}
41  virtual void ClosePopup() {}
42  virtual void SetFocus() {}
43  virtual void OnTemporaryTextMaybeChanged(const std::wstring& display_text,
44                                           bool save_original_selection) {}
45  virtual bool OnInlineAutocompleteTextMaybeChanged(
46      const std::wstring& display_text, size_t user_text_length) {
47    return false;
48  }
49  virtual void OnRevertTemporaryText() {}
50  virtual void OnBeforePossibleChange() {}
51  virtual bool OnAfterPossibleChange() { return false; }
52  virtual gfx::NativeView GetNativeView() const { return 0; }
53  virtual CommandUpdater* GetCommandUpdater() { return NULL; }
54
55 private:
56  DISALLOW_COPY_AND_ASSIGN(TestingAutocompleteEditView);
57};
58
59class TestingAutocompleteEditController : public AutocompleteEditController {
60 public:
61  TestingAutocompleteEditController() {}
62  virtual void OnAutocompleteAccept(const GURL& url,
63                                    WindowOpenDisposition disposition,
64                                    PageTransition::Type transition,
65                                    const GURL& alternate_nav_url) {}
66  virtual void OnChanged() {}
67  virtual void OnInputInProgress(bool in_progress) {}
68  virtual void OnKillFocus() {}
69  virtual void OnSetFocus() {}
70  virtual SkBitmap GetFavIcon() const { return SkBitmap(); }
71  virtual std::wstring GetTitle() const { return std::wstring(); }
72
73 private:
74  DISALLOW_COPY_AND_ASSIGN(TestingAutocompleteEditController);
75};
76
77}
78
79typedef testing::Test AutocompleteEditTest;
80
81// Tests various permutations of AutocompleteModel::AdjustTextForCopy.
82TEST(AutocompleteEditTest, AdjustTextForCopy) {
83  struct Data {
84    const wchar_t* perm_text;
85    const int sel_start;
86    const bool is_all_selected;
87    const wchar_t* input;
88    const wchar_t* expected_output;
89    const bool write_url;
90    const char* expected_url;
91  } input[] = {
92    // Test that http:// is inserted if all text is selected.
93    { L"a.b/c", 0, true, L"a.b/c", L"http://a.b/c", true, "http://a.b/c" },
94
95    // Test that http:// is inserted if the host is selected.
96    { L"a.b/c", 0, false, L"a.b/", L"http://a.b/", true, "http://a.b/" },
97
98    // Tests that http:// is inserted if the path is modified.
99    { L"a.b/c", 0, false, L"a.b/d", L"http://a.b/d", true, "http://a.b/d" },
100
101    // Tests that http:// isn't inserted if the host is modified.
102    { L"a.b/c", 0, false, L"a.c/", L"a.c/", false, "" },
103
104    // Tests that http:// isn't inserted if the start of the selection is 1.
105    { L"a.b/c", 1, false, L"a.b/", L"a.b/", false, "" },
106
107    // Tests that http:// isn't inserted if a portion of the host is selected.
108    { L"a.com/", 0, false, L"a.co", L"a.co", false, "" },
109
110    // Tests that http:// isn't inserted for an https url after the user nukes
111    // https.
112    { L"https://a.com/", 0, false, L"a.com/", L"a.com/", false, "" },
113
114    // Tests that http:// isn't inserted if the user adds to the host.
115    { L"a.b/", 0, false, L"a.bc/", L"a.bc/", false, "" },
116
117    // Tests that we don't get double http if the user manually inserts http.
118    { L"a.b/", 0, false, L"http://a.b/", L"http://a.b/", true, "http://a.b/" },
119  };
120  TestingAutocompleteEditView view;
121  TestingAutocompleteEditController controller;
122  TestingProfile profile;
123  AutocompleteEditModel model(&view, &controller, &profile);
124
125  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input); ++i) {
126    model.UpdatePermanentText(input[i].perm_text);
127
128    std::wstring result(input[i].input);
129    GURL url;
130    bool write_url;
131    model.AdjustTextForCopy(input[i].sel_start, input[i].is_all_selected,
132                            &result, &url, &write_url);
133    EXPECT_EQ(input[i].expected_output, result) << "@: " << i;
134    EXPECT_EQ(input[i].write_url, write_url) << " @" << i;
135    if (write_url)
136      EXPECT_EQ(input[i].expected_url, url.spec()) << " @" << i;
137  }
138}
139