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 virtual KeyedService
* BuildServiceInstanceFor(
37 content::BrowserContext
* context
) const OVERRIDE
{
38 ADD_FAILURE() << "This isn't part of the tests!";
42 virtual void BrowserContextShutdown(content::BrowserContext
* context
)
44 fill_on_shutdown_
->push_back(name_
);
48 const std::string name_
;
49 std::vector
<std::string
>* fill_on_shutdown_
;
52 // Tests that we can deal with a single component.
53 TEST_F(BrowserContextDependencyManagerUnittests
, SingleCase
) {
54 TestService
service("service", shutdown_order(), manager());
56 manager()->DestroyBrowserContextServices(NULL
);
58 ASSERT_EQ(1U, shutdown_order()->size());
59 EXPECT_STREQ("service", (*shutdown_order())[0].c_str());
62 // Tests that we get a simple one component depends on the other case.
63 TEST_F(BrowserContextDependencyManagerUnittests
, SimpleDependency
) {
64 TestService
parent("parent", shutdown_order(), manager());
65 TestService
child("child", shutdown_order(), manager());
66 DependOn(&child
, &parent
);
68 manager()->DestroyBrowserContextServices(NULL
);
70 ASSERT_EQ(2U, shutdown_order()->size());
71 EXPECT_STREQ("child", (*shutdown_order())[0].c_str());
72 EXPECT_STREQ("parent", (*shutdown_order())[1].c_str());
75 // Tests two children, one parent
76 TEST_F(BrowserContextDependencyManagerUnittests
, TwoChildrenOneParent
) {
77 TestService
parent("parent", shutdown_order(), manager());
78 TestService
child1("child1", shutdown_order(), manager());
79 TestService
child2("child2", shutdown_order(), manager());
80 DependOn(&child1
, &parent
);
81 DependOn(&child2
, &parent
);
83 manager()->DestroyBrowserContextServices(NULL
);
85 ASSERT_EQ(3U, shutdown_order()->size());
86 EXPECT_STREQ("child2", (*shutdown_order())[0].c_str());
87 EXPECT_STREQ("child1", (*shutdown_order())[1].c_str());
88 EXPECT_STREQ("parent", (*shutdown_order())[2].c_str());
91 // Tests an M configuration
92 TEST_F(BrowserContextDependencyManagerUnittests
, MConfiguration
) {
93 TestService
parent1("parent1", shutdown_order(), manager());
94 TestService
parent2("parent2", shutdown_order(), manager());
96 TestService
child_of_1("child_of_1", shutdown_order(), manager());
97 DependOn(&child_of_1
, &parent1
);
99 TestService
child_of_12("child_of_12", shutdown_order(), manager());
100 DependOn(&child_of_12
, &parent1
);
101 DependOn(&child_of_12
, &parent2
);
103 TestService
child_of_2("child_of_2", shutdown_order(), manager());
104 DependOn(&child_of_2
, &parent2
);
106 manager()->DestroyBrowserContextServices(NULL
);
108 ASSERT_EQ(5U, shutdown_order()->size());
109 EXPECT_STREQ("child_of_2", (*shutdown_order())[0].c_str());
110 EXPECT_STREQ("child_of_12", (*shutdown_order())[1].c_str());
111 EXPECT_STREQ("child_of_1", (*shutdown_order())[2].c_str());
112 EXPECT_STREQ("parent2", (*shutdown_order())[3].c_str());
113 EXPECT_STREQ("parent1", (*shutdown_order())[4].c_str());
116 // Tests that it can deal with a simple diamond.
117 TEST_F(BrowserContextDependencyManagerUnittests
, DiamondConfiguration
) {
118 TestService
parent("parent", shutdown_order(), manager());
120 TestService
middle_row_1("middle_row_1", shutdown_order(), manager());
121 DependOn(&middle_row_1
, &parent
);
123 TestService
middle_row_2("middle_row_2", shutdown_order(), manager());
124 DependOn(&middle_row_2
, &parent
);
126 TestService
bottom("bottom", shutdown_order(), manager());
127 DependOn(&bottom
, &middle_row_1
);
128 DependOn(&bottom
, &middle_row_2
);
130 manager()->DestroyBrowserContextServices(NULL
);
132 ASSERT_EQ(4U, shutdown_order()->size());
133 EXPECT_STREQ("bottom", (*shutdown_order())[0].c_str());
134 EXPECT_STREQ("middle_row_2", (*shutdown_order())[1].c_str());
135 EXPECT_STREQ("middle_row_1", (*shutdown_order())[2].c_str());
136 EXPECT_STREQ("parent", (*shutdown_order())[3].c_str());
139 // A final test that works with a more complex graph.
140 TEST_F(BrowserContextDependencyManagerUnittests
, ComplexGraph
) {
141 TestService
everything_depends_on_me(
142 "everything_depends_on_me", shutdown_order(), manager());
144 TestService
intermediary_service(
145 "intermediary_service", shutdown_order(), manager());
146 DependOn(&intermediary_service
, &everything_depends_on_me
);
148 TestService
specialized_service(
149 "specialized_service", shutdown_order(), manager());
150 DependOn(&specialized_service
, &everything_depends_on_me
);
151 DependOn(&specialized_service
, &intermediary_service
);
153 TestService
other_root("other_root", shutdown_order(), manager());
155 TestService
other_intermediary(
156 "other_intermediary", shutdown_order(), manager());
157 DependOn(&other_intermediary
, &other_root
);
159 TestService
bottom("bottom", shutdown_order(), manager());
160 DependOn(&bottom
, &specialized_service
);
161 DependOn(&bottom
, &other_intermediary
);
163 manager()->DestroyBrowserContextServices(NULL
);
165 ASSERT_EQ(6U, shutdown_order()->size());
166 EXPECT_STREQ("bottom", (*shutdown_order())[0].c_str());
167 EXPECT_STREQ("specialized_service", (*shutdown_order())[1].c_str());
168 EXPECT_STREQ("other_intermediary", (*shutdown_order())[2].c_str());
169 EXPECT_STREQ("intermediary_service", (*shutdown_order())[3].c_str());
170 EXPECT_STREQ("other_root", (*shutdown_order())[4].c_str());
171 EXPECT_STREQ("everything_depends_on_me", (*shutdown_order())[5].c_str());