Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(297)

Side by Side Diff: chrome/browser/ui/toolbar/toolbar_model_unittest.cc

Issue 10867038: A simple version of search term replacement for testing . (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: added whitespace Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/toolbar/toolbar_model.h" 5 #include "chrome/browser/ui/toolbar/toolbar_model.h"
6 6
7 #include "base/command_line.h"
7 #include "base/utf_string_conversions.h" 8 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/search_engines/template_url.h"
10 #include "chrome/browser/search_engines/template_url_service.h"
11 #include "chrome/browser/search_engines/template_url_service_factory.h"
8 #include "chrome/browser/ui/browser.h" 12 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/browser_tabstrip.h" 13 #include "chrome/browser/ui/browser_tabstrip.h"
10 #include "chrome/browser/ui/toolbar/toolbar_model.h" 14 #include "chrome/browser/ui/toolbar/toolbar_model.h"
15 #include "chrome/common/chrome_switches.h"
11 #include "chrome/test/base/browser_with_test_window_test.h" 16 #include "chrome/test/base/browser_with_test_window_test.h"
12 #include "content/public/browser/web_contents.h" 17 #include "content/public/browser/web_contents.h"
13 #include "content/public/common/url_constants.h" 18 #include "content/public/common/url_constants.h"
14 19
15 using content::OpenURLParams; 20 using content::OpenURLParams;
16 using content::Referrer; 21 using content::Referrer;
17 using content::WebContents; 22 using content::WebContents;
18 23
24 namespace {
25
26 struct TestItem {
27 GURL url;
28 string16 expected_text;
29 // The expected text to display when Extended Instant is inactive.
30 string16 expected_replace_text_inactive;
31 // The expected text to display when Extended Instant is active.
32 string16 expected_replace_text_active;
33 bool would_replace;
34 bool should_display;
35 } test_items[] = {
36 {
37 GURL("view-source:http://www.google.com"),
38 ASCIIToUTF16("view-source:www.google.com"),
39 ASCIIToUTF16("view-source:www.google.com"),
40 ASCIIToUTF16("view-source:www.google.com"),
41 false,
42 true
43 },
44 {
45 GURL("view-source:chrome://newtab/"),
46 ASCIIToUTF16("view-source:chrome://newtab"),
47 ASCIIToUTF16("view-source:chrome://newtab"),
48 ASCIIToUTF16("view-source:chrome://newtab"),
49 false,
50 true
51 },
52 {
53 GURL("chrome-extension://monkey/balls.html"),
54 string16(),
55 string16(),
56 string16(),
57 false,
58 false
59 },
60 {
61 GURL("chrome://newtab/"),
62 string16(),
63 string16(),
64 string16(),
65 false,
66 false
67 },
68 {
69 GURL(chrome::kAboutBlankURL),
70 ASCIIToUTF16(chrome::kAboutBlankURL),
71 ASCIIToUTF16(chrome::kAboutBlankURL),
72 ASCIIToUTF16(chrome::kAboutBlankURL),
73 false,
74 true
75 },
76 {
77 GURL("http://searchurl/?q=tractor+supply"),
78 ASCIIToUTF16("searchurl/?q=tractor+supply"),
79 ASCIIToUTF16("searchurl/?q=tractor+supply"),
80 ASCIIToUTF16("searchurl/?q=tractor+supply"),
81 false,
82 true
83 },
84 {
85 GURL("http://google.com/search?q=tractor+supply"),
86 ASCIIToUTF16("google.com/search?q=tractor+supply"),
87 ASCIIToUTF16("google.com/search?q=tractor+supply"),
88 ASCIIToUTF16("google.com/search?q=tractor+supply"),
89 false,
90 true
91 },
92 {
93 GURL("http://google.com/search?q=tractor+supply&espv=1"),
94 ASCIIToUTF16("google.com/search?q=tractor+supply&espv=1"),
95 ASCIIToUTF16("google.com/search?q=tractor+supply&espv=1"),
96 ASCIIToUTF16("tractor supply"),
97 true,
98 true
99 }
100 };
101
102 } // end namespace
103
19 class ToolbarModelTest : public BrowserWithTestWindowTest { 104 class ToolbarModelTest : public BrowserWithTestWindowTest {
20 public: 105 public:
21 ToolbarModelTest() {} 106 ToolbarModelTest() {}
22 107
108 virtual void SetUp() OVERRIDE {
109 BrowserWithTestWindowTest::SetUp();
110 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
111 profile(), &TemplateURLServiceFactory::BuildInstanceFor);
112 }
113 virtual void TearDown() OVERRIDE { BrowserWithTestWindowTest::TearDown(); }
114
23 protected: 115 protected:
24 void NavigateAndCheckText(const std::string& url, 116
25 const std::string& expected_text, 117 void ResetDefaultTemplateURL() {
118 TemplateURLData data;
119 data.SetURL("http://google.com/search?q={searchTerms}");
120 TemplateURL* search_template_url = new TemplateURL(profile(), data);
121 TemplateURLService* template_url_service =
122 TemplateURLServiceFactory::GetForProfile(profile());
123 template_url_service->Add(search_template_url);
124 template_url_service->SetDefaultSearchProvider(search_template_url);
125 ASSERT_NE(0, search_template_url->id());
126 template_url_service->Load();
127 }
128
129 void NavigateAndCheckText(const GURL& url,
130 const string16& expected_text,
131 const string16& expected_replace_text,
132 bool would_replace,
26 bool should_display) { 133 bool should_display) {
134 NavigateAndCheckTextImpl(url, false, expected_text, would_replace,
135 should_display);
136 NavigateAndCheckTextImpl(url, true, expected_replace_text, would_replace,
137 should_display);
138 }
139
140 private:
141 void NavigateAndCheckTextImpl(const GURL& url,
142 bool can_replace,
143 const string16 expected_text,
144 bool would_replace,
145 bool should_display) {
27 WebContents* contents = chrome::GetWebContentsAt(browser(), 0); 146 WebContents* contents = chrome::GetWebContentsAt(browser(), 0);
28 browser()->OpenURL(OpenURLParams( 147 browser()->OpenURL(OpenURLParams(
29 GURL(url), Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED, 148 url, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
30 false)); 149 false));
31 150
151 ToolbarModel* toolbar_model = browser()->toolbar_model();
152
32 // Check while loading. 153 // Check while loading.
33 EXPECT_EQ(should_display, browser()->toolbar_model()->ShouldDisplayURL()); 154 EXPECT_EQ(should_display, toolbar_model->ShouldDisplayURL());
34 EXPECT_EQ(ASCIIToUTF16(expected_text), 155 EXPECT_EQ(expected_text, toolbar_model->GetText(can_replace));
35 browser()->toolbar_model()->GetText()); 156 EXPECT_EQ(would_replace,
157 toolbar_model->WouldReplaceSearchURLWithSearchTerms());
36 158
37 // Check after commit. 159 // Check after commit.
38 CommitPendingLoad(&contents->GetController()); 160 CommitPendingLoad(&contents->GetController());
39 EXPECT_EQ(should_display, browser()->toolbar_model()->ShouldDisplayURL()); 161 EXPECT_EQ(should_display, toolbar_model->ShouldDisplayURL());
40 EXPECT_EQ(ASCIIToUTF16(expected_text), 162 EXPECT_EQ(expected_text, toolbar_model->GetText(can_replace));
41 browser()->toolbar_model()->GetText()); 163 EXPECT_EQ(would_replace,
164 toolbar_model->WouldReplaceSearchURLWithSearchTerms());
42 } 165 }
43 }; 166 };
44 167
45 // Test that URLs are correctly shown or hidden both during navigation and 168 // Test that we don't replace any URLs when the InstantExtended API is disabled.
46 // after commit. 169 TEST_F(ToolbarModelTest, ShouldDisplayURLInstantExtendedAPIDisabled) {
47 TEST_F(ToolbarModelTest, ShouldDisplayURL) { 170 ASSERT_FALSE(CommandLine::ForCurrentProcess()->HasSwitch(
171 switches::kEnableInstantExtendedAPI))
172 << "This test expects Extended Instant to be disabled.";
173
174 ResetDefaultTemplateURL();
48 AddTab(browser(), GURL(chrome::kAboutBlankURL)); 175 AddTab(browser(), GURL(chrome::kAboutBlankURL));
176 for (size_t i = 0; i < arraysize(test_items); ++i) {
177 const TestItem& test_item = test_items[i];
178 NavigateAndCheckText(test_item.url,
179 test_item.expected_text,
180 test_item.expected_replace_text_inactive,
181 false,
182 test_item.should_display);
183 }
184 }
49 185
50 NavigateAndCheckText("view-source:http://www.google.com", 186 // Test that we don't replace any URLs when the InstantExtended API is enabled.
51 "view-source:www.google.com", true); 187 TEST_F(ToolbarModelTest, ShouldDisplayURLInstantExtendedAPIEnabled) {
52 NavigateAndCheckText("view-source:chrome://newtab/", 188 CommandLine::ForCurrentProcess()->AppendSwitch(
53 "view-source:chrome://newtab", true); 189 switches::kEnableInstantExtendedAPI);
54 NavigateAndCheckText("chrome-extension://monkey/balls.html", "", false); 190
55 NavigateAndCheckText("chrome://newtab/", "", false); 191 ResetDefaultTemplateURL();
56 NavigateAndCheckText(chrome::kAboutBlankURL, chrome::kAboutBlankURL, true); 192 AddTab(browser(), GURL(chrome::kAboutBlankURL));
193 for (size_t i = 0; i < arraysize(test_items); ++i) {
194 const TestItem& test_item = test_items[i];
195 NavigateAndCheckText(test_item.url,
196 test_item.expected_text,
197 test_item.expected_replace_text_active,
198 test_item.would_replace,
199 test_item.should_display);
200 }
57 } 201 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/toolbar/toolbar_model.cc ('k') | chrome/browser/ui/views/location_bar/location_bar_view.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698