// Copyright 2015 The Project Buendia Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License.  You may obtain a copy
// of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distrib-
// uted under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
// OR CONDITIONS OF ANY KIND, either express or implied.  See the License for
// specific language governing permissions and limitations under the License.

package org.projectbuendia.client.ui.login;

import org.projectbuendia.client.R;
import org.projectbuendia.client.ui.FunctionalTestCase;
import org.projectbuendia.client.ui.matchers.UserMatchers;

import java.util.Date;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasToString;

/** Tests for {@link LoginActivity}. */
public class LoginActivityTest extends FunctionalTestCase {

    /** Adds a new user and logs in. */
    public void testAddUser() {
        screenshot("Test Start");
        final long n = new Date().getTime()%100000;
        final String username = "test" + n;
        final String given = "Testgiven" + n;
        final String family = "Testfamily" + n;

        // Add new user
        click(viewWithText("Add User"));
        screenshot("After Add User Clicked");
        type(given, viewWithId(R.id.add_user_given_name_tv));
        type(family, viewWithId(R.id.add_user_family_name_tv));
        screenshot("After User Populated");
        click(viewWithText("OK"));
        screenshot("After OK Pressed");

        waitForProgressFragment();

        // Click new user
        expectVisible(dataThat(hasToString(equalTo("TT"))));
        screenshot("In User Selection");
        click(dataThat(new UserMatchers.HasFullName(given + " " + family)));

        // Should be logged in
        expectVisible(viewWithText(given + " " + family));
        screenshot("After User Selected");
        click(viewWithText("TT"));
        expectVisible(viewWithText(given + " " + family));
        screenshot("After User Selected in Action Bar");
    }

    /** Logs in as the guest user and logs out. */
    public void testGuestLoginLogout() {
        // Click guest user
        expectVisible(viewWithText("GU"));
        screenshot("Test Start");
        click(viewWithText("Guest User"));

        // Should be logged in; log out
        expectVisible(viewWithText(R.string.title_location_list));
        screenshot("After Guest User Clicked");
        click(viewWithText("GU"));
        expectVisible(viewWithText("Guest User"));
        screenshot("After Guest User Selected in Action Bar");
        click(viewWithId(R.id.button_log_out));

        waitForProgressFragment();

        // Should be back at the user list
        expectVisible(viewWithText("Add User"));
        screenshot("After Logout");
    }
}

@@ -811,103 +811,8 @@
 e;%0A%0A
-import static org.hamcrest.Matchers.equalTo;%0Aimport static org.hamcrest.Matchers.hasToString;%0A%0A
 /** 
@@ -1659,33 +1659,57 @@
 hat(
-hasToString(equalTo(%22TT%22)
+new UserMatchers.HasFullName(given + %22 %22 + family
 )));
@@ -1863,67 +1863,8 @@
  in%0A
-        expectVisible(viewWithText(given + %22 %22 + family));%0A
     

Fix LoginActivityTest.