1// Copyright (c) 2011 The Chromium Authors. All rights reserved. Use of this
2// source code is governed by a BSD-style license that can be found in the
3// LICENSE file.
4
5#include "chrome/browser/profiles/profile.h"
6#include "chrome/browser/search_engines/template_url_model.h"
7#include "chrome/browser/search_engines/template_url_prepopulate_data.h"
8#include "chrome/browser/ui/browser.h"
9#include "chrome/test/in_process_browser_test.h"
10#include "chrome/test/ui_test_utils.h"
11#include "content/common/notification_registrar.h"
12#include "content/common/notification_source.h"
13#include "content/common/notification_type.h"
14#include "net/base/mock_host_resolver.h"
15#include "net/base/net_util.h"
16
17namespace {
18class TemplateURLScraperTest : public InProcessBrowserTest {
19 public:
20  TemplateURLScraperTest() {
21  }
22
23 private:
24  DISALLOW_COPY_AND_ASSIGN(TemplateURLScraperTest);
25};
26
27class TemplateURLModelLoader : public NotificationObserver {
28 public:
29  explicit TemplateURLModelLoader(TemplateURLModel* model) : model_(model) {
30    registrar_.Add(this, NotificationType::TEMPLATE_URL_MODEL_LOADED,
31                   Source<TemplateURLModel>(model));
32    model_->Load();
33    ui_test_utils::RunMessageLoop();
34  }
35
36  virtual void Observe(NotificationType type,
37                       const NotificationSource& source,
38                       const NotificationDetails& details) {
39    if (type == NotificationType::TEMPLATE_URL_MODEL_LOADED &&
40        Source<TemplateURLModel>(source).ptr() == model_) {
41      MessageLoop::current()->Quit();
42    }
43  }
44
45 private:
46  NotificationRegistrar registrar_;
47
48  TemplateURLModel* model_;
49
50  DISALLOW_COPY_AND_ASSIGN(TemplateURLModelLoader);
51};
52
53}  // namespace
54
55/*
56IN_PROC_BROWSER_TEST_F(TemplateURLScraperTest, ScrapeWithOnSubmit) {
57  host_resolver()->AddRule("*.foo.com", "localhost");
58
59  TemplateURLModel* template_urls = browser()->profile()->GetTemplateURLModel();
60  TemplateURLModelLoader loader(template_urls);
61
62  std::vector<const TemplateURL*> all_urls = template_urls->GetTemplateURLs();
63
64  // We need to substract the default pre-populated engines that the profile is
65  // set up with.
66  size_t default_index = 0;
67  std::vector<TemplateURL*> prepopulate_urls;
68  TemplateURLPrepopulateData::GetPrepopulatedEngines(
69      browser()->profile()->GetPrefs(),
70      &prepopulate_urls,
71      &default_index);
72
73  EXPECT_EQ(prepopulate_urls.size(), all_urls.size());
74
75  scoped_refptr<HTTPTestServer> server(
76      HTTPTestServer::CreateServerWithFileRootURL(
77          L"chrome/test/data/template_url_scraper/submit_handler", L"/",
78          g_browser_process->io_thread()->message_loop()));
79  ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
80      browser(), GURL("http://www.foo.com:1337/"), 2);
81
82  all_urls = template_urls->GetTemplateURLs();
83  EXPECT_EQ(1, all_urls.size() - prepopulate_urls.size());
84}
85*/
86