OLD | NEW |
---|---|
(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 class MediaPerceptionAPIManagerTest : public testing::Test { | |
26 public: | |
27 MediaPerceptionAPIManagerTest() | |
28 : thread_bundle_(content::TestBrowserThreadBundle::DEFAULT) {} | |
29 | |
30 void SetUp() override { | |
31 browser_context_ = base::MakeUnique<content::TestBrowserContext>(); | |
32 | |
33 std::unique_ptr<chromeos::DBusThreadManagerSetter> dbus_setter = | |
34 chromeos::DBusThreadManager::GetSetterForTesting(); | |
35 auto media_analytics_client = | |
36 base::MakeUnique<chromeos::FakeMediaAnalyticsClient>(); | |
37 media_analytics_client_ = media_analytics_client.get(); | |
38 dbus_setter->SetMediaAnalyticsClient(std::move(media_analytics_client)); | |
39 | |
40 auto upstart_client = base::MakeUnique<chromeos::FakeUpstartClient>(); | |
41 upstart_client_ = upstart_client.get(); | |
42 dbus_setter->SetUpstartClient(std::move(upstart_client)); | |
43 | |
44 chromeos::DBusThreadManager::Initialize(); | |
tbarzic
2017/05/16 20:56:31
you don't need this :)
Luke Sorenson
2017/05/16 21:33:33
Done.
| |
45 manager_ = | |
46 base::MakeUnique<MediaPerceptionAPIManager>(browser_context_.get()); | |
47 } | |
48 | |
49 void TearDown() override { | |
50 // Need to make sure that the MediaPerceptionAPIManager is destructed before | |
51 // the DbusThreadManager. | |
52 manager_.reset(); | |
53 chromeos::DBusThreadManager::Shutdown(); | |
54 } | |
55 | |
56 std::unique_ptr<MediaPerceptionAPIManager> manager_; | |
57 std::unique_ptr<content::TestBrowserContext> browser_context_; | |
tbarzic
2017/05/16 20:56:31
content::TestBrowserContext browser_context_;
Luke Sorenson
2017/05/16 21:33:33
Done.
| |
58 | |
59 // Ownership of both is passed on to chromeos::DbusThreadManager. | |
60 chromeos::FakeMediaAnalyticsClient* media_analytics_client_; | |
61 chromeos::FakeUpstartClient* upstart_client_; | |
62 | |
63 content::TestBrowserThreadBundle thread_bundle_; | |
tbarzic
2017/05/16 20:56:30
nit: move up with browser_context
Luke Sorenson
2017/05/16 21:33:33
Done.
| |
64 }; | |
tbarzic
2017/05/16 20:56:31
Add DISALLOW_COPY_AND_ASSIGN
Luke Sorenson
2017/05/16 21:33:33
Done.
| |
65 | |
66 void SetStateUpstartFailureCallback(CallbackStatus status, | |
tbarzic
2017/05/16 20:56:31
move before test calss, and put it into an anonymo
Luke Sorenson
2017/05/16 21:33:33
Done.
| |
67 media_perception::State state) { | |
68 EXPECT_EQ(status, CallbackStatus::PROCESS_IDLE_ERROR); | |
69 } | |
70 | |
71 TEST_F(MediaPerceptionAPIManagerTest, UpstartFailure) { | |
72 upstart_client_->SetStartMediaAnalyticsWillSucceed(false); | |
73 media_perception::State state; | |
74 state.status = media_perception::STATUS_RUNNING; | |
75 manager_->SetState(state, base::Bind(&SetStateUpstartFailureCallback)); | |
tbarzic
2017/05/16 20:56:31
given that SetState is async you should use run lo
Luke Sorenson
2017/05/16 21:33:33
Done.
| |
76 } | |
77 | |
78 void SetStateSuccessCallback(const base::Closure& quit_run_loop, | |
79 CallbackStatus status, | |
80 media_perception::State state) { | |
81 EXPECT_EQ(status, CallbackStatus::SUCCESS); | |
82 EXPECT_EQ(state.status, media_perception::STATUS_RUNNING); | |
83 quit_run_loop.Run(); | |
84 } | |
85 | |
86 void GetStateFailureDbusCallback(CallbackStatus status, | |
87 media_perception::State state) { | |
88 EXPECT_EQ(status, CallbackStatus::DBUS_ERROR); | |
89 } | |
90 | |
91 TEST_F(MediaPerceptionAPIManagerTest, MediaAnalyticsDbusError) { | |
92 media_perception::State state; | |
93 state.status = media_perception::STATUS_RUNNING; | |
94 base::RunLoop run_loop; | |
95 manager_->SetState( | |
96 state, base::Bind(&SetStateSuccessCallback, run_loop.QuitClosure())); | |
97 run_loop.Run(); | |
98 media_analytics_client_->set_process_running(false); | |
99 manager_->GetState(base::Bind(&GetStateFailureDbusCallback)); | |
100 } | |
101 | |
102 } // namespace extensions | |
OLD | NEW |