This source file includes following definitions.
- SetUpCommandLine
- StartIsSearchProviderInstalledTest
- FinishIsSearchProviderInstalledTest
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include "base/command_line.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/browser_test_utils.h"
#include "net/test/spawned_test_server/spawned_test_server.h"
namespace {
struct IsSearchProviderTestData {
IsSearchProviderTestData() : tab(NULL) {}
IsSearchProviderTestData(content::WebContents* t, std::string h, GURL url)
: tab(t), host(h), test_url(url) {
}
content::WebContents* tab;
std::string host;
GURL test_url;
};
}
class SearchProviderTest : public InProcessBrowserTest {
protected:
SearchProviderTest() {}
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
ASSERT_TRUE(test_server()->Start());
std::string host_rule(
"MAP * " + test_server()->host_port_pair().ToString());
command_line->AppendSwitchASCII(switches::kHostRules, host_rule);
command_line->AppendSwitch(switches::kNoProxyServer);
search_provider_test_url_ =
test_server()->GetURL("files/is_search_provider_installed.html");
}
IsSearchProviderTestData StartIsSearchProviderInstalledTest(
Browser* browser,
const char* host,
const char* expected_result) {
GURL test_url(std::string("http://") + host +
search_provider_test_url_.path() + "#" + expected_result);
ui_test_utils::NavigateToURLWithDisposition(
browser, test_url, NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
content::WebContents* tab =
browser->tab_strip_model()->GetActiveWebContents();
return IsSearchProviderTestData(tab, host, test_url);
}
void FinishIsSearchProviderInstalledTest(
const IsSearchProviderTestData& data) {
base::string16 title = data.tab->GetTitle();
if (title.empty()) {
content::TitleWatcher title_watcher(data.tab, base::ASCIIToUTF16("OK"));
title_watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
title = title_watcher.WaitAndGetTitle();
}
EXPECT_EQ(base::ASCIIToUTF16("OK"), title);
}
GURL search_provider_test_url_;
private:
DISALLOW_COPY_AND_ASSIGN(SearchProviderTest);
};
#if 1
#define MAYBE_TestIsSearchProviderInstalled \
DISABLED_TestIsSearchProviderInstalled
#elif defined(OS_WIN)
#define MAYBE_TestIsSearchProviderInstalled \
DISABLED_TestIsSearchProviderInstalled
#else
#define MAYBE_TestIsSearchProviderInstalled TestIsSearchProviderInstalled
#endif
IN_PROC_BROWSER_TEST_F(SearchProviderTest,
MAYBE_TestIsSearchProviderInstalled) {
const char* test_hosts[] = { "www.google.com",
"www.bing.com",
"localhost" };
const char* expected_results[] = { "2",
"1",
"0" };
COMPILE_ASSERT(arraysize(test_hosts) == arraysize(expected_results),
there_should_be_a_result_for_each_host);
IsSearchProviderTestData test_data[2 * arraysize(test_hosts)];
for (size_t i = 0; i < arraysize(test_hosts); ++i) {
test_data[i] = StartIsSearchProviderInstalledTest(
browser(), test_hosts[i], expected_results[i]);
FinishIsSearchProviderInstalledTest(test_data[i]);
}
Browser* incognito_browser = CreateIncognitoBrowser();
for (size_t i = 0; i < arraysize(test_hosts); ++i) {
test_data[i + arraysize(test_hosts)] = StartIsSearchProviderInstalledTest(
incognito_browser, test_hosts[i], "0");
FinishIsSearchProviderInstalledTest(test_data[i + arraysize(test_hosts)]);
}
#if 0
for (size_t i = 0; i < arraysize(test_data); ++i) {
FinishIsSearchProviderInstalledTest(test_data[i]);
}
#endif
}
IN_PROC_BROWSER_TEST_F(SearchProviderTest,
TestIsSearchProviderInstalledWithException) {
search_provider_test_url_ = test_server()->GetURL(
"files/is_search_provider_installed_with_exception.html");
FinishIsSearchProviderInstalledTest(StartIsSearchProviderInstalledTest(
browser(), "www.google.com", ""));
}