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

Side by Side Diff: dbus/values_util_unittest.cc

Issue 221393004: dbus/values_util.h: Add functions to append collection type values to message. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed keybuk@'s comment, unmarked old functions as deprecated as they are useful. Created 6 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « dbus/values_util.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "dbus/values_util.h" 5 #include "dbus/values_util.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/float_util.h" 9 #include "base/float_util.h"
10 #include "base/json/json_writer.h" 10 #include "base/json/json_writer.h"
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 ASSERT_TRUE(value.get() != NULL); 442 ASSERT_TRUE(value.get() != NULL);
443 EXPECT_TRUE(value->Equals(&kIntegerValue)); 443 EXPECT_TRUE(value->Equals(&kIntegerValue));
444 value.reset(PopDataAsValue(&reader)); 444 value.reset(PopDataAsValue(&reader));
445 ASSERT_TRUE(value.get() != NULL); 445 ASSERT_TRUE(value.get() != NULL);
446 EXPECT_TRUE(value->Equals(&kDoubleValue)); 446 EXPECT_TRUE(value->Equals(&kDoubleValue));
447 value.reset(PopDataAsValue(&reader)); 447 value.reset(PopDataAsValue(&reader));
448 ASSERT_TRUE(value.get() != NULL); 448 ASSERT_TRUE(value.get() != NULL);
449 EXPECT_TRUE(value->Equals(&kStringValue)); 449 EXPECT_TRUE(value->Equals(&kStringValue));
450 } 450 }
451 451
452 TEST(ValuesUtilTest, AppendValueDataBasicTypes) {
453 const base::FundamentalValue kBoolValue(false);
454 const base::FundamentalValue kIntegerValue(42);
455 const base::FundamentalValue kDoubleValue(4.2);
456 const base::StringValue kStringValue("string");
457
458 scoped_ptr<Response> response(Response::CreateEmpty());
459 MessageWriter writer(response.get());
460 AppendValueData(&writer, kBoolValue);
461 AppendValueData(&writer, kIntegerValue);
462 AppendValueData(&writer, kDoubleValue);
463 AppendValueData(&writer, kStringValue);
464
465 MessageReader reader(response.get());
466 scoped_ptr<base::Value> value;
467 value.reset(PopDataAsValue(&reader));
468 ASSERT_TRUE(value.get() != NULL);
469 EXPECT_TRUE(value->Equals(&kBoolValue));
470 value.reset(PopDataAsValue(&reader));
471 ASSERT_TRUE(value.get() != NULL);
472 EXPECT_TRUE(value->Equals(&kIntegerValue));
473 value.reset(PopDataAsValue(&reader));
474 ASSERT_TRUE(value.get() != NULL);
475 EXPECT_TRUE(value->Equals(&kDoubleValue));
476 value.reset(PopDataAsValue(&reader));
477 ASSERT_TRUE(value.get() != NULL);
478 EXPECT_TRUE(value->Equals(&kStringValue));
479 }
480
481 TEST(ValuesUtilTest, AppendValueDataAsVariantBasicTypes) {
482 const base::FundamentalValue kBoolValue(false);
483 const base::FundamentalValue kIntegerValue(42);
484 const base::FundamentalValue kDoubleValue(4.2);
485 const base::StringValue kStringValue("string");
486
487 scoped_ptr<Response> response(Response::CreateEmpty());
488 MessageWriter writer(response.get());
489 AppendValueDataAsVariant(&writer, kBoolValue);
490 AppendValueDataAsVariant(&writer, kIntegerValue);
491 AppendValueDataAsVariant(&writer, kDoubleValue);
492 AppendValueDataAsVariant(&writer, kStringValue);
493
494 MessageReader reader(response.get());
495 scoped_ptr<base::Value> value;
496 value.reset(PopDataAsValue(&reader));
497 ASSERT_TRUE(value.get() != NULL);
498 EXPECT_TRUE(value->Equals(&kBoolValue));
499 value.reset(PopDataAsValue(&reader));
500 ASSERT_TRUE(value.get() != NULL);
501 EXPECT_TRUE(value->Equals(&kIntegerValue));
502 value.reset(PopDataAsValue(&reader));
503 ASSERT_TRUE(value.get() != NULL);
504 EXPECT_TRUE(value->Equals(&kDoubleValue));
505 value.reset(PopDataAsValue(&reader));
506 ASSERT_TRUE(value.get() != NULL);
507 EXPECT_TRUE(value->Equals(&kStringValue));
508 }
509
510 TEST(ValuesUtilTest, AppendDictionary) {
511 // Set up the input dictionary.
512 const std::string kKey1 = "one";
513 const std::string kKey2 = "two";
514 const std::string kKey3 = "three";
515 const std::string kKey4 = "four";
516 const std::string kKey5 = "five";
517 const std::string kKey6 = "six";
518
519 const bool kBoolValue = true;
520 const int32 kInt32Value = -45;
521 const double kDoubleValue = 4.9;
522 const std::string kStringValue = "fifty";
523
524 base::ListValue* list_value = new base::ListValue();
525 list_value->AppendBoolean(kBoolValue);
526 list_value->AppendInteger(kInt32Value);
527
528 base::DictionaryValue* dictionary_value = new base::DictionaryValue();
529 dictionary_value->SetBoolean(kKey1, kBoolValue);
530 dictionary_value->SetInteger(kKey2, kDoubleValue);
531
532 base::DictionaryValue test_dictionary;
533 test_dictionary.SetBoolean(kKey1, kBoolValue);
534 test_dictionary.SetInteger(kKey2, kInt32Value);
535 test_dictionary.SetDouble(kKey3, kDoubleValue);
536 test_dictionary.SetString(kKey4, kStringValue);
537 test_dictionary.Set(kKey5, list_value); // takes ownership
538 test_dictionary.Set(kKey6, dictionary_value); // takes ownership
539
540 scoped_ptr<Response> response(Response::CreateEmpty());
541 MessageWriter writer(response.get());
542 AppendValueData(&writer, test_dictionary);
543 base::FundamentalValue int_value(kInt32Value);
544 AppendValueData(&writer, int_value);
545
546 // Read the data.
547 MessageReader reader(response.get());
548 scoped_ptr<base::Value> value;
549 value.reset(PopDataAsValue(&reader));
550 ASSERT_TRUE(value.get() != NULL);
551 EXPECT_TRUE(value->Equals(&test_dictionary));
552 value.reset(PopDataAsValue(&reader));
553 ASSERT_TRUE(value.get() != NULL);
554 EXPECT_TRUE(value->Equals(&int_value));
555 }
556
557 TEST(ValuesUtilTest, AppendDictionaryAsVariant) {
558 // Set up the input dictionary.
559 const std::string kKey1 = "one";
560 const std::string kKey2 = "two";
561 const std::string kKey3 = "three";
562 const std::string kKey4 = "four";
563 const std::string kKey5 = "five";
564 const std::string kKey6 = "six";
565
566 const bool kBoolValue = true;
567 const int32 kInt32Value = -45;
568 const double kDoubleValue = 4.9;
569 const std::string kStringValue = "fifty";
570
571 base::ListValue* list_value = new base::ListValue();
572 list_value->AppendBoolean(kBoolValue);
573 list_value->AppendInteger(kInt32Value);
574
575 base::DictionaryValue* dictionary_value = new base::DictionaryValue();
576 dictionary_value->SetBoolean(kKey1, kBoolValue);
577 dictionary_value->SetInteger(kKey2, kDoubleValue);
578
579 base::DictionaryValue test_dictionary;
580 test_dictionary.SetBoolean(kKey1, kBoolValue);
581 test_dictionary.SetInteger(kKey2, kInt32Value);
582 test_dictionary.SetDouble(kKey3, kDoubleValue);
583 test_dictionary.SetString(kKey4, kStringValue);
584 test_dictionary.Set(kKey5, list_value); // takes ownership
585 test_dictionary.Set(kKey6, dictionary_value); // takes ownership
586
587 scoped_ptr<Response> response(Response::CreateEmpty());
588 MessageWriter writer(response.get());
589 AppendValueDataAsVariant(&writer, test_dictionary);
590 base::FundamentalValue int_value(kInt32Value);
591 AppendValueData(&writer, int_value);
592
593 // Read the data.
594 MessageReader reader(response.get());
595 scoped_ptr<base::Value> value;
596 value.reset(PopDataAsValue(&reader));
597 ASSERT_TRUE(value.get() != NULL);
598 EXPECT_TRUE(value->Equals(&test_dictionary));
599 value.reset(PopDataAsValue(&reader));
600 ASSERT_TRUE(value.get() != NULL);
601 EXPECT_TRUE(value->Equals(&int_value));
602 }
603
604 TEST(ValuesUtilTest, AppendList) {
605 // Set up the input list.
606 const std::string kKey1 = "one";
607 const std::string kKey2 = "two";
608
609 const bool kBoolValue = true;
610 const int32 kInt32Value = -45;
611 const double kDoubleValue = 4.9;
612 const std::string kStringValue = "fifty";
613
614 base::ListValue* list_value = new base::ListValue();
615 list_value->AppendBoolean(kBoolValue);
616 list_value->AppendInteger(kInt32Value);
617
618 base::DictionaryValue* dictionary_value = new base::DictionaryValue();
619 dictionary_value->SetBoolean(kKey1, kBoolValue);
620 dictionary_value->SetInteger(kKey2, kDoubleValue);
621
622 base::ListValue test_list;
623 test_list.AppendBoolean(kBoolValue);
624 test_list.AppendInteger(kInt32Value);
625 test_list.AppendDouble(kDoubleValue);
626 test_list.AppendString(kStringValue);
627 test_list.Append(list_value); // takes ownership
628 test_list.Append(dictionary_value); // takes ownership
629
630 scoped_ptr<Response> response(Response::CreateEmpty());
631 MessageWriter writer(response.get());
632 AppendValueData(&writer, test_list);
633 base::FundamentalValue int_value(kInt32Value);
634 AppendValueData(&writer, int_value);
635
636 // Read the data.
637 MessageReader reader(response.get());
638 scoped_ptr<base::Value> value;
639 value.reset(PopDataAsValue(&reader));
640 ASSERT_TRUE(value.get() != NULL);
641 EXPECT_TRUE(value->Equals(&test_list));
642 value.reset(PopDataAsValue(&reader));
643 ASSERT_TRUE(value.get() != NULL);
644 EXPECT_TRUE(value->Equals(&int_value));
645 }
646
647 TEST(ValuesUtilTest, AppendListAsVariant) {
648 // Set up the input list.
649 const std::string kKey1 = "one";
650 const std::string kKey2 = "two";
651
652 const bool kBoolValue = true;
653 const int32 kInt32Value = -45;
654 const double kDoubleValue = 4.9;
655 const std::string kStringValue = "fifty";
656
657 base::ListValue* list_value = new base::ListValue();
658 list_value->AppendBoolean(kBoolValue);
659 list_value->AppendInteger(kInt32Value);
660
661 base::DictionaryValue* dictionary_value = new base::DictionaryValue();
662 dictionary_value->SetBoolean(kKey1, kBoolValue);
663 dictionary_value->SetInteger(kKey2, kDoubleValue);
664
665 base::ListValue test_list;
666 test_list.AppendBoolean(kBoolValue);
667 test_list.AppendInteger(kInt32Value);
668 test_list.AppendDouble(kDoubleValue);
669 test_list.AppendString(kStringValue);
670 test_list.Append(list_value); // takes ownership
671 test_list.Append(dictionary_value); // takes ownership
672
673 scoped_ptr<Response> response(Response::CreateEmpty());
674 MessageWriter writer(response.get());
675 AppendValueDataAsVariant(&writer, test_list);
676 base::FundamentalValue int_value(kInt32Value);
677 AppendValueData(&writer, int_value);
678
679 // Read the data.
680 MessageReader reader(response.get());
681 scoped_ptr<base::Value> value;
682 value.reset(PopDataAsValue(&reader));
683 ASSERT_TRUE(value.get() != NULL);
684 EXPECT_TRUE(value->Equals(&test_list));
685 value.reset(PopDataAsValue(&reader));
686 ASSERT_TRUE(value.get() != NULL);
687 EXPECT_TRUE(value->Equals(&int_value));
688 }
689
452 } // namespace dbus 690 } // namespace dbus
OLDNEW
« no previous file with comments | « dbus/values_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698