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: fix typo Created April 5, 2017, 4:53 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-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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 710 matching lines...) Expand 10 before | Expand all | Expand 10 after
721 721
722 TEST_F(FilterEngineWithFreshFolder, LangAndAASubscriptionsAreChosenOnFirstRun) 722 TEST_F(FilterEngineWithFreshFolder, LangAndAASubscriptionsAreChosenOnFirstRun)
723 { 723 {
724 AppInfo appInfo; 724 AppInfo appInfo;
725 appInfo.locale = "zh"; 725 appInfo.locale = "zh";
726 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu s.org/easylistchina+easylist.txt"; 726 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu s.org/easylistchina+easylist.txt";
727 auto jsEngine = createJsEngine(appInfo); 727 auto jsEngine = createJsEngine(appInfo);
728 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); 728 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine);
729 const auto subscriptions = filterEngine->GetListedSubscriptions(); 729 const auto subscriptions = filterEngine->GetListedSubscriptions();
730 ASSERT_EQ(2u, subscriptions.size()); 730 ASSERT_EQ(2u, subscriptions.size());
731 const auto aaUrl = filterEngine->GetPref("subscriptions_exceptionsurl")->AsStr ing();
732 SubscriptionPtr aaSubscription; 731 SubscriptionPtr aaSubscription;
733 SubscriptionPtr langSubscription; 732 SubscriptionPtr langSubscription;
734 if (subscriptions[0]->GetProperty("url")->AsString() == aaUrl) 733 if (subscriptions[0]->IsAA())
735 { 734 {
736 aaSubscription = subscriptions[0]; 735 aaSubscription = subscriptions[0];
737 langSubscription = subscriptions[1]; 736 langSubscription = subscriptions[1];
738 } 737 }
739 else if (subscriptions[1]->GetProperty("url")->AsString() == aaUrl) 738 else if (subscriptions[1]->IsAA())
740 { 739 {
741 aaSubscription = subscriptions[1]; 740 aaSubscription = subscriptions[1];
742 langSubscription = subscriptions[0]; 741 langSubscription = subscriptions[0];
743 } 742 }
744 ASSERT_NE(nullptr, aaSubscription); 743 ASSERT_NE(nullptr, aaSubscription);
745 ASSERT_NE(nullptr, langSubscription); 744 ASSERT_NE(nullptr, langSubscription);
746 EXPECT_EQ(aaUrl, aaSubscription->GetProperty("url")->AsString());
747 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url")->AsString( )); 745 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url")->AsString( ));
746 EXPECT_TRUE(filterEngine->IsAAEnabled());
748 } 747 }
749 748
750 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun) 749 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun)
751 { 750 {
752 auto jsEngine = createJsEngine(); 751 auto jsEngine = createJsEngine();
753 FilterEngine::CreationParameters createParams; 752 FilterEngine::CreationParameters createParams;
754 createParams.preconfiguredPrefs["first_run_subscription_auto_select"] = jsEngi ne->NewValue(false); 753 createParams.preconfiguredPrefs["first_run_subscription_auto_select"] = jsEngi ne->NewValue(false);
755 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine, createParams); 754 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine, createParams);
756 const auto subscriptions = filterEngine->GetListedSubscriptions(); 755 const auto subscriptions = filterEngine->GetListedSubscriptions();
757 EXPECT_EQ(0u, subscriptions.size()); 756 EXPECT_EQ(0u, subscriptions.size());
757 EXPECT_FALSE(filterEngine->IsAAEnabled());
758 } 758 }
759 759
760 namespace AA_ApiTest
761 {
762 const std::string kOtherSubscriptionUrl = "https://non-existing-subscription.t xt";
763 enum class AAStatus
764 {
765 absent,
766 enabled,
767 disabled_present
768 };
769
770 ::std::ostream& operator<<(std::ostream& os, AAStatus aaStatus)
771 {
772 switch (aaStatus)
773 {
774 case AAStatus::absent:
775 os << "absent";
776 break;
777 case AAStatus::enabled:
778 os << "enabled";
779 break;
780 case AAStatus::disabled_present:
781 os << "disabled_present";
782 break;
783 default:
784 ;
785 }
786 return os;
787 }
788
789 enum class Action
790 {
791 disable, enable, remove
792 };
793
794 ::std::ostream& operator<<(std::ostream& os, Action action)
795 {
796 switch (action)
797 {
798 case Action::disable:
799 os << "disable";
800 break;
801 case Action::enable:
802 os << "enable";
803 break;
804 case Action::remove:
805 os << "remove";
806 break;
807 default:
808 ;
809 }
810 return os;
811 }
812
813 struct Parameters
814 {
815 AAStatus initialAAStatus;
816 Action action;
817 AAStatus expectedAAStatus;
818 Parameters(AAStatus aaStatus, Action action)
819 {
820 // if `expect` is not called then no effect is expected, initialize it now
821 initialAAStatus = expectedAAStatus = aaStatus;
822 this->action = action;
823 }
824 Parameters& expect(AAStatus aaStatus)
825 {
826 expectedAAStatus = aaStatus;
827 return *this;
828 }
829 // it's merely to satisfy compiler (std::tuple requires default ctr) and
830 // testing internals even calls it.
831 Parameters()
832 {
833 }
834 };
835
836 // human readable printing for failed tests
837 ::std::ostream& operator<<(::std::ostream& os, const Parameters& params)
838 {
839 os << "initial AA: " << params.initialAAStatus
840 << " action: " << params.action
841 << " expected AA: " << params.expectedAAStatus;
842 return os;
843 }
844 class Test : public FilterEngineTest
845 , public ::testing::WithParamInterface<::testing::tuple<Parameters, /*number of other subscriptions*/uint8_t>>
846 {
847 public:
848 static std::vector<Parameters> VaryPossibleCases()
849 {
850 // AA API test matrix
851 // each column but other-subs is about AA subscription
852 // enabled exists other-subs action => expected
853 // => everywhere no effect on other subs
854 // 1.
855 // false false false disable => no effect
856 // false false false enable => add and enable
857 //
858 // false false true disable => no effect
859 // false false true enable => add and enable
860 // 2.
861 // false true false disable => no effect
862 // false true false enable => enable
863 //
864 // false true true disable => no effect
865 // false true true enable => enable
866 // 3.
867 // true true false disable => disable
868 // ture true false enable => no effect
869 //
870 // true true true disable => disable
871 // ture true true enable => no effect
872 // 4.
873 // false true false remove => remove
874 // false true true remove => remove
875 // ture true false remove => remove
876 // ture true true remove => remove
877 std::vector<Parameters> retValue;
878 // 1.
879 retValue.emplace_back(Parameters(AAStatus::absent, Action::disable));
880 retValue.emplace_back(Parameters(AAStatus::absent, Action::enable).expect( AAStatus::enabled));
881 // 2.
882 retValue.emplace_back(Parameters(AAStatus::disabled_present, Action::disab le));
883 retValue.emplace_back(Parameters(AAStatus::disabled_present, Action::enabl e).expect(AAStatus::enabled));
884 // 3.
885 retValue.emplace_back(Parameters(AAStatus::enabled, Action::disable).expec t(AAStatus::disabled_present));
886 retValue.emplace_back(Parameters(AAStatus::enabled, Action::enable));
887 // 4.
888 retValue.emplace_back(Parameters(AAStatus::disabled_present, Action::remov e).expect(AAStatus::absent));
889 retValue.emplace_back(Parameters(AAStatus::enabled, Action::remove).expect (AAStatus::absent));
890 // since AA should not affect other subscriptions, the number of other
891 // subscriptions is not specified here, it goes as another item in test pa rameters tuple.
892 return retValue;
893 }
894 protected:
895 void init(AAStatus aaStatus, uint8_t otherSubscriptionsNumber)
896 {
897 // for the sake of simplicity test only with one suplimentary subscription
898 ASSERT_TRUE(otherSubscriptionsNumber == 0u || otherSubscriptionsNumber == 1u);
899
900 // this method also tests the result of intermediate steps.
901
902 {
903 // no subscription (because of preconfigured prefs.json and patterns.ini ),
904 // though it should be enabled by default in a non-test environment, it' s tested in
905 // corresponding tests.
906 const auto subscriptions = filterEngine->GetListedSubscriptions();
907 EXPECT_EQ(0u, subscriptions.size()); // no any subscription including AA
908 EXPECT_FALSE(filterEngine->IsAAEnabled());
909 }
910 if (otherSubscriptionsNumber == 1u)
911 {
912 auto subscription = filterEngine->GetSubscription(kOtherSubscriptionUrl) ;
913 ASSERT_TRUE(subscription);
914 subscription->AddToList();
915 const auto subscriptions = filterEngine->GetListedSubscriptions();
916 ASSERT_EQ(1u, subscriptions.size());
917 EXPECT_FALSE(subscriptions[0]->IsAA());
918 EXPECT_EQ(kOtherSubscriptionUrl, subscriptions[0]->GetProperty("url")->A sString());
919 }
920 if (isAASatusPresent(aaStatus))
921 {
922 filterEngine->SetAAEnabled(true); // add AA by enabling it
923 if (aaStatus == AAStatus::disabled_present)
924 {
925 filterEngine->SetAAEnabled(false);
926 }
927 testSubscriptionState(aaStatus, otherSubscriptionsNumber);
928 }
929 }
930 bool isAASatusPresent(AAStatus aaStatus)
931 {
932 return aaStatus != AAStatus::absent;
933 }
934 void testSubscriptionState(AAStatus aaStatus, int otherSubscriptionsNumber)
935 {
936 if (aaStatus == AAStatus::enabled)
937 EXPECT_TRUE(filterEngine->IsAAEnabled());
938 else
939 EXPECT_FALSE(filterEngine->IsAAEnabled());
940
941 SubscriptionPtr aaSubscription;
942 SubscriptionPtr otherSubscription;
943 const auto subscriptions = filterEngine->GetListedSubscriptions();
944 for (const auto& subscription : subscriptions)
945 {
946 (subscription->IsAA() ? aaSubscription : otherSubscription) = subscripti on;
947 }
948 if (otherSubscriptionsNumber == 1u)
949 {
950 if (isAASatusPresent(aaStatus))
951 {
952 EXPECT_EQ(2u, subscriptions.size());
953 EXPECT_TRUE(aaSubscription);
954 EXPECT_TRUE(otherSubscription);
955 }
956 else
957 {
958 EXPECT_EQ(1u, subscriptions.size());
959 EXPECT_FALSE(aaSubscription);
960 EXPECT_TRUE(otherSubscription);
961 }
962 }
963 else if (otherSubscriptionsNumber == 0u)
964 {
965 if (isAASatusPresent(aaStatus))
966 {
967 EXPECT_EQ(1u, subscriptions.size());
968 EXPECT_TRUE(aaSubscription);
969 EXPECT_FALSE(otherSubscription);
970 }
971 else
972 {
973 EXPECT_EQ(0u, subscriptions.size());
974 }
975 }
976 }
977 };
978
979 INSTANTIATE_TEST_CASE_P(AA_ApiTests, Test,
980 ::testing::Combine(::testing::ValuesIn(Test::VaryPossibleCases()), ::testing ::Values<uint8_t>(0, 1)));
981
982 TEST_P(Test, VaryPossibleCases) {
983 const auto parameter = ::testing::get<0>(GetParam());
984 uint8_t otherSubscriptionsNumber = ::testing::get<1>(GetParam());
985 init(parameter.initialAAStatus, otherSubscriptionsNumber);
986
987 if (parameter.action == Action::enable)
988 filterEngine->SetAAEnabled(true);
989 else if (parameter.action == Action::disable)
990 filterEngine->SetAAEnabled(false);
991 else if (parameter.action == Action::remove)
992 {
993 SubscriptionPtr aaSubscription;
994 for (const auto& subscription : filterEngine->GetListedSubscriptions())
995 {
996 if (subscription->IsAA())
997 {
998 aaSubscription = subscription;
999 break;
1000 }
1001 }
1002 ASSERT_TRUE(aaSubscription);
1003 aaSubscription->RemoveFromList();
1004 }
1005
1006 testSubscriptionState(parameter.expectedAAStatus, otherSubscriptionsNumber);
1007 }
1008 }
1009
760 TEST_F(FilterEngineIsAllowedConnectionTest, AbsentCallbackAllowsUpdating) 1010 TEST_F(FilterEngineIsAllowedConnectionTest, AbsentCallbackAllowsUpdating)
761 { 1011 {
762 createParams.isConnectionAllowedCallback = FilterEngine::IsConnectionAllowedCa llback(); 1012 createParams.isConnectionAllowedCallback = FilterEngine::IsConnectionAllowedCa llback();
763 auto subscription = EnsureExampleSubscriptionAndForceUpdate(); 1013 auto subscription = EnsureExampleSubscriptionAndForceUpdate();
764 EXPECT_EQ("synchronize_ok", subscription->GetProperty("downloadStatus")->AsStr ing()); 1014 EXPECT_EQ("synchronize_ok", subscription->GetProperty("downloadStatus")->AsStr ing());
765 EXPECT_EQ(1u, subscription->GetProperty("filters")->AsList().size()); 1015 EXPECT_EQ(1u, subscription->GetProperty("filters")->AsList().size());
766 } 1016 }
767 1017
768 TEST_F(FilterEngineIsAllowedConnectionTest, AllowingCallbackAllowsUpdating) 1018 TEST_F(FilterEngineIsAllowedConnectionTest, AllowingCallbackAllowsUpdating)
769 { 1019 {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
836 filterEngine->SetAllowedConnectionType(&testConnection); 1086 filterEngine->SetAllowedConnectionType(&testConnection);
837 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB"); 1087 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB");
838 EXPECT_EQ("synchronize_ok", subscription->GetProperty("downloadStatus")->AsS tring()); 1088 EXPECT_EQ("synchronize_ok", subscription->GetProperty("downloadStatus")->AsS tring());
839 EXPECT_EQ(1u, subscription->GetProperty("filters")->AsList().size()); 1089 EXPECT_EQ(1u, subscription->GetProperty("filters")->AsList().size());
840 auto capturedConnectionTypes = this->capturedConnectionTypes.GetStrings(); 1090 auto capturedConnectionTypes = this->capturedConnectionTypes.GetStrings();
841 ASSERT_EQ(1u, capturedConnectionTypes.size()); 1091 ASSERT_EQ(1u, capturedConnectionTypes.size());
842 EXPECT_TRUE(capturedConnectionTypes[0].first); 1092 EXPECT_TRUE(capturedConnectionTypes[0].first);
843 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second); 1093 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second);
844 } 1094 }
845 } 1095 }
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