toolbar_model_unittest.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 "chrome/browser/ui/toolbar/toolbar_model.h" 6 7#include "base/command_line.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" 12#include "chrome/browser/ui/browser.h" 13#include "chrome/browser/ui/browser_tabstrip.h" 14#include "chrome/browser/ui/search/search.h" 15#include "chrome/browser/ui/toolbar/toolbar_model.h" 16#include "chrome/common/chrome_switches.h" 17#include "chrome/test/base/browser_with_test_window_test.h" 18#include "content/public/browser/web_contents.h" 19#include "content/public/common/url_constants.h" 20 21using content::OpenURLParams; 22using content::Referrer; 23using content::WebContents; 24 25namespace { 26 27struct TestItem { 28 GURL url; 29 string16 expected_text; 30 // The expected text to display when Extended Instant is inactive. 31 string16 expected_replace_text_inactive; 32 // The expected text to display when Extended Instant is active. 33 string16 expected_replace_text_active; 34 bool would_replace; 35 bool should_display; 36} test_items[] = { 37 { 38 GURL("view-source:http://www.google.com"), 39 ASCIIToUTF16("view-source:www.google.com"), 40 ASCIIToUTF16("view-source:www.google.com"), 41 ASCIIToUTF16("view-source:www.google.com"), 42 false, 43 true 44 }, 45 { 46 GURL("view-source:chrome://newtab/"), 47 ASCIIToUTF16("view-source:chrome://newtab"), 48 ASCIIToUTF16("view-source:chrome://newtab"), 49 ASCIIToUTF16("view-source:chrome://newtab"), 50 false, 51 true 52 }, 53 { 54 GURL("chrome-extension://monkey/balls.html"), 55 string16(), 56 string16(), 57 string16(), 58 false, 59 false 60 }, 61 { 62 GURL("chrome://newtab/"), 63 string16(), 64 string16(), 65 string16(), 66 false, 67 false 68 }, 69 { 70 GURL(chrome::kAboutBlankURL), 71 ASCIIToUTF16(chrome::kAboutBlankURL), 72 ASCIIToUTF16(chrome::kAboutBlankURL), 73 ASCIIToUTF16(chrome::kAboutBlankURL), 74 false, 75 true 76 }, 77 { 78 GURL("http://searchurl/?q=tractor+supply"), 79 ASCIIToUTF16("searchurl/?q=tractor+supply"), 80 ASCIIToUTF16("searchurl/?q=tractor+supply"), 81 ASCIIToUTF16("searchurl/?q=tractor+supply"), 82 false, 83 true 84 }, 85 { 86 GURL("https://google.ca/search?q=tractor+supply"), 87 ASCIIToUTF16("https://google.ca/search?q=tractor+supply"), 88 ASCIIToUTF16("https://google.ca/search?q=tractor+supply"), 89 ASCIIToUTF16("https://google.ca/search?q=tractor+supply"), 90 false, 91 true 92 }, 93 { 94 GURL("https://google.com/search?q=tractor+supply"), 95 ASCIIToUTF16("https://google.com/search?q=tractor+supply"), 96 ASCIIToUTF16("https://google.com/search?q=tractor+supply"), 97 ASCIIToUTF16("tractor supply"), 98 true, 99 true 100 } 101}; 102 103} // end namespace 104 105class ToolbarModelTest : public BrowserWithTestWindowTest { 106 public: 107 ToolbarModelTest() {} 108 109 virtual void SetUp() OVERRIDE { 110 BrowserWithTestWindowTest::SetUp(); 111 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( 112 profile(), &TemplateURLServiceFactory::BuildInstanceFor); 113 } 114 virtual void TearDown() OVERRIDE { BrowserWithTestWindowTest::TearDown(); } 115 116 protected: 117 118 void ResetDefaultTemplateURL() { 119 TemplateURLData data; 120 data.SetURL("http://google.com/search?q={searchTerms}"); 121 TemplateURL* search_template_url = new TemplateURL(profile(), data); 122 TemplateURLService* template_url_service = 123 TemplateURLServiceFactory::GetForProfile(profile()); 124 template_url_service->Add(search_template_url); 125 template_url_service->SetDefaultSearchProvider(search_template_url); 126 ASSERT_NE(0, search_template_url->id()); 127 template_url_service->Load(); 128 } 129 130 void NavigateAndCheckText(const GURL& url, 131 const string16& expected_text, 132 const string16& expected_replace_text, 133 bool would_replace, 134 bool should_display) { 135 NavigateAndCheckTextImpl(url, false, expected_text, would_replace, 136 should_display); 137 NavigateAndCheckTextImpl(url, true, expected_replace_text, would_replace, 138 should_display); 139 } 140 141 private: 142 void NavigateAndCheckTextImpl(const GURL& url, 143 bool can_replace, 144 const string16 expected_text, 145 bool would_replace, 146 bool should_display) { 147 WebContents* contents = chrome::GetWebContentsAt(browser(), 0); 148 browser()->OpenURL(OpenURLParams( 149 url, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED, 150 false)); 151 152 ToolbarModel* toolbar_model = browser()->toolbar_model(); 153 154 // Check while loading. 155 EXPECT_EQ(should_display, toolbar_model->ShouldDisplayURL()); 156 EXPECT_EQ(expected_text, toolbar_model->GetText(can_replace)); 157 EXPECT_EQ(would_replace, 158 toolbar_model->WouldReplaceSearchURLWithSearchTerms()); 159 160 // Check after commit. 161 CommitPendingLoad(&contents->GetController()); 162 EXPECT_EQ(should_display, toolbar_model->ShouldDisplayURL()); 163 EXPECT_EQ(expected_text, toolbar_model->GetText(can_replace)); 164 EXPECT_EQ(would_replace, 165 toolbar_model->WouldReplaceSearchURLWithSearchTerms()); 166 } 167}; 168 169// Test that we don't replace any URLs when the InstantExtended API is disabled. 170TEST_F(ToolbarModelTest, ShouldDisplayURLInstantExtendedAPIDisabled) { 171 ASSERT_FALSE(CommandLine::ForCurrentProcess()->HasSwitch( 172 switches::kEnableInstantExtendedAPI)) 173 << "This test expects Extended Instant to be disabled."; 174 175 ResetDefaultTemplateURL(); 176 AddTab(browser(), GURL(chrome::kAboutBlankURL)); 177 for (size_t i = 0; i < arraysize(test_items); ++i) { 178 const TestItem& test_item = test_items[i]; 179 NavigateAndCheckText(test_item.url, 180 test_item.expected_text, 181 test_item.expected_replace_text_inactive, 182 false, 183 test_item.should_display); 184 } 185} 186 187// Test that we replace URLs when the InstantExtended API is enabled. 188TEST_F(ToolbarModelTest, ShouldDisplayURLInstantExtendedAPIEnabled) { 189 CommandLine::ForCurrentProcess()->AppendSwitch( 190 switches::kEnableInstantExtendedAPI); 191 192 // Avoid tests on branded Chrome where channel is set to CHANNEL_STABLE. 193 if (!chrome::search::IsInstantExtendedAPIEnabled(profile())) 194 return; 195 196 ResetDefaultTemplateURL(); 197 AddTab(browser(), GURL(chrome::kAboutBlankURL)); 198 for (size_t i = 0; i < arraysize(test_items); ++i) { 199 const TestItem& test_item = test_items[i]; 200 NavigateAndCheckText(test_item.url, 201 test_item.expected_text, 202 test_item.expected_replace_text_active, 203 test_item.would_replace, 204 test_item.should_display); 205 } 206} 207