1 // Copyright 2014 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.
5 #include "components/keyed_service/content/browser_context_dependency_manager.h"
6 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 class BrowserContextDependencyManagerUnittests
: public ::testing::Test
{
11 // To get around class access:
12 void DependOn(BrowserContextKeyedServiceFactory
* child
,
13 BrowserContextKeyedServiceFactory
* parent
) {
14 child
->DependsOn(parent
);
17 BrowserContextDependencyManager
* manager() { return &dependency_manager_
; }
19 std::vector
<std::string
>* shutdown_order() { return &shutdown_order_
; }
22 BrowserContextDependencyManager dependency_manager_
;
24 std::vector
<std::string
> shutdown_order_
;
27 class TestService
: public BrowserContextKeyedServiceFactory
{
29 TestService(const std::string
& name
,
30 std::vector
<std::string
>* fill_on_shutdown
,
31 BrowserContextDependencyManager
* manager
)
32 : BrowserContextKeyedServiceFactory("TestService", manager
),
34 fill_on_shutdown_(fill_on_shutdown
) {}
36 KeyedService
* BuildServiceInstanceFor(
37 content::BrowserContext
* context
) const override
{
38 ADD_FAILURE() << "This isn't part of the tests!";
42 void BrowserContextShutdown(content::BrowserContext
* context
) override
{
43 fill_on_shutdown_
->push_back(name_
);
47 const std::string name_
;
48 std::vector
<std::string
>* fill_on_shutdown_
;
51 // Tests that we can deal with a single component.
52 TEST_F(BrowserContextDependencyManagerUnittests
, SingleCase
) {
53 TestService
service("service", shutdown_order(), manager());
55 manager()->DestroyBrowserContextServices(NULL
);
57 ASSERT_EQ(1U, shutdown_order()->size());
58 EXPECT_STREQ("service", (*shutdown_order())[0].c_str());
61 // Tests that we get a simple one component depends on the other case.
62 TEST_F(BrowserContextDependencyManagerUnittests
, SimpleDependency
) {
63 TestService
parent("parent", shutdown_order(), manager());
64 TestService
child("child", shutdown_order(), manager());
65 DependOn(&child
, &parent
);
67 manager()->DestroyBrowserContextServices(NULL
);
69 ASSERT_EQ(2U, shutdown_order()->size());
70 EXPECT_STREQ("child", (*shutdown_order())[0].c_str());
71 EXPECT_STREQ("parent", (*shutdown_order())[1].c_str());
74 // Tests two children, one parent
75 TEST_F(BrowserContextDependencyManagerUnittests
, TwoChildrenOneParent
) {
76 TestService
parent("parent", shutdown_order(), manager());
77 TestService
child1("child1", shutdown_order(), manager());
78 TestService
child2("child2", shutdown_order(), manager());
79 DependOn(&child1
, &parent
);
80 DependOn(&child2
, &parent
);
82 manager()->DestroyBrowserContextServices(NULL
);
84 ASSERT_EQ(3U, shutdown_order()->size());
85 EXPECT_STREQ("child2", (*shutdown_order())[0].c_str());
86 EXPECT_STREQ("child1", (*shutdown_order())[1].c_str());
87 EXPECT_STREQ("parent", (*shutdown_order())[2].c_str());
90 // Tests an M configuration
91 TEST_F(BrowserContextDependencyManagerUnittests
, MConfiguration
) {
92 TestService
parent1("parent1", shutdown_order(), manager());
93 TestService
parent2("parent2", shutdown_order(), manager());
95 TestService
child_of_1("child_of_1", shutdown_order(), manager());
96 DependOn(&child_of_1
, &parent1
);
98 TestService
child_of_12("child_of_12", shutdown_order(), manager());
99 DependOn(&child_of_12
, &parent1
);
100 DependOn(&child_of_12
, &parent2
);
102 TestService
child_of_2("child_of_2", shutdown_order(), manager());
103 DependOn(&child_of_2
, &parent2
);
105 manager()->DestroyBrowserContextServices(NULL
);
107 ASSERT_EQ(5U, shutdown_order()->size());
108 EXPECT_STREQ("child_of_2", (*shutdown_order())[0].c_str());
109 EXPECT_STREQ("child_of_12", (*shutdown_order())[1].c_str());
110 EXPECT_STREQ("child_of_1", (*shutdown_order())[2].c_str());
111 EXPECT_STREQ("parent2", (*shutdown_order())[3].c_str());
112 EXPECT_STREQ("parent1", (*shutdown_order())[4].c_str());
115 // Tests that it can deal with a simple diamond.
116 TEST_F(BrowserContextDependencyManagerUnittests
, DiamondConfiguration
) {
117 TestService
parent("parent", shutdown_order(), manager());
119 TestService
middle_row_1("middle_row_1", shutdown_order(), manager());
120 DependOn(&middle_row_1
, &parent
);
122 TestService
middle_row_2("middle_row_2", shutdown_order(), manager());
123 DependOn(&middle_row_2
, &parent
);
125 TestService
bottom("bottom", shutdown_order(), manager());
126 DependOn(&bottom
, &middle_row_1
);
127 DependOn(&bottom
, &middle_row_2
);
129 manager()->DestroyBrowserContextServices(NULL
);
131 ASSERT_EQ(4U, shutdown_order()->size());
132 EXPECT_STREQ("bottom", (*shutdown_order())[0].c_str());
133 EXPECT_STREQ("middle_row_2", (*shutdown_order())[1].c_str());
134 EXPECT_STREQ("middle_row_1", (*shutdown_order())[2].c_str());
135 EXPECT_STREQ("parent", (*shutdown_order())[3].c_str());
138 // A final test that works with a more complex graph.
139 TEST_F(BrowserContextDependencyManagerUnittests
, ComplexGraph
) {
140 TestService
everything_depends_on_me(
141 "everything_depends_on_me", shutdown_order(), manager());
143 TestService
intermediary_service(
144 "intermediary_service", shutdown_order(), manager());
145 DependOn(&intermediary_service
, &everything_depends_on_me
);
147 TestService
specialized_service(
148 "specialized_service", shutdown_order(), manager());
149 DependOn(&specialized_service
, &everything_depends_on_me
);
150 DependOn(&specialized_service
, &intermediary_service
);
152 TestService
other_root("other_root", shutdown_order(), manager());
154 TestService
other_intermediary(
155 "other_intermediary", shutdown_order(), manager());
156 DependOn(&other_intermediary
, &other_root
);
158 TestService
bottom("bottom", shutdown_order(), manager());
159 DependOn(&bottom
, &specialized_service
);
160 DependOn(&bottom
, &other_intermediary
);
162 manager()->DestroyBrowserContextServices(NULL
);
164 ASSERT_EQ(6U, shutdown_order()->size());
165 EXPECT_STREQ("bottom", (*shutdown_order())[0].c_str());
166 EXPECT_STREQ("specialized_service", (*shutdown_order())[1].c_str());
167 EXPECT_STREQ("other_intermediary", (*shutdown_order())[2].c_str());
168 EXPECT_STREQ("intermediary_service", (*shutdown_order())[3].c_str());
169 EXPECT_STREQ("other_root", (*shutdown_order())[4].c_str());
170 EXPECT_STREQ("everything_depends_on_me", (*shutdown_order())[5].c_str());