Exemplo n.º 1
0
 public void EnsureGetSimilarArtistsReturnsErrorForFailedCall()
 {
     IMusicClient client = new MusicClient("test", "test", "gb", new FailedMockApiRequestHandler());
     client.GetSimilarArtists(
         (ListResponse<Artist> result) =>
         {
             Assert.IsNotNull(result, "Expected a result");
             Assert.IsNotNull(result.StatusCode, "Expected a status code");
             Assert.IsTrue(result.StatusCode.HasValue, "Expected a status code");
             Assert.AreNotEqual(HttpStatusCode.OK, result.StatusCode.Value, "Expected a non-OK response");
             Assert.IsNotNull(result.Error, "Expected an error");
             Assert.AreEqual(typeof(ApiCallFailedException), result.Error.GetType(), "Expected an ApiCallFailedException");
         },
         "test");
 }
Exemplo n.º 2
0
 public void EnsureGetSimilarArtistsReturnsItems()
 {
     IMusicClient client = new MusicClient("test", "test", "gb", new SuccessfulMockApiRequestHandler());
     client.GetSimilarArtists(
         (ListResponse<Artist> result) =>
         {
             Assert.IsNotNull(result, "Expected a result");
             Assert.IsNotNull(result.StatusCode, "Expected a status code");
             Assert.IsTrue(result.StatusCode.HasValue, "Expected a status code");
             Assert.AreEqual(HttpStatusCode.OK, result.StatusCode.Value, "Expected a 200 response");
             Assert.IsNotNull(result.Result, "Expected a list of results");
             Assert.IsNull(result.Error, "Expected no error");
             Assert.Greater(result.Result.Count, 0, "Expected more than 0 results");
         },
         new Artist() { Id = "test" });
 }
        public void EnsureGetSimilarArtistsReturnsItems()
        {
            IMusicClient client = new MusicClient("test", "test", "gb", new MockApiRequestHandler(Resources.artist_similar));
            client.GetSimilarArtists(
                (ListResponse<Artist> result) =>
                {
                    Assert.IsNotNull(result, "Expected a result");
                    Assert.IsNotNull(result.StatusCode, "Expected a status code");
                    Assert.IsTrue(result.StatusCode.HasValue, "Expected a status code");
                    Assert.AreEqual(HttpStatusCode.OK, result.StatusCode.Value, "Expected a 200 response");
                    Assert.IsNotNull(result.Result, "Expected a list of results");
                    Assert.IsNull(result.Error, "Expected no error");
                    Assert.Greater(result.Result.Count, 0, "Expected more than 0 results");

                    foreach (Artist artist in result.Result)
                    {
                        Assert.IsFalse(string.IsNullOrEmpty(artist.Id), "Expected Id to be populated");
                        Assert.IsFalse(string.IsNullOrEmpty(artist.Name), "Expected Name to be populated");
                        Assert.IsNotNull(artist.Genres, "Expected a genre list");
                        Assert.Greater(artist.Genres.Length, 0, "Expected more than 0 genres");
                    }
                },
                new Artist() { Id = "test" });
        }
 public void EnsureGetSimilarArtistsThrowsExceptionForNullCallback()
 {
     IMusicClient client = new MusicClient("test", "test", "gb", new MockApiRequestHandler(Resources.artist_similar));
     client.GetSimilarArtists(null, "test");
 }
 public void EnsureGetSimilarArtistsThrowsExceptionForNullArtist()
 {
     Artist nullArtist = null;
     IMusicClient client = new MusicClient("test", "test", "gb", new MockApiRequestHandler(Resources.artist_similar));
     client.GetSimilarArtists((ListResponse<Artist> result) => { }, nullArtist);
 }
Exemplo n.º 6
0
 public void EnsureGetSimilarArtistsThrowsExceptionForNullArtistId()
 {
     string nullId = null;
     IMusicClient client = new MusicClient("test", "test", "gb", new SuccessfulMockApiRequestHandler());
     client.GetSimilarArtists((ListResponse<Artist> result) => { }, nullId);
 }
Exemplo n.º 7
0
        /// <summary>
        /// Builds the recommended artists lists.
        /// This method initiates a chain of requests, which requests similar
        /// artists information for each of the local artists one after another.
        /// </summary>
        private void FetchRecommendations()
        {
            if (!initialized || localArtists.Count <= recommendResponses)
            {
                if (initialized && App.ViewModel.Recommendations.Count <= 0)
                {
                    App.ViewModel.NoRecommendedVisibility = Visibility.Visible;
                }
                else
                {
                    App.ViewModel.NoRecommendedVisibility = Visibility.Collapsed;
                }

                // limit the number of recommended artists to 20
                if (localArtists.Count == recommendResponses)
                {
                    int i = App.ViewModel.Recommendations.Count - 1;
                    while (i > 20)
                    {
                        App.ViewModel.Recommendations.RemoveAt(i);
                        i--;
                    }
                }

                return;
            }

            client.GetSimilarArtists((ListResponse <Artist> response) =>
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    // Use results
                    if (response != null && response.Result != null && response.Result.Count > 0)
                    {
                        foreach (Artist a in response.Result)
                        {
                            bool handled = false;

                            // Don't recommend artists already stored in device.
                            if (App.ViewModel.IsLocalArtist(a.Name))
                            {
                                handled = true;
                            }

                            // Check if the artist has already been recommended -> add some weight
                            // to recommendation.
                            if (!handled)
                            {
                                for (int i = 0; i < App.ViewModel.Recommendations.Count; i++)
                                {
                                    if (App.ViewModel.Recommendations[i].Name == a.Name)
                                    {
                                        handled = true;
                                        App.ViewModel.Recommendations[i].SimilarArtistCount++;

                                        // position according to weight
                                        if (i > 1)
                                        {
                                            int j = 1;

                                            for (j = i - 1; j > 1; j--)
                                            {
                                                if (App.ViewModel.Recommendations[j].SimilarArtistCount >=
                                                    App.ViewModel.Recommendations[i].SimilarArtistCount)
                                                {
                                                    j++; // This item (j) has been ranked higher or equal - correct index is one more
                                                    break;
                                                }
                                            }

                                            if (i > j)
                                            {
                                                ArtistModel artist = App.ViewModel.Recommendations[i];
                                                App.ViewModel.Recommendations.RemoveAt(i);
                                                App.ViewModel.Recommendations.Insert(j, artist);
                                                break;
                                            }
                                        }
                                    }
                                }
                            }

                            // If the artist is not present in the device and has not yet been
                            // recommended, do it now.
                            if (!handled)
                            {
                                App.ViewModel.Recommendations.Add(new ArtistModel()
                                {
                                    Name               = a.Name,
                                    Country            = CountryCodes.CountryNameFromTwoLetter(a.Country),
                                    Genres             = a.Genres[0].Name,
                                    Thumb100Uri        = a.Thumb100Uri,
                                    Thumb200Uri        = a.Thumb200Uri,
                                    Thumb320Uri        = a.Thumb320Uri,
                                    Id                 = a.Id,
                                    SimilarArtistCount = 1,
                                    ItemWidth          = "205",
                                    ItemHeight         = "205"
                                });
                            }
                        }
                    }

                    if (response != null && response.Error != null)
                    {
                        ShowNokiaMusicApiError();
                    }
                    HideProgressIndicator("FetchRecommendations()");

                    recommendResponses++;
                    FetchRecommendations();
                });
            }, localArtists[recommendResponses].Id);
            ShowProgressIndicator("FetchRecommendations()");
        }