Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / browser / media / media_internals_unittest.cc
1 // Copyright (c) 2011 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 "content/browser/media/media_internals.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/json/json_reader.h"
10 #include "base/run_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "media/audio/audio_parameters.h"
14 #include "media/base/channel_layout.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace {
18 const int kTestComponentID = 0;
19 const char kTestDeviceID[] = "test-device-id";
20 }  // namespace
21
22 namespace content {
23
24 class MediaInternalsTest
25     : public testing::TestWithParam<media::AudioLogFactory::AudioComponent> {
26  public:
27   MediaInternalsTest()
28       : media_internals_(MediaInternals::GetInstance()),
29         update_cb_(base::Bind(&MediaInternalsTest::UpdateCallbackImpl,
30                               base::Unretained(this))),
31         test_params_(media::AudioParameters::AUDIO_PCM_LINEAR,
32                      media::CHANNEL_LAYOUT_MONO,
33                      48000,
34                      16,
35                      128),
36         test_component_(GetParam()),
37         audio_log_(media_internals_->CreateAudioLog(test_component_)) {
38     media_internals_->AddUpdateCallback(update_cb_);
39   }
40
41   virtual ~MediaInternalsTest() {
42     media_internals_->RemoveUpdateCallback(update_cb_);
43   }
44
45  protected:
46   // Extracts and deserializes the JSON update data; merges into |update_data_|.
47   void UpdateCallbackImpl(const base::string16& update) {
48     // Each update string looks like "<JavaScript Function Name>({<JSON>});", to
49     // use the JSON reader we need to strip out the JavaScript code.
50     std::string utf8_update = base::UTF16ToUTF8(update);
51     const std::string::size_type first_brace = utf8_update.find('{');
52     const std::string::size_type last_brace = utf8_update.rfind('}');
53     scoped_ptr<base::Value> output_value(base::JSONReader::Read(
54         utf8_update.substr(first_brace, last_brace - first_brace + 1)));
55     CHECK(output_value);
56
57     base::DictionaryValue* output_dict = NULL;
58     CHECK(output_value->GetAsDictionary(&output_dict));
59     update_data_.MergeDictionary(output_dict);
60   }
61
62   void ExpectInt(const std::string& key, int expected_value) {
63     int actual_value = 0;
64     ASSERT_TRUE(update_data_.GetInteger(key, &actual_value));
65     EXPECT_EQ(expected_value, actual_value);
66   }
67
68   void ExpectString(const std::string& key, const std::string& expected_value) {
69     std::string actual_value;
70     ASSERT_TRUE(update_data_.GetString(key, &actual_value));
71     EXPECT_EQ(expected_value, actual_value);
72   }
73
74   void ExpectStatus(const std::string& expected_value) {
75     ExpectString("status", expected_value);
76   }
77
78   TestBrowserThreadBundle thread_bundle_;
79   MediaInternals* const media_internals_;
80   MediaInternals::UpdateCallback update_cb_;
81   base::DictionaryValue update_data_;
82   const media::AudioParameters test_params_;
83   const media::AudioLogFactory::AudioComponent test_component_;
84   scoped_ptr<media::AudioLog> audio_log_;
85 };
86
87 TEST_P(MediaInternalsTest, AudioLogCreateStartStopErrorClose) {
88   audio_log_->OnCreated(
89       kTestComponentID, test_params_, kTestDeviceID);
90   base::RunLoop().RunUntilIdle();
91
92   ExpectString("channel_layout",
93                media::ChannelLayoutToString(test_params_.channel_layout()));
94   ExpectInt("sample_rate", test_params_.sample_rate());
95   ExpectInt("frames_per_buffer", test_params_.frames_per_buffer());
96   ExpectInt("channels", test_params_.channels());
97   ExpectInt("input_channels", test_params_.input_channels());
98   ExpectString("device_id", kTestDeviceID);
99   ExpectInt("component_id", kTestComponentID);
100   ExpectInt("component_type", test_component_);
101   ExpectStatus("created");
102
103   // Verify OnStarted().
104   audio_log_->OnStarted(kTestComponentID);
105   base::RunLoop().RunUntilIdle();
106   ExpectStatus("started");
107
108   // Verify OnStopped().
109   audio_log_->OnStopped(kTestComponentID);
110   base::RunLoop().RunUntilIdle();
111   ExpectStatus("stopped");
112
113   // Verify OnError().
114   const char kErrorKey[] = "error_occurred";
115   std::string no_value;
116   ASSERT_FALSE(update_data_.GetString(kErrorKey, &no_value));
117   audio_log_->OnError(kTestComponentID);
118   base::RunLoop().RunUntilIdle();
119   ExpectString(kErrorKey, "true");
120
121   // Verify OnClosed().
122   audio_log_->OnClosed(kTestComponentID);
123   base::RunLoop().RunUntilIdle();
124   ExpectStatus("closed");
125 }
126
127 TEST_P(MediaInternalsTest, AudioLogCreateClose) {
128   audio_log_->OnCreated(
129       kTestComponentID, test_params_, kTestDeviceID);
130   base::RunLoop().RunUntilIdle();
131   ExpectStatus("created");
132
133   audio_log_->OnClosed(kTestComponentID);
134   base::RunLoop().RunUntilIdle();
135   ExpectStatus("closed");
136 }
137
138 INSTANTIATE_TEST_CASE_P(
139     MediaInternalsTest, MediaInternalsTest, testing::Values(
140         media::AudioLogFactory::AUDIO_INPUT_CONTROLLER,
141         media::AudioLogFactory::AUDIO_OUTPUT_CONTROLLER,
142         media::AudioLogFactory::AUDIO_OUTPUT_STREAM));
143
144 }  // namespace content