1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// found in the LICENSE file.
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/prefs/pref_service.h"
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/autocomplete/autocomplete_controller.h"
7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/ui/omnibox/omnibox_controller.h"
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/common/pref_names.h"
9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/test/base/testing_profile.h"
105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/omnibox/autocomplete_provider.h"
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class OmniboxControllerTest : public testing::Test {
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) protected:
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  OmniboxControllerTest();
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual ~OmniboxControllerTest();
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void CreateController();
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void AssertProviders(int expected_providers);
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PrefService* GetPrefs() { return profile_.GetPrefs(); }
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const AutocompleteController::Providers& GetAutocompleteProviders() const {
23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return omnibox_controller_->autocomplete_controller()->providers();
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) private:
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  TestingProfile profile_;
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<OmniboxController> omnibox_controller_;
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(OmniboxControllerTest);
31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)};
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)OmniboxControllerTest::OmniboxControllerTest() {
34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)OmniboxControllerTest::~OmniboxControllerTest() {
37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void OmniboxControllerTest::CreateController() {
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  omnibox_controller_.reset(new OmniboxController(NULL, &profile_));
41c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Checks that the list of autocomplete providers used by the OmniboxController
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// matches the one in the |expected_providers| bit field.
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void OmniboxControllerTest::AssertProviders(int expected_providers) {
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const AutocompleteController::Providers& providers =
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      GetAutocompleteProviders();
48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  for (size_t i = 0; i < providers.size(); ++i) {
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Ensure this is a provider we wanted.
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    int type = providers[i]->type();
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ASSERT_TRUE(expected_providers & type);
53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Remove it from expectations so we fail if it's there twice.
55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    expected_providers &= ~type;
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Ensure we saw all the providers we expected.
59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ASSERT_EQ(0, expected_providers);
60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
62c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(OmniboxControllerTest, CheckDefaultAutocompleteProviders) {
63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  CreateController();
64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // First collect the basic providers.
65c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  int observed_providers = 0;
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const AutocompleteController::Providers& providers =
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      GetAutocompleteProviders();
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  for (size_t i = 0; i < providers.size(); ++i)
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    observed_providers |= providers[i]->type();
70c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Ensure we have at least one provider.
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ASSERT_NE(0, observed_providers);
72c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
73eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Ensure instant extended includes all the provides in classic Chrome.
74eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  int providers_with_instant_extended = observed_providers;
75c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(beaudoin): remove TYPE_SEARCH once it's no longer needed to pass
76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // the Instant suggestion through via FinalizeInstantQuery.
77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  CreateController();
78c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AssertProviders(providers_with_instant_extended);
79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
80