View Javadoc
1   package com.simpligility.maven.plugins.android.standalonemojos;
2   
3   import java.io.File;
4   import java.util.Arrays;
5   
6   import org.apache.maven.model.Build;
7   import org.apache.maven.project.MavenProject;
8   import org.easymock.EasyMock;
9   import org.junit.Ignore;
10  import org.junit.runner.RunWith;
11  import org.powermock.api.easymock.PowerMock;
12  import org.powermock.core.classloader.annotations.PrepareForTest;
13  import org.powermock.modules.junit4.PowerMockRunner;
14  import org.powermock.reflect.Whitebox;
15  
16  import com.simpligility.maven.plugins.android.AbstractAndroidMojoTestCase;
17  import com.simpligility.maven.plugins.android.CommandExecutor;
18  import com.simpligility.maven.plugins.android.config.ConfigHandler;
19  import com.simpligility.maven.plugins.android.standalonemojos.MonkeyMojo;
20  
21  /**
22   * Test the Monkey mojo. Tests options' default values and parsing. We do not test the command line that is passed to
23   * the adb bridge, it should be possible to mock it though.
24   * 
25   * @author Stéphane Nicolas - snicolas@octo.com
26   * 
27   */
28  @Ignore("This test has to be migrated to be an IntegrationTest using AbstractAndroidMojoIntegrationTest") 
29  @RunWith( PowerMockRunner.class )
30  @PrepareForTest(
31  { CommandExecutor.Factory.class, ConfigHandler.class } )
32  public class MonkeyMojoTest extends AbstractAndroidMojoTestCase< MonkeyMojo >
33  {
34      @Override
35      public String getPluginGoalName()
36      {
37          return "monkey";
38      }
39  
40      /**
41       * Tests all options, checks if their default values are correct.
42       * 
43       * @throws Exception
44       */
45      public void testDefaultMonkeyConfig() throws Exception
46      {
47          // given
48          MonkeyMojo mojo = createMojo( "monkey-config-project0" );
49          final ConfigHandler cfh = new ConfigHandler( mojo, this.session, this.execution );
50          cfh.parseConfiguration();
51  
52          // when
53          Boolean automatorSkip = Whitebox.getInternalState( mojo, "parsedSkip" );
54  
55          // then
56          assertTrue( "Monkey skip parameter should be true", automatorSkip );
57      }
58  
59      /**
60       * Tests all options, checks if their default values are correct.
61       * 
62       * @throws Exception
63       */
64      public void testDefaultUnskippedMonkeyConfig() throws Exception
65      {
66          // given
67          MonkeyMojo mojo = createMojo( "monkey-config-project1" );
68  
69          MavenProject project = EasyMock.createNiceMock( MavenProject.class );
70          Whitebox.setInternalState( mojo, "project", project );
71          File projectBaseDir = new File( getBasedir() );
72          Build projectBuild = new Build();
73          String buildName = "monkey-config-project1-15.4.3.1011";
74          projectBuild.setFinalName( buildName );
75          projectBuild.setDirectory( "target/" );
76          projectBuild.setSourceDirectory( "src/" );
77          projectBuild.setOutputDirectory( "classes/" );
78          EasyMock.expect( project.getBasedir() ).andReturn( projectBaseDir ).anyTimes();
79          EasyMock.expect( project.getBuild() ).andReturn( projectBuild ).anyTimes();
80          PowerMock.replay( project );
81  
82          // when
83          final ConfigHandler cfh = new ConfigHandler( mojo, this.session, this.execution );
84          cfh.parseConfiguration();
85  
86          // then
87          Boolean monkeySkip = Whitebox.getInternalState( mojo, "parsedSkip" );
88          Integer monkeyEventCount = Whitebox.getInternalState( mojo, "parsedEventCount" );
89          Long monkeySeed = Whitebox.getInternalState( mojo, "parsedSeed" );
90          Long monkeyThrottle = Whitebox.getInternalState( mojo, "parsedThrottle" );
91          Integer monkeyPercentTouch = Whitebox.getInternalState( mojo, "parsedPercentTouch" );
92          Integer monkeyPercentMotion = Whitebox.getInternalState( mojo, "parsedPercentMotion" );
93          Integer monkeyPercentTrackball = Whitebox.getInternalState( mojo, "parsedPercentTrackball" );
94          Integer monkeyPercentNav = Whitebox.getInternalState( mojo, "parsedPercentNav" );
95          Integer monkeyPercentMajorNav = Whitebox.getInternalState( mojo, "parsedPercentMajorNav" );
96          Integer monkeyPercentSyskeys = Whitebox.getInternalState( mojo, "parsedPercentSyskeys" );
97          Integer monkeyPercentAppSwitch = Whitebox.getInternalState( mojo, "parsedPercentAppswitch" );
98          Integer monkeyPercentAnyEvent = Whitebox.getInternalState( mojo, "parsedPercentAnyevent" );
99  
100         String[] monkeyPackages = Whitebox.getInternalState( mojo, "parsedPackages" );
101         String[] monkeyCategories = Whitebox.getInternalState( mojo, "parsedCategories" );
102 
103         Boolean monkeyDebugNoEvents = Whitebox.getInternalState( mojo, "parsedDebugNoEvents" );
104         Boolean monkeyHprof = Whitebox.getInternalState( mojo, "parsedHprof" );
105         Boolean monkeyIgnoreCrashes = Whitebox.getInternalState( mojo, "parsedIgnoreCrashes" );
106         Boolean monkeyIgnoreTimeouts = Whitebox.getInternalState( mojo, "parsedIgnoreTimeouts" );
107         Boolean monkeyIgnoreSecurityExceptions = Whitebox.getInternalState( mojo, "parsedIgnoreSecurityExceptions" );
108         Boolean monkeyKillProcessAfterError = Whitebox.getInternalState( mojo, "parsedKillProcessAfterError" );
109         Boolean monkeyMonitorNativeCrashes = Whitebox.getInternalState( mojo, "parsedMonitorNativeCrashes" );
110         Boolean monkeyCreateReport = Whitebox.getInternalState( mojo, "parsedCreateReport" );
111 
112         assertFalse( "Monkey skip parameter should be false", monkeySkip );
113         final int expectedEventCount = 1000;
114         assertEquals( "Monkey eventCount parameter should be 5000", new Integer( expectedEventCount ), //
115                 monkeyEventCount );
116         assertNull( "Monkey seed should be null", monkeySeed );
117         assertNull( "Monkey throttle should be null", monkeyThrottle );
118         assertNull( "Monkey percentTouch should be null", monkeyPercentTouch );
119         assertNull( "Monkey percentMotion should be null", monkeyPercentMotion );
120         assertNull( "Monkey percentTrackball should be null", monkeyPercentTrackball );
121         assertNull( "Monkey percentNav should be null", monkeyPercentNav );
122         assertNull( "Monkey percentMajorNav should be null", monkeyPercentMajorNav );
123         assertNull( "Monkey percentSyskeys should be null", monkeyPercentSyskeys );
124         assertNull( "Monkey percentAppswitch should be null", monkeyPercentAppSwitch );
125         assertNull( "Monkey percentAnyevent should be null", monkeyPercentAnyEvent );
126 
127         assertNull( "Monkey packages should be null", monkeyPackages );
128         assertNull( "Monkey categories should be null", monkeyCategories );
129 
130         assertFalse( "Monkey debugNoEvents should be false", monkeyDebugNoEvents );
131         assertFalse( "Monkey hprof should be false", monkeyHprof );
132         assertFalse( "Monkey ignoreCrashes should be false", monkeyIgnoreCrashes );
133         assertFalse( "Monkey ignoreTimeouts should be false", monkeyIgnoreTimeouts );
134         assertFalse( "Monkey ignoreSecurityExceptions should be false", monkeyIgnoreSecurityExceptions );
135         assertFalse( "Monkey killProcessAfterError should be false", monkeyKillProcessAfterError );
136         assertFalse( "Monkey monitorNativeCrashes should be false", monkeyMonitorNativeCrashes );
137         assertFalse( "Monkey createReport should be false", monkeyCreateReport );
138     }
139 
140     /**
141      * Tests all options, checks if they are parsed correctly.
142      * 
143      * @throws Exception
144      */
145     public void testCustomMonkeyConfig() throws Exception
146     {
147         // given
148         MonkeyMojo mojo = createMojo( "monkey-config-project2" );
149         MavenProject project = EasyMock.createNiceMock( MavenProject.class );
150         Whitebox.setInternalState( mojo, "project", project );
151         File projectBaseDir = new File( getBasedir() );
152         Build projectBuild = new Build();
153         String buildName = "ui-automator-config-project1-15.4.3.1011";
154         projectBuild.setFinalName( buildName );
155         projectBuild.setDirectory( "target/" );
156         projectBuild.setSourceDirectory( "src/" );
157         projectBuild.setOutputDirectory( "classes/" );
158         EasyMock.expect( project.getBasedir() ).andReturn( projectBaseDir ).anyTimes();
159         EasyMock.expect( project.getBuild() ).andReturn( projectBuild ).anyTimes();
160 
161         PowerMock.replay( project );
162 
163         // when
164         final ConfigHandler cfh = new ConfigHandler( mojo, this.session, this.execution );
165         cfh.parseConfiguration();
166 
167         // then
168         Boolean monkeySkip = Whitebox.getInternalState( mojo, "parsedSkip" );
169         Integer monkeyEventCount = Whitebox.getInternalState( mojo, "parsedEventCount" );
170         Long monkeySeed = Whitebox.getInternalState( mojo, "parsedSeed" );
171         Long monkeyThrottle = Whitebox.getInternalState( mojo, "parsedThrottle" );
172         Integer monkeyPercentTouch = Whitebox.getInternalState( mojo, "parsedPercentTouch" );
173         Integer monkeyPercentMotion = Whitebox.getInternalState( mojo, "parsedPercentMotion" );
174         Integer monkeyPercentTrackball = Whitebox.getInternalState( mojo, "parsedPercentTrackball" );
175         Integer monkeyPercentNav = Whitebox.getInternalState( mojo, "parsedPercentNav" );
176         Integer monkeyPercentMajorNav = Whitebox.getInternalState( mojo, "parsedPercentMajorNav" );
177         Integer monkeyPercentSyskeys = Whitebox.getInternalState( mojo, "parsedPercentSyskeys" );
178         Integer monkeyPercentAppSwitch = Whitebox.getInternalState( mojo, "parsedPercentAppswitch" );
179         Integer monkeyPercentAnyEvent = Whitebox.getInternalState( mojo, "parsedPercentAnyevent" );
180 
181         String[] monkeyPackages = Whitebox.getInternalState( mojo, "parsedPackages" );
182         String[] monkeyCategories = Whitebox.getInternalState( mojo, "parsedCategories" );
183 
184         Boolean monkeyDebugNoEvents = Whitebox.getInternalState( mojo, "parsedDebugNoEvents" );
185         Boolean monkeyHprof = Whitebox.getInternalState( mojo, "parsedHprof" );
186         Boolean monkeyIgnoreCrashes = Whitebox.getInternalState( mojo, "parsedIgnoreCrashes" );
187         Boolean monkeyIgnoreTimeouts = Whitebox.getInternalState( mojo, "parsedIgnoreTimeouts" );
188         Boolean monkeyIgnoreSecurityExceptions = Whitebox.getInternalState( mojo, "parsedIgnoreSecurityExceptions" );
189         Boolean monkeyKillProcessAfterError = Whitebox.getInternalState( mojo, "parsedKillProcessAfterError" );
190         Boolean monkeyMonitorNativeCrashes = Whitebox.getInternalState( mojo, "parsedMonitorNativeCrashes" );
191         Boolean monkeyCreateReport = Whitebox.getInternalState( mojo, "parsedCreateReport" );
192 
193         assertFalse( "Monkey skip parameter should be false", monkeySkip );
194         final int expectedEventCount = 5000;
195         assertEquals( "Monkey eventCount parameter should be 5000", new Integer( expectedEventCount ), //
196                 monkeyEventCount );
197         final int expectedSeed = 123456;
198         assertEquals( "Monkey seed should be 123456", new Long( expectedSeed ), monkeySeed );
199         assertEquals( "Monkey throttle should be 10", new Long( 10 ), monkeyThrottle );
200         assertEquals( "Monkey percentTouch should be 10", new Integer( 10 ), monkeyPercentTouch );
201         assertEquals( "Monkey percentMotion should be 10", new Integer( 10 ), monkeyPercentMotion );
202         assertEquals( "Monkey percentTrackball should be 10", new Integer( 10 ), monkeyPercentTrackball );
203         assertEquals( "Monkey percentNav should be 10", new Integer( 10 ), monkeyPercentNav );
204         assertEquals( "Monkey percentMajorNav should be 10", new Integer( 10 ), monkeyPercentMajorNav );
205         assertEquals( "Monkey percentSyskeys should be 10", new Integer( 10 ), monkeyPercentSyskeys );
206         assertEquals( "Monkey percentAppswitch should be 10", new Integer( 10 ), monkeyPercentAppSwitch );
207         assertEquals( "Monkey percentAnyevent should be 10", new Integer( 10 ), monkeyPercentAnyEvent );
208 
209         String[] expectedPackages = new String[]
210         { "com.foo", "com.bar" };
211         assertTrue( "Monkey packages should be [com.foo,com.bar]", Arrays.equals( expectedPackages, monkeyPackages ) );
212         String[] expectedCategories = new String[]
213         { "foo", "bar" };
214         assertTrue( "Monkey categories should be [foo,bar]", Arrays.equals( expectedCategories, monkeyCategories ) );
215 
216         assertTrue( "Monkey debugNoEvents should be true", monkeyDebugNoEvents );
217         assertTrue( "Monkey hprof should be true", monkeyHprof );
218         assertTrue( "Monkey ignoreCrashes should be true", monkeyIgnoreCrashes );
219         assertTrue( "Monkey ignoreTimeouts should be true", monkeyIgnoreTimeouts );
220         assertTrue( "Monkey ignoreSecurityExceptions should be true", monkeyIgnoreSecurityExceptions );
221         assertTrue( "Monkey killProcessAfterError should be true", monkeyKillProcessAfterError );
222         assertTrue( "Monkey monitorNativeCrashes should be true", monkeyMonitorNativeCrashes );
223         assertTrue( "Monkey createReport should be true", monkeyCreateReport );
224 
225     }
226 
227 }