 Chromium Code Reviews
 Chromium Code Reviews Issue 2858353002:
  MediaPerceptionPrivate API impl and testing.  (Closed)
    
  
    Issue 2858353002:
  MediaPerceptionPrivate API impl and testing.  (Closed) 
  | Index: extensions/browser/api/media_perception_private/media_perception_api_manager_unittest.cc | 
| diff --git a/extensions/browser/api/media_perception_private/media_perception_api_manager_unittest.cc b/extensions/browser/api/media_perception_private/media_perception_api_manager_unittest.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..be808348a17e44b7b3182230b045193d6eb9b760 | 
| --- /dev/null | 
| +++ b/extensions/browser/api/media_perception_private/media_perception_api_manager_unittest.cc | 
| @@ -0,0 +1,111 @@ | 
| +// Copyright 2017 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#include "extensions/browser/api/media_perception_private/media_perception_api_manager.h" | 
| + | 
| +#include "base/bind.h" | 
| +#include "base/memory/ptr_util.h" | 
| +#include "base/run_loop.h" | 
| +#include "chromeos/dbus/dbus_thread_manager.h" | 
| +#include "chromeos/dbus/fake_media_analytics_client.h" | 
| +#include "chromeos/dbus/fake_upstart_client.h" | 
| +#include "chromeos/dbus/media_analytics_client.h" | 
| +#include "chromeos/dbus/upstart_client.h" | 
| +#include "content/public/test/test_browser_context.h" | 
| +#include "content/public/test/test_browser_thread_bundle.h" | 
| +#include "testing/gtest/include/gtest/gtest.h" | 
| + | 
| +namespace media_perception = extensions::api::media_perception_private; | 
| + | 
| +namespace extensions { | 
| + | 
| +using CallbackStatus = MediaPerceptionAPIManager::CallbackStatus; | 
| + | 
| +namespace { | 
| + | 
| +void RecordStatusAndRunClosure(base::Closure quit_run_loop, | 
| + CallbackStatus* status, | 
| + CallbackStatus result_status, | 
| + media_perception::State result_state) { | 
| + *status = result_status; | 
| + quit_run_loop.Run(); | 
| +} | 
| + | 
| +CallbackStatus SetState(MediaPerceptionAPIManager* manager, | 
| + const media_perception::State& state) { | 
| + base::RunLoop run_loop; | 
| + CallbackStatus status; | 
| + manager->SetState(state, base::Bind(&RecordStatusAndRunClosure, | 
| + run_loop.QuitClosure(), &status)); | 
| + run_loop.Run(); | 
| + return status; | 
| +} | 
| + | 
| +CallbackStatus GetState(MediaPerceptionAPIManager* manager) { | 
| + base::RunLoop run_loop; | 
| + CallbackStatus status; | 
| + manager->GetState( | 
| + base::Bind(&RecordStatusAndRunClosure, run_loop.QuitClosure(), &status)); | 
| + run_loop.Run(); | 
| + return status; | 
| +} | 
| + | 
| +} // namespace | 
| + | 
| +class MediaPerceptionAPIManagerTest : public testing::Test { | 
| + public: | 
| + MediaPerceptionAPIManagerTest() | 
| + : thread_bundle_(content::TestBrowserThreadBundle::DEFAULT) {} | 
| + | 
| + void SetUp() override { | 
| + std::unique_ptr<chromeos::DBusThreadManagerSetter> dbus_setter = | 
| + chromeos::DBusThreadManager::GetSetterForTesting(); | 
| + auto media_analytics_client = | 
| + base::MakeUnique<chromeos::FakeMediaAnalyticsClient>(); | 
| + media_analytics_client_ = media_analytics_client.get(); | 
| + dbus_setter->SetMediaAnalyticsClient(std::move(media_analytics_client)); | 
| + | 
| + auto upstart_client = base::MakeUnique<chromeos::FakeUpstartClient>(); | 
| + upstart_client_ = upstart_client.get(); | 
| + dbus_setter->SetUpstartClient(std::move(upstart_client)); | 
| + | 
| + manager_ = base::MakeUnique<MediaPerceptionAPIManager>(&browser_context_); | 
| + } | 
| + | 
| + void TearDown() override { | 
| + // Need to make sure that the MediaPerceptionAPIManager is destructed before | 
| + // the DbusThreadManager. | 
| + manager_.reset(); | 
| + chromeos::DBusThreadManager::Shutdown(); | 
| + } | 
| + | 
| + std::unique_ptr<MediaPerceptionAPIManager> manager_; | 
| + content::TestBrowserContext browser_context_; | 
| + content::TestBrowserThreadBundle thread_bundle_; | 
| + | 
| + // Ownership of both is passed on to chromeos::DbusThreadManager. | 
| + chromeos::FakeMediaAnalyticsClient* media_analytics_client_; | 
| + chromeos::FakeUpstartClient* upstart_client_; | 
| + | 
| + DISALLOW_COPY_AND_ASSIGN(MediaPerceptionAPIManagerTest); | 
| +}; | 
| + | 
| +TEST_F(MediaPerceptionAPIManagerTest, UpstartFailure) { | 
| + upstart_client_->SetStartMediaAnalyticsWillSucceed(false); | 
| + media_perception::State state; | 
| + state.status = media_perception::STATUS_RUNNING; | 
| + EXPECT_EQ(CallbackStatus::PROCESS_IDLE_ERROR, | 
| + SetState(manager_.get(), state)); | 
| +} | 
| 
tbarzic
2017/05/17 21:04:42
Can you add a test for SetState/GetState while Ups
 
Luke Sorenson
2017/05/17 23:07:49
Done.
 | 
| + | 
| +TEST_F(MediaPerceptionAPIManagerTest, MediaAnalyticsDbusError) { | 
| + media_perception::State state; | 
| + state.status = media_perception::STATUS_RUNNING; | 
| + EXPECT_EQ(CallbackStatus::SUCCESS, SetState(manager_.get(), state)); | 
| + // Disable the functionality of the fake process. | 
| + media_analytics_client_->set_process_running(false); | 
| + EXPECT_EQ(CallbackStatus::DBUS_ERROR, GetState(manager_.get())); | 
| 
tbarzic
2017/05/17 21:04:42
test what happens with set state in this case.
 
Luke Sorenson
2017/05/17 23:07:49
Done.
 | 
| +} | 
| + | 
| +} // namespace extensions |