diff options
Diffstat (limited to 'service_unittest.cc')
-rw-r--r-- | service_unittest.cc | 53 |
1 files changed, 30 insertions, 23 deletions
diff --git a/service_unittest.cc b/service_unittest.cc index f53c84b..2f571ab 100644 --- a/service_unittest.cc +++ b/service_unittest.cc @@ -30,7 +30,10 @@ #include "dbus/apmanager/dbus-constants.h" #endif // __ANDROID__ +#include "apmanager/error.h" +#include "apmanager/fake_config_adaptor.h" #include "apmanager/mock_config.h" +#include "apmanager/mock_control.h" #include "apmanager/mock_dhcp_server.h" #include "apmanager/mock_dhcp_server_factory.h" #include "apmanager/mock_file_writer.h" @@ -42,6 +45,7 @@ using brillo::ProcessMock; using ::testing::_; using ::testing::Mock; using ::testing::Return; +using ::testing::ReturnNew; using ::testing::SetArgPointee; namespace { @@ -63,40 +67,45 @@ namespace apmanager { class ServiceTest : public testing::Test { public: ServiceTest() - : manager_(nullptr), - hostapd_monitor_(new MockHostapdMonitor()), - service_(&manager_, kServiceIdentifier) {} - + : manager_(&control_interface_), + hostapd_monitor_(new MockHostapdMonitor()) { + ON_CALL(control_interface_, CreateConfigAdaptorRaw()) + .WillByDefault(ReturnNew<FakeConfigAdaptor>()); + // Defer creation of Service object to allow ControlInterface to + // setup expectations for generating fake adaptors. + service_.reset(new Service(&manager_, kServiceIdentifier)); + } virtual void SetUp() { - service_.dhcp_server_factory_ = &dhcp_server_factory_; - service_.file_writer_ = &file_writer_; - service_.process_factory_ = &process_factory_; - service_.hostapd_monitor_.reset(hostapd_monitor_); + service_->dhcp_server_factory_ = &dhcp_server_factory_; + service_->file_writer_ = &file_writer_; + service_->process_factory_ = &process_factory_; + service_->hostapd_monitor_.reset(hostapd_monitor_); } bool StartService(brillo::ErrorPtr* error) { - return service_.StartInternal(error); + return service_->StartInternal(error); } void StartDummyProcess() { - service_.hostapd_process_.reset(new brillo::ProcessImpl); - service_.hostapd_process_->AddArg(kBinSleep); - service_.hostapd_process_->AddArg("12345"); - CHECK(service_.hostapd_process_->Start()); - LOG(INFO) << "DummyProcess: " << service_.hostapd_process_->pid(); + service_->hostapd_process_.reset(new brillo::ProcessImpl); + service_->hostapd_process_->AddArg(kBinSleep); + service_->hostapd_process_->AddArg("12345"); + CHECK(service_->hostapd_process_->Start()); + LOG(INFO) << "DummyProcess: " << service_->hostapd_process_->pid(); } void SetConfig(Config* config) { - service_.config_.reset(config); + service_->config_.reset(config); } protected: + MockControl control_interface_; MockManager manager_; MockDHCPServerFactory dhcp_server_factory_; MockFileWriter file_writer_; MockProcessFactory process_factory_; MockHostapdMonitor* hostapd_monitor_; - Service service_; + std::unique_ptr<Service> service_; }; MATCHER_P(IsServiceErrorStartingWith, message, "") { @@ -115,18 +124,16 @@ TEST_F(ServiceTest, StartWhenServiceAlreadyRunning) { } TEST_F(ServiceTest, StartWhenConfigFileFailed) { - MockConfig* config = new MockConfig(); + MockConfig* config = new MockConfig(&manager_); SetConfig(config); brillo::ErrorPtr error; EXPECT_CALL(*config, GenerateConfigFile(_, _)).WillOnce(Return(false)); EXPECT_FALSE(StartService(&error)); - EXPECT_THAT(error, IsServiceErrorStartingWith( - "Failed to generate config file")); } TEST_F(ServiceTest, StartSuccess) { - MockConfig* config = new MockConfig(); + MockConfig* config = new MockConfig(&manager_); SetConfig(config); // Setup mock DHCP server. @@ -154,7 +161,7 @@ TEST_F(ServiceTest, StartSuccess) { TEST_F(ServiceTest, StopWhenServiceNotRunning) { brillo::ErrorPtr error; - EXPECT_FALSE(service_.Stop(&error)); + EXPECT_FALSE(service_->Stop(&error)); EXPECT_THAT(error, IsServiceErrorStartingWith( "Service is not currently running")); } @@ -162,11 +169,11 @@ TEST_F(ServiceTest, StopWhenServiceNotRunning) { TEST_F(ServiceTest, StopSuccess) { StartDummyProcess(); - MockConfig* config = new MockConfig(); + MockConfig* config = new MockConfig(&manager_); SetConfig(config); brillo::ErrorPtr error; EXPECT_CALL(*config, ReleaseDevice()).Times(1); - EXPECT_TRUE(service_.Stop(&error)); + EXPECT_TRUE(service_->Stop(&error)); Mock::VerifyAndClearExpectations(config); } |