Guest User

Untitled

a guest
Jan 23rd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. [Test]
  2. public async void GetProfileTest ()
  3. {
  4.     var profileFragment = new ProfileFragment();
  5.     var result = await profileFragment.GetProfileAsync();
  6.  
  7.     Assert.IsNotNull (result);
  8. }
  9.  
  10. //#define TEST // Uncomment this to start UnitTestig
  11.  
  12. . . .
  13.  
  14. [ /* Your ActivityAttributes */ ]
  15. public class YourLauncherActivity : Activity {
  16.  
  17. protected override void OnCreate(Bundle savedInstanceState)
  18. {
  19. base.OnCreate(savedInstanceState);
  20. #if (DEBUG && TEST)
  21. // Your TestConfiguration.cs Activity
  22. Intent iTestConfiguration = new Intent(this, typeof(TestConfiguration));
  23. StartActivity(iTestConfiguration);
  24. #endif
  25. #if !TEST
  26. // Run your normal Activity here, like Login, etc...
  27. #endif
  28. }
  29. }
  30.  
  31. ...
  32.  
  33. [Activity(Label = "TestConfiguration")]
  34. public class TestConfiguration : Xunit.Runners.UI.RunnerActivity
  35. {
  36. //public static Context Context { get; set; }
  37.  
  38. protected override void OnCreate(Bundle bundle)
  39. {
  40. // tests can be inside the main assembly
  41. AddTestAssembly(Assembly.GetExecutingAssembly());
  42. AddExecutionAssembly(typeof(ExtensibilityPointFactory).Assembly);
  43. // or in any reference assemblies
  44. //this.AutoStart = true;
  45. base.OnCreate(bundle);
  46. //Context = this;
  47. }
  48. }
  49.  
  50. public class MyTests
  51. {
  52.  
  53. public MyTests()
  54. {
  55. // Before each test
  56. }
  57.  
  58.  
  59. [Fact]
  60. public void FailingTest()
  61. {
  62. Assert.True(false);
  63. }
  64. }
  65.  
  66. . . .
  67. public static TestSuiteInstrumentation CurrentInstrumentation { get; set; }
  68.  
  69. public TestInstrumentation(IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer)
  70. {
  71. CurrentInstrumentation = this;
  72. }
  73. . . .
  74.  
  75. [TestFixture]
  76. public class LoginTests
  77. {
  78. private TestSuiteInstrumentation instrument = TestInstrumentation.CurrentInstrumentation;
  79. private Activity CurrentActivity;
  80.  
  81. [SetUp]
  82. public void SetUp()
  83. {
  84. Instrumentation.ActivityMonitor monitor = instrument.AddMonitor($"{instrument.TargetContext.PackageName}." + nameof(Login), null, false);
  85.  
  86. Intent intent = new Intent();
  87. intent.AddFlags(ActivityFlags.NewTask);
  88. intent.SetClassName(instrument.TargetContext, $"{instrument.TargetContext.PackageName}." + nameof(Login));
  89.  
  90. Task.Run(() => instrument.StartActivitySync(intent)).GetAwaiter();
  91.  
  92. Activity currentActivity = instrument.WaitForMonitor(monitor);
  93.  
  94. }
  95.  
  96. [Test]
  97. public void LoginTest()
  98. {
  99. // Verify your activity is not null
  100. Assert.NotNull(CurrentActivity);
  101.  
  102. // Convert CurrentActivity to your Activity
  103. Login login = CurrentActivity as Login;
  104.  
  105.  
  106. // Verify your views are not null, finding views in your Activity
  107. Assert.NotNull(login);
  108. Assert.NotNull(login.FindViewById<EditText>(Resource.Id.etLoginUsername));
  109. Assert.NotNull(login.FindViewById<EditText>(Resource.Id.etLoginPassword));
  110.  
  111.  
  112. instrument.RunOnMainSync(() => {
  113.  
  114. // Here you can run your UI methods for Views, example
  115. login.FindViewById<EditText>(Resource.Id.etLoginUsername).Text = "hello";
  116. login.FindViewById<EditText>(Resource.Id.etLoginPassword).Text = "world";
  117. });
  118.  
  119. // Here will be your assertions
  120. }
  121. }
  122.  
  123. [Activity( ... , Name ="com.yourpackage.android.Login", ...)]
Add Comment
Please, Sign In to add comment