OLD | NEW |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/callback.h" | 6 #include "base/callback.h" |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/prefs/pref_value_map.h" | 9 #include "base/prefs/pref_value_map.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 kTestPolicy, kTestPref, 0, 10, true); | 523 kTestPolicy, kTestPref, 0, 10, true); |
524 | 524 |
525 // Check that values lying in the accepted range are written to the pref. | 525 // Check that values lying in the accepted range are written to the pref. |
526 policy_map.Set(kTestPolicy, | 526 policy_map.Set(kTestPolicy, |
527 POLICY_LEVEL_MANDATORY, | 527 POLICY_LEVEL_MANDATORY, |
528 POLICY_SCOPE_USER, | 528 POLICY_SCOPE_USER, |
529 new base::FundamentalValue(0), | 529 new base::FundamentalValue(0), |
530 NULL); | 530 NULL); |
531 prefs.Clear(); | 531 prefs.Clear(); |
532 handler.ApplyPolicySettings(policy_map, &prefs); | 532 handler.ApplyPolicySettings(policy_map, &prefs); |
533 expected.reset(base::Value::CreateDoubleValue(0.0)); | 533 expected.reset(new base::FundamentalValue(0.0)); |
534 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 534 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
535 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 535 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
536 | 536 |
537 policy_map.Set(kTestPolicy, | 537 policy_map.Set(kTestPolicy, |
538 POLICY_LEVEL_MANDATORY, | 538 POLICY_LEVEL_MANDATORY, |
539 POLICY_SCOPE_USER, | 539 POLICY_SCOPE_USER, |
540 new base::FundamentalValue(5), | 540 new base::FundamentalValue(5), |
541 NULL); | 541 NULL); |
542 prefs.Clear(); | 542 prefs.Clear(); |
543 handler.ApplyPolicySettings(policy_map, &prefs); | 543 handler.ApplyPolicySettings(policy_map, &prefs); |
544 expected.reset(base::Value::CreateDoubleValue(0.05)); | 544 expected.reset(new base::FundamentalValue(0.05)); |
545 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 545 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
546 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 546 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
547 | 547 |
548 policy_map.Set(kTestPolicy, | 548 policy_map.Set(kTestPolicy, |
549 POLICY_LEVEL_MANDATORY, | 549 POLICY_LEVEL_MANDATORY, |
550 POLICY_SCOPE_USER, | 550 POLICY_SCOPE_USER, |
551 new base::FundamentalValue(10), | 551 new base::FundamentalValue(10), |
552 NULL); | 552 NULL); |
553 prefs.Clear(); | 553 prefs.Clear(); |
554 handler.ApplyPolicySettings(policy_map, &prefs); | 554 handler.ApplyPolicySettings(policy_map, &prefs); |
555 expected.reset(base::Value::CreateDoubleValue(0.1)); | 555 expected.reset(new base::FundamentalValue(0.1)); |
556 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 556 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
557 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 557 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
558 | 558 |
559 // Check that values lying outside the accepted range are clamped and written | 559 // Check that values lying outside the accepted range are clamped and written |
560 // to the pref. | 560 // to the pref. |
561 policy_map.Set(kTestPolicy, | 561 policy_map.Set(kTestPolicy, |
562 POLICY_LEVEL_MANDATORY, | 562 POLICY_LEVEL_MANDATORY, |
563 POLICY_SCOPE_USER, | 563 POLICY_SCOPE_USER, |
564 new base::FundamentalValue(-5), | 564 new base::FundamentalValue(-5), |
565 NULL); | 565 NULL); |
566 prefs.Clear(); | 566 prefs.Clear(); |
567 handler.ApplyPolicySettings(policy_map, &prefs); | 567 handler.ApplyPolicySettings(policy_map, &prefs); |
568 expected.reset(base::Value::CreateDoubleValue(0.0)); | 568 expected.reset(new base::FundamentalValue(0.0)); |
569 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 569 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
570 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 570 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
571 | 571 |
572 policy_map.Set(kTestPolicy, | 572 policy_map.Set(kTestPolicy, |
573 POLICY_LEVEL_MANDATORY, | 573 POLICY_LEVEL_MANDATORY, |
574 POLICY_SCOPE_USER, | 574 POLICY_SCOPE_USER, |
575 new base::FundamentalValue(15), | 575 new base::FundamentalValue(15), |
576 NULL); | 576 NULL); |
577 prefs.Clear(); | 577 prefs.Clear(); |
578 handler.ApplyPolicySettings(policy_map, &prefs); | 578 handler.ApplyPolicySettings(policy_map, &prefs); |
579 expected.reset(base::Value::CreateDoubleValue(0.1)); | 579 expected.reset(new base::FundamentalValue(0.1)); |
580 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 580 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
581 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 581 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
582 } | 582 } |
583 | 583 |
584 TEST(IntPercentageToDoublePolicyHandler, ApplyPolicySettingsDontClamp) { | 584 TEST(IntPercentageToDoublePolicyHandler, ApplyPolicySettingsDontClamp) { |
585 PolicyMap policy_map; | 585 PolicyMap policy_map; |
586 PrefValueMap prefs; | 586 PrefValueMap prefs; |
587 scoped_ptr<base::Value> expected; | 587 scoped_ptr<base::Value> expected; |
588 const base::Value* value; | 588 const base::Value* value; |
589 | 589 |
590 // This tests needs to modify an int policy. The exact policy used and its | 590 // This tests needs to modify an int policy. The exact policy used and its |
591 // semantics outside the test are irrelevant. | 591 // semantics outside the test are irrelevant. |
592 IntPercentageToDoublePolicyHandler handler( | 592 IntPercentageToDoublePolicyHandler handler( |
593 kTestPolicy, kTestPref, 0, 10, true); | 593 kTestPolicy, kTestPref, 0, 10, true); |
594 | 594 |
595 // Check that values lying in the accepted range are written to the pref. | 595 // Check that values lying in the accepted range are written to the pref. |
596 policy_map.Set(kTestPolicy, | 596 policy_map.Set(kTestPolicy, |
597 POLICY_LEVEL_MANDATORY, | 597 POLICY_LEVEL_MANDATORY, |
598 POLICY_SCOPE_USER, | 598 POLICY_SCOPE_USER, |
599 new base::FundamentalValue(0), | 599 new base::FundamentalValue(0), |
600 NULL); | 600 NULL); |
601 prefs.Clear(); | 601 prefs.Clear(); |
602 handler.ApplyPolicySettings(policy_map, &prefs); | 602 handler.ApplyPolicySettings(policy_map, &prefs); |
603 expected.reset(base::Value::CreateDoubleValue(0.0)); | 603 expected.reset(new base::FundamentalValue(0.0)); |
604 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 604 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
605 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 605 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
606 | 606 |
607 policy_map.Set(kTestPolicy, | 607 policy_map.Set(kTestPolicy, |
608 POLICY_LEVEL_MANDATORY, | 608 POLICY_LEVEL_MANDATORY, |
609 POLICY_SCOPE_USER, | 609 POLICY_SCOPE_USER, |
610 new base::FundamentalValue(5), | 610 new base::FundamentalValue(5), |
611 NULL); | 611 NULL); |
612 prefs.Clear(); | 612 prefs.Clear(); |
613 handler.ApplyPolicySettings(policy_map, &prefs); | 613 handler.ApplyPolicySettings(policy_map, &prefs); |
614 expected.reset(base::Value::CreateDoubleValue(0.05)); | 614 expected.reset(new base::FundamentalValue(0.05)); |
615 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 615 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
616 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 616 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
617 | 617 |
618 policy_map.Set(kTestPolicy, | 618 policy_map.Set(kTestPolicy, |
619 POLICY_LEVEL_MANDATORY, | 619 POLICY_LEVEL_MANDATORY, |
620 POLICY_SCOPE_USER, | 620 POLICY_SCOPE_USER, |
621 new base::FundamentalValue(10), | 621 new base::FundamentalValue(10), |
622 NULL); | 622 NULL); |
623 prefs.Clear(); | 623 prefs.Clear(); |
624 handler.ApplyPolicySettings(policy_map, &prefs); | 624 handler.ApplyPolicySettings(policy_map, &prefs); |
625 expected.reset(base::Value::CreateDoubleValue(0.1)); | 625 expected.reset(new base::FundamentalValue(0.1)); |
626 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 626 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
627 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); | 627 EXPECT_TRUE(base::Value::Equals(expected.get(), value)); |
628 } | 628 } |
629 | 629 |
630 TEST(SchemaValidatingPolicyHandlerTest, CheckAndGetValue) { | 630 TEST(SchemaValidatingPolicyHandlerTest, CheckAndGetValue) { |
631 std::string error; | 631 std::string error; |
632 static const char kSchemaJson[] = | 632 static const char kSchemaJson[] = |
633 "{" | 633 "{" |
634 " \"type\": \"object\"," | 634 " \"type\": \"object\"," |
635 " \"properties\": {" | 635 " \"properties\": {" |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
810 handler_recommended.ApplyPolicySettings(policy_map_mandatory, &prefs); | 810 handler_recommended.ApplyPolicySettings(policy_map_mandatory, &prefs); |
811 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref)); | 811 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref)); |
812 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref)); | 812 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref)); |
813 | 813 |
814 EXPECT_FALSE( | 814 EXPECT_FALSE( |
815 handler_none.CheckPolicySettings(policy_map_recommended, &errors)); | 815 handler_none.CheckPolicySettings(policy_map_recommended, &errors)); |
816 EXPECT_FALSE(errors.empty()); | 816 EXPECT_FALSE(errors.empty()); |
817 } | 817 } |
818 | 818 |
819 } // namespace policy | 819 } // namespace policy |
OLD | NEW |