Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/FilterEngine.cpp

Issue 29366747: Issue 4657 - Add Acceptable Ads API (Closed)
Patch Set: address comment Created March 17, 2017, 10:09 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/FilterEngine.cpp ('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 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 578 matching lines...) Expand 10 before | Expand all | Expand 10 after
589 589
590 TEST_F(FilterEngineWithFreshFolder, LangAndAASubscriptionsAreChosenOnFirstRun) 590 TEST_F(FilterEngineWithFreshFolder, LangAndAASubscriptionsAreChosenOnFirstRun)
591 { 591 {
592 AppInfo appInfo; 592 AppInfo appInfo;
593 appInfo.locale = "zh"; 593 appInfo.locale = "zh";
594 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu s.org/easylistchina+easylist.txt"; 594 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu s.org/easylistchina+easylist.txt";
595 auto jsEngine = createJsEngine(appInfo); 595 auto jsEngine = createJsEngine(appInfo);
596 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); 596 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine);
597 const auto subscriptions = filterEngine->GetListedSubscriptions(); 597 const auto subscriptions = filterEngine->GetListedSubscriptions();
598 ASSERT_EQ(2u, subscriptions.size()); 598 ASSERT_EQ(2u, subscriptions.size());
599 const auto aaUrl = filterEngine->GetPref("subscriptions_exceptionsurl")->AsStr ing();
600 SubscriptionPtr aaSubscription; 599 SubscriptionPtr aaSubscription;
601 SubscriptionPtr langSubscription; 600 SubscriptionPtr langSubscription;
602 if (subscriptions[0]->GetProperty("url")->AsString() == aaUrl) 601 if (subscriptions[0]->IsAA())
603 { 602 {
604 aaSubscription = subscriptions[0]; 603 aaSubscription = subscriptions[0];
605 langSubscription = subscriptions[1]; 604 langSubscription = subscriptions[1];
606 } 605 } else if (subscriptions[1]->IsAA())
607 else if (subscriptions[1]->GetProperty("url")->AsString() == aaUrl)
608 { 606 {
609 aaSubscription = subscriptions[1]; 607 aaSubscription = subscriptions[1];
610 langSubscription = subscriptions[0]; 608 langSubscription = subscriptions[0];
611 } 609 }
612 ASSERT_NE(nullptr, aaSubscription); 610 ASSERT_NE(nullptr, aaSubscription);
613 ASSERT_NE(nullptr, langSubscription); 611 ASSERT_NE(nullptr, langSubscription);
614 EXPECT_EQ(aaUrl, aaSubscription->GetProperty("url")->AsString());
615 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url")->AsString( )); 612 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url")->AsString( ));
613 EXPECT_TRUE(filterEngine->IsAAEnabled());
616 } 614 }
617 615
618 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun) 616 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun)
619 { 617 {
620 auto jsEngine = createJsEngine(); 618 auto jsEngine = createJsEngine();
621 FilterEngine::CreationParameters createParams; 619 FilterEngine::CreationParameters createParams;
622 createParams.preconfiguredPrefs["first_run_subscription_auto_select"] = jsEngi ne->NewValue(false); 620 createParams.preconfiguredPrefs["first_run_subscription_auto_select"] = jsEngi ne->NewValue(false);
623 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine, createParams); 621 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine, createParams);
624 const auto subscriptions = filterEngine->GetListedSubscriptions(); 622 const auto subscriptions = filterEngine->GetListedSubscriptions();
625 EXPECT_EQ(0u, subscriptions.size()); 623 EXPECT_EQ(0u, subscriptions.size());
624 EXPECT_FALSE(filterEngine->IsAAEnabled());
626 } 625 }
626
627 namespace AA_ApiTest
628 {
629 const std::string kOtherSubscriptionUrl = "https://non-existing-subscription.t xt";
630 enum class AAStatus
631 {
632 absent,
633 enabled,
634 disabled_present
635 };
636
637 ::std::ostream& operator<<(std::ostream& os, AAStatus aaStatus)
638 {
639 switch (aaStatus)
640 {
641 case AAStatus::absent:
642 os << "absent";
643 break;
644 case AAStatus::enabled:
645 os << "enabled";
646 break;
647 case AAStatus::disabled_present:
648 os << "disabled_present";
649 break;
650 default:
651 ;
652 }
653 return os;
654 }
655
656 enum class Action
657 {
658 disable, enable, remove
659 };
660
661 ::std::ostream& operator<<(std::ostream& os, Action action)
662 {
663 switch (action)
664 {
665 case Action::disable:
666 os << "disable";
667 break;
668 case Action::enable:
669 os << "enable";
670 break;
671 case Action::remove:
672 os << "remove";
673 break;
674 default:
675 ;
676 }
677 return os;
678 }
679
680 struct Parameters
681 {
682 AAStatus initialAAStatus;
683 Action action;
684 AAStatus expectedAAStatus;
685 Parameters(AAStatus aaStatus, Action action)
686 {
687 // if `expect` is not called then no effect is expected, initialize it now
688 initialAAStatus = expectedAAStatus = aaStatus;
689 this->action = action;
690 }
691 Parameters& expect(AAStatus aaStatus)
692 {
693 expectedAAStatus = aaStatus;
694 return *this;
695 }
696 // it's merely to satisfy compiler (std::tuple requires default ctr) and
697 // testing internals even calls it.
698 Parameters()
699 {
700 }
701 };
702
703 // human readable printing for failed tests
704 ::std::ostream& operator<<(::std::ostream& os, const Parameters& params)
705 {
706 os << "initial AA: " << params.initialAAStatus
707 << " action: " << params.action
708 << " expected AA: " << params.expectedAAStatus;
709 return os;
710 }
711 class Test : public FilterEngineTest
712 , public ::testing::WithParamInterface<::testing::tuple<Parameters, /*number of other subscriptions*/uint8_t>>
713 {
714 public:
715 static std::vector<Parameters> VaryPossibleCases()
716 {
717 // AA API test matrix
718 // each column but other-subs is about AA subscription
719 // enabled exists other-subs action => expected
720 // => everywhere no effect on other subs
721 // 1.
722 // false false false disable => no effect
723 // false false false enable => add and enable
724 //
725 // false false true disable => no effect
726 // false false true enable => add and enable
727 // 2.
728 // false true false disable => no effect
729 // false true false enable => enable
730 //
731 // false true true disable => no effect
732 // false true true enable => enable
733 // 3.
734 // true true false disable => disable
735 // ture true false enable => no effect
736 //
737 // true true true disable => disable
738 // ture true true enable => no effect
739 // 4.
740 // false true false remove => remove
741 // false true true remove => remove
742 // ture true false remove => remove
743 // ture true true remove => remove
744 std::vector<Parameters> retValue;
745 // 1.
746 retValue.emplace_back(Parameters(AAStatus::absent, Action::disable));
747 retValue.emplace_back(Parameters(AAStatus::absent, Action::enable).expect( AAStatus::enabled));
748 // 2.
749 retValue.emplace_back(Parameters(AAStatus::disabled_present, Action::disab le));
750 retValue.emplace_back(Parameters(AAStatus::disabled_present, Action::enabl e).expect(AAStatus::enabled));
751 // 3.
752 retValue.emplace_back(Parameters(AAStatus::enabled, Action::disable).expec t(AAStatus::disabled_present));
753 retValue.emplace_back(Parameters(AAStatus::enabled, Action::enable));
754 // 4.
755 retValue.emplace_back(Parameters(AAStatus::disabled_present, Action::remov e).expect(AAStatus::absent));
756 retValue.emplace_back(Parameters(AAStatus::enabled, Action::remove).expect (AAStatus::absent));
757 // since AA should not affect other subscriptions, the number of other
758 // subscriptions is not specified here, it goes as another item in test pa rameters tuple.
759 return retValue;
760 }
761 protected:
762 void init(AAStatus aaStatus, uint8_t otherSubscriptionsNumber)
763 {
764 // for the sake of simplicity test only with one suplimentary subscription
765 ASSERT_TRUE(otherSubscriptionsNumber == 0u || otherSubscriptionsNumber == 1u);
766
767 // this method also tests the result of intermediate steps.
768
769 {
770 // no subscription (because of preconfigured prefs.json and patterns.ini ),
771 // though it should be enabled by default in a non-test environment, it' s tested in
772 // corresponding tests.
773 const auto subscriptions = filterEngine->GetListedSubscriptions();
774 EXPECT_EQ(0u, subscriptions.size()); // no any subscription including AA
775 EXPECT_FALSE(filterEngine->IsAAEnabled());
776 }
777 if (otherSubscriptionsNumber == 1u)
778 {
779 auto subscruption = filterEngine->GetSubscription(kOtherSubscriptionUrl) ;
780 ASSERT_TRUE(subscruption);
781 subscruption->AddToList();
782 const auto subscriptions = filterEngine->GetListedSubscriptions();
783 ASSERT_EQ(1u, subscriptions.size());
784 EXPECT_FALSE(subscriptions[0]->IsAA());
785 EXPECT_EQ(kOtherSubscriptionUrl, subscriptions[0]->GetProperty("url")->A sString());
786 }
787 if (isAASatusPresent(aaStatus))
788 {
789 filterEngine->SetAAEnabled(true); // add AA by enabling it
790 if (aaStatus == AAStatus::disabled_present)
791 {
792 filterEngine->SetAAEnabled(false);
793 }
794 testSubscriptionState(aaStatus, otherSubscriptionsNumber);
795 }
796 }
797 bool isAASatusPresent(AAStatus aaStatus)
798 {
799 return aaStatus != AAStatus::absent;
800 }
801 void testSubscriptionState(AAStatus aaStatus, int otherSubscriptionsNumber)
802 {
803 if (aaStatus == AAStatus::enabled)
804 EXPECT_TRUE(filterEngine->IsAAEnabled());
805 else
806 EXPECT_FALSE(filterEngine->IsAAEnabled());
807
808 SubscriptionPtr aaSubscription;
809 SubscriptionPtr otherSubscription;
810 const auto subscriptions = filterEngine->GetListedSubscriptions();
811 for (const auto& subscription : subscriptions)
812 {
813 (subscription->IsAA() ? aaSubscription : otherSubscription) = subscripti on;
814 }
815 if (otherSubscriptionsNumber == 1u)
816 {
817 if (isAASatusPresent(aaStatus))
818 {
819 EXPECT_EQ(2u, subscriptions.size());
820 EXPECT_TRUE(aaSubscription);
821 EXPECT_TRUE(otherSubscription);
822 }
823 else
824 {
825 EXPECT_EQ(1u, subscriptions.size());
826 EXPECT_FALSE(aaSubscription);
827 EXPECT_TRUE(otherSubscription);
828 }
829 }
830 else if (otherSubscriptionsNumber == 0u)
831 {
832 if (isAASatusPresent(aaStatus))
833 {
834 EXPECT_EQ(1u, subscriptions.size());
835 EXPECT_TRUE(aaSubscription);
836 EXPECT_FALSE(otherSubscription);
837 }
838 else
839 {
840 EXPECT_EQ(0u, subscriptions.size());
841 }
842 }
843 }
844 };
845
846 INSTANTIATE_TEST_CASE_P(AA_ApiTests, Test,
847 ::testing::Combine(::testing::ValuesIn(Test::VaryPossibleCases()), ::testing ::Values<uint8_t>(0, 1)));
848
849 TEST_P(Test, VaryPossibleCases) {
850 const auto parameter = ::testing::get<0>(GetParam());
851 uint8_t otherSubscriptionsNumber = ::testing::get<1>(GetParam());
852 init(parameter.initialAAStatus, otherSubscriptionsNumber);
853
854 if (parameter.action == Action::enable)
855 filterEngine->SetAAEnabled(true);
856 else if (parameter.action == Action::disable)
857 filterEngine->SetAAEnabled(false);
858 else if (parameter.action == Action::remove)
859 {
860 SubscriptionPtr aaSubscription;
861 for (const auto& subscription : filterEngine->GetListedSubscriptions())
862 {
863 if (subscription->IsAA())
864 {
865 aaSubscription = subscription;
866 break;
867 }
868 }
869 ASSERT_TRUE(aaSubscription);
870 aaSubscription->RemoveFromList();
871 }
872
873 testSubscriptionState(parameter.expectedAAStatus, otherSubscriptionsNumber);
874 }
875 }
OLDNEW
« no previous file with comments | « src/FilterEngine.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld