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.UIAutomatorMojo;
20  
21  /**
22   * Test the UIAutomator mojo. Tests options' default values and parsing. We do not test the command line that is passed
23   * to 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 UIAutomatorMojoTest extends AbstractAndroidMojoTestCase< UIAutomatorMojo >
33  {
34      @Override
35      public String getPluginGoalName()
36      {
37          return "uiautomator";
38      }
39  
40      /**
41       * Tests all options, checks if their default values are correct.
42       * 
43       * @throws Exception
44       */
45      public void testDefaultUIAutomatorConfig() throws Exception
46      {
47          // given
48          UIAutomatorMojo mojo = createMojo( "ui-automator-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( "UIAutomator 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 testDefaultUnskippedUIAutomatorConfig() throws Exception
65      {
66          // given
67          UIAutomatorMojo mojo = createMojo( "ui-automator-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 = "ui-automator-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 automatorSkip = Whitebox.getInternalState( mojo, "parsedSkip" );
88          Boolean automatorDebug = Whitebox.getInternalState( mojo, "parsedDebug" );
89          String automatorJarFile = Whitebox.getInternalState( mojo, "parsedJarFile" );
90          String[] automatorTestClassOrMethods = Whitebox.getInternalState( mojo, "parsedTestClassOrMethods" );
91          Boolean automatorTakeScreenshotOnFailure = Whitebox.getInternalState( mojo, "parsedTakeScreenshotOnFailure" );
92          String automatorScreenshotsPathOnDevice = Whitebox.getInternalState( mojo, "parsedScreenshotsPathOnDevice" );
93  		Boolean automatorCreateReport = Whitebox.getInternalState( mojo, "parsedCreateReport" );
94  		String automatorReportSuffix = Whitebox.getInternalState( mojo, "parsedReportSuffix" );
95  		String automatorPropertiesKeyPrefix = Whitebox.getInternalState( mojo, "parsedPropertiesKeyPrefix" );
96  
97          assertFalse( "UIAutomator skip parameter should be false", automatorSkip );
98          assertFalse( "UIAutomator debug parameter should be false", automatorDebug );
99          String expectedJarFile = buildName + ".jar";
100         assertNotNull( "UIAutomator jarFile parameter should be not null", automatorJarFile );
101         assertEquals( "UIAutomator jarFile parameter should be equal to artifact name", expectedJarFile,
102                 automatorJarFile );
103         assertNull( "UIAutomator testClassOrMethods parameter should be null", automatorTestClassOrMethods );
104         assertFalse( "UIAutomator takeScreenshotOnFailure parameter should be equal false",
105                 automatorTakeScreenshotOnFailure );
106         assertEquals( "UIAutomator screenshotsPath on device be equal /sdcard/uiautomator-screenshots/",
107                 "/sdcard/uiautomator-screenshots/", automatorScreenshotsPathOnDevice );
108 		assertFalse( "UIAutomator createReport parameter should be false", automatorCreateReport );
109 		assertNull( "UIAutomator reportSuffix parameter should be null", automatorReportSuffix );
110 		assertNull( "UIAutomator propertiesKeyPrefix parameter should be null", automatorPropertiesKeyPrefix );
111     }
112 
113     /**
114      * Tests all options, checks if they are parsed correctly.
115      * 
116      * @throws Exception
117      */
118     public void testCustomUIAutomatorConfig() throws Exception
119     {
120         // given
121         UIAutomatorMojo mojo = createMojo( "ui-automator-config-project2" );
122         MavenProject project = EasyMock.createNiceMock( MavenProject.class );
123         Whitebox.setInternalState( mojo, "project", project );
124         File projectBaseDir = new File( getBasedir() );
125         Build projectBuild = new Build();
126         String buildName = "ui-automator-config-project1-15.4.3.1011";
127         projectBuild.setFinalName( buildName );
128         projectBuild.setDirectory( "target/" );
129         projectBuild.setSourceDirectory( "src/" );
130         projectBuild.setOutputDirectory( "classes/" );
131         EasyMock.expect( project.getBasedir() ).andReturn( projectBaseDir ).anyTimes();
132         EasyMock.expect( project.getBuild() ).andReturn( projectBuild ).anyTimes();
133 
134         PowerMock.replay( project );
135 
136         // when
137         final ConfigHandler cfh = new ConfigHandler( mojo, this.session, this.execution );
138         cfh.parseConfiguration();
139 
140         // then
141         Boolean automatorSkip = Whitebox.getInternalState( mojo, "parsedSkip" );
142         Boolean automatorDebug = Whitebox.getInternalState( mojo, "parsedDebug" );
143         String automatorJarFile = Whitebox.getInternalState( mojo, "parsedJarFile" );
144         String[] automatorTestClassOrMethods = Whitebox.getInternalState( mojo, "parsedTestClassOrMethods" );
145         Boolean automatorTakeScreenshotOnFailure = Whitebox.getInternalState( mojo, "parsedTakeScreenshotOnFailure" );
146         String automatorScreenshotsPathOnDevice = Whitebox.getInternalState( mojo, "parsedScreenshotsPathOnDevice" );
147 		Boolean automatorCreateReport = Whitebox.getInternalState( mojo, "parsedCreateReport" );
148 		String automatorReportSuffix = Whitebox.getInternalState( mojo, "parsedReportSuffix" );
149 		String automatorPropertiesKeyPrefix = Whitebox.getInternalState( mojo, "parsedPropertiesKeyPrefix" );
150 
151         assertFalse( "UIAutomator skip parameter should be false", automatorSkip );
152         assertFalse( "UIAutomator debug parameter should be false", automatorDebug );
153         assertNotNull( "UIAutomator jarFile parameter should be not null", automatorJarFile );
154         String expectedJarFile = buildName + ".jar";
155         assertEquals( "UIAutomator jarFile parameter should be equal to artifact name", expectedJarFile,
156                 automatorJarFile );
157 
158         assertNotNull( "UIAutomator jarFile parameter should be not null", automatorTestClassOrMethods );
159         String[] expectedTestClassOrMethods = new String[]
160         { "a", "b#c" };
161         assertTrue( "UIAutomator testClassOrMethods parameter should be equal [a,b#c]",
162                 Arrays.equals( expectedTestClassOrMethods, automatorTestClassOrMethods ) );
163         assertTrue( "UIAutomator takeScreenshotOnFailure parameter should be equal true",
164                 automatorTakeScreenshotOnFailure );
165         assertEquals( "UIAutomator screenshotsPath on device be equal /mnt/sdcard/screenshots/",
166                 "/mnt/sdcard/screenshots/", automatorScreenshotsPathOnDevice );
167 		assertTrue( "UIAutomator createReport parameter should be true", automatorCreateReport );
168 		String expectedReportSuffix = "-mySpecialReport";
169 		assertEquals( "UIAutomator reportSuffix parameter should be equal to "+expectedReportSuffix,
170 				expectedReportSuffix, automatorReportSuffix );
171 		String expectedPropertiesKeyPrefix = "UIA";
172 		assertEquals( "UIAutomator propertiesKeyPrefix should be equal to " + expectedPropertiesKeyPrefix,
173 				expectedPropertiesKeyPrefix, automatorPropertiesKeyPrefix);
174     }
175 
176 }