OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromecast/base/device_capabilities_impl.h" | 5 #include "chromecast/base/device_capabilities_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
| 8 #include <utility> |
8 | 9 |
9 #include "base/macros.h" | 10 #include "base/macros.h" |
10 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
12 #include "base/values.h" | 13 #include "base/values.h" |
13 #include "chromecast/base/serializers.h" | 14 #include "chromecast/base/serializers.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 namespace chromecast { | 18 namespace chromecast { |
(...skipping 20 matching lines...) Expand all Loading... |
38 // untouched. | 39 // untouched. |
39 FakeCapabilityManagerSimple(DeviceCapabilities* capabilities, | 40 FakeCapabilityManagerSimple(DeviceCapabilities* capabilities, |
40 const std::string& key, | 41 const std::string& key, |
41 scoped_ptr<base::Value> init_value, | 42 scoped_ptr<base::Value> init_value, |
42 bool accept_changes) | 43 bool accept_changes) |
43 : DeviceCapabilities::Validator(capabilities), | 44 : DeviceCapabilities::Validator(capabilities), |
44 key_(key), | 45 key_(key), |
45 accept_changes_(accept_changes) { | 46 accept_changes_(accept_changes) { |
46 capabilities->Register(key, this); | 47 capabilities->Register(key, this); |
47 if (init_value) | 48 if (init_value) |
48 SetValidatedValue(key, init_value.Pass()); | 49 SetValidatedValue(key, std::move(init_value)); |
49 } | 50 } |
50 | 51 |
51 // Unregisters itself as Validator. | 52 // Unregisters itself as Validator. |
52 ~FakeCapabilityManagerSimple() override { | 53 ~FakeCapabilityManagerSimple() override { |
53 capabilities()->Unregister(key_, this); | 54 capabilities()->Unregister(key_, this); |
54 } | 55 } |
55 | 56 |
56 void Validate(const std::string& path, | 57 void Validate(const std::string& path, |
57 scoped_ptr<base::Value> proposed_value) override { | 58 scoped_ptr<base::Value> proposed_value) override { |
58 ASSERT_TRUE(path.find(key_) == 0); | 59 ASSERT_TRUE(path.find(key_) == 0); |
59 if (accept_changes_) | 60 if (accept_changes_) |
60 SetValidatedValue(path, proposed_value.Pass()); | 61 SetValidatedValue(path, std::move(proposed_value)); |
61 } | 62 } |
62 | 63 |
63 private: | 64 private: |
64 const std::string key_; | 65 const std::string key_; |
65 const bool accept_changes_; | 66 const bool accept_changes_; |
66 }; | 67 }; |
67 | 68 |
68 // Used to test that capabilities/validator can be read and written in | 69 // Used to test that capabilities/validator can be read and written in |
69 // Validate() without encountering deadlocks/unexpected behavior. | 70 // Validate() without encountering deadlocks/unexpected behavior. |
70 class FakeCapabilityManagerComplex : public DeviceCapabilities::Validator { | 71 class FakeCapabilityManagerComplex : public DeviceCapabilities::Validator { |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 EXPECT_EQ(capabilities->GetValidator(key), manager); | 198 EXPECT_EQ(capabilities->GetValidator(key), manager); |
198 // Read Capability | 199 // Read Capability |
199 EXPECT_TRUE(base::Value::Equals(capabilities->GetCapability(key).get(), | 200 EXPECT_TRUE(base::Value::Equals(capabilities->GetCapability(key).get(), |
200 init_value.get())); | 201 init_value.get())); |
201 // Unregister | 202 // Unregister |
202 delete manager; | 203 delete manager; |
203 | 204 |
204 // Write capability again. Provides way of checking that this function | 205 // Write capability again. Provides way of checking that this function |
205 // ran and was successful. | 206 // ran and was successful. |
206 scoped_ptr<base::Value> new_value = GetSampleDefaultCapabilityNewValue(); | 207 scoped_ptr<base::Value> new_value = GetSampleDefaultCapabilityNewValue(); |
207 capabilities->SetCapability(key, new_value.Pass()); | 208 capabilities->SetCapability(key, std::move(new_value)); |
208 } | 209 } |
209 | 210 |
210 // See TestBasicOperations() comment. | 211 // See TestBasicOperations() comment. |
211 void AssertBasicOperationsSuccessful(const DeviceCapabilities* capabilities) { | 212 void AssertBasicOperationsSuccessful(const DeviceCapabilities* capabilities) { |
212 base::RunLoop().RunUntilIdle(); | 213 base::RunLoop().RunUntilIdle(); |
213 std::string key; | 214 std::string key; |
214 scoped_ptr<base::Value> init_value; | 215 scoped_ptr<base::Value> init_value; |
215 GetSampleDefaultCapability(&key, &init_value); | 216 GetSampleDefaultCapability(&key, &init_value); |
216 scoped_ptr<base::Value> value = capabilities->GetCapability(key); | 217 scoped_ptr<base::Value> value = capabilities->GetCapability(key); |
217 ASSERT_TRUE(value); | 218 ASSERT_TRUE(value); |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 FakeCapabilityManagerSimple manager(capabilities(), key, | 394 FakeCapabilityManagerSimple manager(capabilities(), key, |
394 init_value->CreateDeepCopy(), true); | 395 init_value->CreateDeepCopy(), true); |
395 | 396 |
396 // 2nd call | 397 // 2nd call |
397 capabilities()->SetCapability(key, GetSampleDefaultCapabilityNewValue()); | 398 capabilities()->SetCapability(key, GetSampleDefaultCapabilityNewValue()); |
398 | 399 |
399 // Observer should not get called when value does not change | 400 // Observer should not get called when value does not change |
400 capabilities()->SetCapability(key, GetSampleDefaultCapabilityNewValue()); | 401 capabilities()->SetCapability(key, GetSampleDefaultCapabilityNewValue()); |
401 | 402 |
402 // 3rd call | 403 // 3rd call |
403 capabilities()->SetCapability(key, init_value.Pass()); | 404 capabilities()->SetCapability(key, std::move(init_value)); |
404 base::RunLoop().RunUntilIdle(); | 405 base::RunLoop().RunUntilIdle(); |
405 } | 406 } |
406 | 407 |
407 // Test adding dynamic capabilities | 408 // Test adding dynamic capabilities |
408 TEST_F(DeviceCapabilitiesImplTest, SetCapabilityDynamic) { | 409 TEST_F(DeviceCapabilitiesImplTest, SetCapabilityDynamic) { |
409 std::string key; | 410 std::string key; |
410 scoped_ptr<base::Value> init_value; | 411 scoped_ptr<base::Value> init_value; |
411 GetSampleDynamicCapability(&key, &init_value); | 412 GetSampleDynamicCapability(&key, &init_value); |
412 | 413 |
413 ASSERT_FALSE(capabilities()->GetCapability(key)); | 414 ASSERT_FALSE(capabilities()->GetCapability(key)); |
(...skipping 14 matching lines...) Expand all Loading... |
428 *new_value)); | 429 *new_value)); |
429 } | 430 } |
430 | 431 |
431 // Tests that SetCapability() works with expanded paths when there is a | 432 // Tests that SetCapability() works with expanded paths when there is a |
432 // capability of type DictionaryValue. | 433 // capability of type DictionaryValue. |
433 TEST_F(DeviceCapabilitiesImplTest, SetCapabilityDictionary) { | 434 TEST_F(DeviceCapabilitiesImplTest, SetCapabilityDictionary) { |
434 std::string key("dummy_dictionary_key"); | 435 std::string key("dummy_dictionary_key"); |
435 scoped_ptr<base::Value> init_value = | 436 scoped_ptr<base::Value> init_value = |
436 DeserializeFromJson(kSampleDictionaryCapability); | 437 DeserializeFromJson(kSampleDictionaryCapability); |
437 ASSERT_TRUE(init_value); | 438 ASSERT_TRUE(init_value); |
438 FakeCapabilityManagerSimple manager(capabilities(), key, init_value.Pass(), | 439 FakeCapabilityManagerSimple manager(capabilities(), key, |
439 true); | 440 std::move(init_value), true); |
440 | 441 |
441 capabilities()->SetCapability( | 442 capabilities()->SetCapability( |
442 "dummy_dictionary_key.dummy_field_bool", | 443 "dummy_dictionary_key.dummy_field_bool", |
443 make_scoped_ptr(new base::FundamentalValue(false))); | 444 make_scoped_ptr(new base::FundamentalValue(false))); |
444 base::RunLoop().RunUntilIdle(); | 445 base::RunLoop().RunUntilIdle(); |
445 bool value_bool = true; | 446 bool value_bool = true; |
446 scoped_ptr<base::Value> value = | 447 scoped_ptr<base::Value> value = |
447 capabilities()->GetCapability("dummy_dictionary_key.dummy_field_bool"); | 448 capabilities()->GetCapability("dummy_dictionary_key.dummy_field_bool"); |
448 ASSERT_TRUE(value); | 449 ASSERT_TRUE(value); |
449 EXPECT_TRUE(value->GetAsBoolean(&value_bool)); | 450 EXPECT_TRUE(value->GetAsBoolean(&value_bool)); |
(...skipping 10 matching lines...) Expand all Loading... |
460 EXPECT_EQ(value_int, 100); | 461 EXPECT_EQ(value_int, 100); |
461 } | 462 } |
462 | 463 |
463 // Tests that SetCapability() works with expanded paths when there is a | 464 // Tests that SetCapability() works with expanded paths when there is a |
464 // capability of type DictionaryValue and invalid changes are proposed. | 465 // capability of type DictionaryValue and invalid changes are proposed. |
465 TEST_F(DeviceCapabilitiesImplTest, SetCapabilityDictionaryInvalid) { | 466 TEST_F(DeviceCapabilitiesImplTest, SetCapabilityDictionaryInvalid) { |
466 std::string key("dummy_dictionary_key"); | 467 std::string key("dummy_dictionary_key"); |
467 scoped_ptr<base::Value> init_value = | 468 scoped_ptr<base::Value> init_value = |
468 DeserializeFromJson(kSampleDictionaryCapability); | 469 DeserializeFromJson(kSampleDictionaryCapability); |
469 ASSERT_TRUE(init_value); | 470 ASSERT_TRUE(init_value); |
470 FakeCapabilityManagerSimple manager(capabilities(), key, init_value.Pass(), | 471 FakeCapabilityManagerSimple manager(capabilities(), key, |
471 false); | 472 std::move(init_value), false); |
472 | 473 |
473 capabilities()->SetCapability( | 474 capabilities()->SetCapability( |
474 "dummy_dictionary_key.dummy_field_bool", | 475 "dummy_dictionary_key.dummy_field_bool", |
475 make_scoped_ptr(new base::FundamentalValue(false))); | 476 make_scoped_ptr(new base::FundamentalValue(false))); |
476 base::RunLoop().RunUntilIdle(); | 477 base::RunLoop().RunUntilIdle(); |
477 bool value_bool = false; | 478 bool value_bool = false; |
478 scoped_ptr<base::Value> value = | 479 scoped_ptr<base::Value> value = |
479 capabilities()->GetCapability("dummy_dictionary_key.dummy_field_bool"); | 480 capabilities()->GetCapability("dummy_dictionary_key.dummy_field_bool"); |
480 ASSERT_TRUE(value); | 481 ASSERT_TRUE(value); |
481 EXPECT_TRUE(value->GetAsBoolean(&value_bool)); | 482 EXPECT_TRUE(value->GetAsBoolean(&value_bool)); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 capabilities()->SetCapability( | 565 capabilities()->SetCapability( |
565 "dummy_validate_key", make_scoped_ptr(new base::FundamentalValue(true))); | 566 "dummy_validate_key", make_scoped_ptr(new base::FundamentalValue(true))); |
566 base::RunLoop().RunUntilIdle(); | 567 base::RunLoop().RunUntilIdle(); |
567 | 568 |
568 // Check that FakeCapabilityManagerComplex::Validate() ran and that behavior | 569 // Check that FakeCapabilityManagerComplex::Validate() ran and that behavior |
569 // was successful | 570 // was successful |
570 AssertBasicOperationsSuccessful(capabilities()); | 571 AssertBasicOperationsSuccessful(capabilities()); |
571 } | 572 } |
572 | 573 |
573 } // namespace chromecast | 574 } // namespace chromecast |
OLD | NEW |