Example #1
0
        public void TestGetNextTip3()
        {
            // Setup
            // Mock the TipManager
            Mock <ITipManager> mockTipManager = new Mock <ITipManager>();

            mockTipManager.Setup(m => m.GetPrioritizedTipGroups()).Returns(GenerateTestTipGroups());

            // Mock the TipHistoryManager
            Mock <VSTipHistoryManager> mockTipHistoryManager = new Mock <VSTipHistoryManager>();

            TipHistoryInfo tipObj1 = new TipHistoryInfo();

            tipObj1.GlobalTipId   = "General-GN001";
            tipObj1.TipLikeStatus = TipLikeEnum.LIKE;

            TipHistoryInfo tipObj2 = new TipHistoryInfo();

            tipObj2.GlobalTipId   = "Editor-ED001";
            tipObj2.TipLikeStatus = TipLikeEnum.NORMAL;

            mockTipHistoryManager.Setup(m => m.GetTipHistory()).Returns(new List <TipHistoryInfo> {
                tipObj1, tipObj2
            });

            TipCalculator tipCalculator = new TipCalculator(mockTipHistoryManager.Object, mockTipManager.Object);

            // Act
            TipInfo nextTip = tipCalculator.GetNextTip();

            // Verify
            Assert.IsNotNull(nextTip);
            Assert.AreEqual("General-GN001", nextTip.globalTipId);
        }
Example #2
0
        /// <summary>
        /// Recursive method.
        /// Logic: From the currentTipHistoryIndex, look to see if there's a tip at a later index.
        /// </summary>
        private TipHistoryInfo GetNextTipFromHistory(List <TipHistoryInfo> tipHistory, int currentTipHistoryIndex)
        {
            int nextTipIndex = currentTipHistoryIndex + 1;

            if (nextTipIndex >= tipHistory.Count)
            {
                // No additional items in the tip history to check.
                return(null);
            }

            // Get the next tip from history
            TipHistoryInfo nextTipHistoryInfo = tipHistory[nextTipIndex];

            // Check if the tip from history can be found in the current list of known tips.
            var tipExists = _tipManager.TipExists(nextTipHistoryInfo.GlobalTipId);

            if (tipExists)
            {
                // Found a tip. Return the TipHistory.
                return(nextTipHistoryInfo);
            }

            // Recursively search forward for a valid next tip.
            return(GetNextTipFromHistory(tipHistory, nextTipIndex));
        }
Example #3
0
        public void TestGetNextTip2()
        {
            // Setup
            Mock <VSTipHistoryManager> mockTipHistoryManager = new Mock <VSTipHistoryManager>();
            TipHistoryInfo             tipObj1 = new TipHistoryInfo();

            tipObj1.GlobalTipId   = "General-GN001";
            tipObj1.TipLikeStatus = TipLikeEnum.LIKE;

            TipHistoryInfo tipObj2 = new TipHistoryInfo();

            tipObj2.GlobalTipId   = "Editor-ED001";
            tipObj2.TipLikeStatus = TipLikeEnum.NORMAL;

            List <TipHistoryInfo> tipsSeen = new List <TipHistoryInfo> {
                tipObj1, tipObj2
            };

            mockTipHistoryManager.Setup(m => m.GetTipHistory()).Returns(tipsSeen);

            TipCalculator tipCalculator = new TipCalculator(mockTipHistoryManager.Object);

            // Act
            TipInfo nextTip = tipCalculator.GetNextTip();

            // Verify
            Assert.IsNotNull(nextTip);
            Assert.AreEqual("General-GN001", nextTip.globalTipId);
        }
Example #4
0
        private TipHistoryInfo GetPreviousTipFromHistory(List <TipHistoryInfo> tipHistory, int currentTipHistoryIndex)
        {
            // Recursive method: Stop when we find a tip, or when the tip history index is < 0
            int prevTipHistoryIndex = currentTipHistoryIndex - 1;

            if (prevTipHistoryIndex < 0)
            {
                // We've reached the beginning of history. There is no previous.
                return(null);
            }

            // Get the previous tip from history. Tip history is ordered with the most recent last.
            // Previous tip is the one before the current tip in the tip history.
            TipHistoryInfo previousTipHistoryInfo = tipHistory[prevTipHistoryIndex];

            // Get the full TipInfo (by the given TipId) from the Tip Manager.
            var tipExists = _tipManager.TipExists(previousTipHistoryInfo.GlobalTipId);

            if (tipExists)
            {
                // Found a tip! Can return the TipHistory.
                return(previousTipHistoryInfo);
            }

            // No Previous tip to show. (It's possible the tip previously shown no longer exists.)
            // Look for next previous tip. (Drop the index back one position and try again)
            // Recursively search backward until currentTipHistoryIndex < 1 or previousTip != null
            return(GetPreviousTipFromHistory(tipHistory, prevTipHistoryIndex));
        }
Example #5
0
        private TipInfo BuildTipInfoFromHistory(TipHistoryInfo tipHistoryInfo)
        {
            TipInfo tipInfo = _tipManager.GetTipInfo(tipHistoryInfo.GlobalTipId);

            tipInfo.TipLikeStatus = tipHistoryInfo.TipLikeStatus;
            return(tipInfo);
        }
Example #6
0
        public void UpdateTipVoteStatus(string globalTipId, TipLikeEnum currentTipVoteStatus)
        {
            // Grab the existing entry for this tip if there is one
            // Otherwise, create new Tip History entry
            TipHistoryInfo tipHistoryInfo;

            if (_tipHistory.Exists(a => a.GlobalTipId.Equals(globalTipId)))
            {
                tipHistoryInfo = _tipHistory.Find(a => a.GlobalTipId.Equals(globalTipId));
            }
            else
            {
                tipHistoryInfo = new TipHistoryInfo
                {
                    GlobalTipId = globalTipId
                };
                _tipHistory.Add(tipHistoryInfo);
            }

            // Update the voting status for the tip
            tipHistoryInfo.TipLikeStatus = currentTipVoteStatus;

            // Keep Settings Store in sync
            // Update the VS settings store with the latest tip history
            PersistTipHistoryToRoamingSettings();
        }
Example #7
0
        private void GoToPrevTip()
        {
            TipHistoryInfo previousTipHistory = FindPreviousTipInHistory();

            // Back out if there is no previous tip.
            if (previousTipHistory == null)
            {
                Debug.WriteLine("Tip of the Day: There is no previous tip to navigate to.");
                return;
            }

            // Prepare the TipInfo to show in the UI and navigate to the previous tip.
            TipInfo previousTip = BuildTipInfoFromHistory(previousTipHistory);

            // Show the tip on the UI
            NavigateToTip(previousTip);
        }
Example #8
0
        private static List <TipHistoryInfo> DeserializeTipHistory(string tipHistoryStr)
        {
            // Return an empty list in no items found.
            List <TipHistoryInfo> tipsInfo = new List <TipHistoryInfo>();

            // Deserialize the raw string. Format = [TipId]:[Vote],[TipId]:[Vote] (eg. "ED001:1,SHL001:0")
            // Note: Vote field might be missing (esp if people have previously run the older version before voting was stored)
            if (!string.IsNullOrEmpty(tipHistoryStr))
            {
                // Split tip history items on comma (',') and parse each tip history item
                foreach (string tipHistoryItemStr in tipHistoryStr.Split(','))
                {
                    TipHistoryInfo tipHistoryItem = DeserializeTipHistoryItem(tipHistoryItemStr);
                    tipsInfo.Add(tipHistoryItem);
                }
            }

            return(tipsInfo);
        }
Example #9
0
        private void GoToNextTip()
        {
            // If there's a next tip in the Tip History, show that. Otherwise, grab a new tip.
            TipInfo        nextTip;
            TipHistoryInfo nextTipInHistory = FindNextTipInHistory();

            if (nextTipInHistory != null)
            {
                // Build a TipInfo object to show in the UI
                nextTip = BuildTipInfoFromHistory(nextTipInHistory);
            }
            else
            {
                // Generate a new Tip to show. Set the LikeStatus to NORMAL.
                nextTip = TipOfTheDay.GetNewTip();
                nextTip.TipLikeStatus = TipLikeEnum.NORMAL;
            }

            // Show the tip
            NavigateToTip(nextTip);
        }
Example #10
0
        private static TipHistoryInfo DeserializeTipHistoryItem(string tipHistoryItemStr)
        {
            TipHistoryInfo tipHistoryItem = new TipHistoryInfo();

            // Split on colon ':' eg. [TipId]:[Vote]
            string[] parts = tipHistoryItemStr.Split(':');

            // The first part is the GlobalTipId
            tipHistoryItem.GlobalTipId = parts[0];

            // The second part is the Vote
            if (parts.Length > 0)
            {
                string tipVote = parts[1];
                if (Enum.TryParse(tipVote, out TipLikeEnum tipVoteEnum))
                {
                    tipHistoryItem.TipLikeStatus = tipVoteEnum;
                }
            }

            return(tipHistoryItem);
        }