blob: b5949848cee7bd14aa697bf6335051529b809fb9 [file] [log] [blame]
[email protected]93ef5f02013-04-26 02:36:251// Copyright 2013 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
avi655876a2015-12-25 07:18:155#include <stddef.h>
6
Jinho Bangcc280792018-01-17 23:33:557#include <memory>
8
avi655876a2015-12-25 07:18:159#include "base/macros.h"
Carlos Caballero1f8f83f2018-11-28 17:30:1310#include "base/test/scoped_task_environment.h"
blundell2102f7c2015-07-09 10:00:5311#include "components/omnibox/browser/autocomplete_controller.h"
12#include "components/omnibox/browser/autocomplete_provider.h"
blundell26a618e2015-07-31 11:16:4513#include "components/omnibox/browser/omnibox_client.h"
blundell7dbd3792015-08-05 15:14:1914#include "components/omnibox/browser/omnibox_controller.h"
Tommy C. Li9649d2a42018-01-17 21:59:5115#include "components/omnibox/browser/test_omnibox_client.h"
blundell47c6d8a2015-09-24 11:06:4016#include "components/sessions/core/session_id.h"
[email protected]93ef5f02013-04-26 02:36:2517#include "testing/gtest/include/gtest/gtest.h"
18
blundell26a618e2015-07-31 11:16:4519class OmniboxControllerTest : public testing::Test {
[email protected]93ef5f02013-04-26 02:36:2520 protected:
21 OmniboxControllerTest();
dcheng131bb7e62014-10-28 00:16:3822 ~OmniboxControllerTest() override;
[email protected]93ef5f02013-04-26 02:36:2523
24 void CreateController();
25 void AssertProviders(int expected_providers);
26
[email protected]f8735432014-07-09 07:35:2427 const AutocompleteController::Providers& GetAutocompleteProviders() const {
[email protected]93ef5f02013-04-26 02:36:2528 return omnibox_controller_->autocomplete_controller()->providers();
29 }
30
31 private:
blundell26a618e2015-07-31 11:16:4532 // testing::Test:
blundell3c100ce2015-07-29 18:16:2033 void SetUp() override;
34 void TearDown() override;
35
Carlos Caballero1f8f83f2018-11-28 17:30:1336 base::test::ScopedTaskEnvironment task_environment_;
dcheng9603ab92016-04-08 04:17:3237 std::unique_ptr<TestOmniboxClient> omnibox_client_;
38 std::unique_ptr<OmniboxController> omnibox_controller_;
[email protected]93ef5f02013-04-26 02:36:2539
40 DISALLOW_COPY_AND_ASSIGN(OmniboxControllerTest);
41};
42
Colin Blundell8e4cb8f2018-05-07 20:37:0043OmniboxControllerTest::OmniboxControllerTest() {}
[email protected]93ef5f02013-04-26 02:36:2544
Colin Blundell8e4cb8f2018-05-07 20:37:0045OmniboxControllerTest::~OmniboxControllerTest() {}
[email protected]93ef5f02013-04-26 02:36:2546
47void OmniboxControllerTest::CreateController() {
blundell3c100ce2015-07-29 18:16:2048 DCHECK(omnibox_client_);
Justin Donnellyce9d1bf2017-08-10 16:24:5549 omnibox_controller_ =
Jinho Bangcc280792018-01-17 23:33:5550 std::make_unique<OmniboxController>(nullptr, omnibox_client_.get());
[email protected]93ef5f02013-04-26 02:36:2551}
52
53// Checks that the list of autocomplete providers used by the OmniboxController
54// matches the one in the |expected_providers| bit field.
55void OmniboxControllerTest::AssertProviders(int expected_providers) {
[email protected]f8735432014-07-09 07:35:2456 const AutocompleteController::Providers& providers =
57 GetAutocompleteProviders();
[email protected]93ef5f02013-04-26 02:36:2558
[email protected]f8735432014-07-09 07:35:2459 for (size_t i = 0; i < providers.size(); ++i) {
[email protected]93ef5f02013-04-26 02:36:2560 // Ensure this is a provider we wanted.
[email protected]f8735432014-07-09 07:35:2461 int type = providers[i]->type();
[email protected]93ef5f02013-04-26 02:36:2562 ASSERT_TRUE(expected_providers & type);
63
64 // Remove it from expectations so we fail if it's there twice.
65 expected_providers &= ~type;
66 }
67
68 // Ensure we saw all the providers we expected.
69 ASSERT_EQ(0, expected_providers);
70}
71
blundell3c100ce2015-07-29 18:16:2072void OmniboxControllerTest::SetUp() {
Jinho Bangcc280792018-01-17 23:33:5573 omnibox_client_ = std::make_unique<TestOmniboxClient>();
blundell3c100ce2015-07-29 18:16:2074}
75
76void OmniboxControllerTest::TearDown() {
77 omnibox_controller_.reset();
78 omnibox_client_.reset();
blundell3c100ce2015-07-29 18:16:2079}
80
[email protected]93ef5f02013-04-26 02:36:2581TEST_F(OmniboxControllerTest, CheckDefaultAutocompleteProviders) {
82 CreateController();
83 // First collect the basic providers.
84 int observed_providers = 0;
[email protected]f8735432014-07-09 07:35:2485 const AutocompleteController::Providers& providers =
86 GetAutocompleteProviders();
87 for (size_t i = 0; i < providers.size(); ++i)
88 observed_providers |= providers[i]->type();
[email protected]93ef5f02013-04-26 02:36:2589 // Ensure we have at least one provider.
90 ASSERT_NE(0, observed_providers);
91
[email protected]32e5b0d2013-06-22 17:08:3892 // Ensure instant extended includes all the provides in classic Chrome.
93 int providers_with_instant_extended = observed_providers;
[email protected]93ef5f02013-04-26 02:36:2594 // TODO(beaudoin): remove TYPE_SEARCH once it's no longer needed to pass
95 // the Instant suggestion through via FinalizeInstantQuery.
[email protected]93ef5f02013-04-26 02:36:2596 CreateController();
97 AssertProviders(providers_with_instant_extended);
[email protected]93ef5f02013-04-26 02:36:2598}