// Copyright 2019 The Oppia Authors. All Rights Reserved.
//
// 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
// distributed 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 the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Page object for the profile page, for use in
 * Protractor tests.
 */
var waitFor = require('./waitFor.js');

var ProfilePage = function() {
  var currUserProfilePhoto = element(
    by.css('.protractor-test-profile-current-user-photo'));
  var otherUserProfilePhoto = element(
    by.css('.protractor-test-profile-other-user-photo'));
  var bio = element(
    by.css('.protractor-test-profile-bio'));
  var interests = element.all(
    by.css('.protractor-test-profile-interest'));
  var interestPlaceholder = element(
    by.css('.protractor-test-profile-no-interest'));
  var allExplorationCardElements = element.all(
    by.css('.protractor-test-exploration-dashboard-card'));
  var createdExplorationStat = element.all(
    by.css('.protractor-test-profile-created-stat'));
  var cardTitleCss = by.css('.protractor-test-exp-summary-tile-title');

  this.get = async function(userName) {
    await browser.get('/profile/' + userName);
    await waitFor.pageToFullyLoad();
  };

  this.expectCurrUserToHaveProfilePhoto = async function() {
    expect(await currUserProfilePhoto.isPresent()).toBe(true);
  };

  this.expectOtherUserToHaveProfilePhoto = async function() {
    expect(await otherUserProfilePhoto.isPresent()).toBe(true);
  };

  this.expectUserToHaveBio = async function(expectedText) {
    expect(await bio.getText()).toMatch(expectedText);
  };

  this.expectUserToHaveNoInterests = async function() {
    var numInterests = await interests.count();
    expect(numInterests).toEqual(0);
  };

  this.expectUserToHaveInterests = async function(expectedInterests) {
    var numInterests = await interests.count();
    expect(numInterests).toEqual(expectedInterests.length);

    var interestTexts = await interests.map(async function(interestElem) {
      return await interestElem.getText();
    });
    interestTexts.forEach(function(interestText) {
      expect(expectedInterests.includes(interestText)).toBe(true);
    });
  };

  this.expectUserToHaveInterestPlaceholder = async function(expectedText) {
    expect(await interestPlaceholder.getText()).toMatch(expectedText);
  };

  this.expectUserToNotHaveInterestPlaceholder = async function() {
    expect(await interestPlaceholder.isPresent()).toBe(false);
  };

  this.expectToHaveExplorationCards = async function() {
    var explorationCardsCount = await allExplorationCardElements.count();
    if (explorationCardsCount === 0) {
      throw new Error('There is no exploration card on this profile');
    }
    expect(explorationCardsCount).toBeGreaterThan(0);
  };

  this.expectToHaveExplorationCardByName = async function(explorationName) {
    var explorationsCardByName = await allExplorationCardElements.filter(
      async function(card) {
        var cardTitle = card.element(cardTitleCss);
        var title = await cardTitle.getText();
        return title === explorationName;
      });

    if (await explorationsCardByName.length === 0) {
      throw new Error(
        'There is no exploration card with name ' + explorationName);
    }
    expect(await explorationsCardByName.length).toBeGreaterThanOrEqual(1);
  };

  this.expectToHaveCreatedExplorationStat = async function(expectedStat) {
    expect(await createdExplorationStat.getText()).toMatch(expectedStat);
  };
};

exports.ProfilePage = ProfilePage;

@@ -1683,264 +1683,468 @@
     
-expect(await currUserProfilePhoto.isPresent()).toBe(true);%0A  %7D;%0A%0A  this.expectOtherUserToHaveProfilePhoto = async function() %7B%0A    expect(await otherUserProfilePhoto.isPresent()).toBe(true);%0A  %7D;%0A%0A  this.expectUserToHaveBio = async function(expectedText) %7B
+await waitFor.visibilityOf(%0A      currUserProfilePhoto,%0A      'Current user profile photo taking too long to display');%0A  %7D;%0A%0A  this.expectOtherUserToHaveProfilePhoto = async function() %7B%0A    await waitFor.presenceOf(%0A      otherUserProfilePhoto,%0A      'Other user profile photo taking too long to display');%0A  %7D;%0A%0A  this.expectUserToHaveBio = async function(expectedText) %7B%0A    await waitFor.visibilityOf(%0A      bio,%0A      'Bio is taking too long to appear');
 %0A   
@@ -2599,24 +2599,134 @@
 restElem) %7B%0A
+      await waitFor.visibilityOf(%0A        interestElem,%0A        'InterestElem is taking too long to appear');%0A
       return
@@ -2968,24 +2968,144 @@
 ctedText) %7B%0A
+    await waitFor.visibilityOf(%0A      interestPlaceholder,%0A      'Interest place holder is taking too long to appear');%0A
     expect(a
@@ -3370,41 +3370,41 @@
     
-var explorationCardsCount = await
+await waitFor.visibilityOf(%0A     
  all
@@ -3431,26 +3431,25 @@
 nts.
-count();
+first(),
 %0A    
-if (e
+  'E
 xplo
@@ -3458,156 +3458,56 @@
 tion
-C
+ c
 ards
-Count === 0) %7B%0A      throw new Error('There is no exploration card on this profile');%0A    %7D%0A    expect(explorationCardsCount).toBeGreaterThan(0
+ is not present or taking time to display'
 );%0A 
@@ -3743,16 +3743,142 @@
 leCss);%0A
+        await waitFor.visibilityOf(%0A          cardTitle,%0A          'CardTitle is not present or taking too long to display');%0A
         

Fix part of #10798: Update ProfilePage.js with action.js functions (#12593)