Home | History | Annotate | Download | only in browser_context_keyed_service
      1 // Copyright (c) 2012 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 "testing/gtest/include/gtest/gtest.h"
      6 
      7 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
      8 #include "components/browser_context_keyed_service/browser_context_keyed_service_factory.h"
      9 
     10 class BrowserContextDependencyManagerUnittests : public ::testing::Test {
     11  protected:
     12   // To get around class access:
     13   void DependOn(BrowserContextKeyedServiceFactory* child,
     14                 BrowserContextKeyedServiceFactory* parent) {
     15     child->DependsOn(parent);
     16   }
     17 
     18   BrowserContextDependencyManager* manager() { return &dependency_manager_; }
     19 
     20   std::vector<std::string>* shutdown_order() { return &shutdown_order_; }
     21 
     22  private:
     23   BrowserContextDependencyManager dependency_manager_;
     24 
     25   std::vector<std::string> shutdown_order_;
     26 };
     27 
     28 class TestService : public BrowserContextKeyedServiceFactory {
     29  public:
     30   TestService(const std::string& name,
     31               std::vector<std::string>* fill_on_shutdown,
     32               BrowserContextDependencyManager* manager)
     33       : BrowserContextKeyedServiceFactory("TestService", manager),
     34         name_(name),
     35         fill_on_shutdown_(fill_on_shutdown) {
     36   }
     37 
     38   virtual BrowserContextKeyedService* BuildServiceInstanceFor(
     39       content::BrowserContext* context) const OVERRIDE {
     40     ADD_FAILURE() << "This isn't part of the tests!";
     41     return NULL;
     42   }
     43 
     44   virtual void BrowserContextShutdown(
     45       content::BrowserContext* context) OVERRIDE {
     46     fill_on_shutdown_->push_back(name_);
     47   }
     48 
     49  private:
     50   const std::string name_;
     51   std::vector<std::string>* fill_on_shutdown_;
     52 };
     53 
     54 // Tests that we can deal with a single component.
     55 TEST_F(BrowserContextDependencyManagerUnittests, SingleCase) {
     56   TestService service("service", shutdown_order(), manager());
     57 
     58   manager()->DestroyBrowserContextServices(NULL);
     59 
     60   ASSERT_EQ(1U, shutdown_order()->size());
     61   EXPECT_STREQ("service", (*shutdown_order())[0].c_str());
     62 }
     63 
     64 // Tests that we get a simple one component depends on the other case.
     65 TEST_F(BrowserContextDependencyManagerUnittests, SimpleDependency) {
     66   TestService parent("parent", shutdown_order(), manager());
     67   TestService child("child", shutdown_order(), manager());
     68   DependOn(&child, &parent);
     69 
     70   manager()->DestroyBrowserContextServices(NULL);
     71 
     72   ASSERT_EQ(2U, shutdown_order()->size());
     73   EXPECT_STREQ("child", (*shutdown_order())[0].c_str());
     74   EXPECT_STREQ("parent", (*shutdown_order())[1].c_str());
     75 }
     76 
     77 // Tests two children, one parent
     78 TEST_F(BrowserContextDependencyManagerUnittests, TwoChildrenOneParent) {
     79   TestService parent("parent", shutdown_order(), manager());
     80   TestService child1("child1", shutdown_order(), manager());
     81   TestService child2("child2", shutdown_order(), manager());
     82   DependOn(&child1, &parent);
     83   DependOn(&child2, &parent);
     84 
     85   manager()->DestroyBrowserContextServices(NULL);
     86 
     87   ASSERT_EQ(3U, shutdown_order()->size());
     88   EXPECT_STREQ("child2", (*shutdown_order())[0].c_str());
     89   EXPECT_STREQ("child1", (*shutdown_order())[1].c_str());
     90   EXPECT_STREQ("parent", (*shutdown_order())[2].c_str());
     91 }
     92 
     93 // Tests an M configuration
     94 TEST_F(BrowserContextDependencyManagerUnittests, MConfiguration) {
     95   TestService parent1("parent1", shutdown_order(), manager());
     96   TestService parent2("parent2", shutdown_order(), manager());
     97 
     98   TestService child_of_1("child_of_1", shutdown_order(), manager());
     99   DependOn(&child_of_1, &parent1);
    100 
    101   TestService child_of_12("child_of_12", shutdown_order(), manager());
    102   DependOn(&child_of_12, &parent1);
    103   DependOn(&child_of_12, &parent2);
    104 
    105   TestService child_of_2("child_of_2", shutdown_order(), manager());
    106   DependOn(&child_of_2, &parent2);
    107 
    108   manager()->DestroyBrowserContextServices(NULL);
    109 
    110   ASSERT_EQ(5U, shutdown_order()->size());
    111   EXPECT_STREQ("child_of_2", (*shutdown_order())[0].c_str());
    112   EXPECT_STREQ("child_of_12", (*shutdown_order())[1].c_str());
    113   EXPECT_STREQ("child_of_1", (*shutdown_order())[2].c_str());
    114   EXPECT_STREQ("parent2", (*shutdown_order())[3].c_str());
    115   EXPECT_STREQ("parent1", (*shutdown_order())[4].c_str());
    116 }
    117 
    118 // Tests that it can deal with a simple diamond.
    119 TEST_F(BrowserContextDependencyManagerUnittests, DiamondConfiguration) {
    120   TestService parent("parent", shutdown_order(), manager());
    121 
    122   TestService middle_row_1("middle_row_1", shutdown_order(), manager());
    123   DependOn(&middle_row_1, &parent);
    124 
    125   TestService middle_row_2("middle_row_2", shutdown_order(), manager());
    126   DependOn(&middle_row_2, &parent);
    127 
    128   TestService bottom("bottom", shutdown_order(), manager());
    129   DependOn(&bottom, &middle_row_1);
    130   DependOn(&bottom, &middle_row_2);
    131 
    132   manager()->DestroyBrowserContextServices(NULL);
    133 
    134   ASSERT_EQ(4U, shutdown_order()->size());
    135   EXPECT_STREQ("bottom", (*shutdown_order())[0].c_str());
    136   EXPECT_STREQ("middle_row_2", (*shutdown_order())[1].c_str());
    137   EXPECT_STREQ("middle_row_1", (*shutdown_order())[2].c_str());
    138   EXPECT_STREQ("parent", (*shutdown_order())[3].c_str());
    139 }
    140 
    141 // A final test that works with a more complex graph.
    142 TEST_F(BrowserContextDependencyManagerUnittests, ComplexGraph) {
    143   TestService everything_depends_on_me("everything_depends_on_me",
    144                                        shutdown_order(), manager());
    145 
    146   TestService intermediary_service("intermediary_service",
    147                                    shutdown_order(), manager());
    148   DependOn(&intermediary_service, &everything_depends_on_me);
    149 
    150   TestService specialized_service("specialized_service",
    151                                   shutdown_order(), manager());
    152   DependOn(&specialized_service, &everything_depends_on_me);
    153   DependOn(&specialized_service, &intermediary_service);
    154 
    155   TestService other_root("other_root", shutdown_order(), manager());
    156 
    157   TestService other_intermediary("other_intermediary",
    158                                  shutdown_order(), manager());
    159   DependOn(&other_intermediary, &other_root);
    160 
    161   TestService bottom("bottom", shutdown_order(), manager());
    162   DependOn(&bottom, &specialized_service);
    163   DependOn(&bottom, &other_intermediary);
    164 
    165   manager()->DestroyBrowserContextServices(NULL);
    166 
    167   ASSERT_EQ(6U, shutdown_order()->size());
    168   EXPECT_STREQ("bottom", (*shutdown_order())[0].c_str());
    169   EXPECT_STREQ("specialized_service", (*shutdown_order())[1].c_str());
    170   EXPECT_STREQ("other_intermediary", (*shutdown_order())[2].c_str());
    171   EXPECT_STREQ("intermediary_service", (*shutdown_order())[3].c_str());
    172   EXPECT_STREQ("other_root", (*shutdown_order())[4].c_str());
    173   EXPECT_STREQ("everything_depends_on_me", (*shutdown_order())[5].c_str());
    174 }
    175