public class ListPaginationBase {
public Integer listSize {get; set;}
public Integer pageSize {get; set;}
public Integer currentPageIndex {get; set;}
public List<Object> fullList {get; set;}
public void initializePagination(Integer listTotalSize, Integer singlePageSize) {
fullList = new List<Object>();
listSize = listTotalSize;
pageSize = singlePageSize;
currentPageIndex = 0;
}
public void initializePagination(List<Object> objectList, Integer singlePageSize) {
fullList = ObjectList;
listSize = fullList.size();
pageSize = singlePageSize;
currentPageIndex = 0;
}
public Integer getPageCount() {
//Returns the number of pages required to show all data given a page limit
//if page limit is not set or is equal to zero, then return 1; dont divide by zero
//is empty list then return 1
if (pageSize <= 0 || listSize <= 0)
return 1;
else
return (Integer) Math.ceil((Decimal)listSize / (Decimal)pageSize);
}
public Boolean getHasNext() {
//Returns true if the list has next pages
return currentPageIndex < (getPageCount() - 1);
}
public Boolean getHasPrevious() {
//Returns true if the list has previous pages
return currentPageIndex > 0;
}
public Integer getfirstRowIndex() {
//Returns the index of the first row to show in the PageBlockTable using the first attribute
return currentPageIndex * pageSize;
}
public List<Object> getObjectPageList() {
List<Object> returnList = new List<Object>();
for(Integer index = getFirstRowIndex(); index < getFirstRowIndex() + pageSize; index++) {
if (index < fullList.size()) {
returnList.add(fullList.get(index));
}
}
return returnList;
}
public PageReference firstAction() {
//Action fired when the user click the First Button
//Set the Page Index to Zero
if(getHasPrevious())
currentPageIndex = 0;
return null;
}
public PageReference lastAction() {
//Action fired when the user click the Last Button
//Set the Page Index to pages count - 1
if(getHasNext())
currentPageIndex = getPageCount() - 1;
return null;
}
public PageReference nextAction() {
//Action fired when the user click the Next Button
//increment the current page index by 1
if(getHasNext())
currentPageIndex++;
return null;
}
public PageReference previousAction() {
//Action fired when the user click the Last Button
//decrement the current page index by 1
if(getHasPrevious())
currentPageIndex--;
return null;
}
}
Test Class
@isTest (SeeAllData=true)
private class TESTAccountOpportunityTabExtension {
static testmethod void AccountOpportunityTabExtension_Test (){
test.StartTest();
Account acc = new Account(Name ='icrm testing acc');
insert acc;
opportunity opp = new opportunity ( Name='testing DIE 4/6/2015' , AccountId= acc.Id,StageName = 'Prospecting',
type= 'parts', Description= 'describe', Inconterms__c = 'FCA', CloseDate = System.today().addDays(30));
insert opp;
opportunity opps = new opportunity ( id = '0068E0000060EhbQAE', Name='testing DIE 4/6/2015' , AccountId= acc.Id,StageName = 'Prospecting',
type= 'Sales', Opportunity_Division__c = 'PRODUCT SUPPORT', Description= 'describe', Inconterms__c = 'FCA', CloseDate = System.today().addDays(30));
update opps;
opportunity Deltaopp = new opportunity ( id = '0068E000009BCivQAG', Name='testing DIE 4/6/2015' , AccountId= acc.Id,StageName = 'Prospecting',
type= 'Sales', Opportunity_Division__c = 'POWER SYSTEM-DELTA(PERKINS)', Description= 'describe', Inconterms__c = 'FCA', CloseDate = System.today().addDays(30));
update Deltaopp;
opportunity EnginePowerGenerationaopp = new opportunity ( id = '0068E000008Myy4QAC', Name='testing DIE 4/6/2015' , AccountId= acc.Id,StageName = 'Prospecting',
type= 'Sales', Opportunity_Division__c = 'ENGINE POWER GENERATION', Description= 'describe', Inconterms__c = 'FCA', CloseDate = System.today().addDays(30));
update EnginePowerGenerationaopp ;
opportunity Otheropp = new opportunity ( id = '0068E000009BDvgQAG', Name='testing DIE 4/6/2015' , AccountId= acc.Id,StageName = 'Prospecting',
type= 'Sales', Opportunity_Division__c = 'OTHERS MISC - INTERNAL', Description= 'describe', Inconterms__c = 'FCA', CloseDate = System.today().addDays(30));
update Otheropp ;
PageReference pref = Page.Opp_Tabs;
pref.getParameters().put('id', acc.id);
Test.setCurrentPage(pref);
ApexPages.StandardController sc = new ApexPages.StandardController(acc);
AccountOpportunityTabExtension mc = new AccountOpportunityTabExtension(sc);
PageReference result = mc.NewOpp();
System.assertNotEquals(null, result);
PageReference result2 = mc.sortConstructionPaginationList();
System.assertNotEquals(null, result);
PageReference result3 = mc.sortProductSupportPaginationList();
System.assertNotEquals(null, result);
PageReference result4 = mc.sortPowerSystemPaginationList();
System.assertNotEquals(null, result);
PageReference result5 = mc.sortDeltaPaginationList();
System.assertNotEquals(null, result);
PageReference result6 = mc.sortOtherPaginationList();
System.assertNotEquals(null, result);
//ListPaginationBase code Coverage
Integer currentPageIndex;
ListPaginationBase p =new ListPaginationBase();
p.listSize = 7;
currentPageIndex = 2;
Boolean test1 = p.getHasNext();
Boolean test2 = p.getHasprevious();
Integer test3 = p.getPageCount();
Integer test4 = p.getfirstRowIndex();
PageReference result10 = p.previousAction();
System.assertNotEquals(null, result);
PageReference result7 = p.firstAction();
System.assertNotEquals(null, result);
PageReference result8 = p.lastAction();
System.assertNotEquals(null, result);
PageReference result9 = p.nextAction();
System.assertNotEquals(null, result);
p.getObjectPageList();
p.getPageCount();
p.PreviousAction();
p.NextAction();
p.firstAction();
p.LastAction();
p.getHasPrevious();
p.getHasNext();
p.getfirstRowIndex();
test.stopTest();
}
public static testMethod void initializePagination() {
Account acc = new Account(id = '0018E00000By5vZ', Name ='icrm testing acc');
update acc;
opportunity opp = new opportunity ( Name='testing DIE 4/6/2015' , AccountId= acc.Id,StageName = 'Prospecting',
type= 'parts', Description= 'describe', Inconterms__c = 'FCA', CloseDate = System.today().addDays(30));
insert opp;
PageReference pageRef = Page.Opp_Tabs;
Test.setCurrentPage(pageRef);
ApexPages.StandardController sc = new ApexPages.StandardController(acc);
AccountOpportunityTabExtension mc = new AccountOpportunityTabExtension(sc);
ListPaginationBase p=new ListPaginationBase();
PageReference result = mc.NewOpp();
System.assertNotEquals(null, result);
PageReference result7 = p.firstAction();
System.assertNotEquals(null, result);
PageReference result8 = p.lastAction();
System.assertNotEquals(null, result);
PageReference result9 = p.nextAction();
System.assertNotEquals(null, result);
PageReference result10 = p.previousAction();
System.assertNotEquals(null, result);
p.NextAction();
p.firstAction();
p.LastAction();
p.PreviousAction();
p.getHasPrevious();
p.getHasNext();
p.getfirstRowIndex();
p.getObjectPageList();
p.listSize = 7;
}
}
Please try to update your test class like below
Let us know if this will help you@isTest
private class ListPaginationBaseTest
{
static testmethod void AccountOpportunityTabExtension_Test ()
{
Account acc = new Account(Name ='icrm testing acc');
insert acc;
List<Opportunity> lstOpp = new List<Opportunity>();
for(Integer i =1 ; i<=20 ;i++)
{
opportunity opp = new opportunity ( Name='testing DIE 4/6/2015'+i , AccountId= acc.Id,StageName = 'Prospecting',
type= 'parts', Description= 'describe', Inconterms__c = 'FCA', CloseDate = System.today().addDays(30) );
lstOpp.add(opp);
}
insert lstOpp;
test.StartTest();
ListPaginationBase p =new ListPaginationBase();
p.listSize = 20;
currentPageIndex = 1;
p.initializePagination(7,1);
p.initializePagination(lstOpp,5);
Boolean test1 = p.getHasNext();
Boolean test2 = p.getHasprevious();
Integer test3 = p.getPageCount();
Integer test4 = p.getfirstRowIndex();
p.getObjectPageList();
p.LastAction();
p.PreviousAction();
p.firstAction();
p.NextAction();
p.getfirstRowIndex();
test.stopTest();
}
}