[email protected] | 93ef5f0 | 2013-04-26 02:36:25 | [diff] [blame] | 1 | // 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 | |||||
avi | 655876a | 2015-12-25 07:18:15 | [diff] [blame] | 5 | #include <stddef.h> |
6 | |||||
Jinho Bang | cc28079 | 2018-01-17 23:33:55 | [diff] [blame] | 7 | #include <memory> |
8 | |||||
avi | 655876a | 2015-12-25 07:18:15 | [diff] [blame] | 9 | #include "base/macros.h" |
Carlos Caballero | 1f8f83f | 2018-11-28 17:30:13 | [diff] [blame] | 10 | #include "base/test/scoped_task_environment.h" |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 11 | #include "components/omnibox/browser/autocomplete_controller.h" |
12 | #include "components/omnibox/browser/autocomplete_provider.h" | ||||
blundell | 26a618e | 2015-07-31 11:16:45 | [diff] [blame] | 13 | #include "components/omnibox/browser/omnibox_client.h" |
blundell | 7dbd379 | 2015-08-05 15:14:19 | [diff] [blame] | 14 | #include "components/omnibox/browser/omnibox_controller.h" |
Tommy C. Li | 9649d2a4 | 2018-01-17 21:59:51 | [diff] [blame] | 15 | #include "components/omnibox/browser/test_omnibox_client.h" |
blundell | 47c6d8a | 2015-09-24 11:06:40 | [diff] [blame] | 16 | #include "components/sessions/core/session_id.h" |
[email protected] | 93ef5f0 | 2013-04-26 02:36:25 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
18 | |||||
blundell | 26a618e | 2015-07-31 11:16:45 | [diff] [blame] | 19 | class OmniboxControllerTest : public testing::Test { |
[email protected] | 93ef5f0 | 2013-04-26 02:36:25 | [diff] [blame] | 20 | protected: |
21 | OmniboxControllerTest(); | ||||
dcheng | 131bb7e6 | 2014-10-28 00:16:38 | [diff] [blame] | 22 | ~OmniboxControllerTest() override; |
[email protected] | 93ef5f0 | 2013-04-26 02:36:25 | [diff] [blame] | 23 | |
24 | void CreateController(); | ||||
25 | void AssertProviders(int expected_providers); | ||||
26 | |||||
[email protected] | f873543 | 2014-07-09 07:35:24 | [diff] [blame] | 27 | const AutocompleteController::Providers& GetAutocompleteProviders() const { |
[email protected] | 93ef5f0 | 2013-04-26 02:36:25 | [diff] [blame] | 28 | return omnibox_controller_->autocomplete_controller()->providers(); |
29 | } | ||||
30 | |||||
31 | private: | ||||
blundell | 26a618e | 2015-07-31 11:16:45 | [diff] [blame] | 32 | // testing::Test: |
blundell | 3c100ce | 2015-07-29 18:16:20 | [diff] [blame] | 33 | void SetUp() override; |
34 | void TearDown() override; | ||||
35 | |||||
Carlos Caballero | 1f8f83f | 2018-11-28 17:30:13 | [diff] [blame] | 36 | base::test::ScopedTaskEnvironment task_environment_; |
dcheng | 9603ab9 | 2016-04-08 04:17:32 | [diff] [blame] | 37 | std::unique_ptr<TestOmniboxClient> omnibox_client_; |
38 | std::unique_ptr<OmniboxController> omnibox_controller_; | ||||
[email protected] | 93ef5f0 | 2013-04-26 02:36:25 | [diff] [blame] | 39 | |
40 | DISALLOW_COPY_AND_ASSIGN(OmniboxControllerTest); | ||||
41 | }; | ||||
42 | |||||
Colin Blundell | 8e4cb8f | 2018-05-07 20:37:00 | [diff] [blame] | 43 | OmniboxControllerTest::OmniboxControllerTest() {} |
[email protected] | 93ef5f0 | 2013-04-26 02:36:25 | [diff] [blame] | 44 | |
Colin Blundell | 8e4cb8f | 2018-05-07 20:37:00 | [diff] [blame] | 45 | OmniboxControllerTest::~OmniboxControllerTest() {} |
[email protected] | 93ef5f0 | 2013-04-26 02:36:25 | [diff] [blame] | 46 | |
47 | void OmniboxControllerTest::CreateController() { | ||||
blundell | 3c100ce | 2015-07-29 18:16:20 | [diff] [blame] | 48 | DCHECK(omnibox_client_); |
Justin Donnelly | ce9d1bf | 2017-08-10 16:24:55 | [diff] [blame] | 49 | omnibox_controller_ = |
Jinho Bang | cc28079 | 2018-01-17 23:33:55 | [diff] [blame] | 50 | std::make_unique<OmniboxController>(nullptr, omnibox_client_.get()); |
[email protected] | 93ef5f0 | 2013-04-26 02:36:25 | [diff] [blame] | 51 | } |
52 | |||||
53 | // Checks that the list of autocomplete providers used by the OmniboxController | ||||
54 | // matches the one in the |expected_providers| bit field. | ||||
55 | void OmniboxControllerTest::AssertProviders(int expected_providers) { | ||||
[email protected] | f873543 | 2014-07-09 07:35:24 | [diff] [blame] | 56 | const AutocompleteController::Providers& providers = |
57 | GetAutocompleteProviders(); | ||||
[email protected] | 93ef5f0 | 2013-04-26 02:36:25 | [diff] [blame] | 58 | |
[email protected] | f873543 | 2014-07-09 07:35:24 | [diff] [blame] | 59 | for (size_t i = 0; i < providers.size(); ++i) { |
[email protected] | 93ef5f0 | 2013-04-26 02:36:25 | [diff] [blame] | 60 | // Ensure this is a provider we wanted. |
[email protected] | f873543 | 2014-07-09 07:35:24 | [diff] [blame] | 61 | int type = providers[i]->type(); |
[email protected] | 93ef5f0 | 2013-04-26 02:36:25 | [diff] [blame] | 62 | 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 | |||||
blundell | 3c100ce | 2015-07-29 18:16:20 | [diff] [blame] | 72 | void OmniboxControllerTest::SetUp() { |
Jinho Bang | cc28079 | 2018-01-17 23:33:55 | [diff] [blame] | 73 | omnibox_client_ = std::make_unique<TestOmniboxClient>(); |
blundell | 3c100ce | 2015-07-29 18:16:20 | [diff] [blame] | 74 | } |
75 | |||||
76 | void OmniboxControllerTest::TearDown() { | ||||
77 | omnibox_controller_.reset(); | ||||
78 | omnibox_client_.reset(); | ||||
blundell | 3c100ce | 2015-07-29 18:16:20 | [diff] [blame] | 79 | } |
80 | |||||
[email protected] | 93ef5f0 | 2013-04-26 02:36:25 | [diff] [blame] | 81 | TEST_F(OmniboxControllerTest, CheckDefaultAutocompleteProviders) { |
82 | CreateController(); | ||||
83 | // First collect the basic providers. | ||||
84 | int observed_providers = 0; | ||||
[email protected] | f873543 | 2014-07-09 07:35:24 | [diff] [blame] | 85 | const AutocompleteController::Providers& providers = |
86 | GetAutocompleteProviders(); | ||||
87 | for (size_t i = 0; i < providers.size(); ++i) | ||||
88 | observed_providers |= providers[i]->type(); | ||||
[email protected] | 93ef5f0 | 2013-04-26 02:36:25 | [diff] [blame] | 89 | // Ensure we have at least one provider. |
90 | ASSERT_NE(0, observed_providers); | ||||
91 | |||||
[email protected] | 32e5b0d | 2013-06-22 17:08:38 | [diff] [blame] | 92 | // Ensure instant extended includes all the provides in classic Chrome. |
93 | int providers_with_instant_extended = observed_providers; | ||||
[email protected] | 93ef5f0 | 2013-04-26 02:36:25 | [diff] [blame] | 94 | // TODO(beaudoin): remove TYPE_SEARCH once it's no longer needed to pass |
95 | // the Instant suggestion through via FinalizeInstantQuery. | ||||
[email protected] | 93ef5f0 | 2013-04-26 02:36:25 | [diff] [blame] | 96 | CreateController(); |
97 | AssertProviders(providers_with_instant_extended); | ||||
[email protected] | 93ef5f0 | 2013-04-26 02:36:25 | [diff] [blame] | 98 | } |