Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / keyed_service / content / browser_context_dependency_manager_unittest.cc
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.
4
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"
8
9 class BrowserContextDependencyManagerUnittests : public ::testing::Test {
10  protected:
11   // To get around class access:
12   void DependOn(BrowserContextKeyedServiceFactory* child,
13                 BrowserContextKeyedServiceFactory* parent) {
14     child->DependsOn(parent);
15   }
16
17   BrowserContextDependencyManager* manager() { return &dependency_manager_; }
18
19   std::vector<std::string>* shutdown_order() { return &shutdown_order_; }
20
21  private:
22   BrowserContextDependencyManager dependency_manager_;
23
24   std::vector<std::string> shutdown_order_;
25 };
26
27 class TestService : public BrowserContextKeyedServiceFactory {
28  public:
29   TestService(const std::string& name,
30               std::vector<std::string>* fill_on_shutdown,
31               BrowserContextDependencyManager* manager)
32       : BrowserContextKeyedServiceFactory("TestService", manager),
33         name_(name),
34         fill_on_shutdown_(fill_on_shutdown) {}
35
36   KeyedService* BuildServiceInstanceFor(
37       content::BrowserContext* context) const override {
38     ADD_FAILURE() << "This isn't part of the tests!";
39     return NULL;
40   }
41
42   void BrowserContextShutdown(content::BrowserContext* context) override {
43     fill_on_shutdown_->push_back(name_);
44   }
45
46  private:
47   const std::string name_;
48   std::vector<std::string>* fill_on_shutdown_;
49 };
50
51 // Tests that we can deal with a single component.
52 TEST_F(BrowserContextDependencyManagerUnittests, SingleCase) {
53   TestService service("service", shutdown_order(), manager());
54
55   manager()->DestroyBrowserContextServices(NULL);
56
57   ASSERT_EQ(1U, shutdown_order()->size());
58   EXPECT_STREQ("service", (*shutdown_order())[0].c_str());
59 }
60
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);
66
67   manager()->DestroyBrowserContextServices(NULL);
68
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());
72 }
73
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);
81
82   manager()->DestroyBrowserContextServices(NULL);
83
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());
88 }
89
90 // Tests an M configuration
91 TEST_F(BrowserContextDependencyManagerUnittests, MConfiguration) {
92   TestService parent1("parent1", shutdown_order(), manager());
93   TestService parent2("parent2", shutdown_order(), manager());
94
95   TestService child_of_1("child_of_1", shutdown_order(), manager());
96   DependOn(&child_of_1, &parent1);
97
98   TestService child_of_12("child_of_12", shutdown_order(), manager());
99   DependOn(&child_of_12, &parent1);
100   DependOn(&child_of_12, &parent2);
101
102   TestService child_of_2("child_of_2", shutdown_order(), manager());
103   DependOn(&child_of_2, &parent2);
104
105   manager()->DestroyBrowserContextServices(NULL);
106
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());
113 }
114
115 // Tests that it can deal with a simple diamond.
116 TEST_F(BrowserContextDependencyManagerUnittests, DiamondConfiguration) {
117   TestService parent("parent", shutdown_order(), manager());
118
119   TestService middle_row_1("middle_row_1", shutdown_order(), manager());
120   DependOn(&middle_row_1, &parent);
121
122   TestService middle_row_2("middle_row_2", shutdown_order(), manager());
123   DependOn(&middle_row_2, &parent);
124
125   TestService bottom("bottom", shutdown_order(), manager());
126   DependOn(&bottom, &middle_row_1);
127   DependOn(&bottom, &middle_row_2);
128
129   manager()->DestroyBrowserContextServices(NULL);
130
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());
136 }
137
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());
142
143   TestService intermediary_service(
144       "intermediary_service", shutdown_order(), manager());
145   DependOn(&intermediary_service, &everything_depends_on_me);
146
147   TestService specialized_service(
148       "specialized_service", shutdown_order(), manager());
149   DependOn(&specialized_service, &everything_depends_on_me);
150   DependOn(&specialized_service, &intermediary_service);
151
152   TestService other_root("other_root", shutdown_order(), manager());
153
154   TestService other_intermediary(
155       "other_intermediary", shutdown_order(), manager());
156   DependOn(&other_intermediary, &other_root);
157
158   TestService bottom("bottom", shutdown_order(), manager());
159   DependOn(&bottom, &specialized_service);
160   DependOn(&bottom, &other_intermediary);
161
162   manager()->DestroyBrowserContextServices(NULL);
163
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());
171 }