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

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 (added TestUpstartClient). 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/test_upstart_client.h"
15 #include "chromeos/dbus/upstart_client.h"
16 #include "content/public/test/test_browser_context.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace media_perception = extensions::api::media_perception_private;
21
22 namespace extensions {
23
24 using CallbackStatus = MediaPerceptionAPIManager::CallbackStatus;
25
26 namespace {
27
28 void StallStateCallback(CallbackStatus result_status,
29 media_perception::State result_state) {}
30
31 void RecordStatusAndRunClosure(base::Closure quit_run_loop,
32 CallbackStatus* status,
33 CallbackStatus result_status,
34 media_perception::State result_state) {
35 *status = result_status;
36 quit_run_loop.Run();
37 }
38
39 CallbackStatus SetState(MediaPerceptionAPIManager* manager,
tbarzic 2017/05/18 21:39:15 nit: consider renaming this to SetStateAndWaitForR
Luke Sorenson 2017/05/19 00:56:43 Done.
40 const media_perception::State& state) {
41 base::RunLoop run_loop;
42 CallbackStatus status;
43 manager->SetState(state, base::Bind(&RecordStatusAndRunClosure,
44 run_loop.QuitClosure(), &status));
45 run_loop.Run();
46 return status;
47 }
48
49 CallbackStatus GetState(MediaPerceptionAPIManager* manager) {
50 base::RunLoop run_loop;
51 CallbackStatus status;
52 manager->GetState(
53 base::Bind(&RecordStatusAndRunClosure, run_loop.QuitClosure(), &status));
54 run_loop.Run();
55 return status;
56 }
57
58 } // namespace
59
60 class MediaPerceptionAPIManagerTest : public testing::Test {
61 public:
62 MediaPerceptionAPIManagerTest()
63 : thread_bundle_(content::TestBrowserThreadBundle::DEFAULT) {}
64
65 void SetUp() override {
66 std::unique_ptr<chromeos::DBusThreadManagerSetter> dbus_setter =
67 chromeos::DBusThreadManager::GetSetterForTesting();
68 auto media_analytics_client =
69 base::MakeUnique<chromeos::FakeMediaAnalyticsClient>();
70 media_analytics_client_ = media_analytics_client.get();
71 dbus_setter->SetMediaAnalyticsClient(std::move(media_analytics_client));
72
73 auto upstart_client = base::MakeUnique<chromeos::TestUpstartClient>();
74 upstart_client_ = upstart_client.get();
75 dbus_setter->SetUpstartClient(std::move(upstart_client));
76
77 manager_ = base::MakeUnique<MediaPerceptionAPIManager>(&browser_context_);
78 }
79
80 void TearDown() override {
81 // Need to make sure that the MediaPerceptionAPIManager is destructed before
82 // the DbusThreadManager.
83 manager_.reset();
84 chromeos::DBusThreadManager::Shutdown();
85 }
86
87 std::unique_ptr<MediaPerceptionAPIManager> manager_;
88 content::TestBrowserContext browser_context_;
89 content::TestBrowserThreadBundle thread_bundle_;
90
91 // Ownership of both is passed on to chromeos::DbusThreadManager.
92 chromeos::FakeMediaAnalyticsClient* media_analytics_client_;
93 chromeos::TestUpstartClient* upstart_client_;
94
95 DISALLOW_COPY_AND_ASSIGN(MediaPerceptionAPIManagerTest);
96 };
97
98 TEST_F(MediaPerceptionAPIManagerTest, UpstartFailure) {
99 media_perception::State state;
100 state.status = media_perception::STATUS_RUNNING;
101 base::RunLoop run_loop;
102 CallbackStatus status;
103 manager_->SetState(state, base::Bind(&RecordStatusAndRunClosure,
104 run_loop.QuitClosure(), &status));
105 EXPECT_TRUE(upstart_client_->HandleNextUpstartRequest(false));
106 run_loop.Run();
107 EXPECT_EQ(CallbackStatus::PROCESS_IDLE_ERROR, status);
108 }
109
110 TEST_F(MediaPerceptionAPIManagerTest, UpstartStall) {
111 media_perception::State state;
112 state.status = media_perception::STATUS_RUNNING;
113 // This call will stall so we don't wait for it to get callback.
114 manager_->SetState(state, base::Bind(&StallStateCallback));
tbarzic 2017/05/18 21:39:15 Pass in RecordStatusAndRunClosure, and make sure t
Luke Sorenson 2017/05/19 00:56:43 Done.
115 EXPECT_EQ(CallbackStatus::PROCESS_LAUNCHING_ERROR, GetState(manager_.get()));
116 EXPECT_EQ(CallbackStatus::PROCESS_LAUNCHING_ERROR,
117 SetState(manager_.get(), state));
118 EXPECT_TRUE(upstart_client_->HandleNextUpstartRequest(true));
119 // Verify that after the slow start, things works as normal.
120 EXPECT_EQ(CallbackStatus::SUCCESS, GetState(manager_.get()));
121 state.status = media_perception::STATUS_SUSPENDED;
122 EXPECT_EQ(CallbackStatus::SUCCESS, SetState(manager_.get(), state));
123 }
124
125 TEST_F(MediaPerceptionAPIManagerTest, MediaAnalyticsDbusError) {
126 // Use FakeUpstartClient in this case because we don't need to override the
127 // StartMediaAnalytics behavior.
128 std::unique_ptr<chromeos::DBusThreadManagerSetter> dbus_setter =
129 chromeos::DBusThreadManager::GetSetterForTesting();
130 auto upstart_client = base::MakeUnique<chromeos::FakeUpstartClient>();
tbarzic 2017/05/18 21:39:15 Can you use TestUpstartClient here as well (but ha
Luke Sorenson 2017/05/19 00:56:43 Done.
131 dbus_setter->SetUpstartClient(std::move(upstart_client));
132
133 media_perception::State state;
134 state.status = media_perception::STATUS_RUNNING;
135 EXPECT_EQ(CallbackStatus::SUCCESS, SetState(manager_.get(), state));
136 // Disable the functionality of the fake process.
137 media_analytics_client_->set_process_running(false);
138 EXPECT_EQ(CallbackStatus::DBUS_ERROR, GetState(manager_.get()));
139 EXPECT_EQ(CallbackStatus::DBUS_ERROR, SetState(manager_.get(), state));
140 }
141
142 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698