Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1066)

Side by Side Diff: extensions/browser/api/media_perception_private/media_perception_api_manager_unittest.cc

Issue 2858353002: MediaPerceptionPrivate API impl and testing. (Closed)
Patch Set: Addressed comments and all tests passing. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 "extensions/browser/api/media_perception_private/media_perception_api_m anager.h"
6
7 #include "base/bind.h"
8 #include "base/memory/ptr_util.h"
9 #include "base/run_loop.h"
10 #include "chromeos/dbus/dbus_thread_manager.h"
11 #include "chromeos/dbus/fake_media_analytics_client.h"
12 #include "chromeos/dbus/fake_upstart_client.h"
13 #include "chromeos/dbus/media_analytics_client.h"
14 #include "chromeos/dbus/upstart_client.h"
15 #include "content/public/test/test_browser_context.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace media_perception = extensions::api::media_perception_private;
20
21 namespace extensions {
22
23 using CallbackStatus = MediaPerceptionAPIManager::CallbackStatus;
24
25 namespace {
26
27 void RecordStatusAndRunClosure(base::Closure quit_run_loop,
28 CallbackStatus* status,
29 CallbackStatus result_status,
30 media_perception::State result_state) {
31 *status = result_status;
32 quit_run_loop.Run();
33 }
34
35 CallbackStatus SetState(MediaPerceptionAPIManager* manager,
36 const media_perception::State& state) {
37 base::RunLoop run_loop;
38 CallbackStatus status;
39 manager->SetState(state, base::Bind(&RecordStatusAndRunClosure,
40 run_loop.QuitClosure(), &status));
41 run_loop.Run();
42 return status;
43 }
44
45 CallbackStatus GetState(MediaPerceptionAPIManager* manager) {
46 base::RunLoop run_loop;
47 CallbackStatus status;
48 manager->GetState(
49 base::Bind(&RecordStatusAndRunClosure, run_loop.QuitClosure(), &status));
50 run_loop.Run();
51 return status;
52 }
53
54 } // namespace
55
56 class MediaPerceptionAPIManagerTest : public testing::Test {
57 public:
58 MediaPerceptionAPIManagerTest()
59 : thread_bundle_(content::TestBrowserThreadBundle::DEFAULT) {}
60
61 void SetUp() override {
62 std::unique_ptr<chromeos::DBusThreadManagerSetter> dbus_setter =
63 chromeos::DBusThreadManager::GetSetterForTesting();
64 auto media_analytics_client =
65 base::MakeUnique<chromeos::FakeMediaAnalyticsClient>();
66 media_analytics_client_ = media_analytics_client.get();
67 dbus_setter->SetMediaAnalyticsClient(std::move(media_analytics_client));
68
69 auto upstart_client = base::MakeUnique<chromeos::FakeUpstartClient>();
70 upstart_client_ = upstart_client.get();
71 dbus_setter->SetUpstartClient(std::move(upstart_client));
72
73 manager_ = base::MakeUnique<MediaPerceptionAPIManager>(&browser_context_);
74 }
75
76 void TearDown() override {
77 // Need to make sure that the MediaPerceptionAPIManager is destructed before
78 // the DbusThreadManager.
79 manager_.reset();
80 chromeos::DBusThreadManager::Shutdown();
81 }
82
83 std::unique_ptr<MediaPerceptionAPIManager> manager_;
84 content::TestBrowserContext browser_context_;
85 content::TestBrowserThreadBundle thread_bundle_;
86
87 // Ownership of both is passed on to chromeos::DbusThreadManager.
88 chromeos::FakeMediaAnalyticsClient* media_analytics_client_;
89 chromeos::FakeUpstartClient* upstart_client_;
90
91 DISALLOW_COPY_AND_ASSIGN(MediaPerceptionAPIManagerTest);
92 };
93
94 TEST_F(MediaPerceptionAPIManagerTest, UpstartFailure) {
95 upstart_client_->SetStartMediaAnalyticsWillSucceed(false);
96 media_perception::State state;
97 state.status = media_perception::STATUS_RUNNING;
98 EXPECT_EQ(CallbackStatus::PROCESS_IDLE_ERROR,
99 SetState(manager_.get(), state));
100 }
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.
101
102 TEST_F(MediaPerceptionAPIManagerTest, MediaAnalyticsDbusError) {
103 media_perception::State state;
104 state.status = media_perception::STATUS_RUNNING;
105 EXPECT_EQ(CallbackStatus::SUCCESS, SetState(manager_.get(), state));
106 // Disable the functionality of the fake process.
107 media_analytics_client_->set_process_running(false);
108 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.
109 }
110
111 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698